Firefly Algorithm
Firefly Algorithm (FA)
- Year introduced
- 2009
- Original authors
- Xin-She Yang
- Source of inspiration
- The flashing light of fireflies
- Search space
- Continuous
- Optimization types
- Hyperparameter Optimization, Feature Selection
- Applied research in the repository
- 3
Overview
Each firefly moves towards every brighter firefly with an attraction that decays exponentially with the squared distance, plus a small random term. The local attraction allows subgroups to form around several peaks.
Source of inspiration and mechanism

Less bright fireflies are attracted to brighter ones, and the light weakens with distance because of absorption. Brightness corresponds to the objective function value.
Initialization
Random positions.
Update mechanism
Pairwise comparisons; a firefly may move several times in one iteration.
Exploration
The random term αε and the weak attraction over long distances.
Exploitation
Attraction to the brightest in the neighbourhood.
Mathematical description
The attraction β is a decreasing function of the distance r; the absorption coefficient γ sets the visibility range.
Attraction
\beta(r) = \beta_0\, e^{-\gamma r^{2}}Movement
x_i \leftarrow x_i + \beta_0 e^{-\gamma r_{ij}^{2}}\,(x_j - x_i) + \alpha\,\epsilon_iε is a random vector (uniform or Gaussian), α the randomization parameter.
Computational complexity: O(T\cdot n^{2}\cdot (D + C_f))
Pseudocode
Initialize fireflies; light intensity I_i = f(x_i)
for t = 1..T:
for i in 1..n:
for j in 1..n:
if I_j better than I_i:
move x_i towards x_j ; evaluate I_i
reduce alpha (optional)
return bestPython implementation
This code was actually run while building the lab (NumPy) on the sphere function to make sure it works; this is a run test, not a performance claim.
import numpy as np
def firefly(f, lb, ub, dim, n=25, iters=200, alpha=0.2, beta0=1.0, gamma=None, seed=0):
"""Firefly Algorithm (Yang, 2009/2010): brighter fireflies attract dimmer ones."""
rng = np.random.default_rng(seed)
scale = ub - lb
gamma = gamma if gamma is not None else 1.0 / scale ** 2 # scale-aware absorption
x = rng.uniform(lb, ub, (n, dim)); I = np.apply_along_axis(f, 1, x)
curve = []
for t in range(iters):
a = alpha * (0.97 ** t) # gradually reduce randomness
for i in range(n):
for j in range(n):
if I[j] < I[i]: # j is brighter (minimisation)
r2 = np.sum((x[i] - x[j]) ** 2)
beta = beta0 * np.exp(-gamma * r2)
x[i] = x[i] + beta * (x[j] - x[i]) + a * scale * (rng.random(dim) - 0.5)
x[i] = np.clip(x[i], lb, ub); I[i] = f(x[i])
curve.append(I.min())
b = I.argmin()
return x[b], I[b], curve
if __name__ == "__main__":
sphere = lambda z: float(np.sum(z ** 2))
best, val, curve = firefly(sphere, -100, 100, dim=10, iters=100)
print("best fitness:", val)
Coefficients
| Parameter | Symbol | Typical value | Range | Description |
|---|---|---|---|---|
| Attractiveness | β₀ | 1 | 0.5 – 1 | Attraction at r=0 |
| Absorption | γ | 1/L² | 0.01 – 10 | Visibility range (L is the problem scale) |
| Randomization | α | 0.2 | 0.01 – 0.5 | Random step size |
Advantages
- Suitable for multimodal functions thanks to subgroups
- Includes PSO as a special case when γ→0.
Limitations
- Quadratic cost in population size
- Very sensitive to the value of γ and the problem scale.
Use in artificial intelligence
Domains documented in the card: Machine learning, Feature Selection, hyperparameter tuning
Multimodal functions, engineering design, low-dimensional parameter tuning.
Related models in the relationship table
- XGBoost — Hyperparameter optimization Published result
Hybridization potential
- DE — Differential Evolution Design this hybrid
- GA — Genetic Algorithm Design this hybrid
- PSO — Particle Swarm Optimization Design this hybrid
These are partnerships suggested theoretically in the card; any resulting design is presented as a "proposed hybrid method" until its results are compared with the literature.
Applied research from the repository
- Exposing the grey wolf, moth-flame, whale, firefly, bat, and antlion algorithms: six misleading optimization techniques inspired by bestial metaphors Verified
- Novel hybrid firefly algorithm: an application to enhance XGBoost tuning for intrusion detection classification Partially verified
- Grey Wolf, Firefly and Bat Algorithms: Three Widespread Algorithms that Do Not Contain Any Novelty Verified
Original reference
Xin-She Yang (2009). Firefly Algorithms for Multimodal Optimization. Lecture Notes in Computer Science (SAGA 2009), Springer Berlin Heidelberg.
Verified DOI: 10.1007/978-3-642-04944-6_14
