差分进化算法(Differential Evolution)由 Storn和 Price 于1997年提出,算法包含三个参数:种群大小NP,变异放缩因子F,交叉概率CR。用向量xi=[xi1,xi2,…,xi,dim]x_i = \left[x_{i1},x_{i2},\dots,x_{i,dim}\right]xi=[xi1,xi2,,xi,dim]表示目标函数的一组候选解,通过不断的变异、交叉和选择在有限的迭代次数内得到满意解。算法流程图如下:

image-20210505205927666

1. 初始化

随机生成初始种群
xij=xj,min+rand∗(xj,max−xj,min) x_{ij}=x_{j,min}+rand*(x_{j,max}-x_{j,min}) xij=xj,min+rand(xj,maxxj,min)
其中xj,maxx_{j,max}xj,maxxj,minx_{j,min}xj,min分别表示在第jjj维的上下限。

2. 变异操作

  1. DE/rand/1策略
    vi,G=xr1,G+F⋅(xr2,G−xr3,G) v_{i,G} = x_{r1,G} + F \cdot (x_{r_2,G} - x_{r_3,G}) vi,G=xr1,G+F(xr2,Gxr3,G)

  2. DE/rand/2策略
    vi,G=xr1,G+F⋅(xr2,G−xr3,G)+F⋅(xr4,G−xr5,G) v_{i,G} = x_{r1,G} + F \cdot (x_{r_2,G} - x_{r_3,G}) + F \cdot (x_{r_4,G} - x_{r_5,G}) vi,G=xr1,G+F(xr2,Gxr3,G)+F(xr4,Gxr5,G)

  3. DE/current to rand/1策略
    vi,G=xi,G+F⋅(xr2,G−xi,G)+F⋅(xr2,G−xr3,G) v_{i,G} = x_{i,G} + F \cdot (x_{r_2,G} - x_{i,G}) + F \cdot (x_{r_2,G} - x_{r_3,G}) vi,G=xi,G+F(xr2,Gxi,G)+F(xr2,Gxr3,G)

  4. DE/best/1策略
    vi,G=xbest,G+F⋅(xr1,G−xr2,G) v_{i,G} = x_{best,G} + F \cdot (x_{r_1,G} - x_{r_2,G}) vi,G=xbest,G+F(xr1,Gxr2,G)

  5. DE/best/2策略
    vi,G=xbest,G+F⋅(xr1,G−xr2,G)+F⋅(xr3,G−xr4,G) v_{i,G} = x_{best,G} + F \cdot (x_{r_1,G} - x_{r_2,G}) + F \cdot (x_{r_3,G} - x_{r_4,G}) vi,G=xbest,G+F(xr1,Gxr2,G)+F(xr3,Gxr4,G)

  6. DE/rand to best/1策略
    vi,G=xr1,G+F⋅(xbest,G−xr1,G)+F⋅(xr2,G−xr3,G) v_{i,G} = x_{r_1,G} + F \cdot (x_{best,G} - x_{r_1,G}) + F \cdot (x_{r_2,G} - x_{r_3,G}) vi,G=xr1,G+F(xbest,Gxr1,G)+F(xr2,Gxr3,G)

  7. DE/current to best/1策略
    vi,G=xi,G+F⋅(xbest,G−xi,G)+F⋅(xr1,G−xr2,G) v_{i,G} = x_{i,G} + F \cdot (x_{best,G} - x_{i,G}) + F \cdot (x_{r_1,G} - x_{r_2,G}) vi,G=xi,G+F(xbest,Gxi,G)+F(xr1,Gxr2,G)

其中{i,r1,r2,r3,r4,r5}⊂{1,2,...,NP}\{i,r_1,r_2,r_3,r_4,r_5\} \subset \{1,2,...,NP\}{i,r1,r2,r3,r4,r5}{1,2,...,NP}xbest,Gx_{best,G}xbest,G表示在第G代种群中的最优个体,xi,Gx_{i,G}xi,G表示在第G代种群中的第iii

个体,F∈(0,2)F\in \left(0,2\right)F(0,2)表示放缩因子。

3. 交叉操作

uij={vijif(rand<CR)orj=rnbr(i)xijotherwise u_{ij} = \left\{ \begin{aligned} v_{ij} \quad & if(rand<CR)\quad or \quad j = rnbr(i)\\ x_{ij} \quad & otherwise \end{aligned} \right. uij={vijxijif(rand<CR)orj=rnbr(i)otherwise

其中CR∈[0,1]CR\in [0,1]CR[0,1]表示变异概率,rnbr(i)rnbr(i)rnbr(i) 表示从维度集合中随机选取的常数,以确保uiju_{ij}uij中至少有一个元素来源于vijv_{ij}vij

4. 选择操作

xi,G+1={ui,Gif(f(ui,G)≤f(xi,G))xi,Gotherwise x_{i,G+1} = \left\{ \begin{aligned} u_{i,G} \quad & if(f(u_{i,G}) \le f(x_{i,G}))\\ x_{i,G} \quad & otherwise \end{aligned} \right. xi,G+1={ui,Gxi,Gif(f(ui,G)f(xi,G))otherwise

5. 测试函数

f(x)=∑i=1dimi⋅xi2 f(x) = \sum_{i=1}^{dim}i \cdot x_{i}^{2} f(x)=i=1dimixi2

6. 代码实现

function [fitbest,xbest,fit_curve] = DE(NP,F,CR,Gm)
%input
%NP: 种群规模,F:放缩因子,CR:交叉概率,Gm:最大迭代数
%output
%bestfit: 寻优最佳适应度,fit_curve:收敛曲线
LB = -100;
UB = 100;
dim = 30;  
X = zeros(NP,dim);
U = zeros(NP,dim);
V = zeros(NP,dim);
fitX = zeros(NP,1);
fitV = zeros(NP,1);
fit_curve = zeros(1,Gm);
%% 种群初始化
for m = 1 : NP
	X(m,:) = LB + rand(1,dim) * (UB - LB);
	fitX(m) = test(X(m,:));
end
%% 主循环
G = 1;
while G <= Gm
	%% 变异
	for i = 1 : NP
		r = randperm(NP,3);
		while ismember(i,r)
			r(r == i) = randi(NP);
		end
		U(i,:) = X(r(1),:) + F * ( X(r(2),:) - X(r(3),:) );
	end
	U(U < LB) = LB;%边界吸收处理
	U(U > UB) = UB;
	%% 交叉
	for j = 1 : NP
		randIndex = randi(dim);
		for k = 1 : dim
			if rand <= CR || randIndex == k
				V(j,k) = X(j,k);
			else
				V(j,k) = U(j,k);
			end
		end
		fitV(j) = test(V(j,:));
	end
	%% 选择
	betterIndex = fitV < fitX;
	X(betterIndex,:) = V(betterIndex,:);
	fitX(betterIndex) = fitV(betterIndex);
	%% 记录每一代的最优解
	[fitbest,Index] = min(fitX);
	xbest = X(Index,:);
	fit_curve(G) = fitbest;
	G = G + 1;
end
%% 绘图
figure;
plot(fit_curve);
title('适应度收敛曲线');
xlabel('迭代次数');
ylabel('适应度');
%% 测试函数
function fvalue = test(x)
y = 1:length(x);
fvalue=sum(y.*(x.^2)) ;
end
end
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