Interactive companion · Chapter 9 · Particle swarm optimization

Particle Swarm Optimization Lab

Watch candidate solutions fly through a search space, learning from their own memory and from their neighbors. Change any parameter and see what it does.

Based on Chapter 9, “Particle swarm optimization,” in Alaa Khamis, Optimization Algorithms: AI techniques for design, planning, and control problems, Manning Publications, 2024 (ISBN 978-1633438835).

A live swarm searching the Rastrigin function; the gold star is the global best.

Part 1 · Source of inspiration

From flocks and schools to a search algorithm

Particle swarm optimization (Kennedy and Eberhart, 1995) treats each candidate solution as a particle in a flock, guided by one principle: emulate the success of neighboring individuals. The panel shows the three local rules that make a flock emerge.

Chapter 9 opens with the wider family of swarm intelligence algorithms and the elements every swarm system shares.

Bird flocking: three local rules

Reynolds boids

Separation, alignment and coherence: set any weight to zero to see the flock lose that behavior.

PSO compared with a genetic algorithm

Unlike a GA, PSO eliminates nobody: each particle’s memory (pbest) and its social link (nbest) replace crossover and mutation.

The book compares PSO with GAs term by term and explains it with a memorable theme-park analogy.

Part 2 · The algorithm

Pseudocode, motion equations and steps

Each particle keeps its position x, velocity v, personal best pbest and the best position in its neighborhood, nbest (gbest for the whole swarm, lbest for a few neighbors). Every iteration it updates its velocity, then moves.

Section 9.2 of the book walks through every step, from initialization to stopping criteria, and explains each term of equations 9.1 to 9.4.

Algorithm: PSO for each particle initialize position x (random) and velocity v (zero or small) pbest ← x end do for each particle calculate fitness f(x) if f(x) is better than f(pbest) # eq. 9.3 pbest ← x end choose nbest (gbest for the star topology) # eq. 9.4 for each particle v ← w·v + c1·r1·(pbest − x) + c2·r2·(nbest − x) # eq. 9.1 x ← x + v # eq. 9.2 end while maximum iterations or minimum error is not reached
vk+1id = w·vkid + c1r1(pbestkid − xkid) + c2r2(gbestkd − xkid)(9.1)
xk+1id = xkid + vk+1id(9.2)

Inertia keeps the particle on course, the cognitive term pulls it toward its own best, and the social term toward the swarm’s best. Drag the points below to see how they combine.

Anatomy of one velocity update

Drag x, pbest, gbest or the tip of vk
inertia w·vkcognitive c1·r1·(pbest − x)social c2·r2·(gbest − x)new velocity vk+1

Choosing the parameters

ParameterTypical valueEffect
Inertia weight w0.792Large w explores; small w exploits.
Acceleration c1, c21.49Weights of the cognitive and social pulls.
Swarm size N20 to 40Larger swarms explore more at higher cost.

Chapter 9 explains how to set every parameter, including Vmax, the initial velocity and the update mode.

Neighborhood topologies

The topology decides who shares information with whom, and so how fast good news spreads. Pick a topology and click a particle to see its neighborhood.

The book explains how each topology trades fast convergence for robust exploration, and which suits unimodal or multimodal problems.

Communication structure

Click a node

Part 3 · Variant 1

Continuous PSO

Every coordinate is a real number, so equations 9.1 and 9.2 apply directly.

Use case 1: worked example, maximize f(x) = −x² + 2x + 11

Four particles maximize f(x) on −2 ≤ x ≤ 2 (maximum 12 at x = 1). Click Next iteration to apply equations 9.1 and 9.2 by hand; the table shows every update.

Chapter 9 works through the first two iterations by hand, with numbers you can check against the panel.

Hand calculation, one iteration at a time

Iteration 0

Use case 2: function optimization with a full swarm

Pick a benchmark, a topology and the parameters, then press Run. Thin lines link each particle to the leader it follows.

Listing 9.1 in the book solves the Michalewicz function from scratch, with pymoo and with PySwarms.

Continuous swarm lab

Minimization in two dimensions
particlegbestknown optimumpbest

Best cost so far (solid) and the swarm mean cost (faint) per iteration. The dashed line is the known optimum.

Swarm diversity: high means exploring, low means exploiting.

Use case 3: trilateration of a connected vehicle

Three C-V2X cells measure their distances to a vehicle; the vehicle position minimizes Σ ei², the squared distance errors. The table shows one hand-calculated iteration. Select C-V2X trilateration in the swarm lab above to watch a full swarm locate the vehicle.

