Skip to content

Firefly Algorithm (FA)

A swarm method for continuous problems, designed for functions with many local optima.

FA

FA(*, gamma=1.0, beta_base=2.0, alpha=0.2, alpha_damp=0.99, delta=0.05, m=2.0, **kwargs)

Firefly Algorithm (Yang, 2009 and 2010).

Every firefly moves toward each brighter (better) firefly in turn. The attractiveness of firefly j seen from firefly i is beta = beta_base * exp(-gamma * r ** m), where r is their distance in code space divided by sqrt(d), the largest possible distance. One move is

x_i = x_i + beta * U(0, 1) * (x_j - x_i) + alpha * delta * U(-1, 1)

with U drawn for every component. The brightest firefly has no brighter neighbor and makes a random walk, x_i = x_i + alpha * delta * U(-1, 1). Brightness is the rank given by the comparator at the start of the iteration, so maximization and constraints work. After all moves, each firefly is evaluated once, so an iteration costs pop_size evaluations. The new fireflies are merged with the old ones and the best pop_size are kept. alpha is multiplied by alpha_damp after every iteration.

Parameters

  • gamma: light absorption coefficient
  • beta_base: attractiveness at distance zero
  • alpha: initial randomization (mutation) coefficient
  • alpha_damp: damping factor of alpha per iteration
  • delta: scale of the random step alpha * delta * U(-1, 1) (code units)
  • m: exponent of the distance in the attractiveness

With the default beta_base = 2 the mean attraction is 1, as with beta_0 = 1 in the paper.

References: X.-S. Yang, "Firefly algorithms for multimodal optimization", Stochastic Algorithms: Foundations and Applications (SAGA 2009), LNCS 5792, pp. 169 to 178. X.-S. Yang, "Nature-Inspired Metaheuristic Algorithms", 2nd edition, Luniver Press, 2010.

Example

import ypea

problem = ypea.Problem(
    ypea.Real("x", shape=20, lb=-10, ub=10),
    lambda sol: ypea.benchmarks.sphere(sol.x),
    vectorized=True,
    name="sphere",
)

alg = ypea.FA(max_nfe=20_000)
result = alg.solve(problem, seed=1)
print(result.summary())
FA on sphere
  best objective value: 0.00716182
  feasible: True (violation 0)
  evaluations: 20000, iterations: 199, time: 0.688 s, stopped by: max_nfe

Differs from Python: MATLAB draws its own random numbers, so the best value differs from Python's.

problem = ypea.Problem( ...
    vars=ypea.Real("x", shape=20, lb=-10, ub=10), ...
    objective=@(sol) ypea.benchmarks.sphere(sol.x), ...
    vectorized=true, ...
    name="sphere");

alg = ypea.FA(max_nfe=20000);
result = alg.solve(problem, seed=1);
disp(result.summary())
FA on sphere
  best objective value: 0.00746448
  feasible: true (violation 0)
  evaluations: 20000, iterations: 199, time: 0.582 s, stopped by: max_nfe

Search on Rastrigin

FA solutions on Rastrigin at four iterations, and its convergence
FA with default settings, 50 iterations on Rastrigin in two dimensions (seed 1). The red points are the fireflies. The open diamond is the best solution so far, and the plus sign the global minimum.