📄 mosga1_3.m
字号:
%多目标优化并列选择法主程序
function f=MOSGA1_3;
OptimizationStyle='Min';VarietyNumber=2;Xmin=[0,0];Xmax=[3*pi/4,3*pi/4];PopulationSize=100;GenotypeLength=10;MaxGeneration=100;Pc=0.9;Pm=0.01;
oldpop=randint(PopulationSize,VarietyNumber*GenotypeLength+VarietyNumber+2);
Generation=1;
while Generation<=MaxGeneration
oldpop1=oldpop(1:PopulationSize/2,:);
oldpop2=oldpop(PopulationSize/2+1:PopulationSize,:);
oldpop1=decode(OptimizationStyle,VarietyNumber,PopulationSize/2,Xmin,Xmax,GenotypeLength,oldpop1,1);
oldpop2=decode(OptimizationStyle,VarietyNumber,PopulationSize/2,Xmin,Xmax,GenotypeLength,oldpop2,2);
newpop1=selection(VarietyNumber,PopulationSize/2,GenotypeLength,oldpop1);
newpop2=selection(VarietyNumber,PopulationSize/2,GenotypeLength,oldpop2);
newpop=[newpop1' newpop2']';
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)]=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([0,3*pi/4,0,3*pi/4]);
subplot(1,2,2);
plot(f1,f2,'k.');
axis('square');
xlabel('f1');
ylabel('f2');
axis([-1,1,-1,1]);
%选择(复制)算子程序
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]=functionf(decodedpop(i,(VarietyNumber*GenotypeLength+1):(VarietyNumber*GenotypeLength+VarietyNumber)));
if (flag==1)
decodedpop(i,VarietyNumber*GenotypeLength+VarietyNumber+1)=f1;
else
decodedpop(i,VarietyNumber*GenotypeLength+VarietyNumber+1)=f2;
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]=functionf(x);
f1=sin(x(1)^2+x(2)^2-1);
f2=sin(x(1)^2+x(2)^2+1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -