多目标遗传算法代码.docx

上传人:b****6 文档编号:4432814 上传时间:2022-12-01 格式:DOCX 页数:19 大小:20.99KB
下载 相关 举报
多目标遗传算法代码.docx_第1页
第1页 / 共19页
多目标遗传算法代码.docx_第2页
第2页 / 共19页
多目标遗传算法代码.docx_第3页
第3页 / 共19页
多目标遗传算法代码.docx_第4页
第4页 / 共19页
多目标遗传算法代码.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

多目标遗传算法代码.docx

《多目标遗传算法代码.docx》由会员分享,可在线阅读,更多相关《多目标遗传算法代码.docx(19页珍藏版)》请在冰豆网上搜索。

多目标遗传算法代码.docx

多目标遗传算法代码

%functionnsga_2(pro)

%%MainFunction

%MainprogramtoruntheNSGA-IIMOEA.

%Readthecorrespondingdocumentationtolearnmoreaboutmultiobjective

%optimizationusingevolutionaryalgorithms.

%initialize_variableshastwoarguments;Firstbeingthepopulationsize

%andthesecondtheproblemnumber.'1'correspondstoMOP1and'2'

%correspondstoMOP2.

%inp_para_definition=input_parameters_definition;

%%Initializethevariables

%Declarethevariablesandinitializetheirvalues

%pop-population

%gen-generations

%pro-problemnumber

%clear;clc;tic;

pop=100;%每一代的种群数

gen=100;%总共的代数

pro=2;%问题选择1或者2,见switch

switchpro

case1

%Misthenumberofobjectives.

M=2;

%Visthenumberofdecisionvariables.Inthiscaseitis

%difficulttovisualizethedecisionvariablesspacewhilethe

%objectivespaceisjusttwodimensional.

V=6;

case2

M=3;

V=12;

case3%case1和case2用来对整个算法进行常规验证,作为调试之用;case3为本工程所需;

M=2;%(outputparameters个数)

V=8;%(inputparameters个数)

K=10;

end

%Initializethepopulation

chromosome=initialize_variables(pop,pro);

%%Sorttheinitializedpopulation

%Sortthepopulationusingnon-domination-sort.Thisreturnstwocolumns

%foreachindividualwhicharetherankandthecrowdingdistance

%correspondingtotheirpositioninthefronttheybelong.真是牛X了。

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-Tournamentsize.OriginalNSGA-IIusesabinarytournament

%selection,buttoseetheeffectoftournamentsizethisiskept

%arbitary,tobechoosenbytheuser.

pool=round(pop/2);

tour=2;

%下面进行二人锦标赛配对,新的群体规模是原来群体的一半

parent_chromosome=tournament_selection(chromosome,pool,tour);

%PerfromcrossoverandMutationoperator

%TheoriginalNSGA-IIalgorithmusesSimulatedBinaryCrossover(SBX)and

%Polynomialcrossover.Crossoverprobabilitypc=0.9andmutation

%probabilityispm=1/n,wherenisthenumberofdecisionvariables.

%Bothreal-codedGAandbinary-codedGAareimplementedintheoriginal

%algorithm,whileinthisprogramonlythereal-codedGAisconsidered.

%Thedistributionindeicesforcrossoverandmutationoperatorsasmu=20

%andmum=20respectively.

mu=20;

mum=20;

%针对对象是上一步产生的新的个体parent_chromosome

%对parent_chromosome每次操作以较大的概率进行交叉(产生两个新的候选人),或者较小的概率变异(一个新的候选人)操作,这样

%就会产生较多的新个体

offspring_chromosome=genetic_operator(parent_chromosome,pro,mu,mum);

%Intermediatepopulation

%Intermediatepopulationisthecombinedpopulationofparentsand

%offspringsofthecurrentgeneration.Thepopulationsizeisalmost1and

%halftimestheinitialpopulation.

[main_pop,temp]=size(chromosome);

[offspring_pop,temp]=size(offspring_chromosome);

intermediate_chromosome(1:

main_pop,:

)=chromosome;

intermediate_chromosome(main_pop+1:

main_pop+offspring_pop,1:

M+V)=offspring_chromosome;

%intermediate_chromosome=inter_chromo(chromosome,offspring_chromosome,pro);

%Non-domination-sortofintermediatepopulation

%Theintermediatepopulationissortedagainbasedonnon-dominationsort

%beforethereplacementoperatorisperformedontheintermediate

%population.

intermediate_chromosome=...

non_domination_sort_mod(intermediate_chromosome,pro);

%PerformSelection

%Oncetheintermediatepopulationissortedonlythebestsolutionis

