Skip to content
coden

Particle swarm optimisation: inertia, personal experience and the swarm’s experience

Boids and flocks, the PSO algorithm, the three components of velocity, a run on the Rastrigin function compared with an evolution strategy, the role of inertia, particle stability, swarm topology and the no free lunch theorem.

13 minutes Matěj Žalmánek 2026-10-15

Series AI basics Part 30 of 52
  1. Map of artificial intelligence: what is what and how it all fits together
  2. A brief history of artificial intelligence: from Turing to language models
  3. Data, model, training, inference: the basic terms on a single example
  4. How to read mathematical notation: symbols, indices, sums and quantifiers
  5. Sets and relations: the building blocks of definitions in AI
  6. Functions and mappings: why every AI model is a function
  7. Logic and proofs: the rules AI reasons by
  8. Graphs and trees: the structure that both search and neural networks stand on
  9. Linear algebra for machine learning: vectors, matrices and why AI mostly multiplies
  10. Derivatives, the gradient and the chain rule: how a model finds out which way to learn
  11. Gradient descent: how a model learns
  12. What probability is: from rolling dice to the Kolmogorov axioms
  13. Conditional probability and Bayes’ theorem: how to update your estimate when you learn something new
  14. Random variables and the cumulative distribution function: one function that tells you everything about a distribution
  15. Probability density and probability mass functions: why a continuous variable has zero probability at a point
  16. Expected value, variance and covariance: where a distribution lies, how wide it is and what goes with what
  17. Overview of probability distributions: Bernoulli, binomial, Poisson, uniform, normal and exponential
  18. Law of large numbers and central limit theorem: why averages behave so predictably
  19. Statistics and parameter estimation: maximum likelihood, MAP and confidence intervals
  20. Information theory: entropy, cross-entropy and KL divergence — where loss functions come from
  21. State-space search: BFS, DFS, UCS and A* with animations
  22. Games and adversaries: minimax, alpha-beta pruning and Monte Carlo tree search
  23. Constraint satisfaction: backtracking, constraint propagation and AC-3 on sudoku and a map of Czech regions
  24. Logic and knowledge systems: forward and backward chaining, resolution and expert systems
  25. Planning: STRIPS, the blocks world, heuristics from relaxation and planning in AI agents
  26. Local search and simulated annealing: a travelling salesman through the Czech regional capitals
  27. Genetic algorithms: population, crossover, mutation and selection on a backpack for a hike
  28. Evolution strategies and genetic programming: from the one-fifth rule to Kepler’s law
  29. Ant colony optimization: how a pheromone trail finds a short path
  30. Particle swarm optimisation: inertia, personal experience and the swarm’s experience
  31. Neuroevolution: evolving the weights and shape of neural networks, and the NEAT algorithm
  32. Fuzzy sets and membership functions: how to write down “somewhat warm”
  33. Fuzzy logic and probability: why “somewhat warm” does not mean “probably warm”
  34. Fuzzy logic: t-norms, implications and IF–THEN rules
  35. Fuzzy control: Mamdani and Sugeno from a fan to a pole on a cart
  36. Fuzzy clustering: the fuzzy c-means algorithm and points between clusters
  37. Neuro-fuzzy systems (ANFIS): fuzzy rules that learn from data
  38. Markov chains: the transition matrix, the stationary distribution and PageRank
  39. Hidden Markov models: the Viterbi algorithm and the dishonest casino
  40. Bayesian networks: why the grass is wet and what it means for the sprinkler
  41. The Kalman filter: how to estimate where a car is and how fast it goes from imprecise measurements
  42. Types of machine learning: supervised, unsupervised, semi-supervised and more
  43. Prompts, context and RAG: how to give a model the right information
  44. Variational autoencoders (VAE): new images from a continuous latent space
  45. Generative adversarial networks (GAN): the forger versus the detective
  46. Diffusion models: how pure noise becomes an image
  47. Markov decision processes: states, actions, rewards and the Bellman equations
  48. Q-learning and DQN: learning the value of actions, from a table to a neural network
  49. Policy gradient and PPO: learning the policy directly
  50. How an AI agent works: a language model in a loop with tools
  51. Programming with AI agents: the task, tests and review
  52. Evaluating and securing AI systems: how to measure and how to keep watch

Full series syllabus 52 of 83 topics published

What you will take away
  • How a swarm particle combines inertia, its own best place and the swarm’s best place.
  • Why inertia and the balance of the components matter, and when the swarm settles.
  • Why there is no optimiser that is best at everything, and what to watch out for in “nature-inspired” algorithms.

Useful to know first: Evolution strategies and genetic programming: from the one-fifth rule to Kepler’s law

A flock of starlings over a city at dusk flows like a single living creature — and yet it has no leader. Each bird watches only a few of its nearest neighbours. In 1995 the social psychologist James Kennedy and the electrical engineer Russell Eberhart asked a question: what if the birds shared not only their neighbours’ positions but also where they had found the most food? Their simulation turned into one of the simplest and most widely used optimisation algorithms of all — particle swarm optimisation (PSO).

It follows on from the parts Evolution strategies and genetic programming and Ant colony optimization. We will let the swarm loose on the same Rastrigin function as the evolution strategy, with the same start and the same budget.

1. Flocks in nature and in the computer

As early as 1987 Craig Reynolds showed that three local rules are enough for believable flocking. His “boids” avoid collisions with their nearest neighbours, align their heading with them and stay close to the centre of the group. Nobody steers the whole; the shape of the flock emerges from the local rules. Kennedy and Eberhart added a fourth thing — a memory of good places — and the animation became an optimiser.

Definition — the three boid rules (simplified)

Reynolds described the rules in words; today they are usually written like this. Let N ≥ 2 boids move in ℝn (n = 2 or 3) and let boid i ∈ {1, …, N} have a position xi ∈ ℝn and a velocity vi ∈ ℝn. Let R > 0 be the perception radius and ‖·‖ the Euclidean norm. The neighbourhood of boid i is the set

Si = {j ∈ {1, …, N} : j ≠ i ∧ ‖xj − xi‖ < R}.

If Si ≠ ∅, denote the average velocity and the average position of the neighbours by

v̄i = 1/|Si| ∑j ∈ Si vj,   x̄i = 1/|Si| ∑j ∈ Si xj.

The boid adds to its velocity vi the sum of three vectors multiplied by weights k1, k2, k3 ≥ 0 (for Si = ∅ the velocity does not change):

  1. separation ai(1) = −∑j ∈ Si xj − xi/‖xj − xi‖2 — repulsion, stronger the closer the neighbour is;
  2. alignment ai(2) = v̄i − vi — bringing its own velocity closer to the average velocity of the neighbours;
  3. cohesion ai(3) = x̄i − xi — a pull towards the centre of the neighbours.

vi ← vi + k1 ai(1) + k2 ai(2) + k3 ai(3),   xi ← xi + vi.

Each boid therefore uses only information from its own neighbourhood. The PSO equations below have the same shape, “velocity ← velocity + pulls towards targets”, except that the targets are not neighbours but the best places found so far.

2. The algorithm

Definition — particle swarm optimisation

Input: an objective function f : ℝn → ℝ that we minimise (n ≥ 1); a number of particles N ∈ ℕ, N ≥ 1; a number of iterations T ∈ ℕ; an inertia w ∈ ℝ and coefficients c1, c2 ≥ 0; initial positions x1, …, xN ∈ ℝn and velocities v1, …, vN ∈ ℝn (usually random within the search area).

Output: a point g ∈ ℝn — the best position found — and the value f(g).

The swarm consists of N particles; particle i ∈ {1, …, N} has a position xi, a velocity vi and its own best position so far, pi; g is the best position of the whole swarm. We write the coordinates of the vector xi as xid, d = 1, …, n.

  1. Initialisation: pi ← xi for i = 1, …, N and g ← pk, where k = arg mini ∈ {1, …, N} f(pi).
  2. Iteration t = 1, …, T: for each particle i = 1, …, N in turn
    1. for each coordinate d = 1, …, n draw r1, r2 independently and uniformly from [0, 1) and set vid ← w · vid + c1 r1 (pid − xid) + c2 r2 (gd − xid);
    2. xi ← xi + vi;
    3. if f(xi) < f(pi), set pi ← xi; if moreover f(pi) < f(g), set g ← pi.
  3. Stopping: after T iterations return g and f(g).

In vector notation (with the random numbers r1, r2 drawn separately for each coordinate) the first two substeps of an iteration are

vi ← w · vi + c1 r1 (pi − xi) + c2 r2 (g − xi),   xi ← xi + vi.

So each particle changes its velocity according to inertia, its own best place and the swarm’s best place. Usual values (Clerc and Kennedy, 2002): w = 0.7298, c1 = c2 = 1.49618.

In plain words

Each particle is a bird flying over a landscape that remembers where it fared best. On top of that it hears where the whole flock fared best. Its next step is a compromise between three things: keep flying where it was flying, return to its own best place and head for the flock’s best place. The random numbers r make sure each bird strikes that compromise a little differently.

vi ← w · vi + c1 r1 (pi − xi) + c2 r2 (g − xi)

vi on the left
the new velocity — how far the particle moves in this step (xi ← xi + vi); the swarm does not control position directly but through velocity, and that gives the motion its smoothness
w · vi
inertia — part of the old velocity remains; w < 1 also damps it so that the motion settles down over time
c1 r1, c2 r2
strength of the pull — c sets the average strength, the random r from [0, 1) shortens it differently each time, so the particle does not land exactly on the target and samples the places around it on the way
pi − xi
the vector from the particle to its best place — the farther away it is, the more strongly it pulls the particle back (like a spring)
g − xi
the vector to the swarm’s best place — the only channel through which other particles’ experience reaches this one
The differences p − x and g − x turn the targets into attractors: the pull grows with distance and is zero at the target.
Definition — velocity clamping

Let vmax > 0. Velocity clamping is a modification of the PSO algorithm: in an iteration step, right after the new velocity vi ∈ ℝn is computed (and before the position is moved), each of its coordinates d = 1, …, n is replaced by the value

vid ← min(vmax, max(−vmax, vid)),

i.e. the coordinate is clipped to the interval [−vmax, vmax] (values inside the interval do not change). Our implementation uses vmax = 5.12, half the width of the search square [−5.12, 5.12]2 — a particle cannot cross more than half the map in a single step.

Example — one step of one particle

A particle is at x = (4.2, −3.8) with velocity v = (0.5, 0.4). Its best place is p = (4.0, −3.1), the swarm’s best place g = (1.1, −2.0). The random numbers drawn are r1 = (0.6, 0.3) and r2 = (0.5, 0.8). The first coordinate (colours as in the formula above):

  • inertia 0.7298 · 0.5 = 0.365,
  • cognitive 1.49618 · 0.6 · (4.0 − 4.2) = −0.180,
  • social 1.49618 · 0.5 · (1.1 − 4.2) = −2.319,
  • new velocity 0.365 − 0.180 − 2.319 = −2.134, new position 4.2 − 2.134 = 2.066.

The second coordinate likewise: 0.292 + 0.314 + 2.154 = 2.761, new position −3.8 + 2.761 = −1.039. Both velocity components are below vmax, so clamping does not apply. The function value (Rastrigin’s, see below) dropped from 45.90 to 6.51. The social component is the largest here because g is far away; the cognitive one is small because p lies just a little way from the particle.

import random

def pso(f, n, castic=40, iteraci=200, w=0.7298, c1=1.49618, c2=1.49618):
    x = [[random.uniform(-5, 5) for _ in range(n)] for _ in range(castic)]
    v = [[0.0] * n for _ in range(castic)]
    p = [xi[:] for xi in x]                              # best place of each particle
    g = min(p, key=f)[:]                                 # best place of the swarm
    for _ in range(iteraci):
        for i in range(castic):
            for d in range(n):
                v[i][d] = (w * v[i][d]
                           + c1 * random.random() * (p[i][d] - x[i][d])
                           + c2 * random.random() * (g[d] - x[i][d]))
                x[i][d] += v[i][d]
            if f(x[i]) < f(p[i]):
                p[i] = x[i][:]
                if f(p[i]) < f(g):
                    g = p[i][:]
    return g

