差分进化算法

作者:神秘网友 发布时间:2020-09-07 13:53:32

差分进化算法

差分进化算法

差分进化算法(DE)是一种新兴的进化计算技术,它是由 Storn 等人于1995年提出,其最初的设想是用于解决切比雪夫多项式问题,后来成为解决复杂优化问题的有效技术。

    差分进化算法的操作如下:

    (1)初始化

    (2)变异

    (3)交叉

    (4)选择

    (5)边界条件处理

   差分进化算法流程图:

   差分进化算法

  matlab仿真实例:


  求函数f(x,y)=3cos(xy)+x+y的最小值,其中x的取值范围为[-4,4], y的取值范围为[-4,4](多个局部极值的函数)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%差分进化算法求函数极值%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%初始化%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all;
close all;
clc;
NP=20;     %种群数量
D=2;       %变量的维数
G=100;     %最大进化代数
F=0.5;     %变异算子
CR=0.1;    %交叉算子
Xs=4;      %上限
Xx=-4;     %下限

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%赋初值%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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)=func2(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,NP]);
     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)=func2(u(:,m));
     end
     
    for m=1:NP
        if Ob1(m)<Ob(m)      %小于先前的目标值
            x(:,m)=u(:,m);
        end
    end
    for m=1:NP
       Ob(m)=func2(x(:,m));
    end
    trace(gen+1)=min(Ob);
end
    [SortOb,Index]=sort(Ob);
    x=x(:,Index);
    X=x(:,1);          %最优变量
    Y=min(Ob);         %最优值
   disp('最优变量');
   disp(X);
   disp('最优值');
   disp(Y);
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%画图%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  figure
  plot(trace);
  %plot(X,Y,'-ro');
  xlabel('迭代次数');
  ylabel('目标函数值');
  title('DE目标函数曲线');
  

  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%适应度函数计算%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  function value=func2(x)
  value=3*cos(x(1)*x(2))+x(1)+x(2);
  end
运行效果如下:

  差分进化算法

差分进化算法


差分进化算法相关教程

  1. 【算法】2 由股票收益问题再看分治算法和递归式
  2. Jvm垃圾回收算法,相关概念和四种引用
  3. 排序算法 - 希尔排序(插入)
  4. 【算法】7 分不清栈和队列?一张图给你完整体会
  5. 游戏掉落概率算法
  6. 算法系列:动态规划详解(五)股票买卖
  7. Linux内存管理—buddy算法
  8. A*寻路算法入门(三)