%selectedbasedonitrankandcrowdingdistance.Eachfrontisfilledin

%ascendingorderuntiltheadditionofpopulationsizeisreached.The

%lastfrontisincludedinthepopulationbasedontheindividualswith

%leastcrowdingdistance

chromosome=replace_chromosome(intermediate_chromosome,pro,pop);

if~mod(i,10)

fprintf('%d\n',i);

end

end

%%Result

%SavetheresultinASCIItextformat.

savesolution.txtchromosome-ASCII

%%Visualize

%Thefollowingisusedtovisualizetheresultforthegivenproblem.

switchpro

case1

plot(chromosome(:

V+1),chromosome(:

V+2),'y+');

title('MOP1usingNSGA-II');

xlabel('f(x_1)');

ylabel('f(x_2)');

case2

plot3(chromosome(:

V+1),chromosome(:

V+2),chromosome(:

V+3),'*');

title('MOP2usingNSGA-II');

xlabel('f(x_1)');

ylabel('f(x_2)');

zlabel('f(x_3)');

end

%disp('runtimeis:

')

%toc;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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;%k=决策变量(M=6)+目标变量(K-M=2)=8

case2

M=12;

K=15;

case3%case1和case2用来对整个算法进行常规验证,作为调试之用;case3为本工程所需;

M=8;%(inputparameters个数)

K=10;

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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

functionf=evaluate_objective(x,problem)

%Functiontoevaluatetheobjectivefunctionsforthegiveninputvector

%x.xhasthedecisionvariables

switchproblem

case1

f=[];

%%Objectivefunctionone

f

(1)=1-exp(-4*x

(1))*(sin(6*pi*x

(1)))^6;

sum=0;

fori=2:

6

sum=sum+x(i)/4;

end

%%Intermediatefunction

g_x=1+9*(sum)^(0.25);

%%Objectivefunctionone

f

(2)=g_x*(1-((f

(1))/(g_x))^2);

case2

f=[];

%%Intermediatefunction

g_x=0;

fori=3:

12

g_x=g_x+(x(i)-0.5)^2;

end

%%Objectivefunctionone

f

(1)=(1+g_x)*cos(0.5*pi*x

(1))*cos(0.5*pi*x

(2));

%%Objectivefunctiontwo

f

(2)=(1+g_x)*cos(0.5*pi*x

(1))*sin(0.5*pi*x

(2));

%%Objectivefunctionthree

f(3)=(1+g_x)*sin(0.5*pi*x

(1));

case3

f=[];

%%Objectivefunctionone

f

(1)=0;

%%Objectivefunctionone

f

(2)=0;

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%Non-DonimationSort%按照目标函数最小了好

%Thisfunctionsortthecurrentpopultionbasedonnon-domination.Allthe

%individualsinthefirstfrontaregivenarankof1,thesecondfront

%individualsareassignedrank2andsoon.Afterassigningtherankthe

%crowdingineachfrontiscalculated.

functionf=non_domination_sort_mod(x,problem)

[N,M]=size(x);

switchproblem

case1

M=2;

V=6;

case2

M=3;

V=12;

case3%case1和case2用来对整个算法进行常规验证,作为调试之用;case3为本工程所需;

M=2;%(outputparameters个数)

V=8;%(inputparameters个数)

K=10;

end

front=1;

%Thereisnothingtothisassignment,usedonlytomanipulateeasilyin

%MATLAB.

F(front).f=[];

individual=[];

fori=1:

N

%Numberofindividualsthatdominatethisindividual支配i的解的个数

individual(i).n=0;

%Individualswhichthisindividualdominate被i支配的解

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%举个例子,其中i=a,b;j=c,d;如果i中的a,b全部大于

individual(i).n=individual(i).n+1;%或者部分大于j中的c,d(但没有小于的情况),则称为i优于j,

elseifdom_more==0&dom_equal~=M%当i优于j的时候,则此时把individual(i)_n加1

individual(i).p=[individual(i).pj];%如果i中的a,b全部小于或者部分小于j中的c,d(但没有大于的情况),则

end%则称为j优于i,则把此时的j放入individual(i)_P中;

end%总之,就是说两个目标变量必须全部大于或者全部小于才能对individual有效。

ifindividual(i).n==0%如果没有劣于i的话,即F(front).f存放的都是差的解.

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)%i表示最优解

if~isempty(individual(F(front).f(i)).p)

forj=1:

length(individual(F(front).f(i)).p)%被前端解i所支配的解得集合,这个集合由j构成,

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

%functionsort:

sort(x_sequence)===>>returnaincreasingorerdatasequence

[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),:

);%对解(染色体)进行排序,按照front分层

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_

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 初中教育

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1