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

📄 mosga1_4.m

📁 基于NSGA-II方法的多目标遗传算法程序,本程序为通用包,可自行修改.
💻 M
字号:
%多目标优化并列选择法主程序
function f=MOSGA1_4;
OptimizationStyle='Min';VarietyNumber=2;Xmin=[-3,-3];Xmax=[3,3];PopulationSize=90;GenotypeLength=20;MaxGeneration=100;Pc=0.9;Pm=0.1;
oldpop=randint(PopulationSize,VarietyNumber*GenotypeLength+VarietyNumber+2);
Generation=1;
while Generation<=MaxGeneration
    oldpop1=oldpop(1:PopulationSize/3,:);
    oldpop2=oldpop(PopulationSize/3+1:2*PopulationSize/3,:);
    oldpop3=oldpop(2*PopulationSize/3+1:PopulationSize,:);
    oldpop1=decode(OptimizationStyle,VarietyNumber,PopulationSize/3,Xmin,Xmax,GenotypeLength,oldpop1,1);
    oldpop2=decode(OptimizationStyle,VarietyNumber,PopulationSize/3,Xmin,Xmax,GenotypeLength,oldpop2,2);
    oldpop3=decode(OptimizationStyle,VarietyNumber,PopulationSize/3,Xmin,Xmax,GenotypeLength,oldpop2,3);
    newpop1=selection(VarietyNumber,PopulationSize/3,GenotypeLength,oldpop1);
    newpop2=selection(VarietyNumber,PopulationSize/3,GenotypeLength,oldpop2);
    newpop3=selection(VarietyNumber,PopulationSize/3,GenotypeLength,oldpop3);
    newpop=[newpop1' newpop2' newpop3']';
    randorder=randperm(PopulationSize);
    for i=1:2:(PopulationSize-1)
        parent1=newpop(randorder(i),:);
        parent2=newpop(randorder(i+1),:);
        [child1,child2]=crossover(VarietyNumber,GenotypeLength,parent1,parent2,Pc);
        newpop(randorder(i),:)=child1;
        newpop(randorder(i+1),:)=child2;
    end
    for i=1:PopulationSize
        newpop(i,:)=mutation(VarietyNumber,GenotypeLength,newpop(i,:),Pm);
    end
    oldpop=newpop;
    Generation=Generation+1;
end
x=newpop(:,VarietyNumber*GenotypeLength+1);
y=newpop(:,VarietyNumber*GenotypeLength+2);
for i=1:PopulationSize
    [f1(i),f2(i),f3(i)]=functionf(newpop(i,(VarietyNumber*GenotypeLength+1):(VarietyNumber*GenotypeLength+VarietyNumber)));
end
subplot(1,2,1);
plot(x,y,'k.');
axis('square');
xlabel('x');
ylabel('y');
axis([-3,3,-3,3]);
subplot(1,2,2);
plot3(f1,f2,f3,'k.');
axis('square');
xlabel('f1');
ylabel('f2');
zlabel('f3');
axis([0,10,15,18,-0.2,0.2]);

%选择(复制)算子程序
function newpop=selection(VarietyNumber,PopulationSize,GenotypeLength,oldpop);
totalfit=sum(oldpop(:,VarietyNumber*GenotypeLength+VarietyNumber+2));
if (totalfit>1e-15)
    prob=oldpop(:,VarietyNumber*GenotypeLength+VarietyNumber+2)/totalfit;
    prob=cumsum(prob); 
    randnum=sort(rand(PopulationSize,1));
    oldorder=1;
    neworder=1;
    while neworder<=PopulationSize
          if (randnum(neworder)<prob(oldorder))
              newpop(neworder,:)=oldpop(oldorder,:);
              neworder=neworder+1;
          else 
              oldorder=oldorder+1;
          end
    end
else
    newpop=oldpop;
end

%交叉算子程序
function [child1,child2]=crossover(VarietyNumber,GenotypeLength,parent1,parent2,Pc);
if(rand<Pc)
    cpoint=round(rand*(VarietyNumber*GenotypeLength-2))+1;
    child1=[parent1(1:cpoint) parent2(cpoint+1:VarietyNumber*GenotypeLength+VarietyNumber+2)];
    child2=[parent2(1:cpoint) parent1(cpoint+1:VarietyNumber*GenotypeLength+VarietyNumber+2)];
else
    child1=parent1;
    child2=parent2;
end

%变异算子程序
function child=mutation(VarietyNumber,GenotypeLength,parent,Pm);
if(rand<Pm)
    child=parent;
    mpoint=round(rand*(VarietyNumber*GenotypeLength-1))+1;
    child(mpoint)=1-parent(mpoint);
else
    child=parent;
end

%解码及求适应度程序
function decodedpop=decode(OptimizationStyle,VarietyNumber,PopulationSize,Xmin,Xmax,GenotypeLength,pop,flag);
decodedpop=pop;
for i=1:VarietyNumber
    decodedpop(:,VarietyNumber*GenotypeLength+i)=(2.^(size(pop(:,(((i-1)*GenotypeLength)+1):(i*GenotypeLength)),2)-1:-1:0)*...
    pop(:,(((i-1)*GenotypeLength)+1):(i*GenotypeLength))')'*(Xmax(i)-Xmin(i))/(2.^GenotypeLength-1)+Xmin(i);         
end
for i=1:PopulationSize
    [f1,f2,f3]=functionf(decodedpop(i,(VarietyNumber*GenotypeLength+1):(VarietyNumber*GenotypeLength+VarietyNumber)));
    if (flag==1)
        decodedpop(i,VarietyNumber*GenotypeLength+VarietyNumber+1)=f1;
    elseif (flag==2)
        decodedpop(i,VarietyNumber*GenotypeLength+VarietyNumber+1)=f2;
    else        decodedpop(i,VarietyNumber*GenotypeLength+VarietyNumber+1)=f3;
    end
end
if (OptimizationStyle=='Max')
    Cmin=min(decodedpop(:,VarietyNumber*GenotypeLength+VarietyNumber+1));
    decodedpop(:,VarietyNumber*GenotypeLength+VarietyNumber+2)=decodedpop(:,VarietyNumber*GenotypeLength+VarietyNumber+1)-Cmin;
else
    Cmax=max(decodedpop(:,VarietyNumber*GenotypeLength+VarietyNumber+1));
    decodedpop(:,VarietyNumber*GenotypeLength+VarietyNumber+2)=Cmax-decodedpop(:,VarietyNumber*GenotypeLength+VarietyNumber+1);
end

%计算目标函数值程序
function [f1,f2,f3]=functionf(x);
f1=0.5*(x(1)^2+x(2)^2)+sin(x(1)^2+x(2)^2);
f2=(3*x(1)-2*x(2)+4)^2/8+(x(1)-x(2)+1)^2/27+15;
f3=1/(x(1)^2+x(2)^2+1)-1.1*exp(-x(1)^2-x(2)^2);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -