pyswarm

Particle Swarm Optimization (PSO) for Python


Keywords
optimization, particle, python, swarm
License
BSD-3-Clause
Install
pip install pyswarm==0.8.0

Documentation

PySwarm

Particle Swarm Optimization (PSO) for Python

Tests Documentation Ruff

codecov Quality Gate Status License: BSD-3

PyPI Downloads Python versions

pyswarm is a gradient-free, evolutionary optimization library for Python that implements Particle Swarm Optimization (PSO) with built-in support for constraints. It is lightweight, easy to use, and suitable for a wide range of optimization problems where gradient information is unavailable or impractical to compute.

Quick example

import numpy as np
from pyswarm import pso


def objective(x):
    x1, x2 = x
    return x1**4 - 2 * x2 * x1**2 + x2**2 + x1**2 - 2 * x1 + 5


lb = [-3, -1]
ub = [2, 6]

xopt, fopt = pso(objective, lb, ub)

print("Optimal solution:", xopt)
print("Function value:", fopt)

Installation

pip install pyswarm

Requires Python 3.10+. No external runtime dependencies. See the full installation guide for uv, poetry, and source builds.

Documentation

License

BSD-3-Clause — see LICENSE.