# 3.代码实现

``````close all;
clear all;
clc;
%--------------------初始化各参数值------------------------------------------------------
NP=100;
CRm=0.5;
G=100;
LP=10;
D=2;
a=-5;
b=5;
Aa=1;
A=[];
ikun1=0.25;
ikun2=0.25;
ikun3=0.25;
ikun4=0.25;
c=0;
t=0;
r=0;
l=0;
C=0;
T=0;
R=0;
L=0;
x=rand(NP,D)*(b-a)+a;        %随机生成初始种群
v=zeros(NP,D);
u=zeros(NP,D);
minx=9999;
best=0;
for i=1:NP                %求各个个体的适应度并取出适应度最好的个体的位置
fit(i)=4*x(i,1)^2-2.1*x(i,1)^4+1/3*x(i,1)^6+x(i,1)*x(i,2)-4*x(i,2)^2+4*x(i,2)^4;
if fit(i)<minx
minx=fit(i)
best=i;
end
end
trace(1)=minx;
for g=1:G                %开始迭代
num=randsrc(1,1,[1 2 3 4;ikun1 ikun2 ikun3 ikun4]);        %对选取的策略按照相应概率进行选取
F=normrnd(0,0.33333)*0.1+0.4;                        %生成符合正态分布的F值
CR=normrnd(0,0.33333)*((CRm-0.1)/2)+((CRm+0.1)/2);        %生成符合正态分布的CR值
%--------------------变异操作----------------------------------------------------------------
for m=1:NP
num1=randperm(NP,1);
if num1==m
num1=randperm(NP,1);
end
num2=randperm(NP,1);
if (num2==m)||(num2==num1)
num2=randperm(NP,1);
end
num3=randperm(NP,1);
if (num3==m)||(num3==num1)||(num3==num2)
num3=randperm(NP,1);
end
num4=randperm(NP,1);
if (num4==m)||(num4==num1)||(num4==num2)||(num4==num3)
num4=randperm(NP,1);
end
num5=randperm(NP,1);
if (num5==m)||(num5==num1)||(num5==num2)||(num5==num3)||(num5==num4)
num5=randperm(NP,1);
end
if num==1
C=C+1;
for i=1:NP
v(i,:)=x(num1,:)+F*(x(num2,:)-x(num3,:));
end
end
if num==2
T=T+1;
for i=1:NP
v(i,:)=x(best,:)+F*(x(num1,:)-x(num2,:))+F*(x(num3,:)-x(num4,:));
end
end
if num==3
R=R+1;
for i=1:NP
v(i,:)=x(num1,:)+F*(x(num2,:)-x(num3,:))+F*(x(num4,:)-x(num5,:));
end
end
if num==4
L=L+1;
for i=1:NP
v(i,:)=x(i,:)+F*(x(best,:)-x(i,:))+F*(x(num1,:)-x(num2,:));
end
end
end
%--------------------交叉操作--------------------------------------------------------
for n=1:1:D
cr=rand;
if(cr<=CR)||(n==r)
u(:,n)=v(:,n);
else
u(:,n)=x(:,n);
end
end
%--------------------修补染色体--------------------------------------------------------
for m=1:1:NP
for n=1:1:D
if u(m,n)<=a
u(m,n)=a;
end
if u(m,n)>=b
u(m,n)=b;
end
end
end
%--------------------重新求适应度，优胜劣汰----------------------
for i=1:NP
fit1(i)=4*u(i,1)^2-2.1*u(i,1)^4+1/3*u(i,1)^6+u(i,1)*u(i,2)-4*u(i,2)^2+4*u(i,2)^4;
if fit1(i)<minx
x(i,:)=u(i,:);
A(1,Aa)=CR;
Aa=Aa+1;
minx=fit1(i);
best=i;
end
end
trace(g+1)=minx;
%--------------------对CR进行自适应取值----------------------
if g==LP
CRm=mean(A(:));                %求所有发生进化的个体所选用的CR的平均值
Aa=1;
clear A；                    %清空重置A矩阵
A=[];
%--------------------根据所选策略与其成功概率决定以后该策略的选取概率----------------------
if num==1
c=c+1;
end
if num==2
t=t+1;
end
if num==3
r=r+1;
end
if num==4
l=l+1;
end
if C~=0&&c~=0
ikunn1=c/C;
else
ikunn1=0;
end
if T~=0&&t~=0
ikunn2=t/T;
else
ikunn2=0;
end
if R~=0&&r~=0
ikunn3=r/R;
else
ikunn3=0;
end
if L~=0&&l~=0
ikunn4=l/L;
else
ikunn4=0;
end
if ~(c==0&&t==0&&r==0&&l==0)
ikun1=ikunn1/(ikunn1+ikunn2+ikunn3+ikunn4);
ikun2=ikunn2/(ikunn1+ikunn2+ikunn3+ikunn4);
ikun3=ikunn3/(ikunn1+ikunn2+ikunn3+ikunn4);
ikun4=ikunn4/(ikunn1+ikunn2+ikunn3+ikunn4);
end
end
end
%--------------------结束迭代，绘制迭代次数与适应度的关系图----------------------
x(1,:);
figure(1);
title(['差分进化算法(DE)','最小值','num2str(tt)']);
xlabel('迭代次数');
ylabel('目标函数值');
plot(trace);
``````

THE END