Listing C.13 in Appendix C solves this localization problem in Python, with the full problem setup.

Part 4 · Variant 2

Binary PSO

In binary PSO each bit is 0 or 1. The velocity stays real but is read as the propensity of the bit to be 1: the sigmoid turns it into a probability, and a random draw sets the bit.

vk+1id = vkid + φ1(pbestkid − xkid) + φ2(gbestkd − xkid)(9.8)
sig(v) = 1 / (1 + e−v)(9.9)
xk+1id = 1 if sig(vk+1id) > r, otherwise 0(9.10)

Section 9.3 of the book explains binary PSO in full, including how φ1, φ2 and Vmax shape the search.

Use case 1: the BPSO worked example

Chapter 9 · Section 9.3

Update bit 3 of particle 4 (bits counted from the left, starting at 0) with the book’s values. Change any value.

Particle (bit to update outlined)
Updated particle

Use case 2: a binary swarm selecting projects under a budget

Practice problem: choose among 16 projects to maximize value under a $60M budget (optimum 91). Rows are particles, columns projects; the shade is sig(v), and a dot marks a 1.

Binary swarm lab

Maximization over {0,1}16
high sig(v)low sig(v)bit = 1gbest row

Best value so far (solid) and mean current value (faint). The dashed line is the optimum.

gbest portfolio
Part 5 · Variant 3

Permutation-based PSO

For the TSP a position is an ordering of cities, so Clerc (2004) redefined the arithmetic: a velocity is a list of swaps. Edit the fields below to try each operation.

Subtraction: position − position = velocity

The swaps that turn Position 2 into Position 1 (Figure 9.9).

Addition: position + velocity = position

Apply the swaps in order (Figure 9.10).

Multiplication: constant × velocity

c = 0 empties, c < 1 truncates and c > 1 augments the list (Figure 9.8).

Section 9.4 of the book builds these operators step by step (Figures 9.8 to 9.10) and shows how equation 9.1 is rewritten with them.

Use case: TSP of 20 major US cities (Listing 9.2)

The 20 US cities from Chapter 8, starting from New York City. A swap-based PSO runs in your browser; turn on 2-opt to see what a simple local search adds.

Listing 9.2 in the book solves this TSP with PySwarms and discusses the route it finds.

Permutation swarm lab

Minimize tour length (km)

Best tour length so far. Dashed lines: the book’s PySwarms result and the best known tour ( km). Basemap: Natural Earth (public domain).

Part 6 · Section 9.5

Adaptive PSO

Adaptive PSO changes w, c1 and c2 while the search runs: it explores broadly at the start and homes in on the best solutions late.

Section 9.5 explains why w > 1 makes the swarm diverge, how velocity clamping helps, and how each parameter shifts the search (Figure 9.11).

Inertia weight schedules (9.5.1)

Three schedules for w: RS picks a random weight each iteration, LTV falls linearly from 0.9 to 0.4, and NLTV falls nonlinearly and reaches the low end early.

Section 9.5.1 gives the formulas for all three schedules (equations 9.11 to 9.13) and plots them in Figure 9.12.

Cognitive and social components (9.5.2)

c1 (learning from oneself) falls and c2 (learning from neighbors) rises, so the swarm starts individualistic and ends cooperative. Press Play t below to watch where the settings sit over time.

Section 9.5.2 gives the c1 and c2 schedules (equations 9.14 and 9.15) and the limit c1 + c2 ≤ 4.

Schedule explorer

Figures 9.12 and 9.13, live

Inertia weight: RS (gold), LTV (blue), NLTV (purple).

c1(t) (gold) falls and c2(t) (blue) rises.

Design space of a PSO: where the LTV and c1, c2 settings at iteration t sit
Random searchCognitive modelSocial modelLocal search
DiversificationIntensification

Strategy comparison: does adapting pay off?

Run each strategy several times on the same seeds and compare average convergence, or try one schedule in the continuous swarm lab.

Adaptive strategies, head to head

Mean of repeated runs

Success means ending within 0.01 of the known optimum; no schedule wins on every problem.

Part 7 · Code

Python in Google Colab

Open the book notebooks in Colab, or the companion notebook with the from-scratch code below, which mirrors each simulation on this page.

Notebooks

From-scratch implementations

