Particle Swarm Optimization — Hidden Intelligence in Bird Flight
Minimal, hackable PSO playground built with pygame, numpy, and matplotlib. Tweak the fitness function, watch the swarm react, and keep iterating.
# using uv (recommended)
uv sync
uv run python main.py
# or plain pip
pip install -e .
python main.pyYou need Python 3.13+ because this repo leans on the latest pygame wheels.
- Vectorized PSO core with inertia/cognitive/social parameters exposed at the top of
main.py. - A 3D surface preview (
preview_3d()) so you can sanity-check the objective before the swarm launches. - A grab bag of alternate functions (Gaussian, Rastrigin, Ackley, Griewank, etc.) ready to uncomment.
- Color-mapped heat field rendered via
pygamefor instant feedback at 60 FPS.
- Objective: swap the lambda assigned to
f(x, y)or plug in your own function. Keep it vectorizable for fast previews. - Swarm behavior: tune
a,b,b_hat, andcto explore constriction vs. exploration regimes. - Grid resolution: adapt
WINDOW_SIZEandPARTICLE_SIZEto trade off fidelity vs. speed. - Visualization: comment out
preview_3d()if you want to skip the Matplotlib surface step.
- Full write-up on the underlying ideas
_reading6 1995 particle swarming.pdffor the original Kennedy & Eberhart paper.


