SlideShare a Scribd company logo
% ======================================================== %
% Files of the Matlab programs included in the book: %
% Xin-She Yang, Nature-Inspired Metaheuristic Algorithms, %
% Second Edition, Luniver Press, (2010). www.luniver.com %
% ======================================================== %
% -------------------------------------------------------- %
% Firefly Algorithm for constrained optimization using %
% for the design of a spring (benchmark) %
% by Xin-She Yang (Cambridge University) Copyright @2009 %
% -------------------------------------------------------- %
function fa_ndim
% parameters [n N_iteration alpha betamin gamma]
para=[20 500 0.5 0.2 1];
help fa_ndim.m
% Simple bounds/limits for d-dimensional problems
d=15;
Lb=zeros(1,d);
Ub=2*ones(1,d);
% Initial random guess
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)
% Exact solutions should be (1,1,...,1)
z=sum((x-1).^2);
%%% End of the part to be modified -------------------%%%
%%% --------------------------------------------------%%%
%%% Do not modify the following codes unless you want %%%
%%% to improve its performance etc %%%
% -------------------------------------------------------
% ===Start of the Firefly Algorithm Implementation ======
% Lb = lower bounds/limits
% Ub = upper bounds/limits
% para == optional (to control the Firefly algorithm)
% Outputs: nbest = the best solution found so far
% fbest = the best objective value
% NumEval = number of evaluations: n*MaxGeneration
% Optional:
% The alpha can be reduced (as to reduce the randomness)
% ---------------------------------------------------------
% 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);
% 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);
% Updating fireflies
for i=1:n,
% The attractiveness parameter beta=exp(-gamma*r)
for j=1:n,
r=sqrt(sum((ns(i,:)-ns(j,:)).^2));
% Update moves
if Lightn(i)>Lighto(j), % Brighter and more attractive
beta0=1; beta=(beta0-betamin)*exp(-gamma*r.^2)+betamin;
tmpf=alpha.*(rand(1,d)-0.5).*scale;
ns(i,:)=ns(i,:).*(1-beta)+nso(j,:).*beta+tmpf;
end
end % end for j
end % end for i
% Check if the updated solutions/locations are within limits
[ns]=findlimits(n,ns,Lb,Ub);
% This function is optional, as it is not in the original FA
% The idea to reduce randomness is to increase the convergence,
% however, if you reduce randomness too quickly, then premature
% convergence can occur. So use with care.
function alpha=alpha_new(alpha,NGen)
% alpha_n=alpha_0(1-delta)^NGen=10^(-4);
% alpha_0=0.9
delta=1-(10^(-4)/0.9)^(1/NGen);
alpha=(1-delta)*alpha;
% Make sure the fireflies are within the bounds/limits
function [ns]=findlimits(n,ns,Lb,Ub)
for i=1:n,
% Apply the lower bound
ns_tmp=ns(i,:);
I=ns_tmp<Lb;
ns_tmp(I)=Lb(I);
% Apply the upper bounds
J=ns_tmp>Ub;
ns_tmp(J)=Ub(J);
% Update this new move
ns(i,:)=ns_tmp;
end
%% ==== End of Firefly Algorithm implementation ======

More Related Content

TXT
Bat algorithm (demo)
PPTX
Java script array methods
PPTX
C programming
PPTX
Firefly algorithm
PPTX
Cuckoo Search & Firefly Algorithms
PDF
1308.3898 1
PDF
1308.3898
PDF
Improved Firefly Algorithm for Unconstrained Optimization Problems
Bat algorithm (demo)
Java script array methods
C programming
Firefly algorithm
Cuckoo Search & Firefly Algorithms
1308.3898 1
1308.3898
Improved Firefly Algorithm for Unconstrained Optimization Problems

Similar to Firefly algorithm (20)

