Particle Swarm, Live
In 1995, a social psychologist and an electrical engineer sat down to animate a flock of birds and accidentally invented one of the most popular optimisers of the next two decades, which is a story I love. I really believe that error is one of the most fruitful forms of novelty creation. Below, you can fly the result yourself. It's the sibling of the ant colony post: another swarm, another crowd of simple agents solving a problem none of them can see individually, with the communication channel turned inside out.
In Ant Colony Optimisation, the ants coordinate through the environment, and no ant knows anything whatsoever. The knowledge lived in the pheromone map, written and read by thousands of insects who are basically automatons. Particles do the opposite, and each one privately remembers the best spot it has ever stood on, while the whole swarm shares a single piece of gossip: the best spot any member has ever found. That's the entire communication system, and it runs without a trail, a map, or a leader, just a telepathic link of where to find the dropped ice cream or sugar granules.
Quick jargon guide
- Particle swarm optimisation (PSO): an optimiser inspired by bird flocking, where a population of candidate solutions ("particles") fly through the search space, each pulled toward its own best-known point and the swarm's best-known point.
- Personal best: the best position a single particle has ever visited. Its private memory, and one of the two forces steering it.
- Global best: the best position any particle in the swarm has ever visited. Shared knowledge: every particle feels its pull.
- Inertia: how much of its current velocity a particle keeps each step. High inertia means momentum and wide exploration; low inertia means the social pulls dominate.
- Velocity clamp: a cap on how fast a particle may move, stopping the update rule from flinging particles out of the search space entirely.
- Premature convergence: the whole swarm collapsing onto one point before the space has been properly searched, then being unable to leave. The characteristic failure of anything that shares information too enthusiastically.
The flock that forgot how to fly
James Kennedy was a social psychologist and Russell Eberhart was an electrical engineer. Their 1995 paper[1] opens with birds rather than mathematics. They wanted to simulate the choreography of a flock, in the tradition of the boids models, where each simulated bird matches its neighbours' speed, avoids collisions, and stays near the roost. They started deleting rules to see which ones mattered.
Collision avoidance went, velocity matching went, nearest-neighbour tracking went. What survived would not fool a birdwatcher, or a pigeon, or a cat, just dots drifting toward places that had scored well. But the dots were doing something more interesting than flocking: they were finding the optimum of whatever function defined "good". The choreography fell away and the optimiser remained, and Kennedy and Eberhart named the agents particles, "birds" probably no longer felt right.
The version below includes one refinement from 1998, when Shi and Eberhart added the inertia weight[2]: a knob controlling how much of its current velocity each particle carries into the next step.
Three kinds of pull
Every step, each particle updates its velocity as a weighted sum of three forces, which, to me at least, feel kind of like personality traits:
- Habit (w): keep doing what you were doing. The inertia weight scales the particle's current velocity.
- Own memory (c1): a pull back toward the best place you have ever found. Call it self-trust, or nostalgia.
- Peer pressure (c2): a pull toward the best place anyone has ever found. Conformism, sheep-ery.
The two social pulls are each multiplied by a fresh random number every step, so no two particles are likely to respond the same. That randomness is what keeps the swarm from collapsing into a single deterministic trajectory.
The result is clamped to a maximum speed, the particle moves, the function gets evaluated at the new spot, memories get updated, and the loop repeats. That's it!
Watch the swarm hunt
Below, a swarm is minimising a two-dimensional landscape, where warm colours are low ground (better) and the dashed ring marks the true optimum, which the swarm does not know about. Watch the characteristic dance: particles overshoot the best-known point, get hauled back, overshoot the other way, and the whole cloud contracts around the answer like a slowing pendulum.
Drag anywhere on the map to scoop the swarm up and drop it there; personal and global bests reset, so the hunt starts over from wherever you left them. The blue dots are particles (with short motion trails), the faint dots are their personal bests, the green ring is the global best (the one piece of gossip everybody shares), and the dashed ring is the true optimum. Shifted bowl is the warm-up; the Banana valley hides its optimum along a curved ridge that the swarm must creep along; the Egg carton is riddled with local minima that swallow overconfident swarms; and the Funnel is nearly flat everywhere except one deep spike, so gossip about the spike is the only useful signal there is.
Break it with the sliders
Crank inertia past 1. Above w = 1 a particle's velocity grows every step instead of decaying, so the only thing stopping the swarm from leaving the map is the speed clamp. The swarm boils, with particles orbiting the best-known point at full speed, permanently overshooting, while the spread chart flatlines high and the fitness chart stalls on momentum.
Now do the opposite: inertia near 0, peer pressure at 3. This is the stampede: with no momentum to resist, every particle dives straight at the first decent global best, the spread chart crashes within a few dozen iterations, and the swarm becomes one throbbing dot.
On the bowl this looks like efficiency, but switch to the egg carton and watch the same setting get eaten: the swarm piles into whichever dimple it saw first, the true optimum sits two ridges away, and nobody has enough independence left to go and look. Premature convergence.
Set own memory (c1) to zero. Pure conformism: particles have no attachment to their own discoveries, and the global best is the only guidance.
Set peer pressure (c2) to zero. Now nobody shares anything, and you get thirty hermits running thirty independent local searches, each rattling around its own personal best and improving it a little.
Now stop breaking it
Then go back to the classic setting (w = 0.72, c1 = c2 = 1.5, numbers the field converged on after years of tuning) and watch the dance it buys you: overshoot, return, contract, with just enough disagreement to keep a few scouts out wide while the core closes in. On the egg carton it will still fail sometimes. The demo isn't broken, that just shows that it's not perfect, and there are still papers being published solving more complex landscapes with PSO.
Personality knobs
Kennedy never stopped pointing out that this is a model of social learning first and an optimiser second: individuals keep private experience, the group broadcasts its best result, and behaviour is a compromise between habit, self-trust and conformism.
Every failure mode you just created is a recognisable social failure. The high-conformism swarm is an echo chamber stampeding to its first confident answer. The zero-gossip swarm is a room full of talented people who never talk. The overheated swarm is a committee that keeps relitigating and never makes an actual decision (sounds like many meetings I've been in).
Annealing, evolution, ants, particles
The through-line of the series: Simulated annealing was one searcher learning when to accept a worse answer. Genetic algorithms were a population improving by dying, the ants stored the answer in the world, and the particles store it in each other. Four strategies, one shared problem: explore enough to find the right valley, exploit enough to reach the bottom of it. See some interesting overlaps in strategy, parameters, landscape advantages and disadvantages?
Anyway. Go scoop up the swarm, drop it in a corner of the egg carton, and watch thirty dots argue their way out. Or fail to.
Common questions
Is this actually how birds flock?
No. Real flocking is much closer to the boids rules Kennedy and Eberhart started from (local alignment, cohesion, separation among a handful of nearest neighbours), and modern starling studies suggest birds track a fixed number of neighbours rather than everyone in range.
How is this different from a genetic algorithm?
Both are population-based and gradient-free, but the mechanics are almost disjoint. In a genetic algorithm, individuals are born, breed and die: information moves between solutions by crossover, and progress comes from selection pressure. In PSO nobody dies and nobody breeds. The same thirty particles persist the whole run, and information moves through shared memory instead of inheritance. In practice PSO tends to converge faster on smooth continuous problems, while evolutionary methods are easier to adapt to weird, structured or combinatorial ones.
Does anyone still use PSO?
Yes, heavily, especially in engineering: antenna design, power systems, controller tuning, and any setting where evaluations are expensive, gradients are unavailable, and a decent answer soon beats a perfect answer never.
References
- Kennedy, J., & Eberhart, R. (1995). Particle swarm optimization. Proceedings of ICNN'95 - International Conference on Neural Networks, 4, 1942–1948. https://doi.org/10.1109/ICNN.1995.488968
- Shi, Y., & Eberhart, R. (1998). A modified particle swarm optimizer. 1998 IEEE International Conference on Evolutionary Computation Proceedings, IEEE World Congress on Computational Intelligence, 69–73. https://doi.org/10.1109/ICEC.1998.699146