The cost of the algorithm is measured in evaluations of f — in real problems each one is expensive (a simulation, training a model). Each particle is evaluated at the start and then once per iteration, in total N + N · T = 40 + 40 · 200 = 8,040 for N = 40 particles and T = 200 iterations. (The pseudocode above calls f several times for readability; the real implementation remembers the values f(pi).)

3. Three forces

own best p swarm best g particle x
One step of one particle: three components and the resulting velocity inertia cognitive social new velocity
  • Inertia w · v — the particle does not want to change direction abruptly. Thanks to it the swarm flies over shallow dips instead of falling into every one of them.
  • Cognitive component c1 r1 (p − x) — own experience: “I did well over there”. It maintains diversity, because each particle remembers something different.
  • Social component c2 r2 (g − x) — the swarm’s experience: “over there is the best anyone has found”. It pulls the swarm together.

Unlike in genetic algorithms, nobody dies here and nothing is crossed over. Information spreads only because particles see the success of others. And unlike in evolution strategies, a particle has a velocity — momentum that carries it on even when things are not going well.

4. The swarm on the Rastrigin function

The Rastrigin function from the previous part: a bowl with a regular grid of dips, the global minimum at the origin. 40 particles start around the point (4.2, −3.8), where f ≈ 45.9 — where the evolution strategy started:

Definition — the Rastrigin function

Let n ∈ ℕ, n ≥ 1. The Rastrigin function is the function f : [−5.12, 5.12]n → ℝ that assigns to a point x = (x1, …, xn) the value

f(x) = 10n + ∑d=1n (xd2 − 10 cos(2πxd)).

It is the sum of n identical functions of one variable, shifted by 10n. The global minimum is f(0, …, 0) = 0; local minima lie near every point with integer coordinates, and within the square there are 11 · 11 = 121 of them in two dimensions.

f(x) = 10n + ∑d (xd2 − 10 cos(2πxd))

f(x)
the value the swarm minimises — a test landscape whose minimum we know, so we can recognise success
10n
an offset — at the origin the cosine is 1 and each coordinate contributes −10; this term compensates so that the minimum has a value of exactly 0
∑d
a sum over coordinates — each contributes independently, so the number of dips grows exponentially with dimension (11n)
xd2
the bowl — an overall slope towards the origin; without it all dips would be equally deep and there would be no global minimum
− 10 cos(2πxd)
ripples with period 1 — they carve a dip at every integer, and with amplitude 10 they make them deep enough for a local method to get stuck in them
Example — the value at the starting point

For x = (4.2, −3.8): cos(2π · 4.2) = cos(8.4π) = cos(0.4π) ≈ 0.309 and cos(2π · (−3.8)) = cos(7.6π) = cos(1.6π) ≈ 0.309. So (colours as in the formula above, n = 2)

f = 20 + (17.64 − 3.090) + (14.44 − 3.090) = 20 + 14.550 + 11.350 ≈ 45.9.

The start lies on the slope of the dip at (4, −4), where f = 20 + (16 − 10) + (16 − 10) = 32; the actual bottom is a hair closer to the origin, with a value of about 31.84. A value of 31.9 after the first iteration (below) therefore means the swarm has slid almost to the bottom of the starting dip. It can only get further towards the minimum across the ridges between dips.

−5−50055 coordinate x₁ (no unit) coordinate x₂ (no unit) iteration 0 iteration 3 iteration 10 iteration 25 iteration 60 iteration 200
A swarm of 40 particles; the star is the swarm’s best place, the cross the global minimum

After the first iteration the swarm’s best place has a value of 31.9. Then something happens that the evolution strategy did not do: the swarm spreads out. The spread of the particles grows from 0.7 to 1.3 in the tenth iteration — particles overshoot their best places and discover new dips on the way. In the 25th iteration the best place has a value of 1.06, one dip away from the minimum, and in the 50th iteration the swarm finds the global minimum. Then it contracts around it.

Definition — swarm spread and a successful run

Let x1, …, xN ∈ ℝn (N ≥ 1) be the positions of the particles of the swarm in one iteration and ‖·‖ the Euclidean norm. The centroid of the swarm is the point

x̄ = 1/N ∑i=1N xi

and the swarm spread is the number

σ = √1/N ∑i=1N ‖xi − x̄‖2 ≥ 0,

