赞
踩
差分进化算法(differential evolution, DE)是一类基于群体的自适应全局优化算法,有较强的鲁棒性和全局寻优能力,主要用于求解实数优化问题。它从数学角度看是一种随机搜索算法,从工程角度看是一种自适应的迭代寻优过程。差分进化算法凭借其独特的优势在数据挖掘、模式识别、电磁学等领域被广泛应用。
差分进化算法采用实数编码,基于差分的简单变异操作和“一对一”的竞争生存策略,具体步骤如下
初始化N个个体
初始化参数D、N_iter
i=1
while(t<=N_iter)
for i=1:N
for j=1:D
进行变异操作和交叉操作,得到临时种群
end for j
计算临时种群中每个个体的适应度值
对临时种群进行选择操作,得到新种群
end for i
t = t+1
end while
输出结果
1.计算函数 f ( x ) = ∑ i = 1 n x i 2 ( − 20 ≤ x i ≤ 20 ) f(x)= \sum_{i=1}^n x_i^2(-20\leq x_i\leq20) f(x)=∑i=1nxi2(−20≤xi≤20)的最小值,其中个体 x x x的维数 n = 10 n=10 n=10。这是一个简单的平方和函数,只有一个极小点 x = ( 0 , 0 , ⋯ , 0 ) x=(0,0,\cdots,0) x=(0,0,⋯,0),理论最小值 f ( 0 , 0 , ⋯ , 0 ) = 0 f(0,0,\cdots,0)=0 f(0,0,⋯,0)=0
仿真过程如下:
(1)初始化个体数目为 N P = 50 NP=50 NP=50,变量维数为 D = 10 D=10 D=10,最大进化代数为 G = 200 G=200 G=200,初始变异算子 F 0 = 0.4 F_0=0.4 F0=0.4,交叉算子 C R = 0.1 CR=0.1 CR=0.1,阈值 y z = 1 0 − 6 yz=10^{-6} yz=10−6。
(2)产生初始种群,计算个体目标函数;进行变异操作、交叉操作、边界条件处理,产生临时种群,其中变异操作采用自适应变异算子,边界条件处理采用在可行域中随机产生参数向量的方式。
(3)计算临时种群个体目标函数,与原种群对应个体进行“一对一”选择操作,产生新种群。
(4)判断是否满足终止条件:若满足,则结束搜索过程,输出优化值;若不满足,则继续进行迭代优化。
优化结束后,DE目标函数曲线如下图所示
%% 差分进化算法求极值 clc; clear; close all; NP = 50; % 种群数量 D = 10; % 变量的维数 G = 200; % 最大的进化代数 F0 = 0.4; % 初始变异算子 CR = 0.1; % 交叉算子 Xs = 20; % 上限 Xx = -20; % 下限 yz = 10^-6; % 阈值 % 赋初值 x = zeros(D, NP); % 初始种群 v = zeros(D, NP); % 变异种群 u = zeros(D, NP); % 选择种群 x = rand(D, NP) * (Xs - Xx) + Xx; % 赋初值 %%%%%%%%%%%%%%计算目标函数%%%%%%%%%%%%%%%% for m=1:NP Ob(m) = func1(x(:, m)); end trace(1) = min(Ob); %%%%%%%%%%%%%%差分进化循环%%%%%%%%%%%%%%%% for gen = 1:G %%%%%%%%%%%%%%%%%%%%%%%%% 变异操作%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%% 自适应变异算子%%%%%%%%%%%%%%% lambda = exp(1-G/(G+1-gen)); F = F0*2^(1-lambda); %%%%%%%%%%%%%%%% r1, r2, r3和m互不相同%%%%%%%%%%%%%%%% for m = 1:NP r1 = randi([1, NP], 1, 1); while(r1==m) r1 = randi([1, NP], 1, 1); end r2 = randi([1, NP], 1, 1); while(r2==m) || (r2==r1) r2 = randi([1, NP], 1, 1); end r3 = randi([1, NP], 1, 1); while (r3==m) || (r3==r1) || (r3==r2) r3 = randi([1, NP], 1, 1); end v(:,m) = x(:,r1) + F*(x(:,r2)-x(:,r3)); end %%%%%%%%%%%%%%%%%%%%%%%交叉操作%%%%%%%%%%%%%%%%%%%%%%%%%%% r = randi([1, D], 1, 1); for n = 1:D cr = rand(1); if(cr<=CR) || (n==r) u(n, :) = v(n, :); else u(n, :) = x(n, :); end end %%%%%%%%%%%%%%%%%边界条件的处理%%%%%%%%%%%%%%%%%%%%%%% for n = 1:D for m = 1:NP if(u(n,m)<Xx || u(n,m)>Xs) u(n,m) = rand*(Xs-Xx) + Xx; end end end %%%%%%%%%%%%%%%%%%%%%选择操作%%%%%%%%%%%%%%%%%%%%%%%%%% for m = 1:NP Ob1(m) = func1(u(:, m)); end for m = 1:NP if Ob1(m) < Ob(m) x(:, m) = u(:, m); end end for m = 1:NP Ob(m) = func1(x(:, m)); end trace(gen+1) = min(Ob); if min(Ob(m)) <yz break end end [SortOb, Index] = sort(Ob); x = x(:, Index); X = x(:, 1); % 最优变量 Y = min(Ob); % 最优值 %%%%%%%%%%%%%%%%%%画图%%%%%%%%%%%%%%%%%% figure plot(trace); xlabel('迭代次数'); ylabel('目标函数值'); title('DE目标函数曲线') %%%%%%%%%%%%适应度函数%%%%%%%%%%%%%%%%%%% function result = func1(x) summ = sum(x.^2); result = summ; end
clc; clear; close all;
%%%%%%%%%%%%%%%f(x,y) =3cos(xy)+x+y%%%%%%%%%%%%%%%%
x = -4:0.02:4;
y = -4:0.02:4;
N = size(x, 2);
for i = 1:N
for j = 1:N
z(i,j) = 3*cos(x(i)*y(j))+x(i)+y(j);
end
end
mesh(x, y, z);
xlabel('x');
ylabel('y');
仿真过程如下:
(1)初始化个体数目为NP=20,变量维数为D=2,最大进化代数为G=100,变异算子F=0.5,交叉算子CR=0.1;
(2)产生初始种群,计算个体目标函数;进行变异操作、交叉操作、边界条件处理,产生临时种群,其中边界条件处理采用边界吸收方式;
(3)计算临时种群个体目标函数,与原种群对应个体进行“一对一”选择操作,产生新种群;
(4)判断是否满足终止条件:若满足,则结束搜索过程,输出优化值;若不满足,则继续进行迭代优化。、
优化结束后,DE目标函数曲线如下图所示
clc; clear; close all; %%%%%%%%%%%%%%%f(x,y) =3cos(xy)+x+y%%%%%%%%%%%%%%%% x = -4:0.02:4; y = -4:0.02:4; N = size(x, 2); for i = 1:N for j = 1:N z(i,j) = 3*cos(x(i)*y(j))+x(i)+y(j); end end mesh(x, y, z); xlabel('x'); ylabel('y'); %% 差分进化算法求极值 NP = 20; % 种群数量 D = 2; % 变量的维数 G = 100; % 最大的进化代数 F = 0.5; % 初始变异算子 CR = 0.1; % 交叉算子 Xs = 4; % 上限 Xx = -4; % 下限 yz = 10^-6; % 阈值 % 赋初值 x = zeros(D, NP); % 初始种群 v = zeros(D, NP); % 变异种群 u = zeros(D, NP); % 选择种群 x = rand(D, NP) * (Xs - Xx) + Xx; % 赋初值 %%%%%%%%%%%%%%计算目标函数%%%%%%%%%%%%%%%% for m=1:NP Ob(m) = func2(x(:, m)); end trace(1) = min(Ob); %%%%%%%%%%%%%%差分进化循环%%%%%%%%%%%%%%%% for gen = 1:G %%%%%%%%%%%%%%%%%%%%%%%%% 变异操作%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%% r1, r2, r3和m互不相同%%%%%%%%%%%%%%%% for m = 1:NP r1 = randi([1, NP], 1, 1); while(r1==m) r1 = randi([1, NP], 1, 1); end r2 = randi([1, NP], 1, 1); while(r2==m) || (r2==r1) r2 = randi([1, NP], 1, 1); end r3 = randi([1, NP], 1, 1); while (r3==m) || (r3==r1) || (r3==r2) r3 = randi([1, NP], 1, 1); end v(:,m) = x(:,r1) + F*(x(:,r2)-x(:,r3)); end %%%%%%%%%%%%%%%%%%%%%%%交叉操作%%%%%%%%%%%%%%%%%%%%%%%%%%% r = randi([1, D], 1, 1); for n = 1:D cr = rand(1); if(cr<=CR) || (n==r) u(n, :) = v(n, :); else u(n, :) = x(n, :); end end %%%%%%%%%%%%%%%%%边界条件的处理%%%%%%%%%%%%%%%%%%%%%%% for n = 1:D for m = 1:NP if(u(n,m)<Xx) u(n,m) = Xx; end if(u(n,m)>Xs) u(n,m) = Xs; end end end %%%%%%%%%%%%%%%%%%%%%选择操作%%%%%%%%%%%%%%%%%%%%%%%%%% for m = 1:NP Ob1(m) = func2(u(:, m)); end for m = 1:NP if Ob1(m) < Ob(m) x(:, m) = u(:, m); end end for m = 1:NP Ob(m) = func2(x(:, m)); end trace(gen+1) = min(Ob); % if min(Ob(m)) <yz % break % end end [SortOb, Index] = sort(Ob); x = x(:, Index); X = x(:, 1); % 最优变量 Y = min(Ob); % 最优值 %%%%%%%%%%%%%%%%%%画图%%%%%%%%%%%%%%%%%% figure plot(trace); xlabel('迭代次数'); ylabel('目标函数值'); title('DE目标函数曲线') %%%%%%%%%%%%适应度函数%%%%%%%%%%%%%%%%%%% function result = func2(x) result = 3*cos(x(1)*x(2))+x(1)+x(2); end
%%%%F(x,y)=-((x~2+y-1).^2+(x+y~2-7)^2)/200+10%%%%
clc; clear; close all;
x=-100:1:100;
y=-100:1:100;
N=size(x,2);
for i=1:N
for j=1:N
z(i,j)=-((x(i)^2+y(j)-1).^2+(x(i) +y(j)^2-7)^2)/200+10;
end
end
mesh(x,y,z);
xlabel('x');
ylabel('y');
仿真过程如下:
(1)初始化个体数目为NP=20,变量维数为D=2,最大进化代数为G=100,变异算子F=0.5,交叉算子CR=0.1
(2)产生数值为[-100,100]内整数的初始种群,计算个体目标函数;进行变异操作,对变异后的种群内数值进行取整操作,然后进行交叉操作、边界条件处理操作,产生临时种群,其中边界条件处理采用边界吸收方式
(3)计算临时种群个体目标函数,与原种群对应个体进行“一对一”选择操作,产生新种群
(4)判断是否满足终止条件:若满足,则结束搜索过程,输出优化值;若不满足,则继续进行迭代优化
优化结束后,DE目标函数曲线如下图所示
%%%%F(x,y)=-((x~2+y-1).^2+(x+y~2-7)^2)/200+10%%%% clc; clear; close all; x=-100:1:100; y=-100:1:100; N=size(x,2); for i=1:N for j=1:N z(i,j)=-((x(i)^2+y(j)-1).^2+(x(i) +y(j)^2-7)^2)/200+10; end end mesh(x,y,z); xlabel('x'); ylabel('y'); %% 差分进化算法求极值 NP = 20; % 种群数量 D = 2; % 变量的维数 G = 100; % 最大的进化代数 F = 0.5; % 初始变异算子 CR = 0.1; % 交叉算子 Xs = 100; % 上限 Xx = -100; % 下限 yz = 10^-6; % 阈值 % 赋初值 x = zeros(D, NP); % 初始种群 v = zeros(D, NP); % 变异种群 u = zeros(D, NP); % 选择种群 x = randi([Xx, Xs], D, NP); % 赋初值 %%%%%%%%%%%%%%计算目标函数%%%%%%%%%%%%%%%% for m=1:NP Ob(m) = func3(x(:, m)); end trace(1) = max(Ob); %%%%%%%%%%%%%%差分进化循环%%%%%%%%%%%%%%%% for gen = 1:G %%%%%%%%%%%%%%%%%%%%%%%%% 变异操作%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%% r1, r2, r3和m互不相同%%%%%%%%%%%%%%%% for m = 1:NP r1 = randi([1, NP], 1, 1); while(r1==m) r1 = randi([1, NP], 1, 1); end r2 = randi([1, NP], 1, 1); while(r2==m) || (r2==r1) r2 = randi([1, NP], 1, 1); end r3 = randi([1, NP], 1, 1); while (r3==m) || (r3==r1) || (r3==r2) r3 = randi([1, NP], 1, 1); end v(:,m) = floor(x(:,r1) + F*(x(:,r2)-x(:,r3))); end %%%%%%%%%%%%%%%%%%%%%%%交叉操作%%%%%%%%%%%%%%%%%%%%%%%%%%% r = randi([1, D], 1, 1); for n = 1:D cr = rand(1); if(cr<=CR) || (n==r) u(n, :) = v(n, :); else u(n, :) = x(n, :); end end %%%%%%%%%%%%%%%%%边界条件的处理%%%%%%%%%%%%%%%%%%%%%%% for n = 1:D for m = 1:NP if(u(n,m)<Xx) u(n,m) = Xx; end if(u(n,m)>Xs) u(n,m) = Xs; end end end %%%%%%%%%%%%%%%%%%%%%选择操作%%%%%%%%%%%%%%%%%%%%%%%%%% for m = 1:NP Ob1(m) = func3(u(:, m)); end for m = 1:NP if Ob1(m) > Ob(m) x(:, m) = u(:, m); end end for m = 1:NP Ob(m) = func3(x(:, m)); end trace(gen+1) = max(Ob); % if min(Ob(m)) <yz % break % end end [SortOb, Index] = sort(Ob); x = x(:, Index); X = x(:, end); % 最优变量 Y = max(Ob); % 最优值 %%%%%%%%%%%%%%%%%%画图%%%%%%%%%%%%%%%%%% figure plot(trace); xlabel('迭代次数'); ylabel('目标函数值'); title('DE目标函数曲线') %%%%%%%%%%%%适应度函数%%%%%%%%%%%%%%%%%%% function result = func3(x) result = -((x(1)^2+x(2)-1).^2+(x(1) +x(2)^2-7)^2)/200+10; end
[1] 包子阳等. 智能优化算法及其MATLAB实例(第3版)[M]. 北京: 电子工业出版社, 2020.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。