版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
遺傳算法程序(一):說明:fga.m為遺傳算法的主程序;采用二進制Gray編碼,采用基于輪盤賭法的非線性排名選擇,均勻交叉,變異操作,而且還引入了倒位操作!function[BestPop,Trace]=fga(FUN,LB,UB,eranum,popsize,pCross,pMutation,pInversion,options)%[BestPop,Trace]=fmaxga(FUN,LB,UB,eranum,popsize,pcross,pmutation)%Findsamaximumofafunctionofseveralvariables.%fmaxgasolvesproblemsoftheform:%maxF(X)subjectto:LB<=X<=UB%BestPop-最優(yōu)的群體即為最優(yōu)的染色體群%Trace-最佳染色體所對應的目標函數(shù)值%FUN-目標函數(shù)%LB-自變量下限%UB-自變量上限%eranum-種群的代數(shù),取100--1000(默認200)%popsize-每一代種群的規(guī)模;此可取50--200(默認100)%pcross-交叉概率,一般取0.5--0.85之間較好(默認0.8)%pmutation-初始變異概率,一般取0.05-0.2之間較好(默認0.1)%pInversion-倒位概率,一般取0.05-0.3之間較好(默認0.2)%options-1*2矩陣,options(1)=0二進制編碼(默認0),option(1)~=0十進制編%碼,option(2)設定求解精度(默認1e-4)%%T1=clock;ifnargin<3,error('FMAXGArequiresatleastthreeinputarguments');endifnargin==3,eranum=200;popsize=100;pCross=0.8;pMutation=0.1;pInversion=0.15;options=[01e-4];endifnargin==4,popsize=100;pCross=0.8;pMutation=0.1;pInversion=0.15;options=[01e-4];endifnargin==5,pCross=0.8;pMutation=0.1;pInversion=0.15;options=[01e-4];endifnargin==6,pMutation=0.1;pInversion=0.15;options=[01e-4];endifnargin==7,pInversion=0.15;options=[01e-4];endiffind((LB-UB)>0)error('數(shù)據(jù)輸入錯誤,請重新輸入(LB<UB):');ends=sprintf('程序運行需要約%.4f秒鐘時間,請稍等',(eranum*popsize/1000));disp(s);globalmnNewPopchildren1children2VarNumbounds=[LB;UB]';bits=[];VarNum=size(bounds,1);precision=options(2);%由求解精度確定二進制編碼長度bits=ceil(log2((bounds(:,2)-bounds(:,1))'./precision));%由設定精度劃分區(qū)間[Pop]=InitPopGray(popsize,bits);%初始化種群[m,n]=size(Pop);NewPop=zeros(m,n);children1=zeros(1,n);children2=zeros(1,n);pm0=pMutation;BestPop=zeros(eranum,n);%分配初始解空間BestPop,TraceTrace=zeros(eranum,length(bits)+1);i=1;whilei<=eranumforj=1:mvalue(j)=feval(FUN(1,:),(b2f(Pop(j,:),bounds,bits)));%計算適應度end[MaxValue,Index]=max(value);BestPop(i,:)=Pop(Index,:);Trace(i,1)=MaxValue;Trace(i,(2:length(bits)+1))=b2f(BestPop(i,:),bounds,bits);[selectpop]=NonlinearRankSelect(FUN,Pop,bounds,bits);%非線性排名選擇[CrossOverPop]=CrossOver(selectpop,pCross,round(unidrnd(eranum-i)/eranum));%采用多點交叉和均勻交叉,且逐步增大均勻交叉的概率%round(unidrnd(eranum-i)/eranum)[MutationPop]=Mutation(CrossOverPop,pMutation,VarNum);%變異[InversionPop]=Inversion(MutationPop,pInversion);%倒位Pop=InversionPop;%更新pMutation=pm0+(iA4)*(pCross/3-pm0)/(eranumA4);%隨著種群向前進化,逐步增大變異率至1/2交叉率p(i)=pMutation;i=i+1;endt=1:eranum;plot(t,Trace(:,1)');title('函數(shù)優(yōu)化的遺傳算法');xlabel('進化世代數(shù)(eranum)');ylabel('每一代最優(yōu)適應度(maxfitness)');[MaxFval,I]=max(Trace(:,1));X=Trace(I,(2:length(bits)+1));holdon;plot(I,MaxFval,'*');text(I+5,MaxFval,['FMAX='num2str(MaxFval)]);str1=sprintf('進化到%d代,自變量為%s時,得本次求解的最優(yōu)值%f\n對應染色體是:%s',I,num2str(X),MaxFval,num2str(BestPop(I,:)));disp(str1);%figure(2);plot(t,p);%繪制變異值增大過程T2=clock;elapsed_time=T2-T1;ifelapsed_time(6)<0elapsed_time(6)=elapsed_time(6)+60;elapsed_time(5)=elapsed_time(5)-1;endifelapsed_time(5)<0elapsed_time(5)=elapsed_time(5)+60;elapsed_time(4)=elapsed_time(4)-1;end%像這種程序當然不考慮運行上小時啦str2=sprintf('程序運行耗時%d小時%d分鐘%.4f',elapsed_time(4),elapsed_time(5),elapsed_time(6));disp(str2);%初始化種群%采用二進制Gray編碼,其目的是為了克服二進制編碼的Hamming懸崖缺點function[initpop]=InitPopGray(popsize,bits)len=sum(bits);initpop=zeros(popsize,len);%Thewholezeroencodingindividualfori=2:popsize-1pop=round(rand(1,len));pop=mod(([0pop]+[pop0]),2);%i=1時,b(1)=a(1);i>1時,b(i)=mod(a(i-1)+a(i),2)%其中原二進制串:a(1)a(2)...a(n),Gray串:b(1)b(2)...b(n)initpop(i,:)=pop(1:end-1);endinitpop(popsize,:)=ones(1,len);%Thewholeoneencodingindividual%解碼function[fval]=b2f(bval,bounds,bits)%fval-表征各變量的十進制數(shù)%bval-表征各變量的二進制編碼串%bounds-各變量的取值范圍%bits-各變量的二進制編碼長度scale=(bounds(:,2)-bounds(:,1))'./(2.Abits-1);%TherangeofthevariablesnumV=size(bounds,1);cs=[0cumsum(bits)];fori=1:numVa=bval((cs(i)+1):cs(i+1));fval(i)=sum(2C(size(a,2)-1:-1:0).*a)*scale(i)+bounds(i,1);end%選擇操作%采用基于輪盤賭法的非線性排名選擇%各個體成員按適應值從大到小分配選擇概率:%P(i)=(q/1-(1-q)An)*(1-q)Ai,其中P(0)>P(1)>...>P(n),sum(P(i))=1function[selectpop]=NonlinearRankSelect(FUN,pop,bounds,bits)globalmnselectpop=zeros(m,n);fit=zeros(m,1);fori=1:mfit(i)=feval(FUN(1,:),(b2f(pop(i,:),bounds,bits)));%以函數(shù)值為適應值做排名依據(jù)endselectprob=fit/sum(fit);%計算各個體相對適應度(0,1)q=max(selectprob);%選擇最優(yōu)的概率x=zeros(m,2);x(:,1)=[m:-1:1]';[yx(:,2)]=sort(selectprob);r=q/(1-(1-q)Am);%標準分布基值newfit(x(:,2))=r*(1-q)C(x(:,1)-1);%生成選擇概率newfit=cumsum(newfit);%計算各選擇概率之和rNums=sort(rand(m,1));fitIn=1;newIn=1;whilenewIn<=mifrNums(newIn)<newfit(fitIn)selectpop(newIn,:)=pop(fitIn,:);newIn=newIn+1;elsefitIn=fitIn+1;endend%交叉操作function[NewPop]=CrossOver(OldPop,pCross,opts)%OldPop為父代種群,pcross為交叉概率globalmnNewPopr=rand(1,m);y1=find(r<pCross);y2=find(r>=pCross);len=length(y1);iflen>2&mod(len,2)==1%如果用來進行交叉的染色體的條數(shù)為奇數(shù),將其調(diào)整為偶數(shù)y2(length(y2)+1)=y1(len);y1(len)=[];endiflength(y1)>=2fori=0:2:length(y1)-2ifopts==0[NewPop(y1(i+1),:),NewPop(y1(i+2),:)]=EqualCrossOver(OldPop(y1(i+1),:),OldPop(y1(i+2),:));else[NewPop(y1(i+1),:),NewPop(y1(i+2),:)]=MultiPointCross(OldPop(y1(i+1),:),OldPop(y1(i+2),:));endendendNewPop(y2,:)=OldPop(y2,:);%采用均勻交叉function[children1,children2]=EqualCrossOver(parent1,parent2)globalnchildren1children2hidecode=round(rand(1,n));%隨機生成掩碼crossposition=find(hidecode==1);holdposition=find(hidecode==0);children1(crossposition)=parent1(crossposition);%掩碼為1,父1為子1提供基因children1(holdposition)=parent2(holdposition);%掩碼為0,父2為子1提供基因children2(crossposition)=parent2(crossposition);%掩碼為1,父2為子2提供基因children2(holdposition)=parent1(holdposition);%掩碼為0,父1為子2提供基因%采用多點交叉,交叉點數(shù)由變量數(shù)決定function[Children1,Children2]=MultiPointCross(Parent1,Parent2)globalnChildren1Children2VarNumChildren1=Parent1;Children2=Parent2;Points=sort(unidrnd(n,1,2*VarNum));fori=1:VarNumChildren1(Points(2*i-1):Points(2*i))=Parent2(Points(2*i-1):Points(2*i));Children2(Points(2*i-1):Points(2*i))=Parent1(Points(2*i-1):Points(2*i));end%變異操作function[NewPop]=Mutation(OldPop,pMutation,VarNum)globalmnNewPopr=rand(1,m);position=find(r<=pMutation);len=length(position);iflen>=1fori=1:lenk=unidrnd(n,1,VarNum);%設置變異點數(shù),一般設置1點forj=1:length(k)ifOldPop(position(i),k(j))==1OldPop(position(i),k(j))=0;elseOldPop(position(i),k(j))=1;endendendendNewPop=OldPop;%倒位操作function[NewPop]=Inversion(OldPop,pInversion)globalmnNewPopNewPop=OldPop;r=rand(1,m);PopIn=find(r<=pInversion);len=length(PopIn);iflen>=1fori=1:lend=sort(unidrnd(n,1,2));ifd(1)~=1&d(2)~=nNewPop(PopIn(i),1:d(1)-1)=OldPop(PopIn(i),1:d(1)-1);NewPop(PopIn(i),d(1):d(2))=OldPop(PopIn(i),d(2):-1:d(1));NewPop(PopIn(i),d(2)+1:n)=OldPop(PopIn(i),d(2)+1:n);endendend遺傳算法程序(二):functionyouhuafunD=code;N=50;%Tunablemaxgen=50;%Tunablecrossrate=0.5;%Tunablemuterate=0.08;%Tunablegeneration=1;num=length(D);fatherrand=randint(num,N,3);score=zeros(maxgen,N);whilegeneration<=maxgenind=randperm(N-2)+2;%隨機配對交叉A=fatherrand(:,ind(1:(N-2)/2));B=fatherrand(:,ind((N-2)/2+1:end));%多點交叉rnd=rand(num,(N-2)/2);ind=rndtmp=A(ind);A(ind)=B(ind);B(ind)=tmp;%%兩點交叉%%%%%%%rndtmp=randint(1,1,num)+1;tmp=A(1:rndtmp,kk);A(1:rndtmp,kk)=B(1:rndtmp,kk);B(1:rndtmp,kk)=tmp;%endfatherrand=[fatherrand(:,1:2),A,B];%變異rnd=rand(num,N);ind=rnd[m,n]=size(ind);tmp=randint(m,n,2)+1;tmp(:,1:2)=0;fatherrand=tmp+fatherrand;fatherrand=mod(fatherrand,3);%fatherrand(ind)=tmp;%評價、選擇scoreN=scorefun(fatherrand,D);%求得N個個體的評價函數(shù)score(generation,:)=scoreN;[scoreSort,scoreind]=sort(scoreN);sumscore=cumsum(scoreSort);sumscore=sumscore./sumscore(end);childind(1:2)=scoreind(end-1:end);fork=3:Ntmprnd=rand;tmpind=tmprnddifind=[0,diff(tmpind)];if~any(difind)difind(1)=1;endchildind(k)=scoreind(logical(difind));endfatherrand=fatherrand(:,childind);generation=generation+1;end%scoremaxV=max(score,[],2);minV=11*300-maxV;plot(minV,'*');title('各代的目標函數(shù)值');F4=D(:,4);FF4=F4-fatherrand(:,1);FF4=max(FF4,1);D(:,5)=FF4;saveDDataDfunctionD=codeloadyouhua.mat%propertiesF2andF3F1=A(:,1);F2=A(:,2);F3=A(:,3);if(max(F2)>1450)||(min(F2)<=900)error('DATApropertyF2exceedit''srange(900,1450]')end%getgrouppropertyF1ofdata,accordingtoF2valueF4=zeros(size(F1));forite=11:-1:1index=find(F2<=900+ite*50);F4(index)=ite;endD=[F1,F2,F3,F4];functionScoreN=scorefun(fatherrand,D)F3=D(:,3);
F4=D(:,4);N=size(fatherrand,2);FF4=F4*ones(1,N);FF4rnd=FF4-fatherrand;FF4rnd=max(FF4rnd,1);ScoreN=ones(1,N)*300*11;%這里有待優(yōu)化fork=1:NFF4k=FF4rnd(:,k);forite=1:11F0index=find(FF4k==ite);if~isempty(F0index)tmpMat=F3(F0index);tmpSco=sum(tmpMat);ScoreBin(ite)=mod(tmpSco,300);endendScorek(k)=sum(ScoreBin);endScoreN=ScoreN-Scorek;遺傳算法程序(三):%最大迭代步數(shù)%最大迭代步數(shù)%當前最大的適應度%停止循環(huán)的適應度%初始取值范圍[0255]%進入加速迭代的間隔%優(yōu)化的次數(shù)MAX_gen=200;best.max_f=0;STOP_f=14.5;RANGE=[0255];SPEEDUP_INTER=5;advance_k=0;popus=init;%初始化forgen=1:MAX_genfitness=fit(popus,RANGE);%求適應度f=fitness.f;picked=choose(popus,fitness);%選擇popus=intercross(popus,picked);%雜交popus=aberrance(popus,picked);%變異ifmax(f)>best.max_fadvance_k=advance_k+1;x_better(advance_k)=fitness.x;best.max_f=max(f);best.popus=popus;best.x=fitness.x;endifmod(advance_k,SPEEDUP_INTER)==0RANGE=minmax(x_better);RANGEadvance=0;endendreturn;functionpopus=init%初始化M=50;%種群個體數(shù)目N=30;%編碼長度popus=round(rand(M,N));return;functionfitness=fit(popus,RANGE)%求適應度[M,N]=size(popus);fitness=zeros(M,1);%適應度f=zeros(M,1);%函數(shù)值A=RANGE(1);B=RANGE(2);%初始取值范圍[0255]form=1:Mx=0;forn=1:Nx=x+popus(m,n)*(2A(n-1));endx=x*((B-A)/(2AN))+A;fork=1:5f(m,1)=f(m,1)-(k*sin((k+1)*x+k));endendf_std=(f-min(f))./(max(f)-min(f));%函數(shù)值標準化fitness.f=f;fitness.f_std=f_std;fitness.x=x;return;functionpicked=choose(popus,fitness)%選擇f=fitness.f;f_std=fitness.f_std;[M,N]=size(popus);choose_N=3;%選擇choose_N對雙親picked=zeros(choose_N,2);%記錄選擇好的雙親p=zeros(M,1);%選擇概率d_order=zeros(M,1);%把父代個體按適應度從大到小排序f_t=sort(f,'descend');%將適應度按降序排列fork=1:Mx=find(f==f_t(k));%降序排列的個體序號d_order(k)=x(1);endform=1:Mpopus_t(m,:)=popus(d_order(m),:);endpopus=popus_t;f=f_t;p=f_std./sum(f_std);%選擇概率c_p=cumsum(p)';%累積概率forcn=1:choose_Npicked(cn,1)=roulette(c_p);%輪盤賭picked(cn,2)=roulette(c_p);%輪盤賭popus=intercross(popus,picked(cn,:));%雜交endpopus=aberrance(popus,picked);%變異return;functionpopus=intercross(popus,picked)%雜交[M_p,N_p]=size(picked);[M,N]=size(popus);forcn=1:M_pp(1)=ceil(rand*N);%生成雜交位置p(2)=ceil(rand*N);p=sort(p);t=popus(picked(cn,1),p(1):p(2));popus(picked(cn,1),p(1):p(2))=popus(picked(cn,2),p(1):p(2));popus(picked(cn,2),p(1):p(2))=t;endreturn;functionpopus=aberrance(popus,picked)%變異P_a=0.05;%變異概率[M,N]=size(popus);[M_p,N_p]=size(picked);U=rand(1,2);forkp=1:M_pifU(2)>=P_a%如果大于變異概率,就不變異continue;endifU(1)>=0.5a=picked(kp,1);elsea=picked(kp,2);endp(1)=ceil(rand*N);%生成變異位置p(2)=ceil(rand*N);ifpopus(a,p(1))==1%01變換popus(a,p(1))=0;elsepopus(a,p(1))=1;endifpopus(a,p(2))==1popus(a,p(2))=0;elsepopus(a,p(2))=1;endendreturn;functionpicked=roulette(c_p)%輪盤賭[M,N]=size(c_p);M=max([MN]);U=rand;ifU<c_p(1)picked=1;return;endform=1:(M-1)ifU>c_p(m)&U<c_p(m+1)picked=m+1;break;endend全方位的兩點雜交、兩點變異的改進的加速遺傳算法(IAGA)遺傳算法優(yōu)化pid參數(shù)matlab程序chap5_4m%GA(GenericAlgorithm)programtooptimizeParametersofPIDclearall;clearall;globalrinyouttimefG=100;Size=30;CodeL=10;MinX(1)=zeros(1);MaxX(1)=20*ones(1);MinX(2)=zeros(1);MaxX(2)=1.0*ones(1);MinX(3)=zeros(1);MaxX(3)=1.0*ones(1);E=round(rand(Size,3*CodeL));%InitianCode!BsJ=0;forkg=1:1:Gtime(kg)=kg;fors=1:1:Sizem=E(s,:);y1=0;y2=0;y3=0;m1=m(1:1:CodeL);fori=1:1:CodeLy1=y1+m1(i)*2A(i-1);endKpid(s,1)=(MaxX(1)-MinX(1))*y1/1023+MinX(1);m2=m(CodeL+1:1:2*CodeL);fori=1:1:CodeLy2=y2+m2(i)*2A(i-1);endKpid(s,2)=(MaxX(2)-MinX(2))*y2/1023+MinX(2);m3=m(2*CodeL+1:1:3*CodeL);fori=1:1:CodeLy3=y3+m3(i)*2A(i-1);endKpid(s,3)=(MaxX(3)-MinX(3))*y3/1023+MinX(3);%*******Step1:EvaluateBestJ*******Kpidi=Kpid(s,:);[Kpidi,BsJ]=chap5_3f(Kpidi,BsJ);BsJi(s)=BsJ;end[OderJi,IndexJi]=sort(BsJi);BestJ(kg)=OderJi(1);BJ=BestJ(kg);Ji=BsJi+1e-10;fi=1./Ji;%Cm=max(Ji);%fi=Cm-Ji;%Avoidingdevidingzero[Oderfi,Indexfi]=sort(fi);%Arrangingfismalltobigger%Bestfi=Oderfi(Size);%LetBestfi=max(fi)%BestS=Kpid(Indexfi(Size),:);%LetBestS=E(m),mistheIndexfibelongto%max(fi)Bestfi=Oderfi(Size);%LetBestfi=max(fi)BestS=E(Indexfi(Size),:);%LetBestS=E(m),mistheIndexfibelongtomax(fi)kgBJBestS;%****Step2:SelectandReproductOperation***fi_sum=sum(fi);fi_Size=(Oderfi/fi_sum)*Size;fi_S=floor(fi_Size);%SelectingBiggerfivaluekk=1;fori=1:1:Sizeforj=1:1:fi_S(i)%SelectandReproduceTempE(kk,:)=E(Indexfi(i),:);kk=kk+1;%kkisusedtoreproduceendend%**********Step3:CrossoverOperation******pc=0.06;n=ceil(20*rand);fori=1:2:(Size-1)temp=rand;ifpc>tempforj=n:1:20TempE(i,j)=E(i+1,j);TempE(i+1,j)=E(i,j);endendendTempE(Size,:)=BestS;E=TempE;%***************Step4:MutationOperation**************%pm=0.001;pm=0.001-[1:1:Size]*(
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 《碳酸鈰-草酸鈰在復雜體系中相平衡數(shù)據(jù)的測定及模擬》
- 《無定形FeNi-MOF材料的可控合成及其析氧性能研究》
- 《經(jīng)典名方桃紅四物湯的物質(zhì)基準研究》
- 《對運動員實施功能性音樂心理訓練的方式與效果研究》
- 《SIP會議應用服務器的設計與原型系統(tǒng)的實現(xiàn)》
- 《“套路貸”的刑法規(guī)制研究》
- 《含喹啉結構的新型酪氨酸激酶抑制劑的設計、合成與生物活性研究》
- 有關新年策劃方案(例文8篇)
- 2024年房產(chǎn)市場分析報告合同
- 2024年式展覽照明工程分包合同
- 業(yè)務招待費審批單
- 建筑工程項目管理咨詢招標(范本)
- 三位數(shù)除兩位數(shù)的除法練習題
- 慢性胃炎的中醫(yī)治療培訓課件
- Python程序設計課件第7章面向?qū)ο蟪绦蛟O計
- 主題班會課防盜
- 幼兒園課件《撓撓小怪物》
- 教師教案檢查八大評分標準教案的評分標準
- 政府會計基礎知識講義
- 幼兒園整合式主題活動設計案例《溫馨家園》
- 荒漠區(qū)生態(tài)治理(麥草沙障、植物固沙)施工方案
評論
0/150
提交評論