PDF
Firefly Algorithm, Levy Flights and Global Optimization
PDF
M01117578
PDF
Firefly Algorithm for Unconstrained Optimization
PDF
Firefly Algorithm: Recent Advances and Applications
PDF
ADVANCED OPTIMIZATION TECHNIQUES META-HEURISTIC ALGORITHMS FOR ENGINEERING AP...
TXT
Flower Pollination Algorithm (matlab code)
PPTX
Firefly algorithm
PPTX
Firefly algorithm
PDF
Firefly Algorithms for Multimodal Optimization
PDF
Firefly Algorithm, Stochastic Test Functions and Design Optimisation
PDF
Nature-inspired algorithms
PPTX
nature inspired algorithms
PDF
Firefly Algorithm based Optimal Reactive Power Flow
PDF
Modified Discrete Firefly Algorithm Combining Genetic Algorithm for Traveling...
PDF
Research on Chaotic Firefly Algorithm and the Application in Optimal Reactive...
PPTX
Natural-Inspired_Amany_Final.pptx
PDF
Multiobjective Firefly Algorithm for Continuous Optimization
PPTX
5. FIREFLY ALGORITHM OPTIMIZATION.pptx
PDF
A Study of Firefly Algorithm and its Application in Non-Linear Dynamic Systems
PDF
COMPARISON BETWEEN ARTIFICIAL BEE COLONY ALGORITHM, SHUFFLED FROG LEAPING ALG...
Firefly Algorithm, Levy Flights and Global Optimization
M01117578
Firefly Algorithm for Unconstrained Optimization
Firefly Algorithm: Recent Advances and Applications
ADVANCED OPTIMIZATION TECHNIQUES META-HEURISTIC ALGORITHMS FOR ENGINEERING AP...
Flower Pollination Algorithm (matlab code)
Firefly algorithm
Firefly algorithm
Firefly Algorithms for Multimodal Optimization
Firefly Algorithm, Stochastic Test Functions and Design Optimisation
Nature-inspired algorithms
nature inspired algorithms
Firefly Algorithm based Optimal Reactive Power Flow
Modified Discrete Firefly Algorithm Combining Genetic Algorithm for Traveling...
Research on Chaotic Firefly Algorithm and the Application in Optimal Reactive...
Natural-Inspired_Amany_Final.pptx
Multiobjective Firefly Algorithm for Continuous Optimization
5. FIREFLY ALGORITHM OPTIMIZATION.pptx
A Study of Firefly Algorithm and its Application in Non-Linear Dynamic Systems
COMPARISON BETWEEN ARTIFICIAL BEE COLONY ALGORITHM, SHUFFLED FROG LEAPING ALG...
Ad

More from Xin-She Yang (20)

PDF
Cuckoo Search Algorithm: An Introduction
PDF
Metaheuristic Algorithms: A Critical Analysis
PDF
Nature-Inspired Optimization Algorithms
PDF
A Biologically Inspired Network Design Model
TXT
Multiobjective Bat Algorithm (demo only)
PDF
Nature-Inspired Metaheuristic Algorithms
PDF
Metaheuristics and Optimiztion in Civil Engineering
PDF
A Biologically Inspired Network Design Model
PDF
Introduction to Computational Mathematics (2nd Edition, 2015)
PDF
Memetic Firefly algorithm for combinatorial optimization
PDF
Two-Stage Eagle Strategy with Differential Evolution
PDF
Accelerated Particle Swarm Optimization and Support Vector Machine for Busine...
PDF
Bat Algorithm for Multi-objective Optimisation
PDF
Are motorways rational from slime mould's point of view?
PDF
Review of Metaheuristics and Generalized Evolutionary Walk Algorithm
PDF
Test Problems in Optimization
PDF
Engineering Optimisation by Cuckoo Search
PDF
A New Metaheuristic Bat-Inspired Algorithm
PDF
Eagle Strategy Using Levy Walk and Firefly Algorithms For Stochastic Optimiza...
PDF
Fractals in Small-World Networks With Time Delay
Cuckoo Search Algorithm: An Introduction
Metaheuristic Algorithms: A Critical Analysis
Nature-Inspired Optimization Algorithms
A Biologically Inspired Network Design Model
Multiobjective Bat Algorithm (demo only)
Nature-Inspired Metaheuristic Algorithms
Metaheuristics and Optimiztion in Civil Engineering
A Biologically Inspired Network Design Model
Introduction to Computational Mathematics (2nd Edition, 2015)
Memetic Firefly algorithm for combinatorial optimization
Two-Stage Eagle Strategy with Differential Evolution
Accelerated Particle Swarm Optimization and Support Vector Machine for Busine...
Bat Algorithm for Multi-objective Optimisation
Are motorways rational from slime mould's point of view?
Review of Metaheuristics and Generalized Evolutionary Walk Algorithm
Test Problems in Optimization
Engineering Optimisation by Cuckoo Search
A New Metaheuristic Bat-Inspired Algorithm
Eagle Strategy Using Levy Walk and Firefly Algorithms For Stochastic Optimiza...
Fractals in Small-World Networks With Time Delay
Ad

Recently uploaded (20)

PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
PPT on Performance Review to get promotions
PDF
Digital Logic Computer Design lecture notes
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
DOCX
573137875-Attendance-Management-System-original
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
composite construction of structures.pdf
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPT
Project quality management in manufacturing
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPT on Performance Review to get promotions
Digital Logic Computer Design lecture notes
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
573137875-Attendance-Management-System-original
Model Code of Practice - Construction Work - 21102022 .pdf
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Foundation to blockchain - A guide to Blockchain Tech
composite construction of structures.pdf
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
CYBER-CRIMES AND SECURITY A guide to understanding
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Project quality management in manufacturing
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
UNIT-1 - COAL BASED THERMAL POWER PLANTS

Firefly algorithm

  • 1. % ======================================================== % % Files of the Matlab programs included in the book: % % Xin-She Yang, Nature-Inspired Metaheuristic Algorithms, % % Second Edition, Luniver Press, (2010). www.luniver.com % % ======================================================== % % -------------------------------------------------------- % % Firefly Algorithm for constrained optimization using % % for the design of a spring (benchmark) % % by Xin-She Yang (Cambridge University) Copyright @2009 % % -------------------------------------------------------- % function fa_ndim % parameters [n N_iteration alpha betamin gamma] para=[20 500 0.5 0.2 1]; help fa_ndim.m % Simple bounds/limits for d-dimensional problems d=15; Lb=zeros(1,d); Ub=2*ones(1,d); % Initial random guess 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) % Exact solutions should be (1,1,...,1) z=sum((x-1).^2); %%% End of the part to be modified -------------------%%% %%% --------------------------------------------------%%% %%% Do not modify the following codes unless you want %%% %%% to improve its performance etc %%% % ------------------------------------------------------- % ===Start of the Firefly Algorithm Implementation ====== % Lb = lower bounds/limits % Ub = upper bounds/limits % para == optional (to control the Firefly algorithm) % Outputs: nbest = the best solution found so far % fbest = the best objective value % NumEval = number of evaluations: n*MaxGeneration % Optional: % The alpha can be reduced (as to reduce the randomness) % --------------------------------------------------------- % 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,
  • 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); % 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 % -------------------------------------------------------
  • 3. % ----- 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); % Updating fireflies for i=1:n, % The attractiveness parameter beta=exp(-gamma*r) for j=1:n, r=sqrt(sum((ns(i,:)-ns(j,:)).^2)); % Update moves if Lightn(i)>Lighto(j), % Brighter and more attractive beta0=1; beta=(beta0-betamin)*exp(-gamma*r.^2)+betamin; tmpf=alpha.*(rand(1,d)-0.5).*scale; ns(i,:)=ns(i,:).*(1-beta)+nso(j,:).*beta+tmpf; end end % end for j end % end for i % Check if the updated solutions/locations are within limits [ns]=findlimits(n,ns,Lb,Ub); % This function is optional, as it is not in the original FA % The idea to reduce randomness is to increase the convergence, % however, if you reduce randomness too quickly, then premature % convergence can occur. So use with care. function alpha=alpha_new(alpha,NGen) % alpha_n=alpha_0(1-delta)^NGen=10^(-4); % alpha_0=0.9 delta=1-(10^(-4)/0.9)^(1/NGen); alpha=(1-delta)*alpha; % Make sure the fireflies are within the bounds/limits function [ns]=findlimits(n,ns,Lb,Ub) for i=1:n, % Apply the lower bound ns_tmp=ns(i,:); I=ns_tmp<Lb; ns_tmp(I)=Lb(I); % Apply the upper bounds J=ns_tmp>Ub; ns_tmp(J)=Ub(J); % Update this new move
  • 4. ns(i,:)=ns_tmp; end %% ==== End of Firefly Algorithm implementation ======