遺傳算法解決全局優化(即爲最值點如圖中C,D),而局部最優解決的是極值點問題(如圖中A,B)
1. 遺傳算法流程;
- %遺傳算法的僞代碼描述:
- %Procedure GA
- %Begin
- % T=0;
- % Initialize p(t) ; //p(t)表示 t代種羣
- % Evaluate p(t); //評估第t代種羣
- % While not finished do
- % Begin
- % T=t+1;
- % Select p(t) from p(t-1); //從上代種羣衆選擇較優秀的個體到下代子羣
- % Reproduce pairs in p(t);
- % Evaluate p(t);
- % End
- %End
生物 |
算法 |
物競天擇 |
選擇、交叉、變異 |
適者生存 |
適應度 |
故遺傳算法主要過程及流程圖如下
1)編碼(適應度函數,產生初始種羣)
2)遺傳算子(選擇、交叉、變異)
3)繁衍種羣
2. 參數初始化,編碼階段
假設目標函數爲
a. 定義個體基因,基因是遺傳密碼,這裏自變量就是基因所攜帶的信息,即用2進制來表示自變量的可能取值。基因序列的長度由自變量取值範圍確定。
b. 定義適應度函數,目標函數是,適應度函數就定義爲。
c. 由a,b可知,我們定義好了個體(基因)與適應度函數,現初始化種羣,定義種羣大小,及繁衍代數。
(1)M:種羣規模
(2)T:遺傳運算的終止進化代數
(3)Pc:交叉概率
(4)Pm:變異概率
- %% @authors Keung Charteris & T.s.road CZQ
- % @version 1.0 ($Revision$)
- % @date 7/9/2016 $LastChangedDate$
- % @addr. GUET, Gui Lin, 540001, P.R.China
- % @contact : [email protected]
- % @date Copyright(c) 2016-2020, All rights reserved.
- % This is an open access code distributed under theCreative Commons Attribution License, which permits
- % unrestricted use, distribution, and reproduction in anymedium, provided the original work is properly cited.
- functionTestGA0904()
- clc; %清除所有
- clear all;%清除變量
- close all;%關閉圖片
- format long
- % step 1 編寫目標函數
- FUN =@(x) (x)^2+81*sin(x);
- % step 2 生成初始種羣,大小爲100
- initPop={[0, 9],100,10};
- [BestPop,Trace]=fga(FUN,initPop{1,1}(1),initPop{1,1}(2),initPop{1,2},initPop{1,3});
3. 遺傳算子
遺傳算法使用選擇運算來實現對羣體中的個體進行優勝劣汰操作:適應度高的個體被遺傳到下一代羣體中的概率大;適應度低的個體,被遺傳到下一代羣體中的概率小。選擇操作的任務就是按某種方法從父代羣體中選取一些個體,遺傳到下一代羣體。
a. 輪盤賭選擇又稱比例選擇算子,它的基本思想是:各個個體被選中的概率與其適應度函數值大小成正比。設羣體大小爲n,個體i 的適應度爲 Fi,則個體i 被選中遺傳到下一代羣體的概率爲:
- %% @authorsKeung Charteris & T.s.road CZQ
- % @version1.0 ($Revision$)
- % @date6/9/2016 $LastChangedDate$
- % @addr.GUET, Gui Lin, 540001, P.R.China
- % @contact :[email protected]
- % @dateCopyright(c) 2016-2020, All rights reserved.
- % This is anopen access code distributed under the Creative Commons Attribution License,which permits
- %unrestricted use, distribution, and reproduction in any medium, provided theoriginal work is properly cited.
- %選擇操作
- %採用基於輪盤賭法的非線性排名選擇
- %各個體成員按適應值從大到小分配選擇概率:
- %P(i)=(q/1-(1-q)^n)*(1-q)^i, 其中 P(0)>P(1)>...>P(n), sum(P(i))=1
- function [selectpop]=NonlinearRankSelect(FUN,pop,bounds,bits)
- global m n
- selectpop=zeros(m,n);
- fit=zeros(m,1);
- for i=1:m
- fit(i)=feval(FUN(1,:),(b2f(pop(i,:),bounds,bits)));%以函數值爲適應值做排名依據
- end
- selectprob=fit/sum(fit);%計算各個體相對適應度(0,1)
- q=max(selectprob);%選擇最優的概率
- x=zeros(m,2);
- x(:,1)=[m:-1:1]';
- [yx(:,2)]=sort(selectprob);
- r=q/(1-(1-q)^m);%標準分佈基值
- newfit(x(:,2))=r*(1-q).^(x(:,1)-1);%生成選擇概率
- newfit=cumsum(newfit);%計算各選擇概率之和
- rNums=sort(rand(m,1));
- fitIn=1;newIn=1;
- while newIn<=m
- ifrNums(newIn)<newfit(fitIn)
- selectpop(newIn,:)=pop(fitIn,:);
- newIn=newIn+1;
- else
- fitIn=fitIn+1;
- end
- end
b. 交叉運算,是指對兩個相互配對的染色體依據交叉概率 Pc 按某種方式相互交換其部分基因,從而形成兩個新的個體。交叉運算是遺傳算法區別於其他進化算法的重要特徵,它在遺傳算法中起關鍵作用,是產生新個體的主要方法。 SGA中交叉算子採用單點交叉算子。
交叉前 |
交叉後 |
00000|01110000000010000 11100|00000111111000101 |
00000|00000111111000101 11100|01110000000010000 |
- %% @authorsKeung Charteris & T.s.road CZQ
- % @version1.0 ($Revision$)
- % @date6/9/2016 $LastChangedDate$
- % @addr.GUET, Gui Lin, 540001, P.R.China
- % @contact :[email protected]
- % @dateCopyright(c) 2016-2020, All rights reserved.
- % This is anopen access code distributed under the Creative Commons Attribution License,which permits
- %unrestricted use, distribution, and reproduction in any medium, provided theoriginal work is properly cited.
- %交叉操作
- function [NewPop]=CrossOver(OldPop,pCross,opts)
- %OldPop爲父代種羣,pcross爲交叉概率
- global m NewPop
- r=rand(1,m);
- y1=find(r<pCross);
- y2=find(r>=pCross);
- len=length(y1);
- if len>2&&mod(len,2)==1%如果用來進行交叉的染色體的條數爲奇數,將其調整爲偶數
- y2(length(y2)+1)=y1(len);
- y1(len)=[];
- end
- if length(y1)>=2
- for i=0:2:length(y1)-2
- ifopts==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),:));
- end
- end
- end
- NewPop(y2,:)=OldPop(y2,:);
c. 所謂變異運算,是指依據變異概率 Pm 將個體編碼串中的某些基因值用其它基因值來替換,從而形成一個新的個體。遺傳算法中的變異運算是產生新個體的輔助方法,它決定了遺傳算法的局部搜索能力,同時保持種羣的多樣性。交叉運算和變異運算的相互配合,共同完成對搜索空間的全局搜索和局部搜索。 SGA中變異算子採用基本位變異算子。
變異前 |
變異後 |
000001110000000010000 |
000001110001000010000 |
- %% @authorsKeung Charteris & T.s.road CZQ
- % @version1.0 ($Revision$)
- % @date6/9/2016 $LastChangedDate$
- % @addr.GUET, Gui Lin, 540001, P.R.China
- % @contact :[email protected]
- % @dateCopyright(c) 2016-2020, All rights reserved.
- % This is anopen access code distributed under the Creative Commons Attribution License,which permits
- %unrestricted use, distribution, and reproduction in any medium, provided theoriginal work is properly cited.
- %變異操作
- function [NewPop]=Mutation(OldPop,pMutation,VarNum)
- global m n NewPop
- r=rand(1,m);
- position=find(r<=pMutation);
- len=length(position);
- if len>=1
- fori=1:len
- k=unidrnd(n,1,VarNum); %設置變異點數,一般設置1點
- forj=1:length(k)
- ifOldPop(position(i),k(j))==1
- OldPop(position(i),k(j))=0;
- else
- OldPop(position(i),k(j))=1;
- end
- end
- end
- end
- NewPop=OldPop;
結果如下: