⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 de.m

📁 差分进化算法,是一种新兴起的智能计算方法.它能求解各种无约束优化问题.
💻 M
字号:
function [Pb,trace]=DE
D=30;%维数
NP=25;%NP为种群规模----
F=0.5;%交叉因子
CR=0.1;%交叉概率
eps=1e-9;%精度
gen_max=800;%最大进化代数
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);%存放最优值
Xb=x(1,:);%存放最优位置
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;
%-------函数值计算-----------------------
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 + -