Swarm Intelligence

Swarm intelligence is the collective problem-solving that emerges when many simple agents follow local rules and share information — bird flocks, fish schools, ant colonies. As an optimization technique, it gives two widely used algorithms: Particle Swarm Optimization (PSO) for continuous search spaces and Ant Colony Optimization (ACO) for combinatorial problems on graphs. Like genetic algorithms, they are population-based and derivative-free, but they improve candidates by movement and communication rather than reproduction.



1. Particle Swarm Optimization

Particle Swarm Optimization: One Velocity Update particle xᵢ personal best pᵢ global best g inertia w·vᵢ new vᵢ vᵢ ← w vᵢ + c₁ r₁ (pᵢ − xᵢ) + c₂ r₂ (g − xᵢ) xᵢ ← xᵢ + vᵢ r₁, r₂ ~ U(0,1) per dimension. Typical w ≈ 0.7, c₁ ≈ c₂ ≈ 1.5.

PSO (Kennedy & Eberhart, 1995) keeps a swarm of particles, each with a position xi (a candidate solution) and a velocity vi. Each particle remembers its own best position pi, and the swarm shares a global best g (or a neighborhood best in "local" topologies). Every iteration, a particle's velocity blends three terms: inertia (keep moving), a cognitive pull toward its own best, and a social pull toward the swarm's best, each scaled by fresh random numbers.

The inertia weight w controls exploration: large w lets particles fly through the space; small w makes them settle. A common schedule decreases w linearly from about 0.9 to 0.4. Velocities are usually clamped to a maximum to prevent particles from exploding out of bounds. A global-best topology converges fast but can be trapped in a local optimum; ring (local-best) topologies spread information more slowly and explore more.


2. Python: PSO from Scratch

import numpy as np

def rastrigin(X):                       # many local minima, global min 0 at origin
    return 10 * X.shape[1] + np.sum(X**2 - 10 * np.cos(2 * np.pi * X), axis=1)

def pso(f, dim=5, n=40, iters=300, lo=-5.12, hi=5.12, seed=0):
    rng = np.random.default_rng(seed)
    x = rng.uniform(lo, hi, (n, dim))
    v = rng.uniform(-1, 1, (n, dim)) * (hi - lo) * 0.1
    pbest, pbest_val = x.copy(), f(x)
    g = pbest[pbest_val.argmin()].copy()
    vmax = 0.2 * (hi - lo)
    c1 = c2 = 1.5
    for t in range(iters):
        w = 0.9 - 0.5 * t / iters                                  # linearly decreasing inertia
        r1, r2 = rng.random((n, dim)), rng.random((n, dim))
        v = w * v + c1 * r1 * (pbest - x) + c2 * r2 * (g - x)
        v = np.clip(v, -vmax, vmax)
        x = np.clip(x + v, lo, hi)
        val = f(x)
        better = val < pbest_val
        pbest[better], pbest_val[better] = x[better], val[better]
        g = pbest[pbest_val.argmin()].copy()
        if t % 50 == 0:
            print(f"iter {t:3d}  best={pbest_val.min():.4f}")
    return g, pbest_val.min()

best_x, best_f = pso(rastrigin)
print("best f:", round(best_f, 4), "at", np.round(best_x, 3))

Libraries: pyswarms (PSO with topologies and plotting) and pymoo/mealpy for broader metaheuristic collections.


3. Ant Colony Optimization

Ant Colony Optimization: Pheromone Reinforces Short Paths Nest Food long path: pheromone evaporates short path: more ants per unit time, more pheromone P(i→j) ∝ τᵢⱼ^α · ηᵢⱼ^β with η = 1/distance; τ ← (1−ρ) τ + Σ Q / Lₖ on edges ant k used

ACO (Dorigo, 1992) mimics how real ants find short routes: they lay pheromone as they walk, follow trails with more pheromone, and pheromone evaporates. Shorter routes are completed more often per unit time, accumulate pheromone faster, and attract more ants — positive feedback that converges on good paths.

Algorithmically, each artificial ant builds a complete solution (e.g. a TSP tour) step by step, choosing the next node j from i with probability proportional to τijα · ηijβ, where τ is pheromone and η a heuristic such as 1/distance. After all ants finish, pheromone evaporates by factor ρ and each edge receives deposits proportional to the quality of tours that used it. Variants such as Ant Colony System and MAX-MIN Ant System bound pheromone levels to avoid stagnation. ACO suits routing, scheduling, and other problems naturally expressed as paths through a graph; see also shortest-path algorithms for the exact single-source case.


4. Other Swarm Methods

Swarm Methods at a Glance PSOcontinuousvelocity + bests ACOgraphs, routingpheromone trails Artificial Beecontinuousscouts + foragers Fireflymultimodalattraction ∝ brightness Common thread: simple local rules + shared information produce global search behavior. PSO and ACO are the two with a substantial track record; prefer them over newer metaphors.

Many other "swarm" algorithms exist (artificial bee colony, firefly, cuckoo search, grey wolf, whale optimization). Several have been criticized in the literature as re-labeled versions of existing ideas with a new metaphor, and few show consistent advantages over well-tuned PSO, differential evolution, or CMA-ES. Benchmark on your problem rather than choosing by metaphor.


5. When to Use Swarm Methods

ProblemReasonable choice
Continuous, low-to-moderate dimension, black-box, cheap evaluationsPSO, differential evolution, or CMA-ES
Routing / TSP / graph-structured combinatorialACO, or specialized solvers (OR-Tools, LKH)
Expensive evaluations (minutes each), < ~20 parametersBayesian optimization instead
Differentiable objectiveGradient-based methods

6. Common Pitfalls


7. A Worked PSO Update

The standard velocity and position updates for particle i in each dimension are

v ← w·v + c1r1(pi − x) + c2r2(g − x)   and   x ← x + v

where r1, r2 are fresh uniform random numbers in [0, 1]. Take one dimension with x = 4, v = 1, personal best pi = 3, global best g = 0, inertia w = 0.7, c1 = c2 = 1.5, and suppose r1 = 0.5, r2 = 0.8:

The particle overshoots the global best at 0 and lands on the other side. That overshoot is deliberate: oscillation around the attractors samples the region between and beyond them, and the inertia weight damps it over time. If velocities were not damped (w ≥ 1 with large c values), the oscillation would grow and the swarm would diverge, which is why velocity clamping or the constriction form is used.

Clerc and Kennedy's (2002) constriction analysis gives a widely used parameter set, w ≈ 0.7298 and c1 = c2 ≈ 1.49618, which guarantees the swarm's dynamics converge without needing velocity clamping. It is a sensible default before any tuning.

8. ACO Step by Step

Ant Colony Optimization: One Iteration Construct each ant builds a tour p ~ tau^a * eta^b Evaluate tour length L_k update best-so-far Evaporate tau = (1 - rho) tau forget old trails Deposit add Q / L_k on edges each ant used repeat until the best tour stops improving or the budget runs out

A small numerical example of the transition rule: an ant at city i can move to j (distance 2, pheromone 1.0) or k (distance 4, pheromone 2.0). With α = 1 and β = 2 and heuristic η = 1/distance, the weights are 1.0 × (1/2)² = 0.25 for j and 2.0 × (1/4)² = 0.125 for k. The probabilities are 0.25 / 0.375 ≈ 0.67 for j and ≈ 0.33 for k. The shorter edge wins despite having half the pheromone, because β weights the distance heuristic more heavily; raising α would shift trust toward the colony's accumulated experience.

Typical settings from the ACO literature are α ≈ 1, β between 2 and 5, evaporation ρ between 0.1 and 0.5, and roughly as many ants as cities for TSP. Most competitive ACO implementations also apply a local search (2-opt or 3-opt) to each ant's tour before depositing pheromone; ACO alone is rarely competitive with dedicated TSP heuristics.

9. Comparison with Other Derivative-Free Optimizers

MethodSearch spaceStrengthWeakness
PSOContinuousFew parameters, simple to implement and parallelisePremature convergence; sensitive to coordinate rotation
Differential evolutionContinuousRobust default on many benchmarksNeeds many evaluations
CMA-ESContinuousLearns correlations between variables; strong on ill-conditioned problemsO(d²) memory; slower beyond a few hundred dimensions
Genetic algorithmsDiscrete, mixed, permutationsFlexible encodings and operatorsMany design choices; slow convergence
ACOPaths and sequences on graphsNatural fit for routing; adapts to dynamic graphsNeeds local search to be competitive
Bayesian optimizationContinuous, low-dimensionalVery sample-efficient for expensive evaluationsOverhead grows with evaluations; weak in high dimension

When the objective is smooth and gradients are available, gradient methods beat all of these by a wide margin. When the problem has exploitable structure (linear constraints, integer variables), a mathematical-programming or constraint solver usually finds better solutions with optimality bounds.

10. Applications

Documented uses of PSO include tuning controller gains (PID and similar, see Control Algorithms), antenna and electromagnetic design, power-system dispatch, and hyperparameter search for small models. ACO has been applied to vehicle routing, job-shop scheduling, network routing (AntNet), and sequence-based problems. Beyond optimization, swarm ideas drive multi-robot coordination and flocking simulations (Reynolds' "boids", 1987: separation, alignment and cohesion), where the emergent group behaviour itself is the goal.

11. Further Reading