您好,欢迎访问三七文档
%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,见switchswitchprocase1%Misthenumberofobjectives.M=2;%Visthenumberofdecisionvariables.Inthiscaseitis%difficulttovisualizethedecisionvariablesspacewhilethe%objectivespaceisjusttwodimensional.V=6;case2M=3;V=12;case3%case1和case2用来对整个算法进行常规验证,作为调试之用;case3为本工程所需;M=2;%(outputparameters个数)V=8;%(inputparameters个数)K=10;end%Initializethepopulationchromosome=initialize_variables(pop,pro);%%Sorttheinitializedpopulation%Sortthepopulationusingnon-domination-sort.Thisreturnstwocolumns%foreachindividualwhicharetherankandthecrowdingdistance%correspondingtotheirpositioninthefronttheybelong.真是牛X了。chromosome=non_domination_sort_mod(chromosome,pro);%%Starttheevolutionprocess%Thefollowingareperformedineachgeneration%Selecttheparents%PerfromcrossoverandMutationoperator%PerformSelectionfori=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%leastcrowdingdistancechromosome=replace_chromosome(intermediate_chromosome,pro,pop);if~mod(i,10)fprintf('%d\n',i);endend%%Result%SavetheresultinASCIItextformat.savesolution.txtchromosome-ASCII%%Visualize%Thefollowingisusedtovisualizetheresultforthegivenproblem.switchprocase1plot(chromosome(:,V+1),chromosome(:,V+2),'y+');title('MOP1usingNSGA-II');xlabel('f(x_1)');ylabel('f(x_2)');case2plot3(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;switchproblemcase1M=6;K=8;%k=决策变量(M=6)+目标变量(K-M=2)=8case2M=12;K=15;case3%case1和case2用来对整个算法进行常规验证,作为调试之用;case3为本工程所需;M=8;%(inputparameters个数)K=10;endfori=1:N%Initializethedecisionvariablesforj=1:Mf(i,j)=rand(1);%i.ef(i,j)=min+(max-min)*rand(1);end%Evaluatetheobjectivefunctionf(i,M+1:K)=evaluate_objective(f(i,:),problem);end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%functionf=evaluate_objective(x,problem)%Functiontoevaluatetheobjectivefunctionsforthegiveninputvector%x.xhasthedecisionvariablesswitchpro
本文标题:多目标遗传算法代码
链接地址:https://www.777doc.com/doc-2504609 .html