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
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 1.780s · 327.1MB
glibcpy 3.10–3.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.fixUse 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.fixYour 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.fixoptions = {'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.fixReview 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.
fixUse: 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).
fixEnsure 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.
fixProvide 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.
fixUse 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.