NSGAII源程序.docx
《NSGAII源程序.docx》由会员分享,可在线阅读,更多相关《NSGAII源程序.docx(17页珍藏版)》请在冰豆网上搜索。
![NSGAII源程序.docx](https://file1.bdocx.com/fileroot1/2022-12/13/42c13273-dc64-4fde-82ca-447b2b51b9a3/42c13273-dc64-4fde-82ca-447b2b51b9a31.gif)
NSGAII源程序
functionf=crowding_distance(x,problem)
%Thisfunctioncalculatesthecrowdingdistance
[N,M]=size(x);
switchproblem
case1
M=2;
V=6;
case2
M=3;
V=12;
end
%Crowdingdistanceforeachfront
fori=1:
length(F(front).f)
y(i,:
)=x(F(front).f(i),:
);
end
fori=1:
M
[sorted(i).individual,sorted(i).index]=sort(y(:
V+i));
distance(sorted(i).index
(1)).individual=Inf;
distance(sorted(i).index(length(sorted(i).index))).individual=Inf;
end
[num,len]=size(y);
%Initializeallthedistanceofindividualsaszero.
fori=1:
M
forj=2:
num-1
distance(j).individual=0;
end
objective(i).range=...
sorted(i).individual(length(sorted(i).individual))-...
sorted(i).individual
(1);
%Maximumandminimumobjectivesvaluefortheithobjective
end
%Caluclatethecrowdingdistanceforfrontone.
fori=1:
M
forj=2:
num-1
distance(j).individual=distance(j).individual+...
(sorted(i).individual(j+1)-sorted(i).individual(j-1))/...
objective(i).range;
y(sorted(i).index(j),M+V+2)=distance(j).individual;
end
end
PublishedwithMATLAB®7.0
functionf=genetic_operator(parent_chromosome,pro,mu,mum);
%Thisfunctionisutilizedtoproduceoffspringsfromparentchromosomes.
%Thegeneticoperatorscorssoverandmutationwhicharecarriedoutwith
%slightmodificationsfromtheoriginaldesign.Formoreinformationread
%thedocumentenclosed.
[N,M]=size(parent_chromosome);
switchpro
case1
M=2;
V=6;
case2
M=3;
V=12;
end
p=1;
was_crossover=0;
was_mutation=0;
l_limit=0;
u_limit=1;
fori=1:
N
ifrand
(1)<0.9
child_1=[];
child_2=[];
parent_1=round(N*rand
(1));
ifparent_1<1
parent_1=1;
end
parent_2=round(N*rand
(1));
ifparent_2<1
parent_2=1;
end
whileisequal(parent_chromosome(parent_1,:
),parent_chromosome(parent_2,:
))
parent_2=round(N*rand
(1));
ifparent_2<1
parent_2=1;
end
end
parent_1=parent_chromosome(parent_1,:
);
parent_2=parent_chromosome(parent_2,:
);
forj=1:
V
%SBX(SimulatedBinaryCrossover)
%Generatearandomnumber
u(j)=rand
(1);
ifu(j)<=0.5
bq(j)=(2*u(j))^(1/(mu+1));
else
bq(j)=(1/(2*(1-u(j))))^(1/(mu+1));
end
child_1(j)=...
0.5*(((1+bq(j))*parent_1(j))+(1-bq(j))*parent_2(j));
child_2(j)=...
0.5*(((1-bq(j))*parent_1(j))+(1+bq(j))*parent_2(j));
ifchild_1(j)>u_limit
child_1(j)=u_limit;
elseifchild_1(j)child_1(j)=l_limit;
end
ifchild_2(j)>u_limit
child_2(j)=u_limit;
elseifchild_2(j)child_2(j)=l_limit;
end
end
child_1(:
V+1:
M+V)=evaluate_objective(child_1,pro);
child_2(:
V+1:
M+V)=evaluate_objective(child_2,pro);
was_crossover=1;
was_mutation=0;
else
parent_3=round(N*rand
(1));
ifparent_3<1
parent_3=1;
end
%Makesurethatthemutationdoesnotresultinvariablesoutof
%thesearchspace.ForboththeMOP'stherangefordecisionspace
%is[0,1].Incasedifferentvariableshavedifferentdecision
%spaceeachvariablecanbeassignedarange.
child_3=parent_chromosome(parent_3,:
);
forj=1:
V
r(j)=rand
(1);
ifr(j)<0.5
delta(j)=(2*r(j))^(1/(mum+1))-1;
else
delta(j)=1-(2*(1-r(j)))^(1/(mum+1));
end
child_3(j)=child_3(j)+delta(j);
ifchild_3(j)>u_limit
child_3(j)=u_limit;
elseifchild_3(j)child_3(j)=l_limit;
end
end
child_3(:
V+1:
M+V)=evaluate_objective(child_3,pro);
was_mutation=1;
was_crossover=0;
end
ifwas_crossover
child(p,:
)=child_1;
child(p+1,:
)=child_2;
was_cossover=0;
p=p+2;
elseifwas_mutation
child(p,:
)=child_3(1,1:
M+V);
was_mutation=0;
p=p+1;
end
end
f=child;
PublishedwithMATLAB®7.0
functionf=initialize_variables(N,problem)
%functionf=initialize_variables(N,problem)
%N-Populationsize
%problem-takesintegervalues1and2where,
%'1'forMOP1
%'2'forMOP2
%
%ThisfunctioninitializesthepopulationwithNindividualsandeach
%individualhavingMdecisionvariablesbasedontheselectedproblem.
%M=6forproblemMOP1andM=12forproblemMOP2.Theobjectivespace
%forMOP1is2dimensionalwhileforMOP2is3dimensional.
%BoththeMOP'shas0to1asitsrangeforallthedecisionvariables.
min=0;
max=1;
switchproblem
case1
M=6;
K=8;
case2
M=12;
K=15;
end
fori=1:
N
%Initializethedecisionvariables
forj=1:
M
f(i,j)=rand
(1);%i.ef(i,j)=min+(max-min)*rand
(1);
end
%Evaluatetheobjectivefunction
f(i,M+1:
K)=evaluate_objective(f(i,:
),problem);
end
PublishedwithMATLAB®7.0
Non-DonimationSort
Thisfunctionsortthecurrentpopultionbasedonnon-domination.Alltheindividualsinthefirstfrontaregivenarankof1,thesecondfrontindividualsareassignedrank2andsoon.Afterassigningtherankthecrowdingineachfrontiscalculated.
[N,M]=size(x);
switchproblem
case1
M=2;
V=6;
case2
M=3;
V=12;
end
front=1;
%Thereisnothingtothisassignment,usedonlytomanipulateeasilyin
%MATLAB.
F(front).f=[];
individual=[];
fori=1:
N
%Numberofindividualsthatdominatethisindividual
individual(i).n=0;
%Individualswhichthisindividualdominate
individual(i).p=[];
forj=1:
N
dom_less=0;
dom_equal=0;
dom_more=0;
fork=1:
M
if(x(i,V+k)dom_less=dom_less+1;
elseif(x(i,V+k)==x(j,V+k))
dom_equal=dom_equal+1;
else
dom_more=dom_more+1;
end
end
ifdom_less==0&dom_equal~=M
individual(i).n=individual(i).n+1;
elseifdom_more==0&dom_equal~=M
individual(i).p=[individual(i).pj];
end
end
ifindividual(i).n==0
x(i,M+V+1)=1;
F(front).f=[F(front).fi];
end
end
%Findthesubsequentfronts
while~isempty(F(front).f)
Q=[];
fori=1:
length(F(front).f)
if~isempty(individual(F(front).f(i)).p)
forj=1:
length(individual(F(front).f(i)).p)
individual(individual(F(front).f(i)).p(j)).n=...
individual(individual(F(front).f(i)).p(j)).n-1;
ifindividual(individual(F(front).f(i)).p(j)).n==0
x(individual(F(front).f(i)).p(j),M+V+1)=...
front+1;
Q=[Qindividual(F(front).f(i)).p(j)];
end
end
end
end
front=front+1;
F(front).f=Q;
end
[temp,index_of_fronts]=sort(x(:
M+V+1));
fori=1:
length(index_of_fronts)
sorted_based_on_front(i,:
)=x(index_of_fronts(i),:
);
end
current_index=0;
%Findthecrowdingdistanceforeachindividualineachfront
forfront=1:
(length(F)-1)
objective=[];
distance=0;
y=[];
previous_index=current_index+1;
fori=1:
length(F(front).f)
y(i,:
)=sorted_based_on_front(current_index+i,:
);
end
current_index=current_index+i;
%Sorteachindividualbasedontheobjective
sorted_based_on_objective=[];
fori=1:
M
[sorted_based_on_objective,index_of_objectives]=...
sort(y(:
V+i));
sorted_based_on_objective=[];
forj=1:
length(index_of_objectives)
sorted_based_on_objective(j,:
)=y(index_of_objectives(j),:
);
end
f_max=...
sorted_based_on_objective(length(index_of_objectives),V+i);
f_min=sorted_based_on_objective(1,V+i);
y(index_of_objectives(length(index_of_objectives)),M+V+1+i)...
=Inf;
y(index_of_objectives
(1),M+V+1+i)=Inf;
forj=2:
length(index_of_objectives)-1
next_obj=sorted_based_on_objective(j+1,V+i);
previous_obj=sorted_based_on_objective(j-1,V+i);
if(f_max-f_min==0)
y(index_of_objectives(j),M+V+1+i)=Inf;
else
y(index_of_objectives(j),M+V+1+i)=...
(next_obj-previous_obj)/(f_max-f_min);
end
end
end
distance=[];
distance(:
1)=zeros(length(F(front).f),1);
fori=1:
M
distance(:
1)=distance(:
1)+y(:
M+V+1+i);
end
y(:
M+V+2)=distance;
y=y(:
1:
M+V+2);
z(previous_index:
current_index,:
)=y;
end
f=z();
PublishedwithMATLAB®7.0
MainFunction
MainprogramtoruntheNSGA-IIMOEA.Readthecorrespondingdocumentationtolearnmoreaboutmultiobjectiveoptimizationusingevolutionaryalgorithms.initialize_variableshastwoarguments;Firstbeingthepopulationsizeandthesecondtheproblemnumber.'1'correspondstoMOP1and'2'correspondstoMOP2.
Contents
∙Initializethevariables
∙Sorttheinitializedpopulation
∙Starttheevolutionprocess
∙Result
∙Visualize
Initializethevariables
Declarethevariablesandinitializetheirvaluespop-populationgen-generationspro-problemnumber
pop=200;
gen=1;
pro=1;
switchpro
case1
%Misthenumberofobjectives.
M=2;
%Visthenumberofdecisionvariables.Inthiscaseitis
%difficulttovisualizethedecisionvariablesspacewhilethe
%objectivespaceisjusttwodimensional.
V=6;
case2
M=3;
V=12;
end
%Initializethepopulation
chromosome=initialize_variables(pop,pro);
Sorttheinitializedpopulation
Sortthepopulationusingnon-domination-sort.Thisreturnstwocolumnsforeachindividualwhicharetherankandthecrowdingdistancecorrespondingtotheirpositioninthefronttheybelong.
chromosome=non_domination_sort_mod(chromosome,pro);
Starttheevolutionprocess
%Thefollowingareperformedineachgeneration
%Selecttheparents
%PerfromcrossoverandMutationoperator
%PerformSelection
fori=1:
gen
%Selecttheparents
%Parentsareselectedforreproductiontogenerateoffspring.The
%originalNSGA-IIusesabinarytournamentselectionbasedonthe
%crowded-comparisionoperator.Theargumentsare
%pool-sizeofthematingpool.Itiscommontohavethistobehalfthe
%populationsize.
%tour-Tournamentsiz