差分進化算法DE與遺傳算法GA非常類似,下面是差分進化算法的步驟。
算法步驟如下:
- 初始化
- 變異
- 交叉
d.選擇
測試函數:
Rastrigr函數
全局最優點:
,
matlab代碼如下:
[plain] view plain copy
- function DE(Gm,F0)
- t0 = cputime;
- %差分進化算法程式
- %F0是變異率 %Gm 最大疊代次數
- Gm = 10000;
- F0 = 0.5;
- Np = 100;
- CR = 0.9; %交叉機率
- G= 1; %初始化代數
- D = 10; %所求問題的維數
- Gmin = zeros(1,Gm); %各代的最優值
- best_x = zeros(Gm,D); %各代的最優解
- value = zeros(1,Np);
- %産生初始種群
- %xmin = -10; xmax = 100;%帶負數的下界
- xmin = -5.12;
- xmax = 5.12;
- function y = f(v)
- %Rastrigr 函數
- y = sum(v.^2 - 10.*cos(2.*pi.*v) + 10);
- end
- X0 = (xmax-xmin)*rand(Np,D) + xmin; %産生Np個D維向量
- XG = X0;
- %%%%%%%%%%%%%----這裡未做評價,不判斷終止條件----%%%%%%%%%%%%%%%%%%%%%%%%
- XG_next_1= zeros(Np,D); %初始化
- XG_next_2 = zeros(Np,D);
- XG_next = zeros(Np,D);
- while G <= Gm
- G
- %%%%%%%%%%%%%%%%%%%%%%%%----變異操作----%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- for i = 1:Np
- %産生j,k,p三個不同的數
- a = 1;
- b = Np;
- dx = randperm(b-a+1) + a- 1;
- j = dx(1);
- k = dx(2);
- p = dx(3);
- %要保證與i不同
- if j == i
- j = dx(4);
- else if k == i
- k = dx(4);
- else if p == i
- p = dx(4);
- end
- end
- end
- %變異算子
- suanzi = exp(1-Gm/(Gm + 1-G));
- F = F0*2.^suanzi;
- %變異的個體來自三個随機父代
- son = XG(p,:) + F*(XG(j,:) - XG(k,:));
- for j = 1: D
- if son(1,j) >xmin & son(1,j) < xmax %防止變異超出邊界
- XG_next_1(i,j) = son(1,j);
- else
- XG_next_1(i,j) = (xmax - xmin)*rand(1) + xmin;
- end
- end
- end
- %%%%%%%%%%%%%%%%%%%%%%%---交叉操作----%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- for i = 1: Np
- randx = randperm(D);% [1,2,3,...D]的随機序列
- for j = 1: D
- if rand > CR & randx(1) ~= j % CR = 0.9
- XG_next_2(i,j) = XG(i,j);
- else
- XG_next_2(i,j) = XG_next_1(i,j);
- end
- end
- end
- %%%%%%%%%%%%%%%%%%----選擇操作---%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- for i = 1:Np
- if f(XG_next_2(i,:)) < f(XG(i,:))
- XG_next(i,:) = XG_next_2(i,:);
- else
- XG_next(i,:) = XG(i,:);
- end
- end
- %找出最小值
- for i = 1:Np
- value(i) = f(XG_next(i,:));
- end
- [value_min,pos_min] = min(value);
- %第G代中的目标函數的最小值
- Gmin(G) = value_min;
- %儲存最優的個體
- best_x(G,:) = XG_next(pos_min,:);
- XG = XG_next;
- trace(G,1) = G;
- trace(G,2) = value_min;
- G = G + 1;
- end
- [value_min,pos_min] = min(Gmin);
- best_value = value_min
- best_vector = best_x(pos_min,:)
- fprintf('DE所耗的時間為:%f \n',cputime - t0);
- %畫出代數跟最優函數值之間的關系圖
- plot(trace(:,1),trace(:,2));
- end
結果:
以上轉載自:http://blog.csdn.net/hehainan_86/article/details/38685231
DE算法的求解步驟:
(1)基本參數的設定,包括NP, F, CR
(2)初始化種群
(3)計算種群适應度值
(4)終止條件不滿足時,進行循環,依次執行變異、交叉、選擇運算,直到終止運算。
DE算法的流程圖:
以上轉載自:http://blog.csdn.net/misayaaaaa/article/details/54407548