1.背景
2009年,Yang受到萤火虫发光吸引同伴捕食的自然行为影响,提出了萤火虫算法(Firefly Alogorithm, FA)。
2.算法原理
2.1算法思想
萤火虫算法两个重要因素:光强和萤火虫间的吸引度。
Yang提出了理想假设:
- 萤火虫间的吸引度只受光强影响
- 吸引度与光强呈正比,与距离呈反比
- 光强表述为目标函数值,即越接近食物位置的萤火虫光强越亮
萤火虫群体中每个个体会受到比自身更亮萤火虫的影响而产生移动,寻找更优解。
2.2算法过程
群体位置初始化:
X
=
l
b
+
r
a
n
d
∗
(
u
b
−
l
b
)
X=lb+rand*(ub-lb)
X=lb+rand∗(ub−lb)
其中,
u
b
,
l
b
ub,lb
ub,lb分别代表萤火虫上下位置边界。
萤火虫间的吸引度:
β
i
j
=
β
0
∗
e
−
γ
∗
r
i
j
2
r
i
j
=
(
x
i
−
x
j
)
2
\beta_{ij}=\beta_0*e^{-\gamma *r_{ij}^2} \\ r_{ij}=\sqrt{(x_i-x_j)^2}
βij=β0∗e−γ∗rij2rij=(xi−xj)2
其中,
β
i
j
,
β
0
,
γ
,
r
i
j
\beta_{ij},\beta_0,\gamma,r_{ij}
βij,β0,γ,rij分别代表萤火虫
i
,
j
i,j
i,j的吸引度,距离为0时的吸引度,光吸收系数和萤火虫
i
,
j
i,j
i,j的欧式距离。
朝着更亮的萤火虫移动:
x
i
t
+
1
=
x
i
t
+
β
i
j
(
x
j
t
−
x
i
t
)
+
α
(
r
a
n
d
−
1
2
)
x_i^{t+1}=x_i^{t}+\beta_{ij}(x_j^{t}-x_i^{t})+\alpha(rand-\frac{1}{2})
xit+1=xit+βij(xjt−xit)+α(rand−21)
其中,
x
i
t
+
1
x_i^{t+1}
xit+1代表萤火虫
i
i
i下一次飞行方向。
伪代码:
3.代码实现
% 萤火虫优化算法
function [Best_pos, Best_fitness, Iter_curve, History_pos, History_best] = FA(pop, dim, ub, lb, fobj, maxIter)
%input
%pop 种群数量
%dim 问题维数
%ub 变量上边界
%lb 变量下边界
%fobj 适应度函数
%maxIter 最大迭代次数
%output
%Best_pos 最优位置
%Best_fitness 最优适应度值
%Iter_curve 每代最优适应度值
%History_pos 每代种群位置
%History_best 每代最优萤火虫位置
%% 萤火虫优化算法参数
beta0 = 2; %最大吸引度
gamma = 1; %光强吸收系数
alpha = 0.2; %步长因子
alpha_damp = 0.98; % 步长下降因子
dmax = norm(ub - lb); %空间最大距离
%% 初始化萤火虫位置
X = zeros(pop, dim);
for i = 1:pop
for j = 1:dim
X(i,j) = (ub(j) - lb(j)) * rand() + lb(j);
end
end
%% 初始化适应度值
fitness = zeros(1,pop);
for i = 1:pop
fitness(i) = fobj(X(i,:));
end
%% 记录全局最优解
[~, index] = min(fitness);
gBestFitness = fitness(index);
gBest = X(index,:);
Xnew = X;
%% 迭代
for t = 1:maxIter
for i = 1:pop
for j = 1:pop
if fitness(j) < fitness(i)
%计算距离
rij = norm(X(i,:) - X(j,:))./dmax;
%计算吸引度
beta = beta0 * exp(-gamma * rij^2);
%更新位置
newSolution = X(i,:) + beta * rand(1,dim).*(X(j,:) - X(i,:)) + alpha.*(rand(1,dim)-1/2);
%边界检查
for k = 1:dim
if newSolution(k) > ub(k)
newSolution(k) = ub(k);
end
if newSolution(k) < lb(k)
newSolution(k) = lb(k);
end
end
newSFitness = fobj(newSolution);
%更新萤火虫位置
if newSFitness < fitness(i)
Xnew(i,:) = newSolution;
fitness(i) = newSFitness;
if newSFitness < gBestFitness
gBestFitness = newSFitness;
gBest = newSolution;
end
end
end
end
end
%新位置与历史位置合并
P = [X;Xnew];
for i = 1:2*pop
Pfit(i) = fobj(P(i,:));
end
[~, sortIndex] = sort(Pfit); %适应度排序
X = P(sortIndex(1:pop),:); %取靠前的pop只萤火虫作为下次迭代初始种群
%记录数据
Iter_curve(t) = gBestFitness;
History_pos{t} = X;
History_best{t} = gBest;
end
Best_pos = gBest;
Best_fitness = gBestFitness;
end
优化问题:
以CEC2005测试函数F7为例
clear,clc,close all
x = -1.28:0.02:1.28;
y = x;
L = length(x);
for i = 1:L
for j = 1:L
f(i,j) = fun([x(i) y(j)]);
end
end
surfc(x, y, f, 'LineStyle', 'none', 'FaceAlpha',0.5);
% 设定萤火虫参数
pop = 50;
dim = 2;
ub = [1.28, 1.28];
lb = [-1.28, -1.28];
maxIter = 100;
fobj = @(x) fun(x);
% 求解
[Best_pos, Best_fitness, Iter_curve, History_pos, History_best] = FA(pop, dim, ub, lb, fobj, maxIter);
4.参考文献
[1] Yang X S. Firefly algorithms for multimodal optimization[C]//International symposium on stochastic algorithms. Berlin, Heidelberg: Springer Berlin Heidelberg, 2009: 169-178.