Nature Inspired Metaheuristic Algorithms
Nature Inspired Metaheuristic Algorithms
Metaheuristic Algorithms
Second Edition
Xin-She Yang
ta h e u r is
d Me ti
i re
Luniver Press
cA
p
N a t u re -I n s
lgorith m s
Xin-She Yang
c Luniver Press
S ec
)
10
on
d E d ion (20
it
Published in 2010 by Luniver Press
Frome, BA11 6TT, United Kingdom
www.luniver.com
All rights reserved. This book, or parts thereof, may not be reproduced in
any form or by any means, electronic or mechanical, including photocopy-
ing, recording or by any information storage and retrieval system, without
permission in writing from the copyright holder.
ISBN-13: 978-1-905986-28-6
ISBN-10: 1-905986-28-9
While every attempt is made to ensure that the information in this publi-
cation is correct, no liability can be accepted by the authors or publishers
for loss, damage or injury caused by any errors in, or omission from, the
information given.
ta h e u r is
d Me ti
i re
cA
p
N a t u re -I n s
lgorith m s
Xin-She Yang
c Luniver Press
S ec
)
10
on
d E d ion (20
it
CONTENTS
1 Introduction 1
1.1 Optimization 1
1.2 Search for Optimality 2
1.3 Nature-Inspired Metaheuristics 4
1.4 A Brief History of Metaheuristics 5
lgorith m s
)
10
on
d E d ion (20 2.4 Optimization as Markov Chains 17
it
i
ii CONTENTS
3 Simulated Annealing 21
3.1 Annealing and Boltzmann Distribution 21
3.2 Parameters 22
3.3 SA Algorithm 23
3.4 Unconstrained Optimization 24
3.5 Stochastic Tunneling 26
5 Genetic Algorithms 41
5.1 Introduction 41
5.2 Genetic Algorithms 42
5.3 Choice of Parameters 43
6 Differential Evolution 47
6.1 Introduction 47
6.2 Differential Evolution 47
6.3 Variants 50
6.4 Implementation 50
lgorith m s
)
10
on
d E d ion (20 7.2.5 Artificial Bee Colony Optimization 61
it
CONTENTS iii
8 Swarm Optimization 63
8.1 Swarm Intelligence 63
8.2 PSO algorithms 64
8.3 Accelerated PSO 65
8.4 Implementation 66
8.5 Convergence Analysis 69
9 Harmony Search 73
9.1 Harmonics and Frequencies 73
9.2 Harmony Search 74
9.3 Implementation 76
10 Firefly Algorithm 81
10.1 Behaviour of Fireflies 81
10.2 Firefly Algorithm 82
10.3 Light Intensity and Attractiveness 83
10.4 Scalings and Asymptotics 84
10.5 Implementation 86
10.6 FA variants 89
10.7 Spring Design 89
11 Bat Algorithm 97
11.1 Echolocation of bats 97
11.1.1 Behaviour of microbats 97
11.1.2 Acoustics of Echolocation 98
11.2 Bat Algorithm 98
11.2.1 Movement of Virtual Bats 99
11.2.2 Loudness and Pulse Emission 100
11.3 Validation and Discussions 101
11.4 Implementation 102
11.5 Further Topics 103
lgorith m s
)
10
on
d E d ion (20 12.4 Choice of Parameters 108
it
iv CONTENTS
References 141
Index 147
ta h e u r is
d Me ti
i re
cA
p
N a t u re -I n s
lgorith m s
Xin-She Yang
c Luniver Press
S ec
)
10
on
d E d ion (20
it
v
Since the publication of the first edition of this book in 2008, significant
developments have been made in metaheuristics, and new nature-inspired
metaheuristic algorithms emerge, including cuckoo search and bat algo-
rithms. Many readers have taken time to write to me personally, providing
valuable feedback, asking for more details of algorithm implementation,
or simply expressing interests in applying these new algorithms in their
applications.
In this revised edition, we strive to review the latest developments in
metaheuristic algorithms, to incorporate readers’ suggestions, and to pro-
vide a more detailed description to algorithms. Firstly, we have added
detailed descriptions of how to incorporate constraints in the actual imple-
mentation. Secondly, we have added three chapters on differential evolu-
tion, cuckoo search and bat algorithms, while some existing chapters such
as ant algorithms and bee algorithms are combined into one due to their
similarity. Thirdly, we also explained artificial neural networks and sup-
port vector machines in the framework of optimization and metaheuristics.
Finally, we have been trying in this book to provide a consistent and uni-
fied approach to metaheuristic algorithms, from a brief history in the first
chapter to the unified approach in the last chapter.
Furthermore, we have provided more Matlab programs. At the same
time, we also omit some of the implementation such as genetic algorithms,
as we know that there are many good software packages (both commercial
and open course). This allows us to focus more on the implementation of
new algorithms. Some of the programs also have a version for constrained
optimization, and readers can modify them for their own applications.
Even with the good intention to cover most popular metaheuristic al-
gorithms, the choice of algorithms is a difficult task, as we do not have
the space to cover every algorithm. The omission of an algorithm does not
mean that it is not popular. In fact, some algorithms are very powerful
and routinely used in many applications. Good examples are Tabu search
and combinatorial algorithms, and interested readers can refer to the refer-
ences provided at the end of the book. The effort in writing this little book
becomes worth while if this book could in some way encourage readers’
interests in metaheuristics.
Xin-She Yang
ta h e u r is August 2010
d Me ti
i re
cA
p
N a t u re -I n s
lgorith m s
Xin-She Yang
c Luniver Press
S ec
)
10
on
d E d ion (20
it
vi
Xin-She Yang
Cambridge, 2008
ta h e u r is
d Me ti
i re
cA
p
N a t u re -I n s
lgorith m s
Xin-She Yang
c Luniver Press
S ec
)
10
on
d E d ion (20
it
Chapter 1
INTRODUCTION
1.1 OPTIMIZATION
single objective. The space spanned by the decision variables is called the
lgorith m s
Xin-She Yang
design space or search space ℜn , while the space formed by the objective
c Luniver Press
function values is called the solution space or response space. The equali-
S ec
)
10
on
d E d i oties 20
i t n ( for hj and inequalities for gk are called constraints. It is worth pointing
Nature-Inspired Metaheuristic Algorithms, 2nd Edition by Xin-She Yang 1
Copyright c 2010 Luniver Press
2 CHAPTER 1. INTRODUCTION
out that we can also write the inequalities in the other way ≥ 0, and we
can also formulate the objectives as a maximization problem.
In a rare but extreme case where there is no objective at all, there are
only constraints. Such a problem is called a feasibility problem because
any feasible solution is an optimal solution.
If we try to classify optimization problems according to the number
of objectives, then there are two categories: single objective M = 1 and
multiobjective M > 1. Multiobjective optimization is also referred to as
multicriteria or even multi-attributes optimization in the literature. In
real-world problems, most optimization tasks are multiobjective. Though
the algorithms we will discuss in this book are equally applicable to mul-
tiobjective optimization with some modifications, we will mainly place the
emphasis on single objective optimization problems.
Similarly, we can also classify optimization in terms of number of con-
straints J + K. If there is no constraint at all J = K = 0, then it is
called an unconstrained optimization problem. If K = 0 and J ≥ 1, it is
called an equality-constrained problem, while J = 0 and K ≥ 1 becomes
an inequality-constrained problem. It is worth pointing out that in some
formulations in the optimization literature, equalities are not explicitly in-
cluded, and only inequalities are included. This is because an equality
can be written as two inequalities. For example h(x) = 0 is equivalent to
h(x) ≤ 0 and h(x) ≥ 0.
We can also use the actual function forms for classification. The objec-
tive functions can be either linear or nonlinear. If the constraints hj and gk
are all linear, then it becomes a linearly constrained problem. If both the
constraints and the objective functions are all linear, it becomes a linear
programming problem. Here ‘programming’ has nothing to do with com-
puting programming, it means planning and/or optimization. However,
generally speaking, all fi , hj and gk are nonlinear, we have to deal with a
nonlinear optimization problem.
Xin-She Yang
if we are told the treasure is placed at the highest peak of a known region,
c Luniver Press
we will then directly climb up to the steepest cliff and try to reach to the
S ec
)
10
on
d E d i o n ( 2 0highest peak, and this scenario corresponds to the classical hill-climbing
it
1.2 SEARCH FOR OPTIMALITY 3
lgorith m s
Xin-She Yang
climbing with a random restart is a good example. The basic idea is to
c Luniver Press use the deterministic algorithm, but start with different initial points. This
has certain advantages over a simple hill-climbing technique, which may be
S ec
)
10
o
nd 0
E d ition (2
4 CHAPTER 1. INTRODUCTION
timescale. There is no guarantee that the best solutions can be found, and
lgorith m s
Xin-She Yang
we even do not know whether an algorithm will work and why if it does
c Luniver Press
work. The idea is to have an efficient but practical algorithm that will
S ec
)
10
on
d E d i o n ( 2 0work most the time and is able to produce good quality solutions. Among
it
1.4 A BRIEF HISTORY OF METAHEURISTICS 5
the found quality solutions, it is expected some of them are nearly optimal,
though there is no guarantee for such optimality.
Two major components of any metaheuristic algorithms are: intensifi-
cation and diversification, or exploitation and exploration. Diversification
means to generate diverse solutions so as to explore the search space on the
global scale, while intensification means to focus on the search in a local
region by exploiting the information that a current good solution is found
in this region. This is in combination with with the selection of the best
solutions. The selection of the best ensures that the solutions will converge
to the optimality, while the diversification via randomization avoids the
solutions being trapped at local optima and, at the same time, increases
the diversity of the solutions. The good combination of these two major
components will usually ensure that the global optimality is achievable.
Metaheuristic algorithms can be classified in many ways. One way is
to classify them as: population-based and trajectory-based. For example,
genetic algorithms are population-based as they use a set of strings, so
is the particle swarm optimization (PSO) which uses multiple agents or
particles.
On the other hand, simulated annealing uses a single agent or solution
which moves through the design space or search space in a piecewise style.
A better move or solution is always accepted, while a not-so-good move
can be accepted with a certain probability. The steps or moves trace a tra-
jectory in the search space, with a non-zero probability that this trajectory
can reach the global optimum.
Before we introduce all popular meteheuristic algorithms in detail, let
us look at their history briefly.
Xin-She Yang
most of time, but there was no guarantee to find the correct solution,
c Luniver Press
but it was a tremendous success. In 1945, Turing was recruited to the
S ec
)
10
on
d E d i oNational20 Physical Laboratory (NPL), UK where he set out his design for
it n (
6 CHAPTER 1. INTRODUCTION
lgorith m s
Xin-She Yang
learning errors and prediction (capability) errors via iterative trials and
c Luniver Press errors.
S ec
)
10
on
d E d ion (20
it
1.4 A BRIEF HISTORY OF METAHEURISTICS 7
lgorith m s
Xin-She Yang
focus on the quality solutions. Since its development, there have been about
c Luniver 20 Pressdifferent variants of particle swarm optimization techniques, and have
been applied to almost all areas of tough optimization problems. There is
S ec
)
10
o
nd 0
E d ition (2
8 CHAPTER 1. INTRODUCTION
some strong evidence that PSO is better than traditional search algorithms
and even better than genetic algorithms for many types of problems, though
this is far from conclusive.
In around 1996 and later in 1997, R. Storn and K. Price developed their
vector-based evolutionary algorithm, called differential evolution (DE), and
this algorithm proves more efficient than genetic algorithms in many ap-
plications.
In 1997, the publication of the ‘no free lunch theorems for optimization’
by D. H. Wolpert and W. G. Macready sent out a shock way to the opti-
mization community. Researchers have been always trying to find better
algorithms, or even universally robust algorithms, for optimization, espe-
cially for tough NP-hard optimization problems. However, these theorems
state that if algorithm A performs better than algorithm B for some opti-
mization functions, then B will outperform A for other functions. That is
to say, if averaged over all possible function space, both algorithms A and B
will perform on average equally well. Alternatively, there is no universally
better algorithms exist. That is disappointing, right? Then, people real-
ized that we do not need the average over all possible functions for a given
optimization problem. What we want is to find the best solutions, which
has nothing to do with average over all possible function space. In addition,
we can accept the fact that there is no universal or magical tool, but we do
know from our experience that some algorithms indeed outperform others
for given types of optimization problems. So the research now focuses on
finding the best and most efficient algorithm(s) for a given problem. The
objective is to design better algorithms for most types of problems, not for
all the problems. Therefore, the search is still on.
At the turn of the 21st century, things became even more exciting. First,
Zong Woo Geem et al. in 2001 developed the harmony search (HS) algo-
rithm, which has been widely applied in solving various optimization prob-
lems such as water distribution, transport modelling and scheduling. In
2004, S. Nakrani and C. Tovey proposed the honey bee algorithm and its
application for optimizing Internet hosting centers, which followed by the
development of a novel bee algorithm by D. T. Pham et al. in 2005 and the
artificial bee colony (ABC) by D. Karaboga in 2005. In 2008, the author of
this book developed the firefly algorithm (FA)1 . Quite a few research arti-
cles on the firefly algorithm then followed, and this algorithm has attracted
a wide range of interests. In 2009, Xin-She Yang at Cambridge University,
UK, and Suash Deb at Raman College of Engineering, India, introduced
an efficient cuckoo search (CS) algorithm, and it has been demonstrated
ta h e u r is that CS is far more effective than most existing metaheuristic algorithms
d Me ti
i re
cA
p
N a t u re -I n s
lgorith m s
)
10
on
d E d ion (20
it
1.4 A BRIEF HISTORY OF METAHEURISTICS 9
REFERENCES
IEEE Int. Conf. on Neural Networks, Piscataway, NJ, pp. 1942-1948 (1995).
lgorith m s
Xin-She Yang
c Luniver Press
2 Novel cuckoo search ‘beats’ particle swarm optimization, Science Daily, news article
S ec
)
10
on
d E d i o(2820
i t n ( May 2010), www.sciencedaily.com
10 CHAPTER 1. INTRODUCTION
lgorith m s
Xin-She Yang
29. History of optimization, http://hse-econ.fi/kitti/opthist.html
c Luniver Press
30. Turing Archive for the History of Computing, www.alanturing.net/
S ec
)
10
on
d E d ion (20
it
Chapter 2
Xin-She Yang
Different random variables will have different distributions. Gaussian
c Luniver Press
distribution or normal distribution is by far the most popular distribu-
S ec
)
10
on
20
d E d i otions, because many physical variables including light intensity, and er-
it n (
Nature-Inspired Metaheuristic Algorithms, 2nd Edition by Xin-She Yang 11
Copyright c 2010 Luniver Press
12 CHAPTER 2. RANDOM WALKS AND LÉVY FLIGHTS
lgorith m s
Xin-She Yang
N
c Luniver Press X
SN = Xi = X1 + ... + XN , (2.6)
S ec
)
10
on
d E d ion (20 i=1
it
2.2 RANDOM WALKS 13
which means the next state SN will only depend the current existing state
SN −1 and the motion or transition XN from the existing state to the next
state. This is typically the main property of a Markov chain to be intro-
duced later.
Here the step size or length in a random walk can be fixed or varying.
Random walks have many applications in physics, economics, statistics,
computer sciences, environmental science and engineering.
Consider a scenario, a drunkard walks on a street, at each step, he
can randomly go forward or backward, this forms a random walk in one-
dimensional. If this drunkard walks on a football pitch, he can walk in
any direction randomly, this becomes a 2D random walk. Mathematically
speaking, a random walk is given by the following equation
St+1 = St + wt , (2.8)
lgorith m s
Xin-She Yang
where v0 is the drift velocity of the system. Here D = s2 /(2τ ) is the
c Luniver Press
effective diffusion coefficient which is related to the step length s over a
S ec
)
10
on
20
d E d i oshort time interval τ during each jump.
it n (
14 CHAPTER 2. RANDOM WALKS AND LÉVY FLIGHTS
Broadly speaking, Lévy flights are a random walk whose step length is
drawn from the Lévy distribution, often in terms of a simple power-law
formula L(s) ∼ |s|−1−β where 0 < β ≤ 2 is an index. Mathematically
speaking, a simple version of Lévy distribution can be defined as
p γ γ 1
2π exp[− 2(s−µ) ] (s−µ)3/2 , 0 < µ < s < ∞
L(s, γ, µ) = (2.11)
0 otherwise,
r
lgorith m s
γ 1
Xin-She Yang L(s, γ, µ) ≈ . (2.12)
c Luniver Press 2π s3/2
S ec
)
10
on
d E d i o n ( 2 0This is a special case of the generalized Lévy distribution.
it
2.3 LÉVY DISTRIBUTION AND LÉVY FLIGHTS 15
1 γ
p(x, γ, µ) = , (2.16)
π γ 2 + (x − µ)2
where µ is the location parameter, while γ controls the scale of this distri-
bution.
For the general case, the inverse integral
1 ∞
Z
L(s) = cos(ks) exp[−α|k|β ]dk, (2.17)
ta h
d M e e u r is
π 0
i re ti
cA
p
N a t u re -I n s
Xin-She Yang
c Luniver Press
α β Γ(β) sin(πβ/2)
L(s) → , s → ∞. (2.18)
S ec
)
10
on
d E d ion (20 π|s|1+β
it
16 CHAPTER 2. RANDOM WALKS AND LÉVY FLIGHTS
where
Me ta h e u r is
n Γ(1 + β) sin(πβ/2) o1/β
i re d ti σu = , σv = 1. (2.23)
Γ[(1 + β)/2] β 2(β−1)/2
cA
p
N a t u re -I n s
lgorith m s
Xin-She Yang
This distribution (for s) obeys the expected Lévy distribution for |s| ≥ |s0 |
c Luniver Press
where s0 is the smallest step. In principle, |s0 | ≫ 0, but in reality s0 can
S ec
)
10
on
d E d i o n ( 2 0be taken as a sensible value such as s0 = 0.1 to 1.
it
2.4 OPTIMIZATION AS MARKOV CHAINS 17
Studies show that Lévy flights can maximize the efficiency of resource
searches in uncertain environments. In fact, Lévy flights have been observed
among foraging patterns of albatrosses and fruit flies, and spider monkeys.
Even humans such as the Ju/’hoansi hunter-gatherers can trace paths of
Lévy-flight patterns. In addition, Lévy flights have many applications.
Many physical phenomena such as the diffusion of fluorescent molecules,
cooling behavior and noise could show Lévy-flight characteristics under the
right conditions.
St+1 = St + wt , (2.25)
Further research along the route of Markov chains is that the devel-
lgorith m s
Xin-She Yang
opment of the Markov chain Monte Carlo (MCMC) method, which is a
c Luniver Press
class of sample-generating methods. It attempts to directly draw samples
S ec
)
10
on
d E d i ofrom 2 0 some highly complex multi-dimensional distribution using a Markov
it n (
18 CHAPTER 2. RANDOM WALKS AND LÉVY FLIGHTS
chain with known transition probability. Since the 1990s, the Markov chain
Monte Carlo has become a powerful tool for Bayesian statistical analysis,
Monte Carlo simulations, and potentially optimization with high nonlin-
earity.
An important link between MCMC and optimization is that some heuris-
tic and metaheuristic search algorithms such as simulated annealing to be
introduced later use a trajectory-based approach. They start with some ini-
tial (random) state, and propose a new state (solution) randomly. Then,
the move is accepted or not, depending on some probability. There is
strongly similar to a Markov chain. In fact, the standard simulated an-
nealing is a random walk.
Mathematically speaking, a great leap in understanding metaheuristic
algorithms is to view a Markov chain Monte carlo as an optimization pro-
cedure. If we want to find the minimum of an objective function f (θ) at
θ = θ∗ so that f∗ = f (θ∗ ) ≤ f (θ), we can convert it to a target distribution
for a Markov chain
π(θ) = e−βf (θ) , (2.26)
where β > 0 is a parameter which acts as a normalized factor. β value
should be chosen so that the probability is close to 1 when θ → θ∗ . At
θ = θ∗ , π(θ) should reach a maximum π∗ = π(θ∗ ) ≥ π(θ). This requires
that the formulation of L(θ) should be non-negative, which means that
some objective functions can be shifted by a large constant A > 0 such as
f ← f + A if necessary.
By constructing a Markov chain Monte Carlo, we can formulate a generic
framework as outlined by Ghate and Smith in 2008, as shown in Figure 2.3.
In this framework, simulated annealing and its many variants are simply a
special case with
∆f
exp[− Tt ] if ft+1 > ft
Pt = ,
1 if ft+1 ≤ ft
In this case, only the difference ∆f between the function values is impor-
tant.
Algorithms such as simulated annealing, to be discussed in the next
chapter, use a single Markov chain, which may not be very efficient. In
practice, it is usually advantageous to use multiple Markov chains in paral-
lel to increase the overall efficiency. In fact, the algorithms such as particle
swarm optimization can be viewed as multiple interacting Markov chains,
though such theoretical analysis remains almost intractable. The theory of
ta h
d M e eu ris tiinteracting Markov chains is complicated and yet still under development,
i re
cA
p
N a t u re -I n s
however, any progress in such areas will play a central role in the under-
lgorith m s
Xin-She Yang
standing how population- and trajectory-based metaheuristic algorithms
c Luniver Press
perform under various conditions. However, even though we do not fully
S ec
)
10
on
d E d i o n ( 2 0understand why metaheuristic algorithms work, this does not hinder us to
it
2.4 OPTIMIZATION AS MARKOV CHAINS 19
end
use these algorithms efficiently. On the contrary, such mysteries can drive
and motivate us to pursue further research and development in metaheuris-
tics.
REFERENCES
10. W. K. Hastings, Monte Carlo sampling methods using Markov chains and
lgorith m s
)
10
on
d E d i o n ( 2 0 annealing, Science, 220, 670-680 (1983).
it
20 CHAPTER 2. RANDOM WALKS AND LÉVY FLIGHTS
ta h e u r is
d Me ti
i re
cA
p
N a t u re -I n s
lgorith m s
Xin-She Yang
c Luniver Press
S ec
)
10
on
d E d ion (20
it
Chapter 10
FIREFLY ALGORITHM
lgorith m s
Xin-She Yang first outline the basic formulation of the Firefly Algorithm (FA) and then
c Luniver discuss Press the implementation in detail.
S ec
)
10
on
d E d ion (20
it
Nature-Inspired Metaheuristic Algorithms, 2nd Edition by Xin-She Yang 81
Copyright c 2010 Luniver Press
82 CHAPTER 10. FIREFLY ALGORITHM
Firefly Algorithm
Objective function f (x), x = (x1 , ..., xd )T
Generate initial population of fireflies xi (i = 1, 2, ..., n)
Light intensity Ii at xi is determined by f (xi )
Define light absorption coefficient γ
while (t <MaxGeneration)
for i = 1 : n all n fireflies
for j = 1 : n all n fireflies (inner loop)
if (Ii < Ij ), Move firefly i towards j; end if
Vary attractiveness with distance r via exp[−γr]
Evaluate new solutions and update light intensity
end for j
end for i
Rank the fireflies and find the current global best g ∗
end while
Postprocess results and visualization
• All fireflies are unisex so that one firefly will be attracted to other
fireflies regardless of their sex;
Xin-She Yang
defined in a similar way to the fitness function in genetic algorithms.
c Luniver Press
Based on these three rules, the basic steps of the firefly algorithm (FA)
S ec
)
10
on
d E d i o n ( 2 0can be summarized as the pseudo code shown in Figure 11.1.
it
10.3 LIGHT INTENSITY AND ATTRACTIVENESS 83
In the firefly algorithm, there are two important issues: the variation of
light intensity and formulation of the attractiveness. For simplicity, we
can always assume that the attractiveness of a firefly is determined by its
brightness which in turn is associated with the encoded objective function.
In the simplest case for maximum optimization problems, the brightness
I of a firefly at a particular location x can be chosen as I(x) ∝ f (x).
However, the attractiveness β is relative, it should be seen in the eyes
of the beholder or judged by the other fireflies. Thus, it will vary with
the distance rij between firefly i and firefly j. In addition, light intensity
decreases with the distance from its source, and light is also absorbed in
the media, so we should allow the attractiveness to vary with the degree of
absorption.
In the simplest form, the light intensity I(r) varies according to the
inverse square law
Is
I(r) = 2 , (10.1)
r
where Is is the intensity at the source. For a given medium with a fixed
light absorption coefficient γ, the light intensity I varies with the distance
r. That is
I = I0 e−γr , (10.2)
where I0 is the original light intensity. In order to avoid the singularity
at r = 0 in the expression Is /r2 , the combined effect of both the inverse
square law and absorption can be approximated as the following Gaussian
form 2
I(r) = I0 e−γr . (10.3)
As a firefly’s attractiveness is proportional to the light intensity seen by
adjacent fireflies, we can now define the attractiveness β of a firefly by
2
β = β0 e−γr , (10.4)
where β0 is the attractiveness at r = 0. As it is often faster to calculate
1/(1 + r2 ) than an exponential function, the above function, if necessary,
can conveniently be approximated as
β0
β= . (10.5)
1 + γr2
√
Both (10.4) and (10.5) define a characteristic distance Γ = 1/ γ over which
the attractiveness changes significantly from β0 to β0 e−1 for equation (10.4)
M e ta h e u r is
i re
d orti β0 /2 for equation (10.5).
cA
p
N a t u re -I n s
Xin-She Yang
any monotonically decreasing functions such as the following generalized
c Luniver Press
form
S ec
m
10
on
d E d ion (20
it β(r) = β0 e−γr , (m ≥ 1). (10.6)
84 CHAPTER 10. FIREFLY ALGORITHM
where xi,k is the kth component of the spatial coordinate xi of ith firefly.
In 2-D case, we have
q
rij = (xi − xj )2 + (yi − yj )2 . (10.10)
Xin-She Yang
c Luniver Press
It is worth pointing out that the distance r defined above is not limited to
S ec
)
10
on
d E d i o n ( 2 0the Euclidean distance. We can define other distance r in the n-dimensional
it
10.4 SCALINGS AND ASYMPTOTICS 85
lgorith m s
Xin-She Yang
more closely around each optimum. It can be expected that the interactions
c Luniver between Press different subregions are minimal in parallel implementation.
S ec
)
10
on
d E d ion (20
it
86 CHAPTER 10. FIREFLY ALGORITHM
1
5
0
−5 0
0
5 −5
10.5 IMPLEMENTATION
where (x, y) ∈ [−5, 5] × [−5, 5]. This function has four peaks. Two local
peaks with f = 1 at (−4, 4) and (4, 4), and two global peaks with fmax = 2
at (0, 0) and (0, −4), as shown in Figure 10.2. We can see that all these
four optima can be found using 25 fireflies in about 20 generations (see Fig.
10.3). So the total number of function evaluations is about 500. This is
much more efficient than most of existing metaheuristic algorithms.
lgorith m s
str2=‘+2*exp(-x^2-(y+4)^2)+2*exp(-x^2-y^2)’;
Xin-She Yang
c Luniver Press
funstr=strcat(str1,str2);
% Converting to an inline function
S ec
)
10
on
d E d i o n ( 2 0f=vectorize(inline(funstr));
it
10.5 IMPLEMENTATION 87
% ------------------------------------------------
alpha=0.2; % Randomness 0--1 (highly random)
gamma=1.0; % Absorption coefficient
% ------------------------------------------------
% 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);
% 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);
lgorith m s
% -----
Xin-She Yang All subfunctions are listed here ---------
c Luniver % The
Press initial locations of n fireflies
function [xn,yn,Lightn]=init_ffa(n,range)
S ec
)
10
on 0
d E d ion (2
it
88 CHAPTER 10. FIREFLY ALGORITHM
5 5
0 0
−5 −5
−5 0 5 −5 0 5
Figure 10.3: The initial locations of 25 fireflies (left) and their final locations
after 20 iterations (right).
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));
lgorith m s
)
10
on
d E d i o n ( 2 0end
it
10.6 FA VARIANTS 89
10.6 FA VARIANTS
The basic firefly algorithm is very efficient, but we can see that the solutions
are still changing as the optima are approaching. It is possible to improve
the solution quality by reducing the randomness.
A further improvement on the convergence of the algorithm is to vary
the randomization parameter α so that it decreases gradually as the optima
are approaching. For example, we can use
where t ∈ [0, tmax ] is the pseudo time for simulations and tmax is the max-
imum number of generations. α0 is the initial randomization parameter
while α∞ is the final value. We can also use a similar function to the
geometrical annealing schedule. That is
α = α0 θt , (10.13)
d Me
ta h e u r is
ti minimize f (x) = x21 x2 (2 + x3 ), (10.14)
i re
cA
p
N a t u re -I n s
lgorith m s
Xin-She Yang
subject to the following constraints
c Luniver Press
x32 x3
g1 (x) = 1 − ≤ 0,
S ec
)
10
on
d E d ion
it (20 71785x41
90 CHAPTER 10. FIREFLY ALGORITHM
4x22 − x1 x2 1
g2 (x) = + − 1 ≤ 0,
12566(x31 x2 − x41 ) 5108x21
140.45x1
g3 (x) = 1 − ≤ 0,
x22 x3
x1 + x2
g4 (x) =
− 1 ≤ 0. (10.15)
1.5
The simple bounds on the design variables are
The best solution found in the literature (e.g., Cagnina et al. 2008) is
% -------------------------------------------------------%
% Firefly Algorithm for constrained optimization %
% by Xin-She Yang (Cambridge University) Copyright @2009 %
% -------------------------------------------------------%
function fa_mincon_demo
% Simple bounds/limits
disp(’Solve the simple spring design problem ...’);
Lb=[0.05 0.25 2.0];
Ub=[2.0 1.3 15.0];
ta h e u r is
d Me ti
i re % Initial random guess
cA
p
N a t u re -I n s
lgorith m s
u0=(Lb+Ub)/2;
Xin-She Yang
c Luniver Press
[u,fval,NumEval]=ffa_mincon(@cost,@constraint,u0,Lb,Ub,para);
S ec
)
10
on
d E d ion (20
it
10.7 SPRING DESIGN 91
% Display results
bestsolution=u
bestojb=fval
total_number_of_function_evaluations=NumEval
%%% --------------------------------------------------%%%
%%% Do not modify the following codes unless you want %%%
%%% to improve its performance etc %%%
% -------------------------------------------------------
% ===Start of the Firefly Algorithm Implementation ======
% Inputs: fhandle => @cost (your own cost function,
% can be an external file )
% nonhandle => @constraint, all nonlinear constraints
% can be an external file or a function
% Lb = lower bounds/limits
% Ub = upper bounds/limits
% para == optional (to control the Firefly algorithm)
% Outputs: nbest = the best solution found so far
d M eta h e%
u r is fbest = the best objective value
i re ti
cA
p
lgorith m s
)
10
on 0
d E d ion (2
it
92 CHAPTER 10. FIREFLY ALGORITHM
% Start FA
function [nbest,fbest,NumEval]...
=ffa_mincon(fhandle,nonhandle,u0, Lb, Ub, para)
% Check input parameters (otherwise set as default values)
if nargin<6, para=[20 50 0.25 0.20 1]; end
if nargin<5, Ub=[]; end
if nargin<4, Lb=[]; end
if nargin<3,
disp(’Usuage: FA_mincon(@cost, @constraint,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);
% 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);
alpha=alpha_new(alpha,MaxGeneration);
N a t u re -I n s
lgorith m s
Xin-She Yang
%
c Luniver Press Evaluate new solutions (for all n fireflies)
for i=1:n,
S ec
)
10
on 0
d E d ion (2
it
10.7 SPRING DESIGN 93
zn(i)=Fun(fhandle,nonhandle,ns(i,:));
Lightn(i)=zn(i);
end
% -------------------------------------------------------
% ----- 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
ta h e%
d Me u ris Move all fireflies toward brighter ones
ti
i re
cA
p
function [ns]=ffa_move(n,d,ns,Lightn,nso,Lighto,...
N a t u re -I n s
lgorith m s
)
10
on 0
d E d ion (2
it
94 CHAPTER 10. FIREFLY ALGORITHM
% 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;
tmf=alpha.*(rand(1,d)-0.5).*scale;
ns(i,:)=ns(i,:).*(1-beta)+nso(j,:).*beta+tmpf;
end
end % end for j
% -----------------------------------------
N a t u re -I n s
lgorith m s
0% Objective
S ec
)
10
on
d E d ion (2
it
10.7 SPRING DESIGN 95
z=fhandle(u);
function Z=getnonlinear(nonhandle,u)
Z=0;
% Penalty constant >> 1
lam=10^15; lameq=10^15;
% Get nonlinear constraints
[g,geq]=nonhandle(u);
REFERENCES
Me ta h e u r is
i re d ti
cA
p
lgorith m s
Xin-She Yang
c Luniver Press
2. L. C. Cagnina, S. C. Esquivel, C. A. Coello, Solving engineering optimization
problems with the simple constrained particle swarm optimizer, Informatica,
S ec
)
10
on
d E d i o n ( 2 0 32, 319-326 (2008).
it
96 CHAPTER 10. FIREFLY ALGORITHM
ta h e u r is
d Me ti
i re
cA
p
N a t u re -I n s
lgorith m s
Xin-She Yang
c Luniver Press
S ec
)
10
on
d E d ion (20
it
144 REFERENCES
52. Sawaragi Y., Nakayama H., Tanino T., Theory of Multiobjective Optimisa-
tion, Academic Press, (1985).
53. Schrijver A., On the history of combinatorial optimization (till 1960), in:
Handbook of Discrete Optimization (Eds K. Aardal, G. L. Nemhauser, R.
Weismantel), Elsevier, Amsterdam, p.1-68 (2005).
54. Sirisalee P., Ashby M. F., Parks G. T., and Clarkson P. J.: Multi-criteria
material selection in engineering design, Adv. Eng. Mater., 6, 84-92 (2004).
55. Siegelmann H. T. and Sontag E. D., Turing computability with neural nets,
Appl. Math. Lett., 4, 77-80 (1991).
56. Seeley T. D., The Wisdom of the Hive, Harvard University Press, (1995).
57. Seeley T. D., Camazine S., Sneyd J., Collective decision-making in honey
bees: how colonies choose among nectar sources, Behavioural Ecology and
Sociobiology, 28, 277-290 (1991).
58. Spall J. C., Introduction to Stochastic Search and optimization: Estimation,
Simulation, and Control, Wiley, Hoboken, NJ, (2003).
59. Storn R., On the usage of differential evolution for function optimization,
Biennial Conference of the North American Fuzzy Information Processing
Society (NAFIPS), pp. 519-523 (1996).
60. Storn R., web pages on differential evolution with various programming
codes, http://www.icsi.berkeley.edu/∼storn/code.html
61. Storn R. and Price K., Differential evolution - a simple and efficient heuristic
for global optimization over continuous spaces, Journal of Global Optimiza-
tion, 11, 341-359 (1997).
62. Swarm intelligence, http://www.swarmintelligence.org
63. Talbi E. G., Metaheuristics: From Design to Implementation, Wiley, (2009).
64. Vapnik V., The Nature of Statistical Learning Theory, Springer, (1995).
65. Wolpert D. H. and Macready W. G., No free lunch theorems for optimiza-
tion, IEEE Trans. on Evol. Computation, 1, 67-82 (1997).
66. Wikipedia, http://en.wikipedia.org
67. Yang X. S., Engineering optimization via nature-inspired virtual bee algo-
rithms, IWINAC 2005, Lecture Notes in Computer Science, 3562, 317-323
(2005).
68. Yang X. S., Biology-derived algorithms in engineering optimization (Chap-
ter 32), in Handbook of Bioinspired Algorithms, edited by Olariu S. and
Zomaya A., Chapman & Hall / CRC, (2005).
69. Yang X. S., New enzyme algorithm, Tikhonov regularization and inverse
ta h e u r is
d Me ti parabolic analysis, in: Advances in Computational Methods in Science and
i re
cA
p
lgorith m s
Xin-She Yang
c Luniver Press
70. Yang X. S., Lees J. M., Morley C. T.: Application of virtual ant algorithms
in the optimization of CFRP shear strengthened precracked structures, Lec-
S ec
)
10
on
d E d ion (20 ture Notes in Computer Sciences, 3991, 834-837 (2006).
it
REFERENCES 145
71. Yang X. S., Firefly algorithms for multimodal optimization, 5th Symposium
on Stochastic Algorithms, Foundations and Applications, SAGA 2009, Eds.
O. Watanabe & T. Zeugmann, LNCS, 5792, 169-178(2009).
72. Yang X. S. and Deb S., Cuckoo search via Lévy flights, in: Proc. of World
Congress on Nature & Biologically Inspired Computing (NaBic 2009), IEEE
Publications, USA, pp. 210-214 (2009).
73. Yang X. S. and Deb S., Engineering optimization by cuckoo search, Int. J.
Math. Modelling & Num. Optimization, 1, 330-343 (2010).
74. Yang X. S., A new metaheuristic bat-inspired algorithm, in: Nature Inspired
Cooperative Strategies for Optimization (NICSO 2010) (Eds. J. R. Gonzalez
et al.), Springer, SCI 284, 65-74 (2010).
75. Yang X. S. and Deb S., Eagle strategy using Lévy walk and firefly algorithms
for stochastic optimization, in: Nature Inspired Cooperative Strategies for
Optimization (NICSO 2010) (Eds. J. R. Gonzalez et al.), Springer, SCI 284,
101-111 (2010).
ta h e u r is
d Me ti
i re
cA
p
N a t u re -I n s
lgorith m s
Xin-She Yang
c Luniver Press
S ec
)
10
on
d E d ion (20
it
ta h e u r is
d Me ti
i re
cA
p
N a t u re -I n s
lgorith m s
Xin-She Yang
c Luniver Press
S ec
)
10
on
d E d ion (20
it
INDEX
constraint, 1 fractal, 16
N a t u re -I n s
lgorith m s
)
10
on
d E d i ocross 2 0 entropy, 136 genetic algorithm, 9, 41, 42, 75
it n (
Nature-Inspired Metaheuristic Algorithms, 2nd Edition by Xin-She Yang 147
Copyright c 2010 Luniver Press
148 INDEX
lgorith m s
Xin-She Yang
particle
c Luniver Press swarm optimization, 5, 9, 63,
66, 73
S ec
)
10
on
d E d i o n ( 2 0pheromone, 53, 56, 57
it