Michalewicz with four topologies
# Continuous PSO from scratch (Chapter 9, Section 9.2)
# Minimizes the Michalewicz function with a selectable neighborhood topology.
import numpy as np
import matplotlib.pyplot as plt


def michalewicz(x, m=10):
    i = np.arange(1, x.shape[-1] + 1)
    return -np.sum(np.sin(x) * np.sin(i * x**2 / np.pi) ** (2 * m), axis=-1)


def neighbors(topology, n):
    """Indices each particle listens to (itself included)."""
    cols = int(np.ceil(np.sqrt(n)))
    nb = []
    for i in range(n):
        if topology == "star":            # gbest PSO: everyone talks to everyone
            s = range(n)
        elif topology == "ring":          # lbest PSO: left and right neighbors
            s = [i, (i - 1) % n, (i + 1) % n]
        elif topology == "von_neumann":   # grid: left, right, up, down
            s = [i, (i - 1) % n, (i + 1) % n, (i - cols) % n, (i + cols) % n]
        elif topology == "wheel":         # particle 0 is the hub
            s = range(n) if i == 0 else [i, 0]
        nb.append(np.unique(list(s)))
    return nb


def pso(f, bounds, n_particles=20, iters=100, w=0.792, c1=1.494, c2=1.494,
        topology="star", vmax_frac=0.2, seed=0):
    rng = np.random.default_rng(seed)
    lo, hi = map(np.asarray, bounds)
    d = lo.size
    x = rng.uniform(lo, hi, (n_particles, d))       # random positions
    v = np.zeros_like(x)                            # zero initial velocity
    vmax = vmax_frac * (hi - lo)
    pbest, pbest_f = x.copy(), f(x)                 # pbest = initial position
    nb = neighbors(topology, n_particles)
    history = [pbest_f.min()]
    for k in range(iters):
        # best position found inside each particle's neighborhood
        nbest = np.array([pbest[idx[np.argmin(pbest_f[idx])]] for idx in nb])
        r1, r2 = rng.random((2, n_particles, d))    # new numbers per dimension
        v = w * v + c1 * r1 * (pbest - x) + c2 * r2 * (nbest - x)   # eq. 9.1
        v = np.clip(v, -vmax, vmax)
        x = np.clip(x + v, lo, hi)                                   # eq. 9.2
        fx = f(x)
        improved = fx < pbest_f                                      # eq. 9.3
        pbest[improved], pbest_f[improved] = x[improved], fx[improved]
        history.append(pbest_f.min())
    g = np.argmin(pbest_f)
    return pbest[g], pbest_f[g], history


bounds = ([0, 0], [np.pi, np.pi])
for topology in ["star", "ring", "von_neumann", "wheel"]:
    best_x, best_f, hist = pso(michalewicz, bounds, topology=topology, seed=1)
    print(f"{topology:12s} x* = {np.round(best_x, 3)}  f* = {best_f:.4f}")
    plt.plot(hist, label=topology)

plt.axhline(-1.8013, ls="--", c="gray", label="known minimum")
plt.xlabel("iteration"); plt.ylabel("best cost"); plt.legend()
plt.title("Michalewicz (d = 2): effect of neighborhood topology")
plt.show()

Run each cell in Colab, or paste a tab’s code into any notebook.

Part 8 · Summary

What to remember

Continuous PSO

Inertia, cognitive and social pulls, plus the topology, balance exploration and exploitation.

Binary PSO

Velocity becomes the probability, through the sigmoid, that a bit is 1.

Permutation PSO

Velocity is a list of swaps, and local search such as 2-opt helps a lot.

Adaptive PSO

Schedules for w, c1 and c2 move the swarm from exploration to exploitation.

References: J. Kennedy and R. Eberhart, “Particle swarm optimization,” 1995. J. Kennedy and R. C. Eberhart, “A discrete binary version of the particle swarm algorithm,” 1997. M. Clerc, “Discrete particle swarm optimization, illustrated by the traveling salesman problem,” 2004. A. Khamis, Optimization Algorithms: AI techniques for design, planning, and control problems, Manning, 2024, Chapter 9.

Part 9 · Quiz

Check your understanding

Two quick questions on Chapter 9. Pick an answer to check it.

Q1. In binary PSO, what does the velocity v of one bit represent?

Q2. Which setting shifts a PSO swarm toward exploitation, refining the area around elite solutions?

For more exercises with full solutions, see Appendix C, “Exercises and solutions,” in Optimization Algorithms. The coding solutions are also on GitHub (Chapter 9).