多语言展示
当前在线:1090今日阅读:167今日分享:16

根据差分进化算法编写Matlab程序的过程

根据文章《Differential Evolution Algorithm With Strategy Adaptation for Global Numerical Optimization》的算法:ALGORITHMIC DESCRIPTION OF DE来说明如何根据算法编写差分进化(DE)的Matlab程序。程序与算法的步骤是一一对应的,该程序为标准的差分进化算法程序,变异、交叉和测试函数都采用函数进行封装,在主程序中调用即可,因此程序的可移植性强,下面将详细说明编程的过程。%@written by Zhan Qian,2015-5-24%测试函数求值用函数testFun(x,FunIndex)%变异向量用函数mutation(X,bestX,F,mutationStrategy)%交叉向量用函数crossover(X,V,CR,crossStrategy)%mutation%mutationStrategy=1:DE/rand/1,%mutationStrategy=2:DE/best/1,%mutationStrategy=3:DE/rand-to-best/1,%mutationStrategy=4:DE/best/2,%mutationStrategy=5:DE/rand/2.%crossover%crossStrategy=1:binomial crossover%crossStrategy=2:Exponential crossover程序可以在下列地址进行下载:http://pan.baidu.com/s/1gdAgXzH
工具/原料

Matlab

方法/步骤
1

所根据的算法如图1所示

2

首先定义常量,包括最大迭代次数、搜索范围、个体维度、缩放因子等。程序如下maxIteration=1000;%最大迭代次数Generation=0;%进化代数,或者当前迭代代数Xmax=30;%搜索上界,可以根据需要改为向量形式Xmin=-30;%搜索下界Dim=30;%个体维数NP=50;%population size,种群规模F=0.5;%scaling factor 缩放因子CR=0.3;%crossover rate 交叉概率FunIndex=3;%测试方程索引mutationStrategy=1;%变异策略crossStrategy=1;%交叉策略

3

步骤1:对应算法中Step 1,即初始化X=(Xmax-Xmin)*rand(NP,Dim)+Xmin;%X:行代表个体i,列代表i的维度j

4

步骤2:对应算法中Step 2:%step2 mutation,crossover,selectionwhile Generation

5

步骤3:显示结果%画图%plot(bestfitnessG);optValue=num2str(fitnessbestX);Location=num2str(bestX);disp(strcat('the optimal value','=',optValue));disp(strcat('the best location','=',Location));

6

%变异向量用函数mutation(X,bestX,F,mutationStrategy)function V=mutation(X,bestX,F,mutationStrategy)NP=length(X);for i=1:NP    %在[1 NP]中产生nrandI个互不相等的随机数,且与i皆不相等    nrandI=5;    r=randi([1,NP],1,nrandI);    for j=1:nrandI    equalr(j)=sum(r==r(j));    end    equali=sum(r==i);    equalval=sum(equalr)+equali;    while(equalval>nrandI)        r=randi([1,NP],1,nrandI);        for j=1:nrandI        equalr(j)=sum(r==r(j));        end        equali=sum(r==i);        equalval=sum(equalr)+equali;    end        switch mutationStrategy        case 1            %mutationStrategy=1:DE/rand/1;            V(i,:)=X(r(1),:)+F*(X(r(2),:)-X(r(3),:));        case 2            %mutationStrategy=2:DE/best/1;            V(i,:)=bestX+F*(X(r(1),:)-X(r(2),:));        case 3            %mutationStrategy=3:DE/rand-to-best/1;            V(i,:)=X(i,:)+F*(bestX-X(i,:))+F*(X(r(1),:)-X(r(2),:));        case 4            %mutationStrategy=4:DE/best/2;            V(i,:)=bestX+F*(X(r(1),:)-X(r(2),:))+F*(X(r(3),:)-X(r(4),:));        case 5            %mutationStrategy=5:DE/rand/2;            V(i,:)=X(r(1),:)+F*(X(r(2),:)-X(r(3),:))+F*(X(r(4),:)-X(r(5),:));        otherwise            error('没有所指定的变异策略,请重新设定mutationStrategy的值');    end    end

7

%交叉函数,根据算法中提供的两种交叉方法编写,即binomial crossover和%Exponential crossoverfunction U=crossover(X,V,CR,crossStrategy)%V为产生的变异向量[NP,Dim]=size(X);switch crossStrategy    %crossStrategy=1:binomial crossover    case 1        for i=1:NP            jRand=floor(rand*Dim);%由于jRand要在[0,1)*Dim中取值,故而用fl%oor            for j=1:Dim                if rand

8

%测试函数,可以根据需要添加相应的程序function y=testFun(x,index)%x代表参数,index代表测试的函数的选择%该测试函数为通用测试函数,可以移植%目录%  函数名            位置                   最优值%1.Sphere             0                       0%2.Camel             多个      %3.Rosenbrockswitch index    case 1 %Sphere函数        y=sum(x.^2);    case 2 %Camel函数,Dim只能取2        if length(x)>2            error('x的维度超出了2');        end        xx=x(1);yy=x(2);y=(4-2.1*xx^2+xx^4/3)*xx^2+xx*yy+(-4+4*yy^2)*yy^2;    case 3 %Rosenbrock函数        y=0;        for i=2:length(x)        y=y+100*(x(i)-x(i-1)^2)^2+(x(i-1)-1)^2;        end    otherwise        disp('no such function, please choose another');end

注意事项

程序下载地址: http://pan.baidu.com/s/1gdAgXzH

推荐信息