萤火虫算法以及避障算法path planning in uncertain environment by using firefly algorithm(萤火虫算法在实际中的应用)
萤火虫算法介绍
2008年,Yang提出了基于自然的元启发式算法。其灵感来自萤火虫的闪烁行为。这是一种新的人工智能方法,被广泛地应用于各个领域的优化和自主系统。
萤火虫通过发光这个信号吸引其他的萤火虫,吸引雌雄萤火虫的吸引力取决于闪光的节奏、闪光的节奏、闪光的速度和闪光的时间、观察到的光的时间。萤火虫的闪光被用作要优化的目标函数。当另一只萤火虫拥有较高的光强度时,另一只萤火虫就有可能被吸引。这就是FA工作的基本概念。
它基于如下三个准则
- 所有萤火虫没有性别,并且被吸引的时候不考虑性别
- 萤火虫的吸引程度与它的亮度成正比。因此,对于任何两只闪光的萤火虫,亮度较低的那只会向亮度较高的那只移动。两只萤火虫之间的距离越小,亮度就越高。萤火虫的随机移动等于闪烁的萤火虫的亮度
- 目标函数用于评价萤火虫的亮度
有如下的优缺点
在特征提取、聚类等问题上的性能胜过大多传统算法 | 发现率低、求解精读不高、求解速度慢。 |
---|---|
定义 β ( r ) (萤火虫的吸引力) , β 0 ( r = 0 时萤火虫的吸引力所乘的吸收光的系数) 定义 \beta(r)(萤火虫的吸引力), \beta_{0}(r=0时萤火虫的吸引力所乘的吸收光的系数) 定义β(r)(萤火虫的吸引力),β0(r=0时萤火虫的吸引力所乘的吸收光的系数)
萤火虫的吸引力通过如下公式给出
β
(
r
)
=
β
0
exp
(
−
γ
r
m
)
;
m
≥
1
\beta(r)=\beta_{0} \exp \left(-\gamma r^{m}\right) ; m \geq 1
β(r)=β0exp(−γrm);m≥1
两个萤火虫之间的距离
r
i
j
=
∥
x
i
−
x
j
∥
=
∑
k
=
1
d
(
x
i
k
−
x
j
k
)
2
r_{i j}=\left\|x_{i}-x_{j}\right\|=\sqrt{\sum_{k=1}^{d}\left(x_{i}^{k}-x_{j}^{k}\right)^{2}}
rij=∥xi−xj∥=k=1∑d(xik−xjk)2
暗萤火虫向明亮萤火虫移动的公式如下
这里贴出github上的代码
% =========================================================%
% Firefly Algorithm by X S Yang (Cambridge University) %
% Usage: firefly_simple([number_of_fireflies,MaxGeneration])
% eg: firefly_simple([12,50]); %
% ======================================================== %
% This is a demo for 2D functions; for higher dimenions, %
% you should use fa_ndim.m or fa_mincon.m %
% Parameters choice:
% Gamma should be linked with scales. Otherwise, the FA %
% the efficiency will be significantly reduced because %
% the beta term may be too small. %
% Similarly, alpha should also be linked with scales, %
% the steps should not too large or too small, often %
% steps are about 1/10 to 1/100 of the domain size. %
% In addition, alpha should be reduced gradually %
% using alpha=alpha_0 delta^t during eteration t. %
% Typically, delta=0.9 to 0.99 will be a good choice. %
% ======================================================== %
function [best]=firefly_simple(instr)
% n=number of fireflies
% MaxGeneration=number of pseudo time steps
if nargin<1, instr=[12 50]; end
n=instr(1); MaxGeneration=instr(2);
% Show info
help firefly_simple.m
rand('state',0); % Reset the random generator
% ------ Four peak functions ---------------------
str1='exp(-(x-4)^2-(y-4)^2)+exp(-(x+4)^2-(y-4)^2)';
str2='+2*exp(-x^2-(y+4)^2)+2*exp(-x^2-y^2)';
funstr=strcat(str1,str2);
% Converting to an inline function
f=vectorize(inline(funstr));
% range=[xmin xmax ymin ymax];
range=[-5 5 -5 5];
% ------------------------------------------------
alpha=0.2; % Randomness 0--1 (highly random)
gamma=1.0; % Absorption coefficient
delta=0.97; % Randomness reduction (similar to
% an annealing schedule)
% ------------------------------------------------
% Grid values are used for display only
Ngrid=100;
dx=(range(2)-range(1))/Ngrid;
dy=(range(4)-range(3))/Ngrid;
[x,y]=meshgrid(range(1):dx:range(2),...
range(3):dy:range(4));
z=f(x,y);
% Display the shape of the objective function
figure(1); surfc(x,y,z);
% ------------------------------------------------
% generating the initial locations of n fireflies
[xn,yn,Lightn]=init_ffa(n,range);
% Display the paths of fireflies in a figure with
% contours of the function to be optimized
figure(2);
pic_num = 1;
% Iterations or pseudo time marching
for i=1:MaxGeneration, %%%%% start iterations
% Show the contours of the function
contour(x,y,z,15); hold on;
% Evaluate new solutions
zn=f(xn,yn);
% Ranking the fireflies by their light intensity
[Lightn,Index]=sort(zn);
xn=xn(Index); yn=yn(Index);
xo=xn; yo=yn; Lighto=Lightn;
% Trace the paths of all roaming fireflies
plot(xn,yn,'.','markersize',10,'markerfacecolor','g');
% Move all fireflies to the better locations
[xn,yn]=ffa_move(xn,yn,Lightn,xo,yo,Lighto,alpha,gamma,range);
drawnow;
% Use "hold on" to show the paths of fireflies
F=getframe(gcf);
I=frame2im(F);
[I,map]=rgb2ind(I,256);
if pic_num == 1
imwrite(I,map,'test.gif','gif', 'Loopcount',inf,'DelayTime',0.2);
else
imwrite(I,map,'test.gif','gif','WriteMode','append','DelayTime',0.2);
end
pic_num = pic_num + 1;
hold off;
% Reduce randomness as iterations proceed
alpha=newalpha(alpha,delta);
end %%%%% end of iterations
best(:,1)=xo'; best(:,2)=yo'; best(:,3)=Lighto';
% ----- All subfunctions are listed here ---------
% The initial locations of n fireflies
function [xn,yn,Lightn]=init_ffa(n,range)
xrange=range(2)-range(1);
yrange=range(4)-range(3);
xn=rand(1,n)*xrange+range(1);
yn=rand(1,n)*yrange+range(3);
Lightn=zeros(size(yn));
% Move all fireflies toward brighter ones
function [xn,yn]=ffa_move(xn,yn,Lightn,xo,yo,...
Lighto,alpha,gamma,range)
ni=size(yn,2); nj=size(yo,2);
for i=1:ni,
% The attractiveness parameter beta=exp(-gamma*r)
for j=1:nj,
r=sqrt((xn(i)-xo(j))^2+(yn(i)-yo(j))^2);
if Lightn(i)<Lighto(j), % Brighter and more attractive
beta0=1; beta=beta0*exp(-gamma*r.^2);
xn(i)=xn(i).*(1-beta)+xo(j).*beta+alpha.*(rand-0.5);
yn(i)=yn(i).*(1-beta)+yo(j).*beta+alpha.*(rand-0.5);
end
end % end for j
end % end for i
[xn,yn]=findrange(xn,yn,range);
% Reduce the randomness during iterations
function alpha=newalpha(alpha,delta)
alpha=alpha*delta;
% Make sure the fireflies are within the range
function [xn,yn]=findrange(xn,yn,range)
for i=1:length(yn),
if xn(i)<=range(1), xn(i)=range(1); end
if xn(i)>=range(2), xn(i)=range(2); end
if yn(i)<=range(3), yn(i)=range(3); end
if yn(i)>=range(4), yn(i)=range(4); end
end
% ============== end =====================================
萤火虫避障算法
避障的流程图如上,效果如下
由于论文中的避障部分代码不详细,思路不详细,不多叙述
参考论文:Path planning in uncertain environment by using firefly algorithm