🐝 Particle Swarm
Swarm intelligence optimization
Iteration 0
Best value:
Function
Parameters
Controls
Stats
Iterations
0
Best value
gbest (x, y)
Status
Ready
Info & Theory

Particle swarm optimization (PSO) mimics a flock of birds searching for food. Each particle is a candidate solution that flies through the search space, remembering where it personally did best and listening to the swarm's best find.

The velocity update

Every step each particle updates its velocity:

v = w·v + c1·r1·(pbest − x) + c2·r2·(gbest − x)

  • w — inertia, how much old motion is kept.
  • c1 — cognitive pull toward its own best.
  • c2 — social pull toward the global best.
  • r1, r2 — fresh random numbers in [0, 1].

Then the position moves: x = x + v.

Cost landscape

The background heatmap is the function value (dark = low cost). The swarm tries to reach the darkest point — the global minimum.

Test functions

  • Sphere — one smooth bowl, easy.
  • Rastrigin — many regular local minima.
  • Ackley — flat outer plate, deep central well.
  • Himmelblau — four equal global minima.

Exploration vs exploitation

High inertia and large coefficients spread the swarm out (exploration); low values let it tighten on the best region (exploitation). Tuning this balance is the art of PSO.

FAQ
What is particle swarm optimization?

A population-based metaheuristic inspired by flocking birds. A swarm of candidate solutions (particles) moves through the search space, each guided by its own best position and the swarm's best position.

How is a particle's velocity updated?

With v = w·v + c1·r1·(pbest − x) + c2·r2·(gbest − x), where w is inertia, c1 and c2 are the cognitive and social coefficients, and r1, r2 are random in [0,1]. Then x = x + v.

What are pbest and gbest?

pbest is the best position a single particle has personally visited; gbest is the best position found by the whole swarm. The velocity pulls each particle toward both.

What does the inertia weight w control?

How much of the previous velocity is carried over. High inertia favours exploration; low inertia favours local exploitation. Values around 0.4 to 0.9 are common.

What are the c1 and c2 coefficients?

c1 (cognitive) weights the pull toward a particle's own best; c2 (social) weights the pull toward the global best. Balancing them trades exploration against swarm consensus.

What test functions are used here?

The sphere (one smooth bowl), Rastrigin and Ackley (many local minima), and Himmelblau (four equal global minima) — standard optimisation benchmarks.

How does the swarm avoid local minima?

Particles keep momentum and are pulled toward different attractors, so the swarm spreads out and samples many basins. When one particle finds a better region, gbest updates.

Does PSO always find the global minimum?

No. It is a heuristic with no global guarantee, especially on rugged functions. Larger swarms, tuned coefficients and multiple runs improve the odds.

How is PSO different from a genetic algorithm?

PSO moves existing solutions through space using velocity and shared information, with no crossover or mutation. Genetic algorithms breed new solutions by recombining parents.

Where is PSO used in practice?

Tuning neural networks and controllers, designing antennas and power systems, scheduling, and many continuous problems where gradients are unavailable.