资源描述:
《「基因演算法」学习速成》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、「基因演算法」學習速成南台科技大學電機系趙春棠講解%以下程式作者:清大張智星教授,摘自“Neuro-FuzzyandSoftComputing,”J.-S.R.Jang,C.-T.Sun,andE.Mizutani【讀者可自張教授網站下載該書籍中的所有Matlab程式】%主程式:go_ga.m%這是書中的一個範例,了解每一個程式指令後,大概就對「基因演算法」,就能有一全面性的初步了解了!clearall;closeall;clc;generation_n=30;%NumberofgenerationspopuSize=20;%Populationsizexover_
2、rate=1.0;%Crossoverratemutate_rate=0.01;%Mutationratebit_n=8;%BitnumberforeachinputvariableglobalOPT_METHOD%optimizationmethod.OPT_METHOD='ga';%Thisisusedfordisplayinpeaksfcnfigure;blackbg;%執行blackbg.mobj_fcn='peaksfcn';%Objectivefunction:'peaksfcn'or'wavefcn'ifstrcmp(obj_fcn,'peaksfcn
3、'),%objectivefunctionis'peaksfcn'var_n=2;%Numberofinputvariablesrange=[-3,3;-3,3];%Rangeoftheinputvariables%Plotpeaksfunctionpeaks;%輸出如下(這是本例所解的問題:兩個輸入變數的函數,求極值)colormap((jet+white)/2);%Plotcontoursofpeaksfunctionfigure;blackbg;[x,y,z]=peaks;pcolor(x,y,z);shadinginterp;holdon;contour(x
4、,y,z,20,'r');holdoff;colormap((jet+white)/2);axissquare;xlabel('X');ylabel('Y');%輸出如下else%objectivefunctionis'wavefcn'var_n=1;%Numberofinputvariablesrange=[0pi];%Rangeoftheinputvariablesx=linspace(range(1),range(2));y=sin(10*x).*sin(x);plot(x,sin(10*x).*sin(x));axis([-infinf-infinf]);%
5、輸出如下(這是另一個求解的問題:單一輸入變數)end%Initialrandompopulationpopu=rand(popuSize,bit_n*var_n)>0.5;%本例即rand(20,8*2)某次輸出(主程式i=1):popu(20*16)=100000101110010000010111001000100100111010101010…………………1100110110001110upper=zeros(generation_n,1);%本例即zeros(30,1)average=zeros(generation_n,1);%本例即zeros(30,1)
6、lower=zeros(generation_n,1);%%本例即zeros(30,1)%MainloopofGAfori=1:generation_n;%本例作30世代%deleteunnecessaryobjectsdelete(findobj(0,'tag','member'));delete(findobj(0,'tag','individual'));delete(findobj(0,'tag','count'));%Evaluateobjectivefunctionforeachindividualfcn_value=evalpopu(popu,bit_
7、n,range,obj_fcn);某次輸出:(主程式i=1)fcn_value(20*1)=0.33040.51620.5897………-0.2983*將以上popu二進位碼,計算出對應的數值,代入函數中即得上述結果。%if(i==1),%fprintf('Initialpopulation.');%fprintf('Pressanykeytocontinue...');%pause;%end%Fillobjectivefunctionmatricesupper(i)=max(fcn_value);%upper、average、lower在本例都是size