All projects

Michigan Tech · Robot Operating Systems

Particle Swarm Optimizer (ROS 2)

A swarm of simulated particles that searches a landscape for its lowest point, built as two ROS 2 nodes for my Robot Operating Systems final project, with a live version you can run in the browser.

  • Particle
  • Its best spot
  • Swarm's best
  • True minimum
Swarm's best score (lower is better, log scale)
Velocity parts (average size per epoch)
  • Inertia
  • Own best
  • Swarm best

Particle 1

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

Click any particle on the map to inspect it.

Settings
Swarm
Run

A browser port of the controller from my ROS 2 project. In ROS, the swarm and the landscape were separate nodes talking through a service; here both run in your browser. Defaults match the ROS runs: 10 particles, w = 0.7, c1 = c2 = 1.4, speed limit 20% of the search width.

Problem

My final project for Robot Operating Systems: minimize a set of benchmark functions with Particle Swarm Optimization, built in ROS 2 around the idea that every part of a system can be a node. The brief fixed the setup: 10 particles, 100 epochs, three benchmark functions (Sphere, Rastrigin, and Rosenbrock) with suggested search areas, a speed limit of 20% of the search width, and results shown in rqt and/or matplotlib.

We were given pseudocode for the algorithm (starting the swarm, the epoch loop, and picking the global best) and Python definitions of the three functions. Everything else was mine: setting up the ROS 2 environment on Ubuntu from scratch, designing the nodes and the service between them, turning the pseudocode into a working controller, and choosing and justifying the parameters. It was heavy on Linux, with no hand-holding.

Approach

  • A /function_service node is the unit under test: it scores any (x, y) through a custom Evaluate2D service, so the benchmark functions live apart from the PSO logic. A /pso_controller node runs the swarm, calls the service to score each move, and publishes the particle positions and the global best.
  • The original idea was one ROS node per particle. That would have been heavy to compute and hard to keep in step, so I kept the swarm inside the controller as arrays of positions, velocities, and personal bests. No synchronization between nodes, and each epoch runs with no delay.
  • Each epoch, every particle’s velocity is a mix of three pulls: keep going (inertia), head back toward the best spot it has found (cognitive), and head toward the best spot anyone has found (social), each with a random strength. The velocity is clamped, positions stay inside the search area, and the global best updates once the whole swarm has moved.
  • The run stops early if the best is tiny and hasn’t moved for 10 epochs. At the end, matplotlib plots the best fitness and the size of each velocity pull.

Choosing the parameters

I tuned the gains on Sphere, then kept one set for all three functions:

  • Inertia w = 0.7. Below 1, so a particle’s speed dies down over time and it doesn’t keep overshooting.
  • Cognitive and social gains c1 = c2 = 1.4. Equal, so the swarm isn’t biased toward following itself or the group, and small enough in total to explore without wild oscillations.
  • Speed limit at 20% of the search width. Big enough for real moves, small enough not to overshoot the minima badly.

What I found

  • Sphere converged fastest, reaching its minimum after about 60 epochs.
  • Rosenbrock showed a sharp velocity burst early, like Sphere, then took longer to settle as the swarm slid along the edges of its steep valley.
  • Rastrigin plateaued and never reached zero in 100 epochs: the particles were trapped in local minima. Its rough surface showed the limits of one fixed setup and a small swarm.
  • In every run the social pull dominated the first few epochs, while the particles were still spread out. Then all three pulls decayed toward zero, oscillating early as particles overshot and corrected.

What I’d do differently

One set of parameters worked for the smooth functions, but it wasn’t aggressive enough for Rastrigin. For rough surfaces I’d use a bigger swarm or more inertia, and tune per function instead of sharing one setup.

The browser version above lets you try exactly that: the “40 particles escape” preset frees the stuck Rastrigin run, and the inertia race starts high and lets it fade.

The browser version

The demo at the top is a port of my controller to JavaScript, so anyone can run it without ROS. It keeps the same update rule, parameters, search areas, speed limit, and stopping rule. In ROS, the swarm and the landscape were separate nodes talking over a service; here both run in your browser. The extra landscapes (Ackley, Himmelblau), the ring layout, and shrinking inertia are additions for the demo, not part of the original project.

Media