i.e. the square root of the mean squared distance of the particles from the centroid.

Let g be the swarm’s best place after the last iteration of a run on the Rastrigin function f. The run found the global minimum if and only if f(g) < 0.01 (the threshold is our choice for this part). The nearest local minima have values around 1 (which is why 1.06 in the 25th iteration means “the next dip over”), so this threshold clearly separates them.

σ = √1/N ∑i ‖xi − x̄‖2

σ
a single number for “how spread out the swarm is” — in coordinate units, so it can be compared with the distance between dips (1)
1/N ∑i
an average over particles — the result does not depend on the size of the swarm
‖xi − x̄‖2
the squared distance from the centroid — we measure from the centre of the swarm, not from the minimum, because we care about the shape of the swarm, not where it is; squaring gives more weight to outlying scouts and the square root then restores the original units

A trace of the same run by iteration:

IterationBest value f(g)Spread σ
131.9270.70
231.9270.74
331.9270.53
525.6250.75
105.9041.29
251.0641.01
500.0070.96
100< 0.0010.51
200< 0.0010.28

For four iterations the best value does not budge, and meanwhile the swarm contracts into the starting dip (in the third iteration σ is only 0.53); then inertia and the pulls towards g stretch it out again and the best value drops dip by dip — 31.9, 25.6, 5.9, 1.06. The minimum is found in the 50th iteration, but σ is still 0.96 at that point: the swarm only closes in around the minimum afterwards.

Out of a hundred runs with different seeds the swarm found the global minimum in 97 — with a budget of 8,040 evaluations. The (15, 100) evolution strategy with 8,000 evaluations of the same problem succeeded in 32 cases. When the swarm starts spread over the whole square instead of in a corner, it succeeds in all hundred.

5. Inertia and the balance of forces

What happens if we change only the inertia? The chart shows how far apart the particles are (the vertical axis is logarithmic):

10010-210-410-610-8 050100150200 iteration (number) swarm spread σ (log scale)
Swarm spread by inertia w w = 0.4w = 0.7298w = 0.9

With w = 0.4 the swarm collapses to a single point before it has a chance to look around — it gets stuck in a dip with value 24.9. With w = 0.9 it stays spread out the whole time: it finds the minimum but never closes in around it and never fine-tunes it. Out of a hundred runs for each setting:

SettingFound the minimumAverage best value
w = 0.7298, c1 = c2 = 1.4961897 of 1000.18
w = 0.993 of 1000.003
w = 0.40 of 10024.2
social only (c1 = 0)55 of 1003.0
cognitive only (c2 = 0)0 of 10018.6

Without the social component the swarm is just forty lonely hill climbers who know nothing of each other — none of them gets out of the corner. Without the cognitive one they all chase a single leader and half the time get stuck with it. Only both components together strike the balance between exploration and exploitation that we already met with simulated annealing.

6. Why the swarm does not run wild

Velocity keeps being added up. Why does it not grow without bound? The answer comes from analysing a single particle while we forget about randomness for a moment.

Theorem — particle stability

Consider a one-dimensional particle without randomness. Let p, g ∈ ℝ be fixed best places (of the particle and of the swarm), w ∈ ℝ the inertia and φ1, φ2 ∈ ℝ fixed numbers in place of c1r1, c2r2, with φ = φ1 + φ2 ≠ 0. Denote

P = φ1p + φ2g/φ

(the weighted average of p and g). Let the positions x0, x1, x2, … ∈ ℝ satisfy, for every t ≥ 1, the linear recurrence

xt+1 = (1 + w − φ) xt − w xt−1 + φP.

Then limt → ∞ xt = P for every choice of initial positions x0, x1 ∈ ℝ if and only if

  1. |w| < 1   and
  2. 0 < φ < 2(1 + w).
In plain words

A particle behaves like a weight on a damped spring: it is pulled towards a point between its own and the swarm’s best place, inertia carries it past that point and then back again. If there is enough damping and the spring is not too stiff, the oscillations shrink and the particle settles. With the values w = 0.7298 and c1 + c2 ≈ 2.99 the condition holds (2 · 1.7298 ≈ 3.46), so the swarm gradually closes in — but slowly enough to look around on the way. In addition, velocity is usually capped from above so that particles do not fly out of the search area.

