🔥 Firefly Algorithm (FA) – Overview
- Adisorn O.
- Aug 3
- 2 min read
Inspired by: Bioluminescent communication of fireflies.Inventor: Xin-She Yang (2008).
Core Idea:
Each firefly is attracted to brighter fireflies.
Brightness is associated with the objective function (fitness).
Movement is influenced by attractiveness and a randomness factor.
✨ Key Mechanisms
Attractiveness (β): Decreases with distance.
Movement: Less bright fireflies move toward brighter ones.
Randomness (α): Adds stochasticity to avoid local optima.
Distance Measure: Typically Euclidean.
🔧 Parameters
α: Randomness strength.
β0: Base attractiveness.
γ: Light absorption coefficient (controls how light fades with distance).
n: Number of fireflies.
📌 MATLAB Code: Firefly Algorithm (Minimization Example)
We solve a benchmark sphere function:
f(x)=∑xi^2
% Firefly Algorithm for Sphere Function
clc; clear; close all;
% Problem settings
d = 5; % Dimension
n = 20; % Fireflies
MaxGen = 100; % Max generations
Lb = -10 * ones(1,d); % Lower bounds
Ub = 10 * ones(1,d); % Upper bounds
% Firefly Algorithm parameters
alpha = 0.2; % Randomness 0--1
beta0 = 1; % Initial attractiveness
gamma = 1; % Light absorption coefficient
% Initialize population
x = rand(n,d).*(Ub - Lb) + Lb; % Firefly positions
f = sum(x.^2, 2); % Objective (sphere)
% Record best
[bestF, idx] = min(f);
bestX = x(idx,:);
% Main loop
for gen = 1:MaxGen
for i = 1:n
for j = 1:n
if f(j) < f(i)
r = norm(x(i,:) - x(j,:));
beta = beta0 * exp(-gamma * r^2);
x(i,:) = x(i,:) + beta*(x(j,:) - x(i,:)) + ...
alpha*(rand(1,d)-0.5).*(Ub - Lb);
% Keep within bounds
x(i,:) = max(min(x(i,:), Ub), Lb);
f(i) = sum(x(i,:).^2);
end
end
end
% Update best
[currentBestF, idx] = min(f);
if currentBestF < bestF
bestF = currentBestF;
bestX = x(idx,:);
end
% Optional: plot convergence
disp(["Generation", gen, "Best f(x)", bestF])
end
fprintf('\nBest Solution: f(x) = %.6f\n', bestF);
🪐 Comparison: Firefly Algorithm (FA) vs Particle Swarm Optimization (PSO)
Feature | Firefly Algorithm (FA) | Particle Swarm Optimization (PSO) |
Inspired by | Flashing behavior of fireflies | Social behavior of bird flocks or fish schools |
Interaction Style | One-way (less bright → more bright) | Two-way social sharing (individual & global best) |
Brightness concept | Directly linked to objective value | Not explicitly used; instead, personal & global bests |
Movement Equation | Attraction-based + random | Velocity-based + inertia + cognitive + social terms |
Convergence Speed | Slower but more explorative | Faster in convergence but may get trapped in local optima |
Randomness | Explicit (controlled by α) | Implicit (via stochastic velocity updates) |
Exploration/Exploitation | Good balance with tunable α, β, γ | Controlled by inertia weight |
Population Diversity | High due to selective attraction | Moderate – swarm may collapse around global best |
Best Use Cases | Complex, multimodal, high-dimensional problems | Well-understood convex or mildly nonconvex functions |
🎯 When to Use Which?
Situation | Recommended |
Want more exploration, especially early? | Firefly Algorithm |
Require fast convergence on convex/low-dimensional problem? | PSO |
Seeking swarm-based global search? | Both are fine, PSO simpler |
Highly multimodal objective landscape? | FA preferred due to richer dynamics |
💡 Suggestions for Structural Engineering Applications
PMM optimization
Pile cap design
CFRP layout
Structural member grouping
Both FA and PSO are viable. But:
PSO suits continuous design variable optimization (like reinforcement ratios, dimension tweaks).
FA can help in layout problems (pile placement, CFRP anchoring), where distance-based interaction metaphor fits better.

