C/C++教程

matlab实例,基于Ackley‘s function的,差分进化算法和粒子群算法的比较

本文主要是介绍matlab实例,基于Ackley‘s function的,差分进化算法和粒子群算法的比较,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

基于Ackley's function的,差分进化算法和粒子群算法的比较。

以下是matlab源码。

%%%%%%%%%%%%

%%%%%%%%%%%%  Ackley's function

clear all;                           %清除所有变量

close all;                           %清图

clc;                                 %清屏

x=-5:0.01:5;

y=-5:0.01:5;

N=size(x,2);

for i=1:N

    for j=1:N

        z(i,j)=-20*exp(-0.2*(0.5*(x(i)^2+y(j)^2))^0.5)-exp(0.5*(cos(2*pi*x(i))+cos(2*pi*y(j))))+20+exp(1);

    end

end

mesh(x,y,z)

xlabel('x')

ylabel('y')

 

粒子群算法:

%%%%%%%%%%%%%%%%%粒子群算法求函数极值%%%%%%%%%%%%%%%%%%%%%%%%%%

tic %程序运行起点

time =[];

for i=1:500

    time(i)=1;

end

%%%%%%%%%%%%%%%%%%%%%%%%初始化%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear all;              %清除所有变量

close all;              %清图

clc;                    %清屏

N=100;                  %群体粒子个数

D=2;                    %粒子维数

T=200;                  %最大迭代次数

c1=1.5;                 %学习因子1

c2=1.5;                 %学习因子2

Wmax=0.8;               %惯性权重最大值

Wmin=0.4;               %惯性权重最小值

Xmax=5;                 %位置最大值

Xmin=-5;                %位置最小值

Vmax=1;                 %速度最大值

Vmin=-1;                %速度最小值

%%%%%%%%%%%%%%%%初始化种群个体(限定位置和速度)%%%%%%%%%%%%%%%%

x=rand(N,D) * (Xmax-Xmin)+Xmin;

v=rand(N,D) * (Vmax-Vmin)+Vmin;

%%%%%%%%%%%%%%%%%%初始化个体最优位置和最优值%%%%%%%%%%%%%%%%%%%

p=x;

pbest=ones(N,1);

for i=1:N

    pbest(i)=func_Ackley(x(i,:));

end

%%%%%%%%%%%%%%%%%%%初始化全局最优位置和最优值%%%%%%%%%%%%%%%%%%

g=ones(1,D);

gbest=inf;

for i=1:N

    if(pbest(i)<gbest)

        g=p(i,:);

        gbest=pbest(i);

    end

end

gb=ones(1,T);

%%%%%%%%%%%按照公式依次迭代直到满足精度或者迭代次数%%%%%%%%%%%%%

for i=1:T

    for j=1:N

        %%%%%%%%%%%%%%更新个体最优位置和最优值%%%%%%%%%%%%%%%%%

        if (func_Ackley(x(j,:))<pbest(j))

            p(j,:)=x(j,:);

            pbest(j)=func_Ackley(x(j,:));

        end

        %%%%%%%%%%%%%%%%更新全局最优位置和最优值%%%%%%%%%%%%%%%

        if(pbest(j)<gbest)

            g=p(j,:);

            gbest=pbest(j);

        end

        %%%%%%%%%%%%%%%%计算动态惯性权重值%%%%%%%%%%%%%%%%%%%%

        w=Wmax-(Wmax-Wmin)*i/T;

        %%%%%%%%%%%%%%%%%跟新位置和速度值%%%%%%%%%%%%%%%%%%%%%

        v(j,:)=w*v(j,:)+c1*rand*(p(j,:)-x(j,:))...

            +c2*rand*(g-x(j,:));

        x(j,:)=x(j,:)+v(j,:);

        %%%%%%%%%%%%%%%%%%%%边界条件处理%%%%%%%%%%%%%%%%%%%%%%

        for ii=1:D

            if (v(j,ii)>Vmax)  |  (v(j,ii)< Vmin)

                v(j,ii)=rand * (Vmax-Vmin)+Vmin;

            end

            if (x(j,ii)>Xmax)  |  (x(j,ii)< Xmin)

                x(j,ii)=rand * (Xmax-Xmin)+Xmin;

            end

        end

    end

    %%%%%%%%%%%%%%%%%%%%记录历代全局最优值%%%%%%%%%%%%%%%%%%%%%

    gb(i)=gbest;

end

g;                         %最优个体        

gb(end);                   %最优值

figure

plot(gb)

xlabel('迭代次数');

ylabel('适应度值');

title('适应度进化曲线')

