二、部分源代码
clc;
clear;
close all;
tic;
%%%%%%%%%%%%%%1 参数设定%%%%%%%%%%%
N0=30;
itmax=30;
dim=30;
pmax=50;
smax=10;
smin=1;
n=3;
delta_initial=3;
delta_final=0.001;
G=0.5;
CR=0.1;
xini=-40+10*rand(N0,dim);
%%%%%%%%%%%%%%%%%%%%%%主循环%%%%%%%%%%%%%%%%%%%%%%%%
iter=1;
while iter<=itmax
Niter=N0;
e=[];
for i=1:Niter
fit=sum(xini(i,:).^2);
e=[e;fit];
end
%%%%%%%%%%%%2 生长繁殖%%%%%%%%%%
Nseed=round(-(smax-smin)/(max(e)-min(e))*(e-min(e))+smax);
%%%%%%%%%%%%%3 空间扩散%%%%%%%%%
delta_iter=(itmax-iter)^n/(itmax)^n*(delta_initial-delta_final)+delta_final;
M=size(Nseed);
[aa bb]=sort(e);
fit_median=mean(e);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
m={};
a=[];
b=[];
for i=1:M(1,1)
%%%%%%%%%%%%%%%%%%%%%%%%%%%
if e(i)>=fit_median
delta_i=(1+0.5*(e(i)-fit_median)/(max(e)-fit_median))*delta_iter;
else
delta_i=(1-0.5*(fit_median-e(i))/(fit_median-min(e)))*delta_iter;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for j=1:dim
% x_iter=normrnd(xini(i,j),delta_iter^2,[1,Nseed(i,1)]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
k=rand();
x_iter=(rand()+k*rand()-k)*normrnd(xini(i,j),delta_iter^2,[1,Nseed(i,1)])+1e-006*rand();
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
a=x_iter;
b=[b,a'];
end
m{i}=b';
b=[];
end
f=[];
for i=1:M(1,1)
for j=1:Nseed(i,1)
c=m{i};
d=c(:,j);
fitness_seed=sum(d(:,1).^2);
end
X=[m,xini]; %新种群
F=[e;f]; %新种群的适应度值
Niter=Niter+sum(Nseed(:,1)); %新种群个体数量
V=size(X);
q=[];
h=X{i};
B=size(h);
for j=1:B(1,2)
l=h(:,j);
q=[q,l];
end
end
o=q';
%%%%%%%%%%4 竞争排除%%%%%%%%%%%%
fitmin=g(1:pmax,1);
Niter=pmax;
else
fitmin=g;
end
xini=q(:,p(1:Niter,1));
iter=iter+1
yy=min(fitmin);
end
%%%%%%%%%%%%%对经过一代繁殖后的杂草种群进行DE操作,产生新种群%%%%%%%%%%%%
it=1;
while it<=50
v=zeros(Niter,dim);
u=zeros(Niter,dim);
for i=1:Niter
p=randint(3,1,[1 Niter]);
r1=p(1);
r2=p(2);
r3=p(3);
for j=1:dim
v(i,j)=o(r1,j)+G*(o(r2,j)-o(r3,j));
rk=randint(1,1,[1 dim]);
else
u(i,j)=o(i,j);
end
end
fitu=sum(u(i,:).^2);
if fitu<F(i)
o(i,:)=u(i,:);
F(i)=fitu;
end
end
it=it+1
end
F
figure(1)
plot(1:itmax,EB(1:itmax))
y=min(fitmin);
y
toc
三、运行结果