clear all;
close all;
size=500;
codel=2;
minx(1)=-2.048;
maxx(1)=2.048;
minx(2)=-2.048;
maxx(2)=2.048;
E(:,1)=minx(1)+(maxx(1)-minx(1))*rand(size,1);
E(:,2)=minx(2)+(maxx(2)-minx(2))*rand(size,1);
G=200;
BsJ=0;
for kg=1:1:G
time(kg)=kg;
for i=1:1:size
xi=E(i,:);
x1=xi(1);
x2=xi(2);
F(i)=100*(x1^2-x2)^2+(1-x1)^2;
Ji=1./F;
BsJi(i)=min(Ji);
end
[OderJi,IndexJi]=sort(BsJi);
BestJ(kg)=OderJi(1);
BJ=BestJ(kg);
Ji=BsJi+1e-10;
fi=F;
[Oderfi,Indexfi]=sort(fi);
Bestfi=Oderfi(size);
BestS=E(Indexfi(size),:);
bfi(kg)=Bestfi;
kg
BestS
fi_sum=sum(fi);
fi_size=(Oderfi/fi_sum)*size;
fi_s=floor(fi_size);
r=size-sum(fi_s);
Rest=fi_size-fi_s;
[RestValue,Index]=sort(Rest);
for i=size:-1:size-r+1
fi_s(Index(i))=fi_s(Index(i))+1;
end
k=1;
for i=size:-1:1
for j=1:1:fi_s(i)
TempE(k,:)=E(Indexfi(i),:);
k=k+1;
end
end
Pc=0.90;
for i=1:2:(size-1)
temp=rand;
if Pc>temp
alfa=rand;
TempE(i,:)=alfa*E(i+1,:)+(1-alfa)*E(i,:);
TempE(i+1,:)=alfa*E(i,:)+(1-alfa)*E(i+1,:);
end
end
TempE(size,:)=BestS;
E=TempE;
Pm=0.10-[1:1:size]*(0.01)/size;
Pm_rand=rand(size,codel);
Mean=(maxx+minx)/2;
Dif=(maxx-minx);
for i=1:1:size
for j=1:1:codel
if Pm(i)>Pm_rand(i,j)
TempE(i,j)=Mean(j)+Dif(j)*(rand-0.5);
end
end
end
TempE(size,:)=BestS;
E=TempE;
end
BestS
Bestfi
figure(1);
plot(time,BestJ,'k');
xlabel('Times');ylabel('Best J');
figure(2);
plot(time,bfi,'k');
xlabel('Times');ylabel('Best F');