遺傳算法的基本原理和matlab實現

         遺傳算法解決全局優化(即爲最值點如圖中C,D),而局部最優解決的是極值點問題(如圖中A,B

1.       遺傳算法流程;

 

[plain] view plain copy
  1. %遺傳算法的僞代碼描述:  
  2. %Procedure GA  
  3. %Begin  
  4. %       T=0;  
  5. %       Initialize  p(t) ; //p(t)表示 t代種羣  
  6. %       Evaluate p(t);  //評估第t代種羣  
  7. %       While not finished do  
  8. %       Begin  
  9. %            T=t+1;  
  10. %            Select p(t) from p(t-1); //從上代種羣衆選擇較優秀的個體到下代子羣  
  11. %            Reproduce pairs in p(t);  
  12. %            Evaluate p(t);  
  13. %       End  
  14. %End  

生物

算法

選擇、交叉、變異

者生存

適應度

 

故遺傳算法主要過程及流程圖如下

1)編碼(適應度函數,產生初始種羣)

2)遺傳算子(選擇、交叉、變異)

3)繁衍種羣

2.       參數初始化,編碼階段

假設目標函數爲 

a.      定義個體基因,基因是遺傳密碼,這裏自變量就是基因所攜帶的信息,即用2進制來表示自變量的可能取值。基因序列的長度由自變量取值範圍確定。

b.     定義適應度函數,目標函數是,適應度函數就定義爲。

c.      由a,b可知,我們定義好了個體(基因)與適應度函數,現初始化種羣,定義種羣大小,及繁衍代數。

1M:種羣規模

2T:遺傳運算的終止進化代數

3Pc:交叉概率

4Pm:變異概率


[plain] view plain copy
  1. %% @authors Keung Charteris & T.s.road CZQ  
  2. % @version 1.0 ($Revision$)  
  3. % @date 7/9/2016 $LastChangedDate$  
  4. % @addr. GUET, Gui Lin, 540001,  P.R.China  
  5. % @contact : [email protected]  
  6. % @date Copyright(c) 2016-2020,  All rights reserved.  
  7. % This is an open access code distributed under theCreative Commons Attribution License, which permits  
  8. % unrestricted use, distribution, and reproduction in anymedium, provided the original work is properly cited.  
  9.   
  10. functionTestGA0904()  
  11.   
  12. clc;  %清除所有  
  13. clear all;%清除變量  
  14. close all;%關閉圖片  
  15. format long  
  16.   
  17. %  step 1 編寫目標函數  
  18. FUN =@(x) (x)^2+81*sin(x);  
  19.   
  20. %  step 2 生成初始種羣,大小爲100  
  21. initPop={[0, 9],100,10};  
  22.   
  23. [BestPop,Trace]=fga(FUN,initPop{1,1}(1),initPop{1,1}(2),initPop{1,2},initPop{1,3});  

 

3.       遺傳算子

         遺傳算法使用選擇運算來實現對羣體中的個體進行優勝劣汰操作:適應度高的個體被遺傳到下一代羣體中的概率大;適應度低的個體,被遺傳到下一代羣體中的概率小。選擇操作的任務就是按某種方法從父代羣體中選取一些個體,遺傳到下一代羣體。

a.         輪盤賭選擇又稱比例選擇算子,它的基本思想是:各個個體被選中的概率與其適應度函數值大小成正比。設羣體大小爲n,個體i 的適應度爲 Fi,則個體i 被選中遺傳到下一代羣體的概率爲:

 

[plain] view plain copy
  1. %% @authorsKeung Charteris & T.s.road CZQ  
  2. % @version1.0 ($Revision$)  
  3. % @date6/9/2016 $LastChangedDate$  
  4. % @addr.GUET, Gui Lin, 540001,  P.R.China  
  5. % @contact :[email protected]  
  6. % @dateCopyright(c)  2016-2020,  All rights reserved.  
  7. % This is anopen access code distributed under the Creative Commons Attribution License,which permits  
  8. %unrestricted use, distribution, and reproduction in any medium, provided theoriginal work is properly cited.  
  9.    
  10. %選擇操作  
  11. %採用基於輪盤賭法的非線性排名選擇  
  12. %各個體成員按適應值從大到小分配選擇概率:  
  13. %P(i)=(q/1-(1-q)^n)*(1-q)^i,  其中 P(0)>P(1)>...>P(n), sum(P(i))=1  
  14.    
  15. function [selectpop]=NonlinearRankSelect(FUN,pop,bounds,bits)  
  16.   
  17. global m n  
  18. selectpop=zeros(m,n);  
  19. fit=zeros(m,1);  
  20. for i=1:m  
  21.        fit(i)=feval(FUN(1,:),(b2f(pop(i,:),bounds,bits)));%以函數值爲適應值做排名依據  
  22. end  
  23. selectprob=fit/sum(fit);%計算各個體相對適應度(0,1)  
  24. q=max(selectprob);%選擇最優的概率  
  25. x=zeros(m,2);  
  26. x(:,1)=[m:-1:1]';  
  27. [yx(:,2)]=sort(selectprob);  
  28. r=q/(1-(1-q)^m);%標準分佈基值  
  29. newfit(x(:,2))=r*(1-q).^(x(:,1)-1);%生成選擇概率  
  30. newfit=cumsum(newfit);%計算各選擇概率之和  
  31. rNums=sort(rand(m,1));  
  32. fitIn=1;newIn=1;  
  33. while newIn<=m  
  34.         ifrNums(newIn)<newfit(fitIn)  
  35.                selectpop(newIn,:)=pop(fitIn,:);  
  36.                 newIn=newIn+1;  
  37.         else  
  38.                 fitIn=fitIn+1;  
  39.         end  
  40. end  


b.        交叉運算,是指對兩個相互配對的染色體依據交叉概率 Pc 按某種方式相互交換其部分基因,從而形成兩個新的個體。交叉運算是遺傳算法區別於其他進化算法的重要特徵,它在遺傳算法中起關鍵作用,是產生新個體的主要方法。 SGA中交叉算子採用單點交叉算子。

交叉前

交叉後

00000|01110000000010000

11100|00000111111000101

00000|00000111111000101

11100|01110000000010000

 

[plain] view plain copy
  1. %% @authorsKeung Charteris & T.s.road CZQ  
  2. % @version1.0 ($Revision$)  
  3. % @date6/9/2016 $LastChangedDate$  
  4. % @addr.GUET, Gui Lin, 540001,  P.R.China  
  5. % @contact :[email protected]  
  6. % @dateCopyright(c)  2016-2020,  All rights reserved.  
  7. % This is anopen access code distributed under the Creative Commons Attribution License,which permits  
  8. %unrestricted use, distribution, and reproduction in any medium, provided theoriginal work is properly cited.  
  9.   
  10. %交叉操作  
  11.   
  12. function [NewPop]=CrossOver(OldPop,pCross,opts)  
  13. %OldPop爲父代種羣,pcross爲交叉概率  
  14.   
  15. global m NewPop  
  16. r=rand(1,m);  
  17. y1=find(r<pCross);  
  18. y2=find(r>=pCross);  
  19. len=length(y1);  
  20. if len>2&&mod(len,2)==1%如果用來進行交叉的染色體的條數爲奇數,將其調整爲偶數  
  21.     y2(length(y2)+1)=y1(len);  
  22.     y1(len)=[];  
  23. end  
  24.   
  25. if length(y1)>=2  
  26.    for i=0:2:length(y1)-2  
  27.        ifopts==0  
  28.           [NewPop(y1(i+1),:),NewPop(y1(i+2),:)]=EqualCrossOver(OldPop(y1(i+1),:),OldPop(y1(i+2),:));  
  29.        else  
  30.           [NewPop(y1(i+1),:),NewPop(y1(i+2),:)]=MultiPointCross(OldPop(y1(i+1),:),OldPop(y1(i+2),:));  
  31.        end  
  32.    end      
  33. end  
  34. NewPop(y2,:)=OldPop(y2,:);  

 

c.         所謂變異運算,是指依據變異概率 Pm 將個體編碼串中的某些基因值用其它基因值來替換,從而形成一個新的個體。遺傳算法中的變異運算是產生新個體的輔助方法,它決定了遺傳算法的局部搜索能力,同時保持種羣的多樣性。交叉運算和變異運算的相互配合,共同完成對搜索空間的全局搜索和局部搜索。 SGA中變異算子採用基本位變異算子。

變異前

變異後

000001110000000010000

000001110001000010000

 

[plain] view plain copy
  1. %% @authorsKeung Charteris & T.s.road CZQ  
  2. % @version1.0 ($Revision$)  
  3. % @date6/9/2016 $LastChangedDate$  
  4. % @addr.GUET, Gui Lin, 540001,  P.R.China  
  5. % @contact :[email protected]  
  6. % @dateCopyright(c)  2016-2020,  All rights reserved.  
  7. % This is anopen access code distributed under the Creative Commons Attribution License,which permits  
  8. %unrestricted use, distribution, and reproduction in any medium, provided theoriginal work is properly cited.  
  9.   
  10. %變異操作  
  11. function [NewPop]=Mutation(OldPop,pMutation,VarNum)  
  12.   
  13. global m n NewPop  
  14. r=rand(1,m);  
  15. position=find(r<=pMutation);  
  16. len=length(position);  
  17. if len>=1  
  18.         fori=1:len  
  19.                 k=unidrnd(n,1,VarNum); %設置變異點數,一般設置1點  
  20.                 forj=1:length(k)  
  21.                         ifOldPop(position(i),k(j))==1  
  22.                                OldPop(position(i),k(j))=0;  
  23.                         else  
  24.                                OldPop(position(i),k(j))=1;  
  25.                         end  
  26.                 end  
  27.         end  
  28. end  
  29. NewPop=OldPop;  


結果如下:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章