toc %程序终点,,计时结束

%%%%%%%%%%%%%%%%%%%%%适应度函数%%%%%%%%%%%%%%%%%%%%%%%

function value=func_Ackley(x)

value=-20*exp(-0.2*(0.5*(x(1)^2+x(2)^2)).^0.5)-exp(0.5*(cos(2*pi*x(1))+cos(2*pi*x(2))))+20+exp(1);

 

差分进化算法:

%%%%%%%%%%%%%%%%%差分进化算法求函数极值%%%%%%%%%%%%%%%%%

tic %程序运行起点

time =[];

for i=1:500

    time(i)=1;

end

%%%%%%%%%%%%%%%%%%%%%%%%%初始化%%%%%%%%%%%%%%%%%%%%%%%%

clear all;                            %清除所有变量

close all;                            %清图

clc;                                  %清屏

NP=20;                                %个体数目

D=2;                                  %变量的维数

G=200;                                %最大进化代数

F=0.5;                                %变异算子

CR=0.1;                               %交叉算子

Xs=5;                                 %上限

Xx=-5;                                %下限

%%%%%%%%%%%%%%%%%%%%%%%%%赋初值%%%%%%%%%%%%%%%%%%%%%%%%

x=zeros(D,NP);                        %初始种群

v=zeros(D,NP);                        %变异种群

u=zeros(D,NP);                        %选择种群

x=rand(D,NP)*(Xs-Xx)+Xx;              %赋初值

%%%%%%%%%%%%%%%%%%%%计算目标函数%%%%%%%%%%%%%%%%%%%%%%%

for m=1:NP

    Ob(m)=func_Ackley(x(:,m));

end

trace(1)=min(Ob);

%%%%%%%%%%%%%%%%%%%%%%%差分进化循环%%%%%%%%%%%%%%%%%%%%%

for gen=1:G

    %%%%%%%%%%%%%%%%%%%%%%变异操作%%%%%%%%%%%%%%%%%%%%%%

    %%%%%%%%%%%%%%%%%%r1,r2,r3和m互不相同%%%%%%%%%%%%%%%

    for m=1:NP

        r1=randint(1,1,[1,NP]);

        while (r1==m)

            r1=randint(1,1,[1,NP]);

        end

        r2=randint(1,1,[1,NP]);

        while (r2==m)|(r2==r1)

            r2=randint(1,1,[1,NP]);

        end

        r3=randint(1,1,[1,NP]);

        while (r3==m)|(r3==r1)|(r3==r2)

            r3=randint(1,1,[1,NP]);

        end

        v(:,m)=x(:,r1)+F*(x(:,r2)-x(:,r3));

    end

    %%%%%%%%%%%%%%%%%%%%%%交叉操作%%%%%%%%%%%%%%%%%%%%%%%

    r=randint(1,1,[1,D]);

    for n=1:D

        cr=rand(1);

        if (cr<=CR)|(n==r)

            u(n,:)=v(n,:);

        else

            u(n,:)=x(n,:);

        end

    end

    %%%%%%%%%%%%%%%%%%%边界条件的处理%%%%%%%%%%%%%%%%%%%%

    %%%%%%%%%%%%%%%%%%%%边界吸收%%%%%%%%%%%%%%%%%%%%%%%%%

    for n=1:D

        for m=1:NP

            if u(n,m)<Xx

                u(n,m)=Xx;

            end

            if u(n,m)>Xs

                u(n,m)=Xs;

            end

        end

    end

    %%%%%%%%%%%%%%%%%%%%%%选择操作%%%%%%%%%%%%%%%%%%%%%%%

    for m=1:NP

        Ob1(m)=func_Ackley(u(:,m));

    end

    for m=1:NP

        if Ob1(m)<Ob(m)

            x(:,m)=u(:,m);

        end

    end

    for m=1:NP

        Ob(m)=func_Ackley(x(:,m));

    end

    trace(gen+1)=min(Ob);

end

[SortOb,Index]=sort(Ob);

x=x(:,Index);

X=x(:,1);                              %最优变量

Y=min(Ob);                             %最优值

%%%%%%%%%%%%%%%%%%%%%%%%%画图%%%%%%%%%%%%%%%%%%%%%%%%%%

figure

plot(trace);

xlabel('迭代次数')

ylabel('目标函数值')

title('DE目标函数曲线')

toc %程序终点,,计时结束

两种算法的运行结果与时间结果如上图,可以看到两种算法均可算出最优解为0,但粒子群算法速度较快。

这篇关于matlab实例,基于Ackley‘s function的,差分进化算法和粒子群算法的比较的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!