Proof — where the recurrence and the conditions come from

Without randomness the one-dimensional step is vt+1 = wvt + φ1(p − xt) + φ2(g − xt) and xt+1 = xt + vt+1. With φ = φ1 + φ2 and P = φ1p + φ2g/φ from the theorem, φ1(p − x) + φ2(g − x) = φ(P − x), and after substituting vt = xt − xt−1:

xt+1 = (1 + w − φ) xt − w xt−1 + φP.

The deviation et = xt − P satisfies the same recurrence without the constant, so et = C1λ1t + C2λ2t, where λ1,2 are the roots of the characteristic equation λ2 − (1 + w − φ)λ + w = 0 (for a double root et = (C1 + C2t)λ1t), and the constants C1, C2 can be set arbitrarily by the choice of x0, x1. Hence et → 0 for all initial positions if and only if |λ1| < 1 and |λ2| < 1. For a quadratic equation λ2 + aλ + b = 0 with real a, b this happens if and only if |b| < 1 and 1 ± a + b > 0 (the Jury criterion). Here b = w, a = −(1 + w − φ):

  • |w| < 1,
  • 1 − (1 + w − φ) + w = φ > 0,
  • 1 + (1 + w − φ) + w = 2(1 + w) − φ > 0. ∎

xt+1 = (1 + w − φ) xt − w xt−1 + φP

xt+1
the next position — it depends on the two previous ones, because velocity is the difference of two positions; that is why the particle behaves like an oscillating body, not like a ball rolling down a slope
(1 + w − φ) xt
the current position pushed on by inertia (+w) and pulled back by attraction (−φ) — a contest of “keep flying” against “go back”
− w xt−1
memory of the previous step — this term brings momentum; its coefficient w is the product of the roots, which is why the damping is set precisely by the inertia
φP
the anchor — the attraction of the weighted average of p and g; the particle oscillates around it and settles into it

How fast does it settle? For the usual φ both roots come out complex (the particle oscillates) and they have the same absolute value √w, because their product is w. The amplitude of the deviation is thus multiplied by √w every iteration and halves in ln 2/−ln √w iterations. With the average φ = c1 + c2/2 = 1.49618 (the random r has mean 1/2):

Inertia wDamping per iteration √wHalf-life of the deviation
0.40.6321.5 iterations
0.72980.8544.4 iterations
0.90.94913.2 iterations

This matches the spread chart above exactly: with w = 0.4 the swarm closes in within a few iterations, with w = 0.9 the oscillations die away over dozens of iterations. It is a simplification — the real r are drawn at every step and p, g change — but the order of magnitude is right. And note: with w = 0.4 the bound 2(1 + w) = 2.8 is smaller than the largest possible φ = c1 + c2 ≈ 2.99, so an exceptionally strong random pull makes the particle swing out for one step (damping 1.280 > 1).

7. Who hears whom: topology

Definition — swarm topology

Let the swarm consist of N ≥ 3 particles numbered 1, …, N, with best positions so far p1, …, pN, and let f be the function being minimised. The topology of the swarm is an undirected graph G = ({1, …, N}, E) on the set of particles; it determines whose best places a particle sees. The neighbourhood of particle i is the set

S(i) = {i} ∪ {j : {i, j} ∈ E}

and the best place of the neighbourhood is gi = pk, where k = arg minj ∈ S(i) f(pj). In the velocity rule of particle i, gi is used instead of g. Two common topologies:

  1. global (gbest): G is the complete graph, i.e. S(i) = {1, …, N} for every i; then gi = g is shared by all;
  2. ring (lbest): E = {{i, i + 1} : i = 1, …, N} with indices modulo N (N + 1 ≡ 1), i.e. S(i) = {i − 1, i, i + 1} (0 ≡ N).

So in the global topology each particle sees all the others, in the ring only its two neighbours in the ring.

In plain words

In the global topology everyone immediately knows about the best find — the swarm is fast, but one lucky particle can easily drag it into a local minimum. In a ring the news spreads from neighbour to neighbour; meanwhile different parts of the swarm explore different places. Slower, but more cautious.

Formally, each particle i has a neighbourhood S(i), and in the velocity rule it uses, instead of the shared g, gi = pk, where k = arg minj ∈ S(i) f(pj). The global topology has S(i) = {1, …, N}, the ring S(i) = {i − 1, i, i + 1} (indices modulo N). How many iterations does it take for news of a new find to reach everyone? In the global topology, one. In a ring it advances by at most one neighbour in each direction per iteration, and the most distant particle in a ring of 40 particles is 20 places away — the news reaches it after 20 iterations at the earliest. For that long, distant parts of the swarm can explore on their own.

On our two-dimensional problem the ring found the minimum in all 100 runs, the global topology in 97. The difference is clearer in ten dimensions, where there are astronomically many dips: after 2,000 iterations the global topology ended at an average value of 5.84, the ring at 4.57. Neither found the global minimum even once — the ten-dimensional Rastrigin function really is hard for 40 particles.

8. Beware of metaphors

Theorem — no free lunch (Wolpert and Macready, 1997)

Averaged over all possible objective functions on a finite set, all algorithms that do not evaluate the same point twice have the same expected performance.

More precisely: let X and Y be finite non-empty sets and 𝓕 = YX the set of all functions f : X → Y (there are |Y||X| of them). An algorithm a in every step chooses, on the basis of the pairs (x, f(x)) seen so far, a point x ∈ X that it has not evaluated yet, and finds out f(x). Let m ∈ {1, …, |X|} and let dmy = (y1, …, ym) ∈ Ym be the sequence of values the algorithm sees during the first m evaluations. Then for any two such algorithms a1, a2 and every sequence dmy ∈ Ym

∑f ∈ 𝓕 P(dmy | f, m, a1) = ∑f ∈ 𝓕 P(dmy | f, m, a2),

where P(dmy | f, m, a) is the probability that the algorithm a sees exactly the sequence dmy on the function f (for a deterministic algorithm 0 or 1).

So the sum runs over all |Y||X| functions. Every performance measure that depends only on the values seen (for example the best one found) therefore has the same average over all functions.

In plain words

There is no universally best optimiser. An algorithm that excels on one kind of problem must be worse elsewhere. The swarm is excellent on smooth functions with a regular structure, like Rastrigin’s; on the travelling salesman it would not hold up without modifications.

After the success of ants and swarms, the floodgates opened for “nature-inspired” algorithms: fireflies, cuckoos, bats, grey wolves, harmony in a jazz band. In 2015 Kenneth Sörensen pointed out that many of them are old ideas in a new costume — the metaphor replaced the mathematical description and made comparison harder. The right question for any new algorithm is therefore not “what inspired it” but “what does it do differently from the known ones, and where does that show”.

9. Swarms in practice

  • Parameter tuning. PSO is popular where there are few parameters, the gradient is unknown and one evaluation is a simulation: tuning controllers, shaping antennas, managing power grids or the hyperparameters of machine learning models.
  • Robot swarms. At Harvard in 2014, 1,024 small Kilobot robots assembled given shapes by following local rules — with no central control, only by communicating with their neighbours.
  • And what about drones at firework shows? Light shows with thousands of drones are not swarm intelligence: each drone flies along a precomputed path from a central plan. A swarm in the true sense makes its own decisions, based on its neighbours.

10. Summary

  • A particle in PSO changes its velocity according to inertia, its own best place and the swarm’s best place.
  • On the Rastrigin function the swarm found the minimum in 97 out of 100 runs — the evolution strategy with the same start and budget in 32.
  • Inertia controls how long the swarm explores: too little makes it collapse prematurely, too much keeps it from fine-tuning the result. Without the social or without the cognitive component the swarm fails.
  • A particle is like a damped spring; for |w| < 1 and 0 < φ < 2(1 + w) it settles.
  • The ring topology spreads information more slowly and tends to be more robust on hard problems; and by the no free lunch theorem there is no optimiser that is best at everything.

Want to try it yourself? Have an AI agent write a PSO with a visualisation of the swarm and give it your own landscape — say, a function with two distant valleys — and watch whether the swarm splits up.

Do you see it differently? Write to support@coden-app.com.

An Agent on your computer, driven from your phone