ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

【优化算法】萤火虫优化算法(FA)【含Matlab源码 482期】

2021-07-01 11:06:58  阅读:321  来源: 互联网

标签:function Lb 亮度 算法 源码 优化 萤火虫 Ub


一、简介

萤火虫算法(Fire-fly algorithm,FA)由剑桥大学 Yang 于 2009 年提出 , 作为最新的群智能优化算法之一 , 该算法具有更好的收敛速度和收敛精度 , 且易于工程实现等优点。
1 算法原理
在FA 中 , 萤火虫发出光亮的主要目的是作为一个信号系统 , 以吸引其他的萤火虫个体 , 其假设为 : 1) 萤火虫不分性别 , 它将会被吸引到所有其他比它更亮的萤火虫那去 ; 2) 萤火虫的吸引力和亮度成正比 , 对于任何两只萤火虫 , 其中一只会向着比它更亮的另一只移动 , 然而 , 亮度是随着距离的增加而减少的 ;3) 如果没有找到一个比给定的萤火虫更亮 , 它会随机移动 。
如上所述 , 萤火虫算法包含两个要素 , 即亮度和吸引度 . 亮度体现了萤火虫所处位置的优劣并决定其移动方向 , 吸引度决定了萤火虫移动的距离 , 通过亮度和吸引度的不断更新 , 从而实现目标优化 . 从数学角度对萤火虫算法的主要参数进行如下描述 :

在这里插入图片描述
算法步骤如下:
(1) 初始化萤火虫算法参数.
(2) 计算各萤火虫的亮度并排序得到亮度最大的萤火虫位置.
(3)判断迭代是否结束:判断是否达到最大迭代次数 T ,达到则转(4),否则转(5).
(4) 输出亮度最大的萤火虫位置及其亮度.
(5) 更新萤火虫位置:根据式(3)更新萤火虫的位置,对处在最佳位置的萤火虫进行随机扰动,搜索次数增加1 ,转(2),进行下一次搜索.

二、源代码

% ===新兴元启发式优化算法==== % 
% ======================================================== % 
% -------------------------------------------------------- %

function fa_ndim
% parameters [n N_iteration alpha betamin gamma]
para=[20 500 0.5 0.2 1];


% Simple bounds/limits
disp('Solve the simple spring design problem ...');
% 定义问题维度
d=15;
Lb=zeros(1,d);
Ub=2*ones(1,d);
u0=Lb+(Ub-Lb).*rand(1,d);

[u,fval,NumEval]=ffa_mincon(@cost,u0,Lb,Ub,para);

% Display results
bestsolution=u
bestojb=fval
total_number_of_function_evaluations=NumEval

%%% Put your own cost/objective function here --------%%%
%% Cost or Objective function   代价函数或目标函数
 function z=cost(x)
     %准确的结果应该是(1,1,1,1,1,...,1,1)
z=sum((x-1).^2)

% Start FA     开始执行萤火虫算法
function [nbest,fbest,NumEval]=ffa_mincon(fhandle,u0, Lb, Ub, para)
% Check input parameters (otherwise set as default values)
if nargin<5, para=[20 500 0.25 0.20 1]; end
if nargin<4, Ub=[]; end
if nargin<3, Lb=[]; end
if nargin<2,
disp('Usuage: FA_mincon(@cost,u0,Lb,Ub,para)');
end

% n=number of fireflies
% MaxGeneration=number of pseudo time steps
% ------------------------------------------------
% alpha=0.25;      % Randomness 0--1 (highly random)
% betamn=0.20;     % minimum value of beta
% gamma=1;         % Absorption coefficient
% ------------------------------------------------
n=para(1);  MaxGeneration=para(2);
alpha=para(3); betamin=para(4); gamma=para(5);%beta0取1,alpha取[0,1],光吸收系数,取[0.01,100]

% Total number of function evaluations         函数运算总次数
NumEval=n*MaxGeneration;

% Check if the upper bound & lower bound are the same size      检查是否越过上下限
if length(Lb) ~=length(Ub),%~=为不等于
    disp('Simple bounds/limits are improper!');
    return
end

% Calcualte dimension          计算维度
d=length(u0);

% Initial values of an array        初始化向量
zn=ones(n,1)*10^100;
% ------------------------------------------------
% generating the initial locations of n fireflies            生成萤火虫位置
[ns,Lightn]=init_ffa(n,d,Lb,Ub,u0);

% Iterations or pseudo time marching
for k=1:MaxGeneration,     %%%%% start iterations        开始迭代

% This line of reducing alpha is optional
 alpha=alpha_new(alpha,MaxGeneration);

% Evaluate new solutions (for all n fireflies)            评估新解
for i=1:n,
   zn(i)=fhandle(ns(i,:));               
   Lightn(i)=zn(i);
end

% Ranking fireflies by their light intensity/objectives 根据亮度强弱排列萤火虫
[Lightn,Index]=sort(zn);
ns_tmp=ns;
for i=1:n,
 ns(i,:)=ns_tmp(Index(i),:);
end

%% Find the current best   找到当前最优
nso=ns; Lighto=Lightn;
nbest=ns(1,:); Lightbest=Lightn(1);

% For output only
fbest=Lightbest;

% Move all fireflies to the better locations       所有的萤火虫飞向亮度更高的萤火虫
[ns]=ffa_move(n,d,ns,Lightn,nso,Lighto,nbest,Lightbest,alpha,betamin,gamma,Lb,Ub);

end   %%%%% end of iterations迭代结束

% -------------------------------------------------------
% ----- All the subfunctions are listed here ------------         子函数
% The initial locations of n fireflies       萤火虫位置初始化
function [ns,Lightn]=init_ffa(n,d,Lb,Ub,u0)
  % if there are bounds/limits,
if length(Lb)>0,
   for i=1:n,
   ns(i,:)=Lb+(Ub-Lb).*rand(1,d);
   end
else
   % generate solutions around the random guess
   for i=1:n,
   ns(i,:)=u0+randn(1,d);
   end
end

% initial value before function evaluations       初始化亮度值
Lightn=ones(n,1)*10^100;

% Move all fireflies toward brighter ones            所有的萤火虫飞向最亮的萤火虫
function [ns]=ffa_move(n,d,ns,Lightn,nso,Lighto,nbest,Lightbest,alpha,betamin,gamma,Lb,Ub)
% Scaling of the system测量系统
scale=abs(Ub-Lb);

三、运行结果

在这里插入图片描述

 

标签:function,Lb,亮度,算法,源码,优化,萤火虫,Ub
来源: https://blog.51cto.com/u_15287606/2962874

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有