Swarm Algorithms
April 2026 · 13 min read · Swarm Intelligence · Optimisation · Nature-Inspired

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.

Scale: A typical colony of 50,000 bees sends roughly 10,000–20,000 foragers to hundreds of flower patches simultaneously. In a single summer day, one colony may visit over 225,000 flower patches and travel a combined distance exceeding the circumference of the Earth. Yet the colony adapts this allocation within 30 minutes of a new source opening.

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:

Waggle duration (s) ≈ flight distance (km) × 0.78 [Seeley 1995] Direction angle θ (from vertical) = compass bearing to source − compass bearing to sun Quality (fitness) ∝ (nectar concentration) × (harvest rate) / (flight energy cost) Number of dance circuits ≈ 1.4 × log(fitness relative to other sources)

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:

🔍
Employed Bees
Each is associated with a specific food source (solution). They exploit the source by searching nearby, then return to share information in the dance — proportional to fitness.
👀
Onlooker Bees
They wait in the hive and select a source to visit with probability proportional to the employed bee's fitness. They then perform their own local search around the chosen source.
🗺️
Scout Bees
When an employed bee's source is exhausted (the limit counter is exceeded), it becomes a scout and discovers a random new source, replacing the stale one.

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:

v[i][j] = x[i][j] + φ[i][j] × (x[i][j] − x[k][j]) where: j = random dimension index ∈ {0, ..., D-1} k = random food source index, k ≠ i φ[i][j] = uniform random in [−1, +1] If f(v[i]) ≥ f(x[i]): replace x[i] with v[i], reset trial[i] = 0 Else: keep x[i], increment trial[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.

Selection probability: p[i] = fit[i] / Σ fit[k] (roulette-wheel selection) where fit[i] = 1 / (1 + |f(x[i])|) for minimisation problems = f(x[i]) for maximisation problems // Onlooker applies the same v[i][j] perturbation as phase 1

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:

if trial[i] ≥ limit: x[i][j] = lb[j] + rand(0,1) × (ub[j] − lb[j]) for all j trial[i] = 0

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:

minimise f(x) subject to x ∈ [lb, ub]^D Population: {x[i]} for i = 1, ..., SN/2 (food sources) Each source has capacity: trial[i] ∈ ℕ (abandonment counter) // ABC exploration-exploitation balance: // - Employed phase: each source perturbed once → exploitation // - Onlooker phase: high-fitness sources perturbed more → exploitation // - Scout phase: exhausted sources replaced randomly → exploration // Approximate per-cycle evaluation count: // employed phase: SN/2 evaluations // onlooker phase: SN/2 evaluations // scout phase: 0–1 evaluations (rarely triggered) // total ≈ SN evaluations per cycle

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

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.

Application range: ABC has been applied to neural network training, feature selection, job-shop scheduling, image clustering, robot path planning, and economic dispatch. For continuous functions below ~50 dimensions, it is generally competitive with CMA-ES and often significantly outperforms GA and basic PSO on multimodal landscapes.

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.