版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
%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;%每一代的種群數(shù)gen=100;%總共的代數(shù)pro=2;%問題選擇1或者2,見switchswitchprocase1%Misthenumberofobjectives.M=2;%Visthenumberofdecisionvariables.Inthiscaseitis%difficulttovisualizethedecisionvariablesspacewhilethe%objectivespaceisjusttwodimensional.=6;case2M=3;=12;case3%case1和case2用來對整個算法進(jìn)行常規(guī)驗證,作為調(diào)試之用;case3為本工程所需;M=2;%(outputparameters個數(shù))V=8;%(inputparameters個數(shù))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;%下面進(jìn)行二人錦標(biāo)賽配對,新的群體規(guī)模是原來群體的一半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;%針對對象是上一步產(chǎn)生的新的個體parent_chromosome%對parent_chromosome每次操作以較大的概率進(jìn)行交叉(產(chǎn)生兩個新的候選人),或者較小的概率變異(一個新的候選人)操作,這樣%就會產(chǎn)生較多的新個體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%ermediate_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)+目標(biāo)變量(K-M=2)=8case2M=12;K=15;case3%case1和case2用來對整個算法進(jìn)行常規(guī)驗證,作為調(diào)試之用;case3為本工程所需;M=8;%(inputparameters個數(shù))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.xhasthedecisionvariablesswitchproblemcase1f=[];%%Objectivefunctiononef(1)=1-exp(-4*x(1))*(sin(6*pi*x(1)))A6;sum=0;fori=2:6sum=sum+x(i)/4;end%%Intermediatefunctiong_x=1+9*(sum)A(0.25);%%Objectivefunctiononef(2)=g_x*(1-((f(1))/(g_x))A2);case2f=[];%%Intermediatefunctiong_x=0;fori=3:12g_x=g_x+(x(i)-0.5)人2;end%%Objectivefunctiononef(1)=(1+g_x)*cos(0.5*pi*x(1))*cos(0.5*pi*x(2));%%Objectivefunctiontwof(2)=(1+g_x)*cos(0.5*pi*x(1))*sin(0.5*pi*x(2));%%Objectivefunctionthreef(3)=(1+g_x)*sin(0.5*pi*x(1));case3f=[];%%Objectivefunctiononef(1)=0;%%Objectivefunctiononef(2)=0;end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Non-DonimationSort%按照目標(biāo)函數(shù)最小了好%Thisfunctionsortthecurrentpopultionbasedonnon-domination.Allthe%individualsinthefirstfrontaregivenarankof1,thesecondfront%individualsareassignedrank2andsoon.Afterassigningtherankthe%crowdingineachfrontiscalculated.functionf=non_domination_sort_mod(x,problem)[N,M]=size(x);switchproblemcase1M=2;V=6;case2M=3;
V=12;case3%case1和case2用來對整個算法進(jìn)行常規(guī)驗證,作為調(diào)試之用;case3為本工程所需;M=2;%(outputparameters個數(shù))V=8;%(inputparameters個數(shù))K=10;endfront=1;%Thereisnothingtothisassignment,usedonlytomanipulateeasilyin%MATLAB.F(front).f=[];individual=[];fori=1:N%Numberofindividualsthatdominatethisindividual支配i的解的個數(shù)individual(i).n=0;%Individualswhichthisindividualdominate被i支配的解individual(i).p=[];forj=1:Ndom_less=0;dom_equal=0;dom_more=0;fork=1:Mif(x(i,V+k)<x(j,V+k))dom_less=dom_less+1;elseif(x(i,V+k)==x(j,V+k))dom_equal=dom_equal+1;elsedom_more=dom_more+1;endend%這里只要考慮到求取得是函數(shù)的最小值就可以了,此時支配的概念就會變?yōu)檎l的函數(shù)值小,誰去支配別的解舉個例子,其中i=a,b;j=c,d;如果i或者部分大于j中的c,d(但沒有小%當(dāng)i優(yōu)于j的時候,則此時把%如果i中的舉個例子,其中i=a,b;j=c,d;如果i或者部分大于j中的c,d(但沒有小%當(dāng)i優(yōu)于j的時候,則此時把%如果i中的a,b全部小于或者%則%總elseifdom_more==0&dom_equal~=Mindividual(i)_n加1individual(i).p=[individual(i).pj];部分小于j中的c,d(但沒有大于的情況),則end稱為j優(yōu)于i,則把此時的j放入individual(i)_P中;end之,就是說兩個目標(biāo)變量必須全部大于或者全部小于才能對individual有效。ifindividual(i).n==0%如果沒有劣于i的話,即F(front).f存放的都是差的解.x(i,M+V+1)=1;F(front).f=[F(front).fi];endend%Findthesubsequentfrontswhile?isempty(F(front).f)Q=[];fori=1:length(F(front).f)%i表示最優(yōu)解if?isempty(individual(F(front).f(i)).p)forj=1:length(individual(F(front).f(i)).p)%被前端解i所支配的解得集合,這個集合由j構(gòu)成,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==0x(individual(F(front).f(i)).p(j),M+V+1)=front+1;Q=[Qindividual(F(front).f(i)).p(j)];endendendendfront=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),:);%對解(染色體)進(jìn)行排序,按照front分層end%到這里分層結(jié)束,下面就是計算距離了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,:);endcurrent_index=current_index+i;%Sorteachindividualbasedontheobjectivesorted_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),:);endf_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;%最大值放lnfy(index_of_objectives(1),M+V+1+i)=Inf;%最小值放lnfforj=2:length(index_of_objectives)-1next_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;elsey(index_of_objectives(j),M+V+1+i)=...(next_obj-previous_obj)/(f_max-f_min);endendenddistance=[];distance(:,1)=zeros(length(F(front).f),1);fori=1:Mdistance(:,1)=distance(:,1)+y(:,M+V+1+i);endy(:,M+V+2)=distance;y=y(:,1:M+V+2);z(previous_index:current_index,:)=y;endf=z;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%functionf=replace_chromosome(intermediate_chromosome,pro,pop)%%replace_chromosome(intermediate_chromosome,pro,pop)%Thisfunctionreplacesthechromosomesbasedonrankandcrowding%distance.Initiallyuntilthepopulationsizeisreachedeachfrontis%addedonebyoneuntiladditionofacompletefrontwhichresultsin%exceedingthepopulationsize.Atthispointthechromosomesinthat%frontisaddedsubsequentlytothepopulationbasedoncrowdingdistance.[N,V]=size(intermediate_chromosome);switchprocase1M=2;=6;case2M=3;=12;case3%case1和case2用來對整個算法進(jìn)行常規(guī)驗證,作為調(diào)試之用;case3為本工程所需;M=2;%(outputparameters個數(shù))V=8;%(inputparameters個數(shù))K=10;end%Gettheindexforthepopulationsortbasedontherank[temp,index]=sort(intermediate_chromosome(:,M+V+1));%Nowsorttheindividualsbasedontheindexfori=1:Nsorted_chromosome(i,:)=intermediate_chromosome(index(i),:);end%Findthemaximumrankinthecurrentpopulationmax_rank=max(intermediate_chromosome(:,M+V+1));%Startaddingeachfrontbasedonrankandcrowingdistanceuntilthe%wholepopulationisfilled.previous_index=0;fori=1:max_rankcurrent_index=max(find(sorted_chromosome(:,M+V+1)==i));ifcurrent_index>popremaining=pop-previous_index;temp_pop=...sorted_chromosome(previous_index+1:current_index,:);temp_pop_1=temp_pop*(-1);[temp_sort_1,temp_sort_index]=...sort(temp_pop_1(:,M+V+2));%在編譯的時候出現(xiàn)的問題找到了,主要是對sort(a,'descend')并不支持。temp_sort=temp_sort_1*(-1);forj=1:remainingf(previous_index+j,:)=temp_pop(temp_sort_index(j),:);endreturn;elseifcurrent_index<popf(previous_index+1:current_index,:)=...sorted_chromosome(previous_index+1:current_index,:);elsef(previous_index+1:current_index,:)=...sorted_chromosome(previous_index+1:current_index,:);return;endprevious_index=current_index;end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%functionf=tournament_selection(chromosome,pool_size,tour_size)%functionselection_individuals(chromosome,pool_size,tour_size)isthe%selectionpolicyforselectingtheindividualsforthematingpool.The%selectionisbasedontournamentselection.Argument'chromosome'isthe%currentgenerationpopulationfromwhichtheindividualsareselectedto%formamatingpoolofsize'pool_size'afterperformingtournament%selection,withsizeofthetournamentbeing'tour_size'.Byvaryingthe%tournamentsizetheselectionpressurecanbeadjusted.[pop,variables]=size(chromosome);rank=variables-1;%表示代表層數(shù)的那一列的索引distance=variables;%表示代表擁擠度距離的那一列的索引fori=1:pool_sizeforj=1:tour_size%這個loop目的是為了隨機(jī)找出tour_size個相互不同的候選染色體candidate(j)=round(pop*rand(1));ifcandidate(j)==0candidate(j)=1;endifj>1while~isempty(find(candidate(1:j-1)==candidate(j)))candidate(j)=round(pop*rand(1));ifcandidate(j)==0candidate(j)=1;endendendendforj=1:tour_sizec_obj_rank(j)=chromosome(candidate(j),rank);%提取出階數(shù)c_obj_distance(j)=chromosome(candidate(j),distance);%提取出距離endmin_candidate=find(c_obj_rank==min(c_obj_rank));%找出層數(shù)最小的那個候選人iflength(min_candidate)~=1%發(fā)現(xiàn)存在層數(shù)相同的候選人,就要比較擁擠度距離了max_candidate=find(c_obj_distance(min_candidate)==max(c_obj_distance(min_candidate)));%返回具有同樣最小層數(shù)的,且擁擠距離最大的那個候選人iflength(max_candidate)~=1max_candidate=max_candidate(1%;層數(shù),擁擠距離都相同,隨便選個就可以了endf(i,:)=chromosome(candidate(min_candidate(max_candidate)),:);elsef(i,:)=chromosome(candidate(min_candidate(1)),:);endend%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%functionf=genetic_operator(parent_chromosome,pro,mu,mum)%Thisfunctionisutilizedtoproduceoffspringsfromparentchromosomes.%Thegeneticoperatorscorssoverandmutationwhicharecarriedoutwith%slightmodificationsfromtheoriginaldesign.Formoreinformationread%thedocumentenclosed.%input_parameters=inp_para_definitiSB^_了一個輸入變量的結(jié)構(gòu),[N,M]=size(parent_chromosome);注意這里的M其實是個垃圾,后面語句會對其重新賦值利用switchprocase1M=2;=6;case2M=3;=12;case3%case1和case2用來對整個算法進(jìn)行常規(guī)驗證,作為調(diào)試之用;case3為本工程所需;M=2;%(outputparameter個數(shù))V=8;%(inputparameters個數(shù))K=10;endp=1;was_crossover=0;was_mutation=0;l_limit=0;u_limit=1;fori=1:Nifrand(1)<0.9child_1=[];child_2=[];parent_1=round(N*rand(1));ifparent_1<1parent_1=1;endparent_2=round(N*rand(1));ifparent_2<1parent_2=1;endwhileisequal(parent_chromosome(parent_1,:),parent_chromosome(parent_2,:))parent_2=round(N*rand(1));ifparent_2<1parent_2=1;endend%目的是隨機(jī)找出兩個不同的候選人parent_1=parent_chro
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年中國社會科學(xué)院考古研究所石窟寺考古研究室考古技師招聘備考題庫完整參考答案詳解
- 2024年唐山市事業(yè)單位招聘考試真題
- 2025年大理州強(qiáng)制隔離戒毒所公開招聘輔警5人備考題庫及完整答案詳解一套
- 青島海明城市發(fā)展有限公司及全資子公司招聘考試真題2024
- 2025 九年級語文下冊戲劇舞臺設(shè)計意圖課件
- 2025年廣西百色市樂業(yè)縣專業(yè)森林消防救援隊伍招聘13人筆試重點題庫及答案解析
- 河口縣公安局公開招聘輔警(16人)備考考試試題及答案解析
- 2025-2026 學(xué)年高一 語文 期末沖刺卷 試卷及答案
- 國家知識產(chǎn)權(quán)局專利局專利審查協(xié)作北京中心福建分中心2026年度專利審查員公開招聘備考題庫帶答案詳解
- 2025年互聯(lián)網(wǎng)保險產(chǎn)品五年政策影響分析報告
- 麻醉科教學(xué)查房課件
- 工作秘密管理課件
- 一級建造師-水利工程實務(wù)電子教材
- 急救物品護(hù)理質(zhì)量管理
- 2025-2030年中國地奧司明行業(yè)市場現(xiàn)狀供需分析及投資評估規(guī)劃分析研究報告
- 前列腺炎病人的護(hù)理
- 國家開放大學(xué)《理工英語4》期末機(jī)考題庫
- 學(xué)校午休設(shè)備管理制度
- T/ZGZS 0302-2023再生工業(yè)鹽氯化鈉
- 聯(lián)合創(chuàng)立品牌協(xié)議書
- 2025人教版(PEP)三年級英語上冊期末專項復(fù)習(xí):補(bǔ)全對話專項(附答案)
評論
0/150
提交評論