?? de.m
字號:
function [Pb,trace]=DE
D=30;%維數(shù)
NP=40;%NP為種群規(guī)模----
F=0.5;%交叉因子
CR=0.1;%交叉概率
eps=1e-9;%精度
gen_max=500;%最大進化代數(shù)
trace=zeros(gen_max,2);
bounds=100*ones(D,2);
bounds(:,1)=-1*bounds(:,1);
rng=(bounds(:,2)-bounds(:,1))';
x=(ones(NP,1)*rng).*(rand(NP,D))+(ones(NP,1)*bounds(:,1)');%初始種群
count=1;
trial=zeros(1,D);
cost=zeros(1,NP);
cost(1)=fitness(x(1,:),D);
Pb=cost(1);%存放最優(yōu)值
Xb=x(1,:);%存放最優(yōu)位置
for i=2:NP
cost(i)=fitness(x(i,:),D);
if(cost(i)<=Pb)
Pb=cost(i);
Xb=x(i,:);
end
end
trace(1,1)=1;
trace(1,2)=Pb;
while(count<gen_max)%count<gen_max abs(Pb)>eps
for i=1:NP
while 2>1
a=floor(rand*NP)+1;
if a~=i
break;
end
end
while 2>1
b=floor(rand*NP)+1;
if b~=i&b~=a
break;
end
end
while 2>1
c=floor(rand*NP)+1;
if c~=i&c~=a&c~=b
break;
end
end
jrand=floor(rand*D+1);
for k=1:D
if(rand<CR|jrand==k)
trial(k)=x(c,k)+F*(x(a,k)-x(b,k));
else
trial(k)=x(i,k);
end
if trial(k)<bounds(k,1)
trial(k)=bounds(k,1);
end
if trial(k)>bounds(k,2)
trial(k)=bounds(k,2);
end
end
score=fitness(trial(:),D);
if(score<=cost(i))
x(i,1:D)=trial(1:D);
cost(i)=score;
end
if cost(i)<=Pb
Pb=cost(i);
Xb(1:D)=x(i,1:D);
end
end
count=count+1;
trace(count,1)=count;
trace(count,2)=Pb;
end
%--------------結束搜索---------------
count;
Pb;
Xb;
%-------函數(shù)值計算-----------------------
function eval=fitness(x,D)
sol=x;
eval=0;
for i=1:D
eval=eval+sol(i)^2;
end
%----------------------------------------------
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -