The Bee Algorithm and Artificial Bee Colony
A honeybee that finds a rich flower patch returns to the hive and performs a waggle dance — a figure-eight movement whose duration encodes distance and whose axis encodes direction. Competing bees observe the dance, follow the encoded bearing, and in turn recruit more followers. The collective result, after millions of years of evolution, is a near-optimal distributed search algorithm that a colony of 50,000 bees runs every day. This article shows how that biological mechanism was distilled into computer code.
1. The biology of honeybee foraging
Honeybee colonies optimise foraging collectively through stigmergic communication: bees modify the shared environment (the hive's dance floor) in ways that influence the behaviour of others. The colony must efficiently allocate foragers among multiple food sources whose quality and distance vary continuously.
A foraging trip has three phases. First, an unemployed bee — one with no information about active food sources — chooses between two options: become a scout (explore a random location) or follow a dancer (exploit a known source). Second, once at a source, a recruited bee harvests nectar, assessing the quality (nectar concentration × volume per visit). Third, back at the hive, a bee either returns to the same source silently (employed forager), dances to advertise the source, or abandons the source and re-enters the unemployed pool.
The dance floor acts as a continuous auction: high-quality sources attract more followers, which attract more followers, creating positive feedback. But exploitation is balanced against exploration — scouts continuously prospect new areas. This tension between exploitation and exploration is the same optimisation trade-off that all modern metaheuristics try to balance.
2. The waggle dance — encoding information
Karl von Frisch decoded the waggle dance in a series of experiments from 1943 to 1973, for which he received the 1973 Nobel Prize in Physiology or Medicine. The dance is performed on the vertical comb inside the dark hive, and encodes two pieces of information simultaneously:
- Distance is encoded by the duration (more precisely, the number of waggles) of the central waggle run. Each 75 ms of waggle run corresponds to approximately 100 metres of flight distance.
- Direction is encoded by the angle of the waggle run relative to vertical. If the waggle run points straight up, the food is in the direction of the sun. If it points 40° left of vertical, the food is 40° left of the sun's current azimuth.
The number of dance circuits a bee performs is proportional to the (logarithm of the) relative quality of the source. A bee visiting a poorer source will dance for fewer circuits after a return trip, and eventually stop dancing — naturally removing the source from the advertisement pool. This is the natural analogue of ABC's "abandonment" mechanism.
Recruits from the dance are not perfect copies — they approach the encoded location with a Gaussian angular error of approximately ±10° and a distance error of ±15%. This noise provides the biological equivalent of a mutation operator, preventing the entire swarm from converging to a single point.
3. Three bee roles in ABC
The Artificial Bee Colony algorithm (Karaboga, 2005) models the hive using three populations that correspond directly to biological roles:
The key structural balance: the number of employed bees equals the number of food sources (solutions). Each employed bee is responsible for exactly one solution at any time, ensuring that the exploitation pressure is spread across the entire pool.
4. The Artificial Bee Colony algorithm
ABC is an iterative algorithm. One cycle consists of three sequential phases. The total population is SN (swarm size), with SN/2 employed bees and SN/2 onlookers.
Phase 1: Employed bee phase
Each employed bee i generates a new candidate solution vi by perturbing its current source xi along a randomly chosen dimension j, using a random neighbour k ≠ i:
Phase 2: Onlooker bee phase
Each onlooker selects a source i with probability proportional to fitness and then performs the same perturbation step as an employed bee on the chosen source.
Phase 3: Scout bee phase
Any source i whose trial counter reaches the limit parameter is abandoned. The employed bee associated with it becomes a scout and generates a completely random new source within the search bounds:
Memory update
After each cycle, the global best solution found so far is updated. The algorithm terminates after a fixed number of cycles (MCN) or when the improvement over the last K cycles falls below a threshold.
5. Mathematical formulation
ABC maintains SN/2 food source positions xi ∈ ℝᴰ where D is the problem dimension. The search for a function f: ℝᴰ → ℝ is global optimisation:
Convergence properties
ABC does not have a formal convergence proof for general non-convex functions, but is known to exhibit stochastic convergence to the global optimum with probability 1 as MCN → ∞, given that the limit parameter allows sufficient exploration. Empirically, ABC achieves competitive results on benchmark functions (Sphere, Rosenbrock, Ackley, Griewank, Rastrigin) and consistently outperforms standard PSO on multimodal functions.
| Parameter | Typical value | Effect |
|---|---|---|
| SN (swarm size) | 40–100 | More sources = slower but more thorough search |
| limit | SN/2 × D | Higher = more exploitation before abandonment |
| MCN (max cycles) | 500–5000 | Stopping criterion; problem-dependent |
| D (dimensions) | 2–100 | Problem dimension; scalability degrades above ~30 |
6. Parameter tuning and convergence
ABC has fewer parameters than most other metaheuristics: swarm size SN, limit, and maximum cycle count MCN. The recommended default for limit is SN × D / 2 (Karaboga and Basturk, 2007). This ensures that each food source is visited approximately D × SN / 2 times before being abandoned, providing enough local search in each dimension.
Common failure modes
- limit too small: Sources are abandoned before local minima are properly exploited; the algorithm behaves like a random restart.
- limit too large: Stale, low-fitness sources persist for too long; diversity collapses and the algorithm stagnates in local optima.
- SN too small: Insufficient diversity; early premature convergence.
- SN/2 not divisible: By convention, if SN is odd, use floor(SN/2) employed bees and floor(SN/2) onlookers; scouts are a switching state, not a fixed population.
Modifications for constrained problems
For constrained optimisation, replace the fitness function with a penalty approach or use feasibility rules: always prefer feasible solutions over infeasible ones regardless of objective value; among infeasible solutions prefer less constrained violation. The scout phase generates random solutions that are clipped to bounds, which already handles box constraints automatically.
7. JavaScript implementation
// Artificial Bee Colony (ABC) — minimisation
// Example: minimise f(x) = Σ x[i]² (Sphere function)
const D = 10; // problem dimension
const SN = 40; // swarm size (must be even)
const LIMIT = SN / 2 * D; // abandonment threshold
const MCN = 500; // max cycles
const lb = Array(D).fill(-5.12); // lower bounds
const ub = Array(D).fill( 5.12); // upper bounds
// Objective function (minimisation)
function f(x) {
return x.reduce((s, xi) => s + xi * xi, 0);
}
// Fitness (higher = better, mapped from objective)
function fitness(obj) {
return obj >= 0 ? 1 / (1 + obj) : 1 + Math.abs(obj);
}
// Initialise population
function randomSource() {
return Array.from({ length: D }, (_, j) =>
lb[j] + Math.random() * (ub[j] - lb[j])
);
}
function runABC() {
const nEmp = SN / 2;
let sources = Array.from({ length: nEmp }, randomSource);
let objs = sources.map(f);
let trials = new Array(nEmp).fill(0);
let bestX = sources[0].slice();
let bestObj = objs[0];
objs.forEach((o, i) => { if (o < bestObj) { bestObj = o; bestX = sources[i].slice(); } });
function perturbSource(i) {
// Pick random dimension j and random neighbour k ≠ i
const j = Math.floor(Math.random() * D);
let k;
do { k = Math.floor(Math.random() * nEmp); } while (k === i);
const phi = (Math.random() * 2 - 1); // φ ∈ [-1, 1]
const v = sources[i].slice();
v[j] = sources[i][j] + phi * (sources[i][j] - sources[k][j]);
// Clip to bounds
v[j] = Math.min(ub[j], Math.max(lb[j], v[j]));
const newObj = f(v);
if (newObj < objs[i]) {
sources[i] = v;
objs[i] = newObj;
trials[i] = 0;
if (newObj < bestObj) { bestObj = newObj; bestX = v.slice(); }
} else {
trials[i]++;
}
}
for (let cycle = 0; cycle < MCN; cycle++) {
// --- Employed bee phase ---
for (let i = 0; i < nEmp; i++) perturbSource(i);
// --- Onlooker bee phase ---
const fits = objs.map(o => fitness(o));
const sumF = fits.reduce((a, b) => a + b, 0);
const probs = fits.map(f => f / sumF);
// Cumulative roulette selection
for (let b = 0; b < nEmp; b++) {
let r = Math.random(), chosen = 0, cumP = 0;
for (let i = 0; i < nEmp; i++) {
cumP += probs[i];
if (r < cumP) { chosen = i; break; }
}
perturbSource(chosen);
}
// --- Scout bee phase ---
for (let i = 0; i < nEmp; i++) {
if (trials[i] >= LIMIT) {
sources[i] = randomSource();
objs[i] = f(sources[i]);
trials[i] = 0;
}
}
}
return { bestX, bestObj };
}
const result = runABC();
console.log('Best value:', result.bestObj.toFixed(8));
// For Sphere D=10: typically converges to < 1e-6 within 500 cycles
Visualising convergence
// Track best-per-cycle history for plotting
function runABCWithHistory() {
// ... (same setup as above, add:)
const history = [];
for (let cycle = 0; cycle < MCN; cycle++) {
// ... employed, onlooker, scout phases ...
history.push(bestObj); // record best after each cycle
}
return { bestObj, history };
}
// Draw convergence curve on canvas
function plotHistory(canvas, history) {
const ctx = canvas.getContext('2d');
const W = canvas.width, H = canvas.height;
const maxVal = Math.log10(history[0] + 1e-10);
const minVal = Math.log10(history[history.length - 1] + 1e-10);
const range = maxVal - minVal || 1;
ctx.strokeStyle = '#10b981';
ctx.lineWidth = 2;
ctx.beginPath();
history.forEach((v, i) => {
const x = (i / (history.length - 1)) * W;
const y = H - ((Math.log10(v + 1e-10) - minVal) / range) * H;
i === 0 ? ctx.moveTo(x, y) : ctx.lineTo(x, y);
});
ctx.stroke();
}
8. Comparison with PSO and ACO
All three major swarm intelligence families — Particle Swarm Optimisation, Ant Colony Optimisation, and Artificial Bee Colony — were inspired by social insects but use different strategies:
| Algorithm | Inspiration | Search mechanism | Exploration control | Best domain |
|---|---|---|---|---|
| PSO | Bird flocking | Velocity + inertia + social/individual bests | Inertia weight / constriction factor | Smooth continuous unimodal functions |
| ACO | Ant pheromones | Pheromone-guided probabilistic path construction | Evaporation rate | Combinatorial problems (TSP, VRP, scheduling) |
| ABC | Honeybee foraging | Neighbour-guided perturbation + roulette onlooker | Limit counter → scout phase | Multimodal continuous optimisation |
The key advantage of ABC over PSO is the explicit abandonment mechanism: exhausted sources are discarded and replaced with random scouts, preventing premature convergence that plagues basic PSO on multimodal functions. PSO address this with topologies (ring, random), but ABC's mechanism is more principled.
The key advantage of PSO over ABC is per-evaluation efficiency on smooth problems: PSO maintains velocity vectors that give particles momentum, enabling faster convergence on bowl-shaped or weakly multimodal functions. ABC's random-neighbour perturbation has no memory of past movement direction.
For combinatorial problems, ACO remains dominant because its pheromone matrix naturally represents discrete solution components — a representation that does not exist in continuous-space algorithms like ABC or PSO.