• 基于分解的多目标进化优化MOEA/D之切比雪夫方法代码


    基于分解的多目标进化优化MOEA/D之切比雪夫方法的理解见上一篇博文。

    这是晓风wangchao  (博客地址:https://blog.csdn.net/qq_40434430)写的代码,是我在网上下载的MOEA/D代码中,我认为写的最好的一版。代码下载链接:https://download.csdn.net/download/jianan_ouyang/12347822

    MOEAD.m

      1 %----------------------------------------------------------------------
      2 %程序功能:实现MOEAD算法,测试函数为ZDT1,ZDT2,ZDT3,ZDT4,ZDT6,DTLZ1,DTLZ2
      3 %说明:遗传算子为模拟二进制交叉和多项式变异
      4 %作者:(晓风)
      5 %email: 18821709267@163.com 
      6 %最初建立时间:2018.09.30
      7 %最近修改时间:2018.10.08
      8 %参考论文:
      9 %MOEA/D: A Multiobjective Evolutionary Algorithm Based on Decomposition
     10 %Qingfu Zhang, Senior Member, IEEE, and Hui Li
     11 %IEEE TRANSACTIONS O
     12 %----------------------------------------------------------
     13 clear all
     14 clc
     15 tic;
     16 %------------------------参数输入--------------------------
     17 format long
     18 global x_max x_min x_num f_num lamda z
     19 rand('state',sum(100*clock));
     20 N=300;%种群大小
     21 T=20;%邻居规模大小
     22 fun='DTLZ2';%
     23 funfun;%测试函数
     24 lamda=genrate_lamda(N,f_num);%均匀分布的N个权重向量
     25 max_gen=250;%进化代数
     26 pc=1;%交叉概率
     27 pm=1/x_num;%变异概率
     28 yita1=2;%模拟二进制交叉参数2
     29 yita2=5;%多项式变异参数5
     30 %------------------------初始条件--------------------------
     31 %%计算任意两个权重向量间的欧式距离,查找每个权向量最近的T个权重向量的索引
     32 B=look_neighbor(lamda,T);
     33 %%在可行空间均匀随机产生初始种群
     34 X=initialize(N,f_num,x_num,x_min,x_max,fun);
     35 %%初始化z
     36 for i=1:f_num
     37     z(i) = min(X(:,x_num+i));
     38 end
     39 %%初始化是否为非支配个体
     40 X=deterdomination(X,N,f_num,x_num);
     41 %%设置EP为初始种群里的非支配个体
     42 EP=[];
     43 for i=1:N
     44     if(X(i,x_num+f_num+1)==1)
     45         EP=[X(i,:);EP];
     46     end
     47 end
     48 %------------------------迭代更新--------------------------
     49 for gen=1:max_gen
     50     for i=1:N
     51         %%基因重组,从B(i)中随机选取两个序列k,l
     52         index1 = randperm(T);
     53         parent1 = B(i,index1(1));
     54         parent2 = B(i,index1(2));
     55         off=cross_mutation(X(parent1,:),X(parent2,:),f_num,x_num,x_min,x_max,pc,pm,yita1,yita2,fun );
     56         %off=cross_mutation2(X(parent1,:),X(parent2,:),f_num,x_num,x_min,x_max,pc,pm,yita1,yita2,fun );
     57         %%更新z
     58         for j=1:f_num
     59             %%if(Zi<fi(y')),zi=fi(y')
     60             z(j)=min(z(j),off(:,x_num+j));
     61         end
     62         %%更新领域解
     63         X=updateNeighbor(lamda,z,X,B(i,:),off,x_num,f_num);
     64         %%更新EP
     65         [number,~]=size(EP);
     66         temp=0;
     67         kk=[];
     68         for k=1:number
     69             less=0;%y'的目标函数值小于个体的目标函数值数目
     70             equal=0;%y'的目标函数值等于个体的目标函数值数目
     71             greater=0;%y'的目标函数值大于个体的目标函数值数目
     72             for mm=1:f_num
     73                 if(off(:,x_num+mm)>EP(k,x_num+mm))
     74                     greater=greater+1;
     75                 elseif(off(:,x_num+mm)==EP(k,x_num+mm))
     76                     equal=equal+1;
     77                 else
     78                     less=less+1;
     79                 end
     80             end
     81             %%%从EP中移除被y'支配的向量
     82             if(greater==0 && equal~=f_num)%y'支配EP中的第K个个体
     83                 kk=[k kk];
     84             end
     85             %%%如果EP中没有支配y'的个体,将y'加入EP
     86             if(less==0 && equal~=f_num)%EP中的第K个个体支配y'
     87                 temp=1;
     88             end
     89         end
     90         if(isempty(kk)==0)
     91             EP(kk,:)=[];
     92         end
     93         if(temp==0)
     94             EP=[EP;off];
     95         end
     96     end
     97     if mod(gen,10) == 0
     98         fprintf('%d gen has completed!
    ',gen);
     99     end
    100 %     if f_num==2
    101 %         plot(EP(:,x_num+1),EP(:,x_num+2),'r*');
    102 %     elseif f_num==3
    103 %         plot3( EP(:,x_num+1), EP(:,x_num+2),EP(:,x_num+3),'r*' );
    104 %         set(gca,'xdir','reverse'); set(gca,'ydir','reverse');
    105 %     end
    106 %     title(num2str(gen));
    107 %     drawnow
    108 end
    109 filepath=pwd;          
    110 cd('G:xjllearnPromatlabLearn临时数据EP_DTLZ2');
    111 save solution5.txt EP -ASCII
    112 cd(filepath);
    113 toc;
    114 %------------------------画图对比--------------------------
    115 if f_num==2
    116     hold on
    117     plot(EP(:,x_num+1),EP(:,x_num+2),'r*');
    118 elseif f_num==3
    119     hold on
    120     plot3( EP(:,x_num+1), EP(:,x_num+2),EP(:,x_num+3),'r*' );
    121     set(gca,'xdir','reverse'); set(gca,'ydir','reverse');
    122 end
    123 % figure;
    124 % if f_num==2
    125 %     hold on
    126 %     plot(X(:,x_num+1),X(:,x_num+2),'r*');
    127 % elseif f_num==3
    128 %     hold on
    129 %     plot3( X(:,x_num+1), X(:,x_num+2),X(:,x_num+3),'r*' );
    130 %     set(gca,'xdir','reverse'); set(gca,'ydir','reverse');
    131 % end
    132 %--------------------Coverage(C-metric)---------------------
    133 A=PP;B=EP(:,(x_num+1):(x_num+f_num));%%%%%%%%%%%%%%%%%%%%
    134 [temp_A,~]=size(A);
    135 [temp_B,~]=size(B);
    136 number=0;
    137 for i=1:temp_B
    138     nn=0;
    139     for j=1:temp_A
    140         less=0;%当前个体的目标函数值小于多少个体的数目
    141         equal=0;%当前个体的目标函数值等于多少个体的数目
    142         for k=1:f_num
    143             if(B(i,k)<A(j,k))
    144                 less=less+1;
    145             elseif(B(i,k)==A(j,k))
    146                 equal=equal+1;
    147             end
    148         end
    149         if(less==0 && equal~=f_num)%B(i)被A(j)支配
    150             nn=nn+1;%被支配个体数目n+1
    151         end
    152     end
    153     if(nn~=0)
    154         number=number+1;
    155     end
    156 end
    157 C_AB=number/temp_B;
    158 disp('C_AB:');
    159 disp(C_AB);
    160 %-----Distance from Representatives in the PF(D-metric)-----
    161 A=EP(:,(x_num+1):(x_num+f_num));P=PP;%%%%%%%%%%%%%%%%%%%
    162 [temp_A,~]=size(A);
    163 [temp_P,~]=size(P);
    164 min_d=0;
    165 for v=1:temp_P
    166     d_va=(A-repmat(P(v,:),temp_A,1)).^2;
    167     min_d=min_d+min(sqrt(sum(d_va,2)));
    168 end
    169 D_AP=(min_d/temp_P);
    170 disp('D_AP:');
    171 disp(D_AP);
    172 filepath=pwd;          
    173 cd('G:xjllearnPromatlabLearn临时数据EP_DTLZ2');
    174 save C_AB5.txt C_AB -ASCII
    175 save D_AP5.txt D_AP -ASCII
    176 cd(filepath);

    funfun.m

     1 %--------------------ZDT1--------------------
     2 if strcmp(fun,'ZDT1')
     3     f_num=2;%目标函数个数
     4     x_num=30;%决策变量个数
     5     x_min=zeros(1,x_num);%决策变量的最小值
     6     x_max=ones(1,x_num);%决策变量的最大值
     7     load('ZDT1.txt');%导入正确的前端解
     8     plot(ZDT1(:,1),ZDT1(:,2),'g*');
     9     PP=ZDT1;
    10 end
    11 %--------------------ZDT2--------------------
    12 if strcmp(fun,'ZDT2')
    13     f_num=2;%目标函数个数
    14     x_num=30;%决策变量个数
    15     x_min=zeros(1,x_num);%决策变量的最小值
    16     x_max=ones(1,x_num);%决策变量的最大值
    17     load('ZDT2.txt');%导入正确的前端解
    18     plot(ZDT2(:,1),ZDT2(:,2),'g*');
    19     PP=ZDT2;
    20 end
    21 %--------------------ZDT3--------------------
    22 if strcmp(fun,'ZDT3')
    23     f_num=2;%目标函数个数
    24     x_num=30;%决策变量个数
    25     x_min=zeros(1,x_num);%决策变量的最小值
    26     x_max=ones(1,x_num);%决策变量的最大值
    27     load('ZDT3.txt');%导入正确的前端解
    28     plot(ZDT3(:,1),ZDT3(:,2),'g*');
    29     PP=ZDT3;
    30 end
    31 %--------------------ZDT4--------------------
    32 if strcmp(fun,'ZDT4')
    33     f_num=2;%目标函数个数
    34     x_num=10;%决策变量个数
    35     x_min=[0,-5,-5,-5,-5,-5,-5,-5,-5,-5];%决策变量的最小值
    36     x_max=[1,5,5,5,5,5,5,5,5,5];%决策变量的最大值
    37     load('ZDT4.txt');%导入正确的前端解
    38     plot(ZDT4(:,1),ZDT4(:,2),'g*');
    39     PP=ZDT4;
    40 end
    41 %--------------------ZDT6--------------------
    42 if strcmp(fun,'ZDT6')
    43     f_num=2;%目标函数个数
    44     x_num=10;%决策变量个数
    45     x_min=zeros(1,x_num);%决策变量的最小值
    46     x_max=ones(1,x_num);%决策变量的最大值
    47     load('ZDT6.txt');%导入正确的前端解
    48     plot(ZDT6(:,1),ZDT6(:,2),'g*');
    49     PP=ZDT6;
    50 end
    51 %-------------------DTLZ1--------------------
    52 if strcmp(fun,'DTLZ1')
    53     f_num=3;%目标函数个数
    54     x_num=10;%决策变量个数
    55     x_min=zeros(1,x_num);%决策变量的最小值
    56     x_max=ones(1,x_num);%决策变量的最大值
    57     load('DTLZ1.txt');%导入正确的前端解
    58     plot3(DTLZ1(:,1),DTLZ1(:,2),DTLZ1(:,3),'g*');
    59     PP=DTLZ1;
    60 end
    61 %-------------------DTLZ2--------------------
    62 if strcmp(fun,'DTLZ2')
    63     f_num=3;%目标函数个数
    64     x_num=10;%决策变量个数
    65     x_min=zeros(1,x_num);%决策变量的最小值
    66     x_max=ones(1,x_num);%决策变量的最大值
    67     load('DTLZ2.txt');%导入正确的前端解
    68     plot3(DTLZ2(:,1),DTLZ2(:,2),DTLZ2(:,3),'g*');
    69     PP=DTLZ2;
    70 end

    ws_approach.m

    1 function f = ws_approach( lamda,f,i )
    2 %ws_approach
    3 f=lamda(i,:)*f';
    4 
    5 
    6 end

    updateNeighbor.m

     1 function X = updateNeighbor( lamda,z,X,Bi,off,x_num,f_num )
     2 %更新领域解
     3 for i=1:length(Bi)
     4     gte_xi=tchebycheff_approach(lamda,z,X(Bi(i),(x_num+1):(x_num+f_num)),Bi(i));
     5     gte_off=tchebycheff_approach(lamda,z,off(:,(x_num+1):(x_num+f_num)),Bi(i));
     6 %     gte_xi=ws_approach(lamda,X(Bi(i),(x_num+1):(x_num+f_num)),Bi(i));
     7 %     gte_off=ws_approach(lamda,off(:,(x_num+1):(x_num+f_num)),Bi(i));
     8     if gte_off <= gte_xi
     9         X(Bi(i),:)=off;
    10     end
    11 end

    tchebycheff_approach.m

    1 function fs = tchebycheff_approach( lamda,z,f,i)
    2 %tchebycheff_approach
    3 for j=1:length(lamda(i,:))
    4     if(lamda(i,j)==0)
    5         lamda(i,j)=0.00001;
    6     end
    7 end
    8 fs=max(lamda(i,:).*abs(f-z));
    9 end

    object_fun.m

     1 function f = object_fun( x,f_num,x_num,fun )
     2 %   测试函数的设置
     3 %--------------------ZDT1--------------------
     4 if strcmp(fun,'ZDT1')
     5     f=[];
     6     f(1)=x(1);
     7     sum=0;
     8     for i=2:x_num
     9         sum = sum+x(i);
    10     end
    11     g=1+9*(sum/(x_num-1));
    12     f(2)=g*(1-(f(1)/g)^0.5);
    13 end
    14 %--------------------ZDT2--------------------
    15 if strcmp(fun,'ZDT2')
    16     f=[];
    17     f(1)=x(1);
    18     sum=0;
    19     for i=2:x_num
    20         sum = sum+x(i);
    21     end
    22     g=1+9*(sum/(x_num-1));
    23     f(2)=g*(1-(f(1)/g)^2);
    24 end
    25 %--------------------ZDT3--------------------
    26 if strcmp(fun,'ZDT3')
    27     f=[];
    28     f(1)=x(1);
    29     sum=0;
    30     for i=2:x_num
    31         sum = sum+x(i);
    32     end
    33     g=1+9*(sum/(x_num-1));
    34     f(2)=g*(1-(f(1)/g)^0.5-(f(1)/g)*sin(10*pi*f(1)));
    35 end
    36 %--------------------ZDT4--------------------
    37 if strcmp(fun,'ZDT4')
    38     f=[];
    39     f(1)=x(1);
    40     sum=0;
    41     for i=2:x_num
    42         sum = sum+(x(i)^2-10*cos(4*pi*x(i)));
    43     end
    44     g=1+9*10+sum;
    45     f(2)=g*(1-(f(1)/g)^0.5);
    46 end
    47 %--------------------ZDT6--------------------
    48 if strcmp(fun,'ZDT6')
    49     f=[];
    50     f(1)=1-(exp(-4*x(1)))*((sin(6*pi*x(1)))^6);
    51     sum=0;
    52     for i=2:x_num
    53         sum = sum+x(i);
    54     end
    55     g=1+9*((sum/(x_num-1))^0.25);
    56     f(2)=g*(1-(f(1)/g)^2);
    57 end
    58 %--------------------------------------------
    59 %--------------------DTLZ1-------------------
    60 if strcmp(fun,'DTLZ1')
    61     f=[];
    62     sum=0;
    63     for i=3:x_num
    64         sum = sum+((x(i)-0.5)^2-cos(20*pi*(x(i)-0.5)));
    65     end
    66     g=100*(x_num-2)+100*sum;
    67     f(1)=(1+g)*x(1)*x(2);
    68     f(2)=(1+g)*x(1)*(1-x(2));
    69     f(3)=(1+g)*(1-x(1));
    70 end
    71 %--------------------------------------------
    72 %--------------------DTLZ2-------------------
    73 if strcmp(fun,'DTLZ2')
    74     f=[];
    75     sum=0;
    76     for i=3:x_num
    77         sum = sum+(x(i))^2;
    78     end
    79     g=sum;
    80     f(1)=(1+g)*cos(x(1)*pi*0.5)*cos(x(2)*pi*0.5);
    81     f(2)=(1+g)*cos(x(1)*pi*0.5)*sin(x(2)*pi*0.5);
    82     f(3)=(1+g)*sin(x(1)*pi*0.5);
    83 end
    84 %--------------------------------------------
    85 end

    look_neighbor.m

     1 function B = look_neighbor( lamda,T )
     2 %计算任意两个权重向量间的欧式距离
     3 N =size(lamda,1);
     4 B=zeros(N,T);
     5 distance=zeros(N,N);
     6 for i=1:N
     7     for j=1:N
     8         l=lamda(i,:)-lamda(j,:);
     9         distance(i,j)=sqrt(l*l');
    10     end
    11 end
    12 %查找每个权向量最近的T个权重向量的索引
    13 for i=1:N
    14     [~,index]=sort(distance(i,:));
    15     B(i,:)=index(1:T);
    16 end

    initialize.m

     1 function chromo = initialize( pop,f_num,x_num,x_min,x_max,fun )
     2 %   种群初始化
     3 chromo = repmat(x_min,pop,1)+rand(pop,x_num).*repmat(x_max-x_min,pop,1); 
     4 for i=1:pop
     5     chromo(i,(x_num+1:(x_num+f_num))) = object_fun(chromo(i,:),f_num,x_num,fun);
     6     chromo(i,(x_num+f_num+1)) = 0;
     7 end
     8 % for i=1:pop
     9 %     for j=1:x_num
    10 %         chromo(i,j)=x_min(j)+(x_max(j)-x_min(j))*rand(1);
    11 %     end
    12 %     chromo(i,(x_num+1:(x_num+f_num))) = object_fun(chromo(i,:),f_num,x_num,fun);
    13 % end

    genrate_lamda.m

     1 function lamda = genrate_lamda( N,f_num )
     2 %产生初始化向量lamda
     3 lamda2=zeros(N+1,f_num);%初始化
     4 if f_num==2
     5     array=(0:N)/N;%均匀分布的值
     6     for i=1:N+1
     7             lamda2(i,1)=array(i);
     8             lamda2(i,2)=1-array(i);
     9     end
    10     len = size(lamda2,1);
    11     index = randperm(len);
    12     index = sort(index(1:N));
    13     lamda = lamda2(index,:);
    14 elseif f_num==3
    15     k = 1;
    16     array = (0:25)/25;%产生均匀分布的值
    17     for i=1:26
    18         for j = 1:26
    19             if i+j<28
    20                 lamda3(k,1) = array(i);
    21                 lamda3(k,2) = array(j);
    22                 lamda3(k,3) = array(28-i-j);
    23                 k=k+1;
    24             end
    25         end
    26     end
    27     len = size(lamda3,1);
    28     index = randperm(len);
    29     index = sort(index(1:N));
    30     lamda = lamda3(index,:);
    31 end
    32 end

    deterdomination.m

     1 function X = deterdomination( X,N,f_num,x_num )
     2 %初始化是否为非支配个体
     3 for i=1:N
     4     X(i,(x_num+f_num+1))=0;
     5 end
     6 
     7 for i=1:N
     8     nn=0;
     9     for j=1:N
    10         less=0;%当前个体的目标函数值小于多少个体的数目
    11         equal=0;%当前个体的目标函数值等于多少个体的数目
    12         for k=1:f_num
    13             if(X(i,x_num+k)<X(j,x_num+k))
    14                 less=less+1;
    15             elseif(X(i,x_num+k)==X(j,x_num+k))
    16                 equal=equal+1;
    17             end
    18         end
    19         if(less==0 && equal~=f_num)
    20             nn=nn+1;%被支配个体数目n+1
    21         end
    22     end
    23     if(nn==0)
    24         X(i,(x_num+f_num+1))=1;
    25     end
    26 end
    27 end

    cross_mutation2.m

     1 function chromo_offspring = cross_mutation2( chromo_parent_1,chromo_parent_2,f_num,x_num,x_min,x_max,pc,pm,yita1,yita2,fun )
     2 %模拟二进制交叉与多项式变异
     3 %%%模拟二进制交叉
     4 if(rand(1)<pc)
     5     %初始化子代种群
     6     off_1=zeros(1,x_num+f_num);
     7     %进行模拟二进制交叉
     8     gama=zeros(1,x_num);
     9     for j=1:x_num
    10         u1=rand;
    11         if u1<=0.5
    12             gama(j)=(2*u1)^(1/(yita1+1));
    13         else
    14             gama(j)=(1/(2*(1-u1)))^(1/(yita1+1));
    15         end
    16         off_1(j)=0.5*((1-gama(j))*chromo_parent_1(j)+(1+gama(j))*chromo_parent_2(j));
    17         %使子代在定义域内
    18         if(off_1(j)>x_max(j))
    19             off_1(j)=x_max(j);
    20         elseif(off_1(j)<x_min(j))
    21             off_1(j)=x_min(j);
    22         end
    23     end
    24     %计算子代个体的目标函数值
    25     off_1(1,(x_num+1):(x_num+f_num))=object_fun(off_1,f_num,x_num,fun);
    26 end
    27 %%%多项式变异
    28 if(rand(1)<pm)
    29     r=randperm(x_num);
    30     ind=r(1);        %选中变异的位置
    31     r=rand; 
    32     if r<0.5
    33         delta=(2*r)^(1/(1+yita2))-1;
    34     else
    35         delta=1-(2*(1-r))^(1/(yita2+1));
    36     end
    37     off_1(ind)=off_1(ind)+delta*(x_max(ind)-x_min(ind));
    38     for j=1:x_num
    39         %使子代在定义域内
    40         if(off_1(j)>x_max(j))
    41             off_1(j)=x_max(j);
    42         elseif(off_1(j)<x_min(j))
    43             off_1(j)=x_min(j);
    44         end
    45     end
    46     %计算子代个体的目标函数值
    47     off_1(1,(x_num+1):(x_num+f_num))=object_fun(off_1,f_num,x_num,fun);
    48 end
    49 chromo_offspring=off_1;
    50 end

    cross_mutation.m

     1 function chromo_offspring = cross_mutation( chromo_parent_1,chromo_parent_2,f_num,x_num,x_min,x_max,pc,pm,yita1,yita2,fun )
     2 %模拟二进制交叉与多项式变异
     3 %%%模拟二进制交叉
     4 if(rand(1)<pc)
     5     %初始化子代种群
     6     off_1=zeros(1,x_num+f_num+1);
     7     %进行模拟二进制交叉
     8     u1=zeros(1,x_num);
     9     gama=zeros(1,x_num);
    10     for j=1:x_num
    11         u1(j)=rand(1);
    12         if u1(j)<=0.5
    13             gama(j)=(2*u1(j))^(1/(yita1+1));
    14         else
    15             gama(j)=(1/(2*(1-u1(j))))^(1/(yita1+1));
    16         end
    17         off_1(j)=0.5*((1-gama(j))*chromo_parent_1(j)+(1+gama(j))*chromo_parent_2(j));
    18         %使子代在定义域内
    19         if(off_1(j)>x_max(j))
    20             off_1(j)=x_max(j);
    21         elseif(off_1(j)<x_min(j))
    22             off_1(j)=x_min(j);
    23         end
    24     end
    25     %计算子代个体的目标函数值
    26     off_1(1,(x_num+1):(x_num+f_num))=object_fun(off_1,f_num,x_num,fun);
    27 end
    28 %%%多项式变异
    29 if(rand(1)<pm)
    30     u2=zeros(1,x_num);
    31     delta=zeros(1,x_num);
    32     for j=1:x_num
    33         u2(j)=rand(1);
    34         if(u2(j)<0.5)
    35             delta(j)=(2*u2(j))^(1/(yita2+1))-1;
    36         else
    37             delta(j)=1-(2*(1-u2(j)))^(1/(yita2+1));
    38         end
    39         off_1(j)=off_1(j)+delta(j);
    40         %使子代在定义域内
    41         if(off_1(j)>x_max(j))
    42             off_1(j)=x_max(j);
    43         elseif(off_1(j)<x_min(j))
    44             off_1(j)=x_min(j);
    45         end
    46     end
    47     %计算子代个体的目标函数值
    48     off_1(1,(x_num+1):(x_num+f_num))=object_fun(off_1,f_num,x_num,fun);
    49 end
    50 chromo_offspring=off_1;
    51 end
  • 相关阅读:
    NVMe固态硬盘工具箱使用说明
    (原创)Python文件与文件系统系列(1)—— file 对象
    Linux系统排查4——网络篇
    Python内置类型——list
    (原)数据结构——线索二叉树
    Python匿名函数——lambda表达式
    Python生成器
    Python内置类型——dict
    Python内置类型——set
    解决Django-1.8.2应用部署到Apache后无法显示admin应用的CSS
  • 原文地址:https://www.cnblogs.com/Vae1990Silence/p/12745314.html
Copyright © 2020-2023  润新知