# 差异进化算法

1997年，Storn與Price在全域最佳化國際學術期刊（Journal of global optimization） [4]發表了差分进化算法。

## 实现代码（MATLAB）

```tic
F = 0.9;
CR = .1;
n = 2; %问题维数，以简单的球函数为目标函数
NP = 30;
lu = [-10,-10 ;10 ,10]; %求解空间的上下界
LB = repmat(lu(1,:),NP,1);
UB = repmat(lu(2,:),NP,1);
%用于生成随机选择个体的表
tab = 1:NP; tab = tab(ones(1,NP),:)';
dig = 1:NP; D =(dig-1)*NP +(1:NP);
tab (D) = [];
tab = reshape(tab,NP-1,[])';
TAB = tab;
%测试次数
TIMES = 10;
Solve = zeros(1,TIMES);
numOfevol = zeros(1,TIMES);
for time = 1:TIMES
%
Result = []; %记录结果
rand('seed',sum(100*clock));
%
X = LB+rand(NP,n).*(UB-LB);
U = X;
%%
fit = fitness (X); %首次评价
FES = NP;
while FES<n*10000
%产生随机个体参与变异
tab = TAB;
rand1 = floor(rand(NP,1)*(NP-1))+1;
rand2 = floor(rand(NP,1)*(NP-2))+2;
rand3 = floor(rand(NP,1)*(NP-3))+3;
RND1 =(rand1-1)*NP+(1:NP)';
RND2 =(rand2-1)*NP+(1:NP)';
RND3 =(rand3-1)*NP+(1:NP)';
r1 = tab (RND1); tab (RND1)=tab(:,1);
r2 = tab (RND2); tab (RND2)=tab(:,2);
r3 = tab (RND3);
% rand/one/变异模式
V = X(r1,:) + F.*(X(r2,:)-X(r3,:));
%越界检验
BL = V<LB ; V(BL) = 2*LB(BL) - V(BL);
BLU = V(BL)>UB(BL); BL (BL) = BLU ; V(BL) = UB (BL);
BU = V>UB;  V (BU) = 2*UB(BU) - V(BU);
BUL = V(BU)<LB(BU); BU (BU) = BUL ; V(BU) = LB (BU);
%交叉操作
J_= mod(floor(rand(NP,1)*n),n)+1;
J =(J_-1)*NP+(1:NP)';
C = rand(NP,n)<CR;
U (J) = V(J);
U (C) = V(C);
%评价子代
fit_ = fitness (U);
%比较并竞争
S = fit_<fit;
X(S,:) = U(S,:);
fit (S) = fit (S)|;
%记录函数评价次数
FES = FES + NP;
%记录结果（用于绘图，并不是算法必要环节）
Result = [Result ,min (fit)];
end
Solve (time) = min (fit);
%试验次数
plot(log10(Result),'b');hold on;
end
disp(['求解结果:',num2str(Solve)]);
toc
%附上球函数代码（新建一个M文件即可）
function Y = fitness (X)
Y = sum(X.^2 ,2);
```

## 参考文献

1. ^ 刘波，王凌，金以慧差分进化算法研究进展，控制与决策，第22卷第7期,721-729
2. Das, S.; Suganthan, P. N. Differential evolution: a survey of the state-of-the-art. IEEE Transactions on Evolutionary Computation. 2011, 15: 4–31.
3. ^ Storn, R.; Price, K. V. Minimizing the real functions of the ICEC 1996 contest by differential evolution. International Conference on Evolutionary Computation. 1996.
4. ^ Storn, R.; Price, K. Differential evolution - a simple and efficient heuristic for global optimization over continuous spaces. Journal of Global Optimization. 1997, 11: 341–359.
5. ^ 代码编写及提供者：rongekuta@gmail.com