# Firefly algorithm

In mathematical optimization, the firefly algorithm is a metaheuristic proposed by Xin-She Yang and inspired by the flashing behaviour of fireflies.[1]

Nature-inspired metaheuristics in general have started to attract criticism in the research community for hiding their lack of novelty behind an elaborate metaphor.[2][3][4][5][6] In response, Springer's Journal of Heuristics has updated their editorial policy to state that:[7]

Implementations should be explained by employing standard optimization terminology, where a solution is called a "solution" and not something else related to some obscure metaphor (e.g., harmony, flies, bats, countries, etc.).

The firefly algorithm specifically has been criticized as differing from the well-established particle swarm optimization only in a negligible way.[8]

## Metaphor

The primary purpose for a firefly's flash is to act as a signal system to attract other fireflies. Xin-She Yang formulated this firefly algorithm by assuming:

1. All fireflies are unisexual, so that any individual firefly will be attracted to all other fireflies;
2. Attractiveness is proportional to their brightness, and for any two fireflies, the less bright one will be attracted by (and thus move towards) the brighter one; however, the intensity (apparent brightness) decrease as their mutual distance increases;
3. If there are no fireflies brighter than a given firefly, it will move randomly.

The brightness should be associated with the objective function.

## Algorithm

In pseudocode the algorithm can be stated as:

Begin
1) Objective function: ${\displaystyle f(\mathbf {x} ),\quad \mathbf {x} =(x_{1},x_{2},...,x_{d})}$;
2) Generate an initial population of fireflies ${\displaystyle \mathbf {x} _{i}\quad (i=1,2,\dots ,n)}$;.
3) Formulate light intensity I so that it is associated with ${\displaystyle f(\mathbf {x} )}$
(for example, for maximization problems, ${\displaystyle I\propto f(\mathbf {x} )}$ or simply ${\displaystyle I=f(\mathbf {x} )}$;)
4) Define absorption coefficient γ

While (t < MaxGeneration)
for i = 1 : n (all n fireflies)
for j = 1 : n (n fireflies)
if (${\displaystyle I_{j}>I_{i}}$),
move firefly i towards j;
Vary attractiveness with distance r via ${\displaystyle \exp(-\gamma \;r)}$;
Evaluate new solutions and update light intensity;
end if
end for j
end for i
Rank fireflies and find the current best;
end while

Post-processing the results and visualization;

end


The main update formula for any pair of two fireflies ${\displaystyle \mathbf {x} _{i}}$ and ${\displaystyle \mathbf {x} _{j}}$ is

${\displaystyle \mathbf {x} _{i}^{t+1}=\mathbf {x} _{i}^{t}+\beta \exp[-\gamma r_{ij}^{2}](\mathbf {x} _{j}^{t}-\mathbf {x} _{i}^{t})+\alpha _{t}{\boldsymbol {\epsilon }}_{t}}$

where ${\displaystyle \alpha _{t}}$ is a parameter controlling the step size, while ${\displaystyle {\boldsymbol {\epsilon }}_{t}}$ is a vector drawn from a Gaussian or other distribution.

It can be shown that the limiting case ${\displaystyle \gamma \rightarrow 0}$ corresponds to the standard Particle Swarm Optimization (PSO). In fact, if the inner loop (for j) is removed and the brightness ${\displaystyle I_{j}}$ is replaced by the current global best ${\displaystyle g^{*}}$, then FA essentially becomes the standard PSO.