function DE(Gm)
t0 = cputime;
Gm=1000;
Np=100;
D=30;
XG_next1 =zeros(Np,D);
XG_next2 =zeros(Gm,D);
XG_test = zeros(Np,D);
XG_next = zeros(Np,D);
t=1;
FEs=0;
MAX_FEs=1e+005;
NF=zeros(Gm,Np);
NCR=zeros(Gm,Np);
Fl=0.1;Fu=0.9;
F=zeros(Gm,Np);
CR=zeros(Gm,Np);
F(Gm,Np)=0.5;%rand(1,Np)*0.2+0.2;
CR(Gm,Np)=0.9;%rand(1,Np)*0.2+0.8;
value_test =zeros(Np,1);
Gmin = zeros(1,Gm); %各代的最优值
best_x = zeros(Gm,D); %各代的最优解
xmin = -10;%-5.12;%-100;%-500;%-50;%-600;%-1.28;%-30;%-100;%-32;%-1;%-2.048;%
xmax = 10;%5.12;%100;%500;%50;%600;%1.28;%30;%32;%1;%2.048;%
X0 = xmin + (xmax - xmin)*rand(Np,D);
XG = X0;
%*******************************************定义目标函数******
function y=f(XX)
%--------- 单峰值函数-----------------
% y=sum(XX.^2);%sphere[-100,100] %***f1*** /3.2416e-018
%-----------------------------------------
% y = sum(abs(XX))+prod(abs(XX));%Schwefel2.22[-10,10] %***f2***0.3589/1.5864e-011
%-------------------------------------------------
% y2=0;
% for n=1:D
% for m=1:n
% y1=sum(XX(m));
% end
% y2=y2+y1^2;
% end
% y=y2;%Schwefel1.2[-100,100] %***f3***1.2006/FCR不保存
%-------------------------------------------------
% y = max(abs(XX));%Schwefel2.21[-100,100] %***f4***11.6821/0.0454
%-------------------------------------------------
% y = sum(100 .*(XX(:,1:D-1).^2 - XX(:,2:D)).^2 + (XX(:,1:D-1)-1).^2, 2);%24.6650
% y = 0;
% for n = 1:(D-1)
% y= y + 100*(XX(n+1) - XX(n)^2)^2 + (XX(n+1) - 1)^2;%Rosenbrock[-30,30]
% end %***f5***
% y = y+390;
%-------------------------------------------------
% y = sum((ceil(XX+0.5)).^2);%Step[-100,100] %***f6***0
%-------------------------------------------------
% y=0;
% for n=1:D
% y=y+n*(XX(n)^4);
% end
% y = y+rand;%Quartic[-1.28,1.28] %***f7***0.0741
%-------------------------------------------------
% y = sum(-XX.*sin(sqrt(abs(XX))));%Schwefel2.26[-500,500] %***f8***
%-------------------------------------------------
% y = 418.98288727243369*D-sum(XX.*sin(sqrt(abs(XX))));%Generalized Schwefel's 2.26[-500,500]-3.6380e-012
%--------------------------------------------------
%-------------------------------------------------
% y=sum(XX.^2-10.*cos(2.*pi.*XX)+10);%Rastrigrin[-5.12,5.12]
%***f9****4.3820e-007/成功的CR不保存到下一代
%-------------------------------------------------
% y1 = 0;y2 = 0;
% for n = 1:D
% y1 = y1 + XX(n)^2;
% y2 = y2 + cos(2*pi*XX(n)); %***f10***1.6490e-010
% end
% y = -20*exp(-0.2*sqrt((1/D)*y1)) - exp(1/D*y2) + 20 + exp(1);%Ackley [-32,32]
%------------------------------------------------
% y1=1;
% for n=1:D
% y1=y1*cos(XX(n)/sqrt(n));
% end
% y = sum(XX.^2)/4000 - y1+1;%Griewank[-600,600] %***f11***0
%-------------------------------------------------
% y1=0;
% for n=1:D
% if XX(n)>10
% u(n)=100*(XX(n)-10)^4;
% elseif XX(n)<-10
% u(n)=100*(-XX(n)-10)^4;
% else
% u(n)=0; %***f12***1.9671e-019
% end %Penalized1[-50,50]
% if n<D
% y1=y1+((0.25*(XX(n)+1)).^2)*(1+10*(sin(pi*(1+0.25*(XX(n+1)+1)))).^2);
% end
% end
% y =(10*(sin(pi*(1+0.25*(XX(1)+1))))^2 +y1+(0.25*(XX(D)+1))^2)*pi/D +sum(u);
%----------------------------------------------------
% for n=1:D
% if XX(n)>5
% u(n)=100*(XX(n)-5)^4;
% elseif XX(n)<-5
% u(n)=100*(-XX(n)-5)^4;
% else
% u(n)=0; %***f13***2.1529e-018
% end %Penalized2[-50,50]
% end
% y = 0.1*((sin(3*pi*XX(1)))^2+sum((XX(:,1:D-1)-1).^2.*(1+(sin(3.*pi.*XX(:,2:D))).^2))+(XX(D)-1)^2*(1+(sin(2*pi*XX(D)))^2))+sum(u);
%----------------------------------------------------
% y1 = (abs(XX)<0.5).*XX+(abs(XX)>=0.5).*(round(XX.*2)./2); %***f14***1.0000
% y = sum(y1.^2 -10*cos(2.*pi.*y1) +10);%Noncontinuous Rastrigin’s function[-5.12,5.12]
%-------------------------------------------------
y = sum(abs(XX.*sin(XX)+0.1.*XX));%Alpine[-10,10] %***f15***3.8726e-004
%-------------------------------------------------
end
while FEs<MAX_FEs%t<=Gm%
for i=1:Np
value_test(i)=f(XG(i,:));
end
[value_min1,pos_min1]=min(value_test);
for i=1:Np
NF(t,i) = F(t,i);
if rand<0.1
NF(t,i)=Fl+Fu*rand;
end
NCR(t,i) = CR(t,i);
if rand<0.1
NCR(t,i)=rand;
end
a=1;
b=Np;
dx=randperm(b-a+1);
m=dx(1);
k=dx(2);
p=dx(3);
if m==i
m=dx(4);
else if k==i
k=dx(4);
else if p==i
p=dx(4);
end
end
end
randx = randperm(D);
for j=1:D
if rand<NCR(t,i) || j==randx(1);
XG_next1(i,j)=XG(m,j)+NF(t,i)*(XG(k,j)-XG(p,j));
else
XG_next1(i,j)=XG(i,j);
end
end
for j=1:D
if XG_next1(i,j)<xmin || XG_next1(i,j)>xmax%防止出界
XG_next1(i,j)=xmin+rand*(xmax-xmin);
end
end
if f(XG_next1(i,:))<=value_test(i)%选择
XG_next(i,:)=XG_next1(i,:);
F(t+1,i) = NF(t,i);
%%jDE中并未说明保存到下一代,但是测试时未达到效果,仅对于f14不保存到下一代
CR(t+1,i)=NCR(t,i);%并保存成功个体对应的参数
else
XG_next(i,:)=XG(i,:);
F(t+1,i)=F(t,i);
CR(t+1,i)=CR(t,i);
end
%FEs = FEs+1;
end
for i = 1:Np
value(i) = f(XG_next(i,:));
end
[value_min,pos_min] = min(value);
FEs = FEs+Np;
%第G代中的目标函数的最小值
Gmin(t) = value_min;
%保存最优的个体
best_x(t,:) = XG_next(pos_min,:);
yy(1,fix((FEs/10000))+1) = Gmin(fix((FEs/100)));
xx(1,fix((FEs/10000))+1) = fix((FEs));
% trace(t,1) = t;
% trace(t,2) = value_min;
XG = XG_next;
t=t+1;
end
[value_min,pos_min] = min(Gmin);
best_value = value_min
best_vector = best_x(pos_min,:)
fprintf('dgisDE所耗的时间为:%f \n',cputime - t0);
%画出代数跟最优函数值之间的关系图
% h1=semilogy(trace(:,1),trace(:,2),'-.m');%h1 = plot(trace(:,1),trace(:,2),'-.m');
h1 = semilogy(xx,yy,'-.k^');
xlabel('FEs')
ylabel('log(f(x))')
[legh,objh,outh,outm]=legend(h1,'jDE');
set(legh,'Box','off');
set(legh,'position',[0.75,0.8,0.1,0.1]);
hold on ;
end
评论3