Registry / ai-ml / pyswarms

pyswarms

JSON →
library1.3.0pypypi✓ verified 86d ago

PySwarms is a Python library for Particle Swarm Optimization (PSO). It provides a simple and intuitive API for implementing PSO algorithms, including global best (gbest), local best (lbest), and binary PSO, as well as tools for visualization and hyperparameter tuning. The current version is 1.3.0, released in 2021. The project is in maintenance mode with quarterly release cadence.

pip install pyswarms
INSTALL
IMPORT
SIG · PYSWARMS
P
pyswarms
ai-mlpythonv1.3.0
Install
12.0s avg
Import
1721ms
Disk
331MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.3.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 1.780s · 327.1MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 12.0s · import 1.662s · 315MB
331MB installed
● package 331MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

GlobalBestPSO
from pyswarms.single import GlobalBestPSO
import pyswarms
Importing the package alone does not give access to optimizer classes; you must import from submodules.
LocalBestPSO
from pyswarms.single import LocalBestPSO
from pyswarms import LocalBestPSO
LocalBestPSO is not in the top-level pyswarms namespace.
BinaryPSO
from pyswarms.discrete import BinaryPSO
from pyswarms.single import BinaryPSO
BinaryPSO is in the discrete submodule, not single.
plot_cost_history
from pyswarms.utils.plotters import plot_cost_history
from pyswarms import plot_cost_history
Plotting functions are under utils.plotters.

Minimal example optimizing the sphere function with global best PSO.

import numpy as np from pyswarms.single import GlobalBestPSO def sphere(x): return np.sum(x**2, axis=1) optimizer = GlobalBestPSO(n_particles=10, dimensions=2, options={'c1': 0.5, 'c2': 0.3, 'w':0.9}) cost, pos = optimizer.optimize(sphere, iters=100) print("Best cost:", cost) print("Best position:", pos)
Debug
Known issues
deprecatedThe 'animate' module and some plotting functions have been deprecated in favor of plotters. Check documentation for the correct API.
fix
Use pyswarms.utils.plotters instead of pyswarms.utils.animate.
affects: 1.0.0+
gotchaThe optimizer modifies the objective function's arguments; ensure the function signature is correct (returns array of shape (n_particles,)). Mistakes lead to shape errors.
fix
Your objective function must accept a 2D array (rows=particles, cols=dimensions) and return a 1D array of costs.
affects: all
gotchaAlways pass options as a dict with keys 'c1', 'c2', 'w' for PSO variants. Missing keys cause KeyError or unexpected behavior.
fix
options = {'c1': 0.5, 'c2': 0.3, 'w': 0.9}
affects: all
breakingVersion 1.0.0 changed the backend API and removed many old functions. Code written for v0.x may not work.
fix
Review migration guide: https://pyswarms.readthedocs.io/en/latest/migration.html
affects: <1.0.0 vs >=1.0.0
Errors
Common errors & fixes
AttributeError: module 'pyswarms' has no attribute 'GlobalBestPSO'
Trying to import optimizer from top-level pyswarms.
fix
Use: from pyswarms.single import GlobalBestPSO
ValueError: operands could not be broadcast together with shapes (10,2) (2,)
Objective function returns wrong shape (e.g., returns a single value for all particles).
fix
Ensure objective function returns an array of shape (n_particles,), i.e., one cost per particle.
KeyError: 'c1'
Missing required key in options dict for optimizer.
fix
Provide options as dict: {'c1': 0.5, 'c2': 0.3, 'w': 0.9}
TypeError: 'NoneType' object is not callable
Using the old animate module incorrectly or passing None as a callback.
fix
Use the new plotters API: from pyswarms.utils.plotters import plot_cost_history
Upgrade
Version history
1.3.0latest on PyPI · released Jan 3, 2021
Audit
Dependencies

No dependency data recorded yet.

Agent activity
29 hits · last 30 days
node
26
OpenAI (training)
1
Resources
pyswarms — pip install pyswarms · libregistry