Subscript indices must either be real positive integers or logicals?
Show older comments
The below code is not able to execute due to the stated error in the line "U(i,:)=max(lb,U(i,:))" , but I really cant find any error.
tic
%%prob settings
lb=[-100 -100 ];
ub= [100 100 ];
%% parameters for differential evolution
np=10;
max=500;
%it=100;
cr=0.7;
F=0.8;
e=exp(1);
%x_ss=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0];
%% START of DE
f=NaN(np,1);
fu=NaN(np,1);
d=length(lb);
U=NaN(np,d);
p= repmat(lb,np,1)+(repmat((ub-lb),np,1).*rand(np,d));
for i=1:np
x=p(i,1);
y=p(i,2);
f(i)= -(20*exp(-0.2*sqrt((1/2)*(x^2+y^2))))+20+e-exp((1/2)*(cos(2*pi*x)+cos(2*pi*y)));
end
%% iteration loop
for it=1:max
for i=1:np
candidates=[1:i-1 i+1:np];
idx= candidates(randperm(np-1,3));
X1= p(idx(1),:);
X2= p(idx(2),:);
X3= p(idx(3),:);
v= X1+F*(X2-X3);
for j=1:d
irand= randi(d,1);
if (rand<=cr || j==irand)
U(i,j)= v(j);
else
U(i,j)=p(i,j);
end
end
end
%% fitness function
for i=1:np
U(i,:)= min(ub,U(i,:));
U(i,:)= max(lb,U(i,:));
A=U(i,:);
x=A(1);
y=A(2);
fu(i)= -(20*exp(-0.2*sqrt((1/2)*(x^2+y^2))))+20+e-exp((1/2)*(cos(2*pi*x)+cos(2*pi*y)));
A=p(i,:);
x=A(1);
y=A(2);
f(i)= -(20*exp(-0.2*sqrt((1/2)*(x^2+y^2))))+20+e-exp((1/2)*(cos(2*pi*x)+cos(2*pi*y)));
if (fu(i)<f(i))
p(i,:)= U(i,:);
f(i)=fu(i);
else
p(i,:)=p(i,:);
end
end
end
toc
[bestfitness ind]= min(f)
bestsol=p(ind,:)
Accepted Answer
More Answers (1)
Image Analyst
on 29 May 2021
1 vote
A thorough explanation is given in the FAQ:
Categories
Find more on Introduction to Installation and Licensing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!