{"id":24409,"library":"pyswarms","title":"PySwarms","description":"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.","status":"maintenance","version":"1.3.0","language":"python","source_language":"en","source_url":"https://github.com/ljvmiranda921/pyswarms","tags":["pso","optimization","swarm-intelligence","particle-swarm","metaheuristic"],"install":[{"cmd":"pip install pyswarms","lang":"bash","label":"Install from PyPI"}],"dependencies":[],"imports":[{"note":"Importing the package alone does not give access to optimizer classes; you must import from submodules.","wrong":"import pyswarms","symbol":"GlobalBestPSO","correct":"from pyswarms.single import GlobalBestPSO"},{"note":"LocalBestPSO is not in the top-level pyswarms namespace.","wrong":"from pyswarms import LocalBestPSO","symbol":"LocalBestPSO","correct":"from pyswarms.single import LocalBestPSO"},{"note":"BinaryPSO is in the discrete submodule, not single.","wrong":"from pyswarms.single import BinaryPSO","symbol":"BinaryPSO","correct":"from pyswarms.discrete import BinaryPSO"},{"note":"Plotting functions are under utils.plotters.","wrong":"from pyswarms import plot_cost_history","symbol":"plot_cost_history","correct":"from pyswarms.utils.plotters import plot_cost_history"}],"quickstart":{"code":"import numpy as np\nfrom pyswarms.single import GlobalBestPSO\n\ndef sphere(x):\n    return np.sum(x**2, axis=1)\n\noptimizer = GlobalBestPSO(n_particles=10, dimensions=2, options={'c1': 0.5, 'c2': 0.3, 'w':0.9})\ncost, pos = optimizer.optimize(sphere, iters=100)\nprint(\"Best cost:\", cost)\nprint(\"Best position:\", pos)","lang":"python","description":"Minimal example optimizing the sphere function with global best PSO."},"warnings":[{"fix":"Use pyswarms.utils.plotters instead of pyswarms.utils.animate.","message":"The 'animate' module and some plotting functions have been deprecated in favor of plotters. Check documentation for the correct API.","severity":"deprecated","affected_versions":"1.0.0+"},{"fix":"Your objective function must accept a 2D array (rows=particles, cols=dimensions) and return a 1D array of costs.","message":"The optimizer modifies the objective function's arguments; ensure the function signature is correct (returns array of shape (n_particles,)). Mistakes lead to shape errors.","severity":"gotcha","affected_versions":"all"},{"fix":"options = {'c1': 0.5, 'c2': 0.3, 'w': 0.9}","message":"Always pass options as a dict with keys 'c1', 'c2', 'w' for PSO variants. Missing keys cause KeyError or unexpected behavior.","severity":"gotcha","affected_versions":"all"},{"fix":"Review migration guide: https://pyswarms.readthedocs.io/en/latest/migration.html","message":"Version 1.0.0 changed the backend API and removed many old functions. Code written for v0.x may not work.","severity":"breaking","affected_versions":"<1.0.0 vs >=1.0.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Use: from pyswarms.single import GlobalBestPSO","cause":"Trying to import optimizer from top-level pyswarms.","error":"AttributeError: module 'pyswarms' has no attribute 'GlobalBestPSO'"},{"fix":"Ensure objective function returns an array of shape (n_particles,), i.e., one cost per particle.","cause":"Objective function returns wrong shape (e.g., returns a single value for all particles).","error":"ValueError: operands could not be broadcast together with shapes (10,2) (2,)"},{"fix":"Provide options as dict: {'c1': 0.5, 'c2': 0.3, 'w': 0.9}","cause":"Missing required key in options dict for optimizer.","error":"KeyError: 'c1'"},{"fix":"Use the new plotters API: from pyswarms.utils.plotters import plot_cost_history","cause":"Using the old animate module incorrectly or passing None as a callback.","error":"TypeError: 'NoneType' object is not callable"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}