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.
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.
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.
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.
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.
| Problem | Reasonable choice |
|---|---|
| Continuous, low-to-moderate dimension, black-box, cheap evaluations | PSO, differential evolution, or CMA-ES |
| Routing / TSP / graph-structured combinatorial | ACO, or specialized solvers (OR-Tools, LKH) |
| Expensive evaluations (minutes each), < ~20 parameters | Bayesian optimization instead |
| Differentiable objective | Gradient-based methods |
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.
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.
| Method | Search space | Strength | Weakness |
|---|---|---|---|
| PSO | Continuous | Few parameters, simple to implement and parallelise | Premature convergence; sensitive to coordinate rotation |
| Differential evolution | Continuous | Robust default on many benchmarks | Needs many evaluations |
| CMA-ES | Continuous | Learns correlations between variables; strong on ill-conditioned problems | O(d²) memory; slower beyond a few hundred dimensions |
| Genetic algorithms | Discrete, mixed, permutations | Flexible encodings and operators | Many design choices; slow convergence |
| ACO | Paths and sequences on graphs | Natural fit for routing; adapts to dynamic graphs | Needs local search to be competitive |
| Bayesian optimization | Continuous, low-dimensional | Very sample-efficient for expensive evaluations | Overhead 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.
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.