How to make MATLAB loop not use the same number?

i ask about matlab give me the same bus , i want location be for example 2,19,25
N=50; % Number of search agents
Max_iter=100; % Maximum number of iterations
lb=[2 975 2 975 2 975 ];
ub=[33 975 33 975 33 975 ];
dim=6;
fobj= @(x) Sphere(x);
but it give me loc 2 2 2
X_opt_loc =
2 2 2
X_opt_size =
975 975 975
I would be glad to give me solution for my problem.

37 commentaires

"How to make MATLAB loop not use the same number?
Where is the loop?
What are bus and location supposed to mean? What are you trying to do?
This looks like an incomplete code, there is no meaning of these lines of code. Show the full code and specify what you are trying to do. Only then can somebody help you.
this part of code to determine the optimal placement for EVCS and DGS
If there is a function on MATLAB to make it not repeat the same location?
You still did not answer my question - What is location supposed to mean?
How are you obtaining X_opt_loc and X_opt_size? What is the use of other variables?
Show all relevant code.
the location and size for electric vehicle charging station for 33 bus system
Are you calling fmincon? Are you calling surrogateopt()? Are you using problem based optimization?
We need to see your code.
i make optimization for determine the location and size of electric vehicle charging stations and DGs
i want to allocate EVCS from bus 2 to 33
The error is on line 19.
It is impossible to answer your question with the limited information you have provided. It's like you complained that when you followed a recipe you didn't get what you wanted. That could be because you didn't follow the recipe correctly (your oven temperature was too high), because you used an incorrect ingredient (sugar instead of salt), or you followed the wrong recipe entirely (you wanted bread but instead followed a recipe for making candy canes)!
  • Post the code you've written.
  • Describe the problem you're experiencing as though we are completely unfamiliar with the problem you're trying to solve.
  • Ask a specific question.
If you don't do that I'm not sure we will be able to help you.
Rose Toto
Rose Toto le 13 Mar 2023
Modifié(e) : Walter Roberson le 13 Mar 2023
clear
close all
clc
N=50; % Number of search agents
Max_iter=100; % Maximum number of iterations
lb=[2 975 3 975 3 975 ];
ub=[33 975 33 975 33 975 ];
dim=6;
fobj= @(x) Sphere(x);
% Load details of the selected benchmark function
[Convergence_curve,gBest,gBestScore]=WHO(N,Max_iter,lb,ub,dim,fobj);
display(['The best location of WHO is: ', num2str(gBest)]);
display(['The best fitness of WHO is: ', num2str(gBestScore)]);
gBestScore
%% Results
figure;
subplot(2,2,[1 2])
plot(Convergence_curve, 'LineWidth', 2);
% semilogy(BestCosts, 'LineWidth', 2);
title('Fitness Function')
xlabel('Iteration');
ylabel('P_loss');
grid on;
% figure;
% plot(loadbus(:,1),voltage(:,1),'-^b');
% grid on;
%% load flow solution
x = gBest;
X_opt_loc=round(x(1:2:end))
X_opt_size=x(2:2:end)
% sum_Size= sum(X_opt_size)
% % figure;
% subplot(2,2,2)
% bar(X_opt_loc,0.25);
% title('Optimal Location');
% xlabel('DG Number');
% ylabel('DG Bus Location');
% grid on;
% subplot(2,2,3)
% bar(X_opt_size,0.25);
% title('Optimal Size')
% xlabel('DG Number');
%
% ylabel('DG Size');
% grid on;
FB_load_flow;
% FB_load_flow_basecase;
when run matlab
X_opt_loc =
2 19 19
X_opt_size =
975 975 975
i want the result for location of EVCS at three different locations like 2, 19, 25
when i change this line
lb=[2 975 2 975 2 975 ];
and run
X_opt_loc =
2 2 2
X_opt_size =
975 975 975
i want function on matlab to make results for three location to be different like 2 19 25 like that
Yes
No, you must be using a different function. The WHO there has
if size(ub,1)==1
ub=ones(1,dim)*ub;
lb=ones(1,dim)*lb;
end
You are passing in row vectors for ub and lb, so size(ub,1)==1 is true for those, so the code would attempt to do matrix multiplication between two row vectors, which is going to fail. The code posted in that contribution cannot work with the code you posted.
If you make ub and lb into column vectors instead of row vectors then you can get past that error message, but the code that assigns random positions to the stallions and the foals is wrong when the ub and lb are column vectors.
I cannot test further without the code for Sphere though.
The code i post here for main not WHO
I will send you WHO and Sphere
But i make run only main
I want solution for my problem please
I cannot test without your Sphere.m
You agreed that you are using https://www.mathworks.com/matlabcentral/fileexchange/90787-wild-horse-optimizer so I installed WHO from there.
Rose Toto
Rose Toto le 14 Mar 2023
Modifié(e) : Walter Roberson le 14 Mar 2023
this is WHO there s comment on
% if size(ub,1)==1
% ub=ones(1,dim)*ub;
% lb=ones(1,dim)*lb;
% end
Max_iter: maximum iterations, N: populatoin size, Convergence_curve: Convergence curve
function [Convergence_curve,gBest,gBestScore]=WHO(N,Max_iter,lb,ub,dim,fobj)
% if size(ub,1)==1
% ub=ones(1,dim)*ub;
% lb=ones(1,dim)*lb;
% end
PS=0.2; % Stallions Percentage
PC=0.13; % Crossover Percentage
NStallion=ceil(PS*N); % number Stallion
Nfoal=N-NStallion;
Convergence_curve = zeros(1,Max_iter);
gBest=zeros(1,dim);
gBestScore=inf;
%create initial population
empty.pos=[];
empty.cost=[];
group=repmat(empty,Nfoal,1);
for i=1:Nfoal
group(i).pos=lb+rand(1,dim).*(ub-lb);
group(i).cost=fobj(group(i).pos);
end
Stallion=repmat(empty,NStallion,1);
for i=1:NStallion
Stallion(i).pos=lb+rand(1,dim).*(ub-lb);
Stallion(i).cost=fobj(Stallion(i).pos);
end
ngroup=length(group);
a=randperm(ngroup);
group=group(a);
i=0;
k=1;
for j=1:ngroup
i=i+1;
Stallion(i).group(k)=group(j);
if i==NStallion
i=0;
k=k+1;
end
end
Stallion=exchange(Stallion);
[value,index]=min([Stallion.cost]);
WH=Stallion(index); % global
gBest=WH.pos;
gBestScore=WH.cost;
Convergence_curve(1)=WH.cost;
l=2; % Loop counter
while l<Max_iter+1
TDR=1-l*((1)/Max_iter);
for i=1:NStallion
ngroup=length(Stallion(i).group);
[~,index]=sort([Stallion(i).group.cost]);
Stallion(i).group=Stallion(i).group(index);
for j=1:ngroup
if rand>PC
z=rand(1,dim)<TDR;
r1=rand;
r2=rand(1,dim);
idx=(z==0);
r3=r1.*idx+r2.*~idx;
rr=-2+4*r3;
Stallion(i).group(j).pos= 2*r3.*cos(2*pi*rr).*(Stallion(i).pos-Stallion(i).group(j).pos)+(Stallion(i).pos);
else
A=randperm(NStallion);
A(A==i)=[];
a=A(1);
c=A(2);
% B=randperm(ngroup);
% BB=randperm(ngroup);
% b1=B(1);b2=BB(1);
x1=Stallion(c).group(end).pos;
x2=Stallion(a).group(end).pos;
y1=(x1+x2)/2; % Crossover
Stallion(i).group(j).pos=y1;
end
Stallion(i).group(j).pos=min(Stallion(i).group(j).pos,ub);
Stallion(i).group(j).pos=max(Stallion(i).group(j).pos,lb);
Stallion(i).group(j).cost=fobj(Stallion(i).group(j).pos);
end
% end
%
% for i=1:NStallion
R=rand;
% z=rand(1,dim)<TDR;
% r1=rand;
% r2=rand(1,dim);
% idx=(z==0);
% r3=r1.*idx+r2.*~idx;
% rr=-2+4*r3;
if R<0.5
k= 2*r3.*cos(2*pi*rr).*(WH.pos-(Stallion(i).pos))+WH.pos;
else
k= 2*r3.*cos(2*pi*rr).*(WH.pos-(Stallion(i).pos))-WH.pos;
end
k=min(k,ub);
k=max(k,lb);
fk=fobj(k);
if fk<Stallion(i).cost
Stallion(i).pos =k;
Stallion(i).cost=fk;
end
end
Stallion=exchange(Stallion);
[value,index]=min([Stallion.cost]);
if value<WH.cost
WH=Stallion(index);
end
gBest=WH.pos;
gBestScore=WH.cost;
Convergence_curve(l)=WH.cost;
disp(['Iteration ' num2str(l) ': Best Cost = ' num2str(Convergence_curve(l))]);
l = l + 1;
end
if i clear comment there be mistake when run main
Error in WHO (line 23)
ub=ones(1,dim)*ub;
Error in main (line 34)
[Convergence_curve,gBest,gBestScore]=WHO(N,Max_iter,lb,ub,dim,fobj);
Rose Toto
Rose Toto le 14 Mar 2023
Modifié(e) : Walter Roberson le 14 Mar 2023
this is sphere
function z = Sphere(x)
%% ______________________%loading data% __________________
%% ______________________loading data 33 bus ______________________
loadbus=load('loaddata331bus.m');
linedata=load('linedata331bus.m');
MVAb=100;
KVb=12.66;
%% ______________________loading data 69 bus ________________________
% loadbus=load('loaddata69bus.m');
% linedata=load('linedata69bus.m');
% MVAb=100;
% KVb=12.66;
%% ______________________loading data 85 bus _________________________
% loadbus=load('loaddata85bus.m');
% linedata=load('linedata85bus.m');
% MVAb=0.03675;
% KVb=11;
%% _______________________loading data 141 bus ________________________
% loadbus=load('loaddata141bus.m');
% linedata=load('linedata141bus.m');
% MVAb=100;
% KVb=12.47;
br=length(linedata);
nob=length(loadbus);
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Zb=(KVb^2)/MVAb;
for i=1:nob
P(i,1)=(loadbus(i,2)/(1000*MVAb));
Q(i,1)=(loadbus(i,3)/(1000*MVAb));
end
%% EVCS 1
optimal_loc_1=round(x(1));
P(optimal_loc_1,1)=P(optimal_loc_1,1)+x(2)/(1000*MVAb);
%% EVCS 2
optimal_loc_2=round(x(3));
P(optimal_loc_2,1)=P(optimal_loc_2,1)+x(4)/(1000*MVAb);
%% EVCS 3
optimal_loc_3=round(x(5));
P(optimal_loc_3,1)=P(optimal_loc_3,1)+x(6)/(1000*MVAb);
%% DG 1
optimal_loc_4=round(x(7));
P(optimal_loc_4,1)=P(optimal_loc_4,1)-x(8)/(1000*MVAb);
%% DG 2
optimal_loc_5=round(x(9));
P(optimal_loc_5,1)=P(optimal_loc_5,1)-x(10)/(1000*MVAb);
%% DG 3
optimal_loc_6=round(x(11));
P(optimal_loc_6,1)=P(optimal_loc_6,1)-x(12)/(1000*MVAb);
for i=1:br
R(i,1)=(linedata(i,4))/Zb;
X(i,1)=(linedata(i,5))/Zb;
end
R;X;P;Q;
%%%%%%%%%%%%%%%%%%%%%%to know end node%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
C=zeros(br,nob);
for i=1:br %%%%%%%for all branch no
a=linedata(i,2); %%%%%% sending bus
b=linedata(i,3); %%%%%% reciving bus
for j=1:nob
if a==j
C(i,j)=1;
end
if b==j
C(i,j)=2;
end
end
end
C;
e=1;
for i=1:nob
d=0;
for j=1:br
if C(j,i)==1
d=1;
end
end
if d==0
endnode(e,1)=i;
e=e+1;
end
end
endnode;
%%%%%%%%%%%%%%%%%%%%%%%%know path for each endnode%%%%%%%%%%%%%%%%%%%%%%%%%
h=length(endnode);
for j=1:h
e=2;
f=endnode(j,1); %%%%%%%store no endnode
for s=1:nob
if (f~=1)
k=1;
for i=1:br
if ((C(i,f)==2)&&(k==1))
f=i;
k=2;
end
end
k=1;
for i=1:nob
if ((C(f,i)==1)&&(k==1))
f=i;
g(j,e)=i;
e=e+1;
k=3;
end
end
end
end
end
for i=1:h
g(i,1)=endnode(i,1);
end
g;
w=length(g(1,:));
for i=1:h
j=1;
for k=1:nob
for t=1:w
if g(i,t)==k
g(i,t)=g(i,j);
g(i,j)=k;
j=j+1;
end
end
end
end
g;
for k=1:br
e=1;
for i=1:h
for j=1:w-1
if (g(i,j)==k)
if g(i,j+1)~=0
srn(k,e)=g(i,j+1);
e=e+1;
else
srn(k,1)=0;
end
end
end
end
end
srn;
for i=1:br-1
for j=h:-1:1
for k=j:-1:2
if srn(i,j)==srn(i,k-1)
srn(i,j)=0;
end
end
end
end
srn;
x=length(srn(:,1));
y=length(srn(1,:));
for i=1:x
for j=1:y
if srn(i,j)==0 && j~=y
if srn(i,j+1)~=0
srn(i,j)=srn(i,j+1);
srn(i,j+1)=0;
end
end
if srn(i,j)~=0
srn(i,j)=srn(i,j)-1;
end
end
end
srn;
for i=1:x-1
for j=1:y
srno(i,j)=srn(i+1,j);
end
end
b=length(srno);
srno;
%%%%%%%%%%%%%voltage and current calaulation%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:nob
Vb(i,1)=1;
end
voltage(:,1)=Vb;
for s=1:200
for i=1:nob
Lc(i,1)=conj((complex(P(i,1),Q(i,1)))/(Vb(i,1)));
end
Lc;
for i=1:br
bc(i,1)=Lc(i+1,1);
end
bc;
n=length(srno(1,:));
for i=br-1:-1:1
for k=1:n
if srno(i,k)~=0
u=srno(i,k);
bc(i,1)=bc(i,1)+bc(u,1);
end
end
end
bc;
for i=2:nob
g=0;
for a=1:b
if n>1
if srno(a,2)==i-1
u=srno(a,1);%%%%%%%bus 2,3,6
Vb(i,1)=((Vb(u,1))-((bc(i-1,1))*(complex((R(i-1,1)),X(i-1,1))))); %%%%%bus 19,23,26
g=1;
end
if srno(a,3)==i-1
u=srno(a,1);
Vb(i,1)=((Vb(u,1))-((bc(i-1,1))*(complex((R(i-1,1)),X(i-1,1)))));
g=1;
end
end
end
if g==0
Vb(i,1)=((Vb(i-1,1))-((bc(i-1,1))*(complex((R(i-1,1)),X(i-1,1)))));
end
end
s=s+1;
voltage(:,s)=Vb;
if abs(abs(voltage(:,s))-abs(voltage(:,s-1)))<0.00001 %%%%%%%tolerance%%%%%%%%%%%
break
end
end
Lc;
bc;
Vb;
vbp=[abs(Vb) angle(Vb)*180/pi];
for i=1:nob
va(i,2:3)=vbp(i,1:2);
end
for i=1:nob
va(i,1)=i;
end
va;
bcp=[abs(bc) angle(bc)*180/pi];
PL(1,1)=0;
QL(1,1)=0;
%% %%%%%%%%%%%losses%%%%%%%%%%%%%%%
for f=1:br
Pl(f,1)=(bcp(f,1)^2)*R(f,1);
Ql(f,1)=(bcp(f,1)^2)*X(f,1);
PL(1,1)=PL(1,1)+Pl(f,1);
QL(1,1)=QL(1,1)+Ql(f,1);
end
Plosseskw=(Pl)*(1000*MVAb);
Qlosseskw=(Ql)*(1000*MVAb);
PL=(PL)*(1000*MVAb);
QL=(QL)*(1000*MVAb);
voltage=vbp(:,1);
% angle=vbp(:,2)*(pi/180);
% figure;
% plot(loadbus(:,1),voltage(:,1),'-^b');
% grid on;
% hold on
%% return value
% P(x(1),1)=P(x(1),1)-x(2)/(1000*MVAb);
% R1=8 ;
% R2=12 ;
% R3=10 ;
% V=80;
z=PL;
end
Okay but we need the files 'loaddata331bus.m' and 'linedata331bus.m' to test with.
By the way, you can simplify your code:
% Original:
for i=1:nob
P(i,1)=(loadbus(i,2)/(1000*MVAb));
Q(i,1)=(loadbus(i,3)/(1000*MVAb));
end
% Easier without a loop:
P = loadbus(:, 2) / (1000 * MVAb);
Q = loadbus(:, 3) / (1000 * MVAb);
% Original:
%% EVCS 1
optimal_loc_1=round(x(1));
P(optimal_loc_1,1)=P(optimal_loc_1,1)+x(2)/(1000*MVAb);
%% EVCS 2
optimal_loc_2=round(x(3));
P(optimal_loc_2,1)=P(optimal_loc_2,1)+x(4)/(1000*MVAb);
%% EVCS 3
optimal_loc_3=round(x(5));
P(optimal_loc_3,1)=P(optimal_loc_3,1)+x(6)/(1000*MVAb);
%% DG 1
optimal_loc_4=round(x(7));
P(optimal_loc_4,1)=P(optimal_loc_4,1)-x(8)/(1000*MVAb);
%% DG 2
optimal_loc_5=round(x(9));
P(optimal_loc_5,1)=P(optimal_loc_5,1)-x(10)/(1000*MVAb);
%% DG 3
optimal_loc_6=round(x(11));
P(optimal_loc_6,1)=P(optimal_loc_6,1)-x(12)/(1000*MVAb);
% Easier with a loop:
for k = [1,3,5,7,9,11]
signx = 1 - 2*(k >= 7);
optimal_loc = round(x(k));
P(optimal_loc) = P(optimal_loc) + signx * x(k+1) / (1000*MVAb);
end
These lines have no effect and are confusing clutter only:
R;X;P;Q;
C;
e=1;
endnode;
g;
srn;
srn;
srn;
srno;
bc;
Lc;
bc;
Vb;
va;
Without a pre-allocation letting an array grow iteratively requires a lot of ressources:
for i=1:nob
va(i,2:3)=vbp(i,1:2);
end
for i=1:nob
va(i,1)=i;
end
Wither create va with a zeros() command before the loops, of easier without a loop:
va = [(1:nob).', vbp(:, 1:2)];
Note: Simpler code is faster and easier to debug.
Rose Toto
Rose Toto le 15 Mar 2023
Modifié(e) : Walter Roberson le 15 Mar 2023
this is line data33 -- linedata331bus.m
1 1 2 0.0922 0.0470
2 2 3 0.4930 0.2511
3 3 4 0.3660 0.1864
4 4 5 0.3811 0.1941
4 5 6 0.8190 0.7070
6 6 7 0.1872 0.6188
7 7 8 1.7114 1.2351
8 8 9 1.0300 0.7400
9 9 10 1.0440 0.7400
10 10 11 0.1966 0.0650
11 11 12 0.3744 0.1238
12 12 13 1.4680 1.1550
13 13 14 0.5416 0.7129
14 14 15 0.5910 0.5260
15 15 16 0.7463 0.5450
16 16 17 1.2890 1.7210
17 17 18 0.7320 0.5740
18 2 19 0.1640 0.1565
19 19 20 1.5042 1.3554
20 20 21 0.4095 0.4784
21 21 22 0.7089 0.9373
22 3 23 0.4512 0.3083
23 23 24 0.8980 0.7091
24 24 25 0.8960 0.7011
25 6 26 0.2030 0.1034
26 26 27 0.2842 0.1447
27 27 28 1.0590 0.9337
28 28 29 0.8042 0.7006
29 29 30 0.5075 0.2585
30 30 31 0.9744 0.9630
31 31 32 0.3105 0.3619
32 32 33 0.3410 0.5302
Rose Toto
Rose Toto le 15 Mar 2023
Modifié(e) : Walter Roberson le 15 Mar 2023
load data -- loaddata331bus.m
1 0 0 0
2 100 60 0
3 90 40 0
4 120 80 0
5 60 30 0
6 60 20 0
7 200 100 0
8 200 100 0
9 60 20 0
10 60 20 0
11 45 30 0
12 60 35 0
13 60 35 0
14 120 80 0
15 60 10 0
16 60 20 0
17 60 20 0
18 90 40 0
19 90 40 0
20 90 40 0
21 90 40 0
22 90 40 0
23 90 50 0
24 420 200 0
25 420 200 0
26 60 25 0
27 60 25 0
28 60 20 0
29 120 70 0
30 200 600 0
31 150 70 0
32 210 100 0
33 60 40 0
these lines on sphere
R;X;P;Q;
C;
e=1;
endnode;
g;
srn;
srn;
srn;
srno;
bc;
Lc;
bc;
Vb;
va;
Jan
Jan le 15 Mar 2023
I do not understand the meaning of these 3 comments.
i ask about your comment that these lines have no effect and are confusing clutter only:
those lines on sphere?
@Rose Toto: What does this sentence mean: "those lines on sphere?" On which sphere? Please add a verb to this sentence or explain in in other words.
I do not know what "this is line data33" means also and this is a puzzle also:
load data
1 0 0 0
2 100 60 0
...
What is the connection of this list of numbers to the problem?
The code line:
va;
does nothing. So it is a waste of time only. You use this repeatedly, but why?
Jan, they posted the contents of two needed data files that for unknown reason have been named as .m files. Those are the missing files needed to run.
these two files wanted to run main file and solve the problem
Jan
Jan le 16 Mar 2023
Sorry, as long as I cannot guess what "those lines on sphere" means, I'm out of this discussion, because I cannot understand the problem.
Jan, the OP is asking why you are saying that those particular lines in the function "sphere" are useless clutter.

Connectez-vous pour commenter.

Réponses (0)

Catégories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by