MATLAB应用实战系列NSGA-II多目标优化算法原理及应用实例(附MATLAB代码)
前言?
NSGA-Ⅱ是最流行的多目標遺傳算法之一,它降低了非劣排序遺傳算法的復雜性,具有運行速度快,解集的收斂性好的優點,成為其他多目標優化算法性能的基準。
NSGA-Ⅱ算法是 Srinivas 和 Deb 于 2000 年在 NSGA 的基礎上提出的,它比 NSGA算法更加優越:它采用了快速非支配排序算法,計算復雜度比 NSGA 大大的降低;采用了擁擠度和擁擠度比較算子,代替了需要指定的共享半徑 shareQ,并在快速排序后的同級比較中作為勝出標準,使準 Pareto 域中的個體能擴展到整個 Pareto 域,并均勻分布,保持了種群的多樣性;引入了精英策略,擴大了采樣空間,防止最佳個體的丟失,提高了算法的運算速度和魯棒性。
以下是博主精心整理的兩個matlab專欄,包含入門到精通及實戰內容,需要的小伙伴可根據自己需求自行訂閱。
MATLAB-30天帶你從入門到精通
https://blog.csdn.net/wenyusuran/category_10614422.html
MATLAB深入理解高級教程(附源碼)
https://blog.csdn.net/wenyusuran/category_2239265.html
需要詳細源碼的小伙伴可參見:
數學建模源碼集錦-基于遺傳算法的多目標優化算法應用實例
https://download.csdn.net/download/wenyusuran/15749792
NSGA-II與常規遺傳算法的區別
選擇過程分兩個部分:
1. 把種群分成一組Pareto非支配集。一個非支配集里的個體不被當前或之后非支配集里的任何個體支配。方法就是每次選出所有不被任何其他個體支配的非支配個體,從種群里刪除當一個非支配集,然后剩下的再不停重復這個過程,直到取完。
2. 按crowd distance排序。就是在各個維度左右相鄰個體的距離之和。
NSGA-II關鍵算法
1.先對M個個體求pareto解。然后得到F1,F2……等這些pareto的集合。
2.把F1的所有個體全部放入N,若N沒滿,繼續放F2,直到有Fk不能全部放入已經放入F1、F2、…、F(k-1)的N(空間)。此時對Fk進行求解。
3.對于Fk中的個體,求出Fk中的每個個體的擁擠距離Lk[i](crowding distance),在fk中按照Lk[i]遞減排序,放入N中,直到N滿。
?
NSGA-II關鍵子程序算法
NSGA-II在常規遺傳算法上的改進,關鍵步驟就3步。
?
1)快速非支配排序算子的設計
?
多目標優化問題的設計關鍵在于求取Pareto最優解集。NSGA-II算法中的快速非支配排序是根據個體的非劣解水平對種群分層,其作用是指引搜索向Pareto最優解集方向進行。它是一個循環的適應值分級過程:首先找出群體中非支配解集,記為第一非支配層F,將其所有個體賦予非支配序值irank=1(其中irank是個體i的非支配排序值),并從整個種群中除去;然后繼續找出余下群體中非支配解集,記為第二非支配排序層F2,個體被賦予非支配序值irank=2;照此進行下去,直到整個種群被分層,同一分層內的個體具有相同的非支配序值irank。
?
2)個體擁擠距離算子設計
?
為了能夠在具有相同irank的個體內進行選擇性排序,NSGA-II提出了個體擁擠距離的概念。個體i的擁擠距離是目標空間上與i相鄰的2個個體i+1和i-1之間的距離,其計算步驟為:
a)對同層的個體初始化距離。令L[i]d=0(其中L[i]d表示任意個體i的擁擠距離);
b)對同層的個體按第m個目標函數值升序排列;
c)使得排序邊緣上的個體具有選擇優勢。給定一個大數M,令L[1]d=L[end]d=M;
d)對排序中間的個體,求擁擠距離:
(其中:L[i+1]m為第i+1個個體的第m目標函數值,和分別為集合中第m目標函數值的最大值和最小值)
e)對不同的目標函數,重復步驟a)~步驟d)操作,得到個體i的擁擠距離L[i]d,通過優先選擇擁擠距離較大的個體,可使計算結果在目標空間比較均勻分布,以維持種群的多樣性。
?
3)精英策略選擇算子
?
精英策略即保留父代中的優良個體直接進入子代,以防止獲得的Pareto最優解丟失。精英策略選擇算子按3個指標對由父代Ci和子代Di合成的種群Ri進行優選,以組成新的父代種群Ci+1。首先淘汰父代中方案校驗標志為不可行的方案;其次按照非支配序值irank從低到高排序,將整層種群依次放入Ci+1,直到放入某一層Fj時出現Ci+1大小超過種群規模限制N的情況;最后,依據Fj中的個體擁擠距離由大到小的順序繼續填充Ci+1直到種群數量達到N時終止。
下面這個圖片能很好的說明NSGA-II的實現過程
最后附上用NSGA-II求解ZDT1函數的MATLAB代碼,ZDT1函數如下:
?
?
源代碼解析
01 ?|?主函數Main?NSGA2
?
主函數Main_NSGA2,運行主函數的時候,命令行窗口會出現Test problem index? :,這時需要輸入1~14中的任意一個數字,意思就是選擇14個測試函數中的任意一個函數。
%%?https://ww2.mathworks.cn/matlabcentral/fileexchange/49806-matlab-code-for-constrained-nsga-ii-dr-s-baskar-s-tamilselvi-and-p-r-varshini clear?all clc global?V?M?xl?xu?etac?etam?p?pop_size?pm%%?Description%?1.?This?is?the?main?program?of?NSGA?II.?It?requires?only?one?input,?which?is?test?problem %????index,?'p'.?NSGA?II?code?is?tested?and?verified?for?14?test?problems. %?2.?This?code?defines?population?size?in?'pop_size',?number?of?design %????variables?in?'V',?number?of?runs?in?'no_runs',?maximum?number?of %????generations?in?'gen_max',?current?generation?in?'gen_count'?and?number?of?objectives %????in?'M'. %?3.?'xl'?and?'xu'?are?the?lower?and?upper?bounds?of?the?design?variables. %?4.?Final?optimal?Pareto?soutions?are?in?the?variable?'pareto_rank1',?with?design %????variables?in?the?coumns?(1:V),?objectives?in?the?columns?(V+1?to?V+M), %????constraint?violation?in?the?column?(V+M+1),?Rank?in?(V+M+2),?Distance?in?(V+M+3). %%?code?starts M=2; p=input('Test?problem?index??:'); pop_size=200;???????????%?Population?size no_runs=1;??????????????%?Number?of?runs gen_max=500;????????????%?MAx?number?of?generations?-?stopping?criteria fname='test_case';??????%?Objective?function?and?constraint?evaluationif?p==13??%?OSYpop_size=100;no_runs=10; end if?(p==2?||?p==5?||?p==7),?gen_max=1000;?endif?p<=9?????%?Unconstrained?test?functionstV=[2;30;3;1;30;4;30;10;10];V=tV(p);txl=[-5*ones(1,V);zeros(1,V);-5*ones(1,V);-1000*ones(1,V);zeros(1,V);-1/sqrt(V)*ones(1,V);zeros(1,V);?0?-5*ones(1,V-1);zeros(1,V)];txu=[10*ones(1,V);?ones(1,V);5*ones(1,V);1000*ones(1,V);ones(1,V);1/sqrt(V)?*ones(1,V);ones(1,V);1?5*ones(1,V-1);ones(1,V)];xl=(txl(p,1:V));????????????%?lower?bound?vectorxu=(txu(p,1:V));????????????%?upper?bound?vectorforetac?=?20;??????????????????%?distribution?index?for?crossoveretam?=?20;??????????????????%?distribution?index?for?mutation?/?mutation?constant else?????????%?Constrained?test?functionsp1=p-9;tV=[2;2;2;6;2];V=tV(p1);txl=[0?0?0?0?0?0;-20?-20?0?0?0?0;0?0?0?0?0?0;0?0?1?0?1?0;0.1?0?0?0?0?0];txu=[5?3?0?0?0?0;20?20?0?0?0?0;pi?pi?0?0?0?0;10?10?5?6?5?10;1?5?0?0?0?0];xl=(txl(p1,1:V));???????????%?lower?bound?vectorxu=(txu(p1,1:V));???????????%?upper?bound?vectorfor?i=1:NNetac?=?20;??????????????????%?distribution?index?for?crossoveretam?=?100;?????????????????%?distribution?index?for?mutation?/?mutation?constant end pm=1/V;?????????????????????%?Mutation?ProbabilityQ=[]; for?run?=?1:no_runs%%?Initial?populationxl_temp=repmat(xl,?pop_size,1);xu_temp=repmat(xu,?pop_size,1);x?=?xl_temp+((xu_temp-xl_temp).*rand(pop_size,V));%%?Evaluate?objective?functionfor?i?=1:pop_size[ff(i,:),err(i,:)]?=feval(fname,?x(i,:));???????????%?Objective?function?evaulationenderror_norm=normalisation(err);??????????????????????%?Normalisation?of?the?constraint?violationpopulation_init=[x?ff?error_norm];[population,front]=NDS_CD_cons(population_init);????%?Non?domination?Sorting?on?initial?population%%?Generation?Startsfor?gen_count=1:gen_max%?selection?(Parent?Pt?of?'N'?pop?size)parent_selected=tour_selection(population);?????????????????????%?10?Tournament?selection%%?Reproduction?(Offspring?Qt?of?'N'?pop?size)child_offspring??=?genetic_operator(parent_selected(:,1:V));????%?SBX?crossover?and?polynomial?mutationfor?ii?=?1:pop_size[fff(ii,:)?err(ii,:)]=feval(fname,?child_offspring(ii,:));??????%?objective?function?evaluation?for?offspringenderror_norm=normalisation(err);child_offspring=[child_offspring?fff?error_norm];%%?INtermediate?population?(Rt=?Pt?U?Qt?of?2N?size)population_inter=[population(:,1:V+M+1)?;?child_offspring(:,1:V+M+1)];[population_inter_sorted,?front]=NDS_CD_cons(population_inter);??????????????%?Non?domination?Sorting?on?offspring%%?Replacement?-?Nnew_pop=replacement(population_inter_sorted,?front);population=new_pop;endnew_pop=sortrows(new_pop,V+1);paretoset(run).trial=new_pop(:,1:V+M+1);Q?=?[Q;?paretoset(run).trial];??????????????????????%?Combining?Pareto?solutions?obtained?in?each?run end%%?Result?and?Pareto?plot if?run==1plot(new_pop(:,V+1),new_pop(:,V+2),'*') else[pareto_filter?front]=NDS_CD_cons(Q);???????????????%?Applying?non?domination?sorting?on?the?combined?Pareto?solution?setrank1_index=find(pareto_filter(:,V+M+2)==1);????????%?Filtering?the?best?solutions?of?rank?1?Paretopareto_rank1=pareto_filter(rank1_index,1:V+M)plot(pareto_rank1(:,V+1),pareto_rank1(:,V+2),'*')???%?Final?Pareto?plot end xlabel('objective?function?1') ylabel('objective?function?2') if?p==1title('?1?-?Test?case?1') elseif?p==2title('?2??-?ZDT1') elseif?p==3title('?3??-?KUR') elseif?p==4title('?4??-?SCH') elseif?p==5title('?5??-?ZDT2') elseif?p==6title('?6??-?Test?case?3') elseif?p==7title('?7??-?ZDT3') elseif?p==8title('?8??-?ZDT4') elseif?p==9title('?9??-?ZDT6') elseif?p==10title('?10?-?BNH') elseif?p==11title('?11?-?SRN') elseif?p==12title('?12?-?TNK') elseif?p==13title('?13?-?OSY') elseif?p==14title('?14?-?CONSTR') end?
02?| 測試函數test_case
?
前9個測試函數是屬于無約束優化函數,后5個測試函數屬于有約束優化函數。返回值fit是目標函數值,返回值err是違反約束(超過約束)的值。比如說,一個函數的約束為x1+x2<=5,此時err=x1+x2-5,即當x1=4,x2=5時,err=4.
%%?Description%?1.?This?function?returns?the?objective?functions?f1,?and?f2?in?the?vector?'fit'?and %????constraints?in?the?vector?'c'?for?the?chromosome?'x'.? %?2.?'V'?is?the?number?of?optimization?variables.? %?3.?All?the?constraints?'c'?are?converted?to?the?form?h(x)<=0.? %?4.?Nine?unconstrained?test?probems?are?used?(p=1?to?p=9) %?5.?Five?constrained?test?problems?are?used???(p=10?to?p=14) %?6.?Refer?above?references?for?the?details?of?each?test?problem:?number?of?objectives,?number?of?design?variabes,?their?lower?and?upper?limits,? %????number?of?constraints,?type?of?constraints?etc,.%%?reference %?1.?BINH,?Thanh.?"A?multiobjective?evolutionary?algorithm.?The?study?cases". %????Technical?report.?Barleben,?Germany.?1999. %?2.?DEB,?Kalyanmoy.?"Multi-Objective?optimization?using?evolutionary %????algorithms".?John?Wiley?&?Sons,?LTD.?Kanpur,?India.?2004.function?[fit?err]=test_case(x) global?p?V %%?Unconstrained?Test?functions?(for?p=1?to?p=9) if?p==1?????%?Test?case?problem?1f1=(4*x(1)^2)+(4*x(2)^2);???????????????????????f2=((x(1)-5)^2)+((x(2)-5)^2);??????????????? endif?p==2?????%?ZDT1?from?Deb?paper?NSGA2cons=[0];f1?=?x(1);g=1+(9*sum(x(2:V),2)/(V-1));????????????f2?=?g*(1-sqrt(x(1)/g));?????????????????? endif?p==3?????%?kUR?from?Debf1=(-10*exp(-0.2*(sqrt(x(1)^2+x(2)^2))))+(-10*exp(-0.2*(sqrt(x(2)^2+x(3)^2))));f2=((abs(x(1))^0.8)?+?(5*sin(x(1))^3))+((abs(x(2))^0.8)?+?(5*sin(x(2))^3))+((abs(x(3))^0.8)?+?(5*sin(x(3))^3)); endif?p==4????%?SCH?frm?Deb?paperf1=x.*x;f2=(x-2).^2; endif?p==5?????%?ZDT2f1?=?(x(1));g=1+(9*sum(x(2:V),2)/(V-1));?????????????f2?=((1-(x(1)/g)^2));???????????????? end???if?p==6?????%?Test?case?problem?2f1=1-exp(-sum((x-1/sqrt(V)).^2,2));f2=1-exp(-sum((x+1/sqrt(V)).^2,2)); endif?p==7?????%?ZDT3f1?=?x(1);?????????????????????????????????g=1+(9*sum(x(2:V),2)/(V-1));???????????????f2?=?(1-(sqrt(x(1)/g))?-?((x(1)/g)*sin(10*pi*x(1))));??????????????? end??if?p==8?????%?ZDT4???????f1?=?x(1);??temp=0;for?ii?=?2:?Vtemp=temp+((x(ii))^2)-(10*cos(4*pi*x(ii)));endg=?1?+?(10*(V-1))?+?temp;???????????f2?=?(1-sqrt(x(1)/g));????????????????? end??if?p==9?????%?ZDT6???????f1?=?1-(exp(-4*x(1)))*(sin(6*pi*x(1)))^6;?g=1+(9*(sum(x(2:V),2)/(V-1))^0.25);????????f2?=?(1-(f1/g)^2);????????????????????? end?? err=?zeros(1,1);%%?Constrained?Test?functions?(for?p=10?to?p=14)if?p==10?????%BNH?f1=4*(x(1)^2)+4*(x(2)^2);f2=(x(1)-5)^2+(x(2)-5)^2;c(1,1)=(x(1)-5)^2?+?x(2)^2?-25;c(1,2)=-(x(1)-8)^2-(x(2)+3)^2+7.7;err=(c>0).*c; end if?p==11?????%SRN??f1=(x(1)-2)^2+(x(2)-1)^2+2;f2=9*x(1)-(x(2)-1)^2;c(1,1)=x(1)^2+x(2)^2-225;c(1,2)=x(1)-(3*x(2))+10;err=(c>0).*c; end if?p==12?????%TNKf1=x(1);f2=x(2);c(1,1)=-x(1)^2-x(2)^2+1+(0.1*cos(16*atan((x(1)/x(2)))));?c(1,2)=(x(1)-0.5)^2+(x(2)-0.5)^2-0.5;err=(c>0).*c; endif?p==13?????%?OSY?f1=-((25*(x(1)-2)^2)+((x(2)-2)^2)+((x(3)-1)^2)+((x(4)-4)^2)+((x(5)-1)^2));f2=(x(1)^2)+(x(2)^2)+(x(3)^2)+(x(4)^2)+(x(5)^2)+(x(6)^2);c(1,1)=-x(1)-x(2)+2;c(1,2)=-6+x(1)+x(2);c(1,3)=-2+x(2)-x(1);c(1,4)=-2+x(1)-3*x(2);c(1,5)=-4+((x(3)-3)^2)+x(4);c(1,6)=-((x(5)-3)^2)-x(6)+4;err=(c>0).*c; endif?p==14????%?CONSTRf1=x(1);f2=(1+x(2))/(x(1));c(1,1)=-x(2)-(9*x(1))+6;c(1,2)=+x(2)-9*x(1)+1;err=(c>0).*c; end fit=[f1?f2];?
03?| 約束函數normalisation
?
normalisation函數將違反約束的數值標準化。比如說輸入[1,4;2,3],那么這兩列中每一列最大值的矩陣為[2,4];然后,將第第一列中每個數除以2,第二列中每個數除以4,則標準化成[0.5,1;1,0.75];最后,將每一行的數值相加,則變成[1.5;1.75]。
function?err_norm??=?normalisation(error_pop)%%?Description %?1.?This?function?normalises?the?constraint?violation?of?various?individuals,?since?the?range?of? %????constraint?violation?of?every?chromosome?is?not?uniform. %?2.?Input?is?in?the?matrix?error_pop?with?size?[pop_size,?number?of?constraints]. %?3.?Output?is?a?normalised?vector,?err_norm?of?size?[pop_size,1]%%?Error?Nomalisation [N,nc]=size(error_pop);con_max=0.001+max(error_pop);con_maxx=repmat(con_max,N,1);cc=error_pop./con_maxx;err_norm=sum(cc,2);????????????????%?finally?sum?up?all?violations?
04?| 排序函數NDS_CD_cons
?
這是關于非支配排序的函數,具體的執行步驟同NSGA-II多目標優化算法講解(附MATLAB代碼)這篇博文的執行步驟。
%%?Description %?1.?This?function?is?to?perform?Deb's?fast?elitist?non-domination?sorting?and?crowding?distance?assignment.? %?2.?Input?is?in?the?variable?'population'?with?size:?[size(popuation),?V+M+1] %?3.?This?function?returns?'chromosome_NDS_CD'?with?size?[size(population),V+M+3] %?4.?A?flag?'problem_type'?is?used?to?identify?whether?the?population?is?fully?feasible?(problem_type=0)?or?fully?infeasible?(problem_type=1)? %????or?partly?feasible?(problem_type=0.5).?%%?Reference: %Kalyanmoy?Deb,?Amrit?Pratap,?Sameer?Agarwal,?and?T.?Meyarivan,?"?A?Fast?and?Elitist?Multiobjective?Genetic?Algorithm:?NSGA-II",? %IEEE?TRANSACTIONS?ON?EVOLUTIONARY?COMPUTATION,?VOL.?6,?No.?2,?APRIL?2002.?%%?function?begins function?[chromosome_NDS_CD?front]?=?NDS_CD_cons(population) global?V?M?problem_type?%%?Initialising?structures?and?variables chromosome_NDS_CD1=[]; infpop=[]; front.fr=[]; struct.sp=[]; rank=1;%%?Segregating?feasible?and?infeasible?solutionsif?all(population(:,V+M+1)==0)problem_type=0;chromosome=population(:,1:V+M);?????????????????????????%?All?Feasible?chromosomes;????pop_size1=size(chromosome,1); elseif?all(population(:,V+M+1)~=0)problem_type=1;pop_size1=0;infchromosome=population;???????????????????????????????%?All?InFeasible?chromosomes;??????? elseproblem_type=0.5;feas_index=find(population(:,V+M+1)==0);chromosome=population(feas_index,1:V+M);????????????????%?Feasible?chromosomes;????pop_size1=size(chromosome,1);infeas_index=find(population(:,V+M+1)~=0);infchromosome=population(infeas_index,1:V+M+1);?????????%?infeasible?chromosomes;???? end%%?Handling?feasible?solutions? if?problem_type==0?|?problem_type==0.5pop_size1?=?size(chromosome,1);f1?=?chromosome(:,V+1);???%?objective?function?valuesf2?=?chromosome(:,V+2); %Non-?Domination?Sorting? %?First?front for?p=1:pop_size1struct(p).sp=find(((f1(p)-f1)<0?&(f2(p)-f2)<0)?|?((f2(p)-f2)==0?&(f1(p)-f1)<0)?|?((f1(p)-f1)==0?&(f2(p)-f2)<0));?n(p)=length(find(((f1(p)-f1)>0?&(f2(p)-f2)>0)?|?((f2(p)-f2)==0?&(f1(p)-f1)>0)?|?((f1(p)-f1)==0?&(f2(p)-f2)>0))); endfront(1).fr=find(n==0); %?Creating?subsequent?fronts while?(~isempty(front(rank).fr))front_indiv=front(rank).fr;n(front_indiv)=inf;chromosome(front_indiv,V+M+1)=rank;rank=rank+1;front(rank).fr=[];for?i?=?1:length(front_indiv)temp=struct(front_indiv(i)).sp;n(temp)=n(temp)-1;end?q=find(n==0);front(rank).fr=[front(rank).fr?q];end chromosome_sorted=sortrows(chromosome,V+M+1);????%?Ranked?population??%Crowding?distance?Assignment rowsindex=1; for?i?=?1:length(front)-1l_f=length(front(i).fr);if?l_f?>?2sorted_indf1=[];sorted_indf2=[];sortedf1=[];sortedf2=[];%?sorting?based?on?f1?and?f2; [sortedf1?sorted_indf1]=sortrows(chromosome_sorted(rowsindex:(rowsindex+l_f-1),V+1)); [sortedf2?sorted_indf2]=sortrows(chromosome_sorted(rowsindex:(rowsindex+l_f-1),V+2));f1min=chromosome_sorted(sorted_indf1(1)+rowsindex-1,V+1); f1max=chromosome_sorted(sorted_indf1(end)+rowsindex-1,V+1);chromosome_sorted(sorted_indf1(1)+rowsindex-1,V+M+2)=inf; chromosome_sorted(sorted_indf1(end)+rowsindex-1,V+M+2)=inf;f2min=chromosome_sorted(sorted_indf2(1)+rowsindex-1,V+2); f2max=chromosome_sorted(sorted_indf2(end)+rowsindex-1,V+2);chromosome_sorted(sorted_indf2(1)+rowsindex-1,V+M+3)=inf; chromosome_sorted(sorted_indf2(end)+rowsindex-1,V+M+3)=inf;for?j?=?2:length(front(i).fr)-1if??(f1max?-?f1min?==?0)?|?(f2max?-?f2min?==?0)chromosome_sorted(sorted_indf1(j)+rowsindex-1,V+M+2)=inf;chromosome_sorted(sorted_indf2(j)+rowsindex-1,V+M+3)=inf;elsechromosome_sorted(sorted_indf1(j)+rowsindex-1,V+M+2)=(chromosome_sorted(sorted_indf1(j+1)+rowsindex-1,V+1)-chromosome_sorted(sorted_indf1(j-1)+rowsindex-1,V+1))/(f1max-f1min);chromosome_sorted(sorted_indf2(j)+rowsindex-1,V+M+3)=(chromosome_sorted(sorted_indf2(j+1)+rowsindex-1,V+2)-chromosome_sorted(sorted_indf2(j-1)+rowsindex-1,V+2))/(f2max-f2min);endendelsechromosome_sorted(rowsindex:(rowsindex+l_f-1),V+M+2:V+M+3)=inf;endrowsindex?=?rowsindex?+?l_f; end chromosome_sorted(:,V+M+4)?=?sum(chromosome_sorted(:,V+M+2:V+M+3),2);? chromosome_NDS_CD1?=?[chromosome_sorted(:,1:V+M)?zeros(pop_size1,1)?chromosome_sorted(:,V+M+1)?chromosome_sorted(:,V+M+4)];?%?Final?Output?Variable end%%?Handling?infeasible?solutions if?problem_type==1?|?problem_type==0.5 infpop=sortrows(infchromosome,V+M+1); infpop=[infpop(:,1:V+M+1)?(rank:rank-1+size(infpop,1))'?inf*(ones(size(infpop,1),1))]; for?kk?=?(size(front,2)):(size(front,2))+(length(infchromosome))-1;front(kk).fr=?pop_size1+1; end end chromosome_NDS_CD?=?[chromosome_NDS_CD1;infpop];??
05?|?tour_selection
?
function?[parent_selected]?=?tour_selection(pool) %%?Description%?1.?Parents?are?selected?from?the?population?pool?for?reproduction?by?using?binary?tournament?selection %????based?on?the?rank?and?crowding?distance.? %?2.?An?individual?is?selected?if?the?rank?is?lesser?than?the?other?or?if %????crowding?distance?is?greater?than?the?other. %?3.?Input?and?output?are?of?same?size?[pop_size,?V+M+3].%%?Binary?Tournament?Selection [pop_size,?distance]=size(pool); rank=distance-1; candidate=[randperm(pop_size);randperm(pop_size)]';for?i?=?1:?pop_sizeparent=candidate(i,:);??????????????????????????????????%?Two?parents?indexes?are?randomly?selectedif?pool(parent(1),rank)~=pool(parent(2),rank)??????????????%?For?parents?with?different?ranksif?pool(parent(1),rank)<pool(parent(2),rank)????????????%?Checking?the?rank?of?two?individualsmincandidate=pool(parent(1),:);elseif?pool(parent(1),rank)>pool(parent(2),rank)mincandidate=pool(parent(2),:);end parent_selected(i,:)=mincandidate;??????????????????????????%?Minimum?rank?individual?is?selected?finallyelse???????????????????????????????????????????????????????%?for?parents?with?same?ranks??if?pool(parent(1),distance)>pool(parent(2),distance)????%?Checking?the?distance?of?two?parentsmaxcandidate=pool(parent(1),:);elseif?pool(parent(1),distance)<?pool(parent(2),distance)maxcandidate=pool(parent(2),:);elsetemp=randperm(2);maxcandidate=pool(parent(temp(1)),:);end? parent_selected(i,:)=maxcandidate;??????????????????????????%?Maximum?distance?individual?is?selected?finally end end?
06?|?genetic_operator
?
function?child_offspring??=?genetic_operator(parent_selected) global?V?xl?xu?etac? %%?Description %?1.?Crossover?followed?by?mutation %?2.?Input?is?in?'parent_selected'?matrix?of?size?[pop_size,V]. %?3.?Output?is?also?of?same?size?in?'child_offspring'.?%%?Reference? %?Deb?&?samir?agrawal,"A?Niched-Penalty?Approach?for?Constraint?Handling?in?Genetic?Algorithms".? %%?SBX?cross?over?operation?incorporating?boundary?constraint [N]?=?size(parent_selected,1); xl1=xl'; xu1=xu'; rc=randperm(N); for?i=1:(N/2)parent1=parent_selected((rc(2*i-1)),:);parent2=parent_selected((rc(2*i)),:);if?(isequal(parent1,parent2))==1?&?rand(1)>0.5child1=parent1;child2=parent2;else?for?j?=?1:?V??if?parent1(j)<parent2(j)beta(j)=?1?+?(2/(parent2(j)-parent1(j)))*(min((parent1(j)-xl1(j)),(xu1(j)-parent2(j))));elsebeta(j)=?1?+?(2/(parent1(j)-parent2(j)))*(min((parent2(j)-xl1(j)),(xu1(j)-parent1(j))));end???endu=rand(1,V);alpha=2-beta.^-(etac+1);betaq=(u<=(1./alpha)).*(u.*alpha).^(1/(etac+1))+(u>(1./alpha)).*(1./(2?-?u.*alpha)).^(1/(etac+1));child1=0.5*(((1?+?betaq).*parent1)?+?(1?-?betaq).*parent2);child2=0.5*(((1?-?betaq).*parent1)?+?(1?+?betaq).*parent2);endchild_offspring((rc(2*i-1)),:)=poly_mutation(child1);???????????%?polynomial?mutationchild_offspring((rc(2*i)),:)=poly_mutation(child2);?????????????%?polynomial?mutation end?
07?|?poly_mutation
?
function?mutated_child?=?poly_mutation(y) global?V?xl?xu?etam?pm%%?Description %?1.?Input?is?the?crossovered?child?of?size?(1,V)?in?the?vector?'y'?from?'genetic_operator.m'. %?2.?Output?is?in?the?vector?'mutated_child'?of?size?(1,V). %%?Polynomial?mutation?including?boundary?constraint del=min((y-xl),(xu-y))./(xu-xl); t=rand(1,V); loc_mut=t<pm;???????? u=rand(1,V); delq=(u<=0.5).*((((2*u)+((1-2*u).*((1-del).^(etam+1)))).^(1/(etam+1)))-1)+(u>0.5).*(1-((2*(1-u))+(2*(u-0.5).*((1-del).^(etam+1)))).^(1/(etam+1))); c=y+delq.*loc_mut.*(xu-xl); mutated_child=c;?
08?|?replacement
?
function?new_pop=replacement(population_inter_sorted,?front) global?pop_size? %%?Description %?The?next?generation?population?is?formed?by?appending?each?front?subsequently?until?the %?population?size?exceeds?the?current?population?size.?If?When?adding?all?the?individuals %?of?any?front,?the?population?exceeds?the?population?size,?then?the?required?number?of? %?remaining?individuals?alone?are?selected?from?that?particular?front?based %?on?crowding?distance. %%?code?starts index=0; ii=1; while?index?<?pop_sizel_f=length(front(ii).fr);if?index+l_f?<?pop_size?new_pop(index+1:index+l_f,:)=?population_inter_sorted(index+1:index+l_f,:);index=index+l_f;elsetemp1=population_inter_sorted(index+1:index+l_f,:);temp2=sortrows(temp1,size(temp1,2));new_pop(index+1:pop_size,:)=?temp2(l_f-(pop_size-index)+1:l_f,:);index=index+l_f;endii=ii+1; end?
?
總結
以上是生活随笔為你收集整理的MATLAB应用实战系列NSGA-II多目标优化算法原理及应用实例(附MATLAB代码)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据结构常见算法原理讲解100篇(一)-
- 下一篇: 职业大揭秘,算法攻城狮在日常工作中都干了