PySwarms Documentation
repository·master·Indexed 23 days ago
https://github.com/ljvmiranda921/pyswarmsAn extensible research toolkit for Particle Swarm Optimization (PSO) in Python. It provides a high-level declarative interface for swarm intelligence problems, featuring single-objective and discrete optimizers, hyperparameter search via RandomSearch, and visualization tools for cost histories and particle movement. The library includes a backend module for building custom swarm implementations and handlers for managing boundaries, velocity, and dynamic options.
What's inside PySwarms
- PySwarms is an extensible research toolkit for particle swarm optimization (PSO) in Python. It provides a high-level declarative interface for implementing PSO in various problems, making it suitable for swarm intelligence researchers, practitioners, and students. It supports basic optimization with PSO and interaction with swarm optimizations.
Overview of PySwarms features
masterPySwarms is a research toolkit for Particle Swarm Optimization (PSO) that includes several key features for both practitioners and researchers:
- Standard PSO Algorithms: Native
numpy-based implementations of classic global-best PSO, local-best PSO, and binary PSO for discrete optimization. - Built-in Objective Functions: A collection of single-objective functions (e.g., sphere, Beale, and Rastrigin functions) for testing optimizers.
- Visualization: A
matplotlib-based plotting environment for visualizing costs and animating swarms in both 2D and 3D. - Hyperparameter Tuning: Tools for performing random and grid search to optimize hyperparameters that control swarm behavior.
- Extensibility: Base classes designed for implementing custom single-objective optimizers.
- Standard PSO Algorithms: Native
Use the pyswarms.single package for single-swarm optimization
masterThepyswarms.singlepackage provides implementations of Particle Swarm Optimization (PSO) algorithms designed for a single swarm. It contains different variants of the optimizer based on how particles update their positions, specifically focusing on whether they follow a global best or a local best strategy.Explore PySwarms utilities
masterThe
pyswarms.utilsmodule provides various tools to assist in the optimization process. These utilities are organized into several submodules based on their functionality:- Decorators (
pyswarms.utils.decorators): Tools for modifying or wrapping functions. - Functions (
pyswarms.utils.functions): Includes benchmark objective functions for testing. - Plotters (
pyswarms.utils.plotters): Functionalities for visualizing optimization results. - Reporter (
pyswarms.utils.reporter): Tools for reporting optimization progress or results. - Search (
pyswarms.utils.search): Utilities for hyperparameter search to optimize the PSO configuration itself.
- Decorators (
Use off-the-shelf optimizers for standard algorithms
masterPySwarms provides off-the-shelf implementations of standard optimization algorithms, including classic variants like global-best and local-best. These are designed for quick-and-easy optimization problems. The optimizers are organized into two main categories:
- Single-objective optimizers: Found in
pyswarms.single. - Discrete optimizers: Found in
pyswarms.discrete.
- Single-objective optimizers: Found in
What is Particle Swarm Optimization (PSO)?
masterParticle Swarm Optimization (PSO) is a computational method that emulates the social behavior of groups (swarms), such as birds or fish, to find the global optimum of a function.
In PSO:
- Particles: Represent individual candidate solutions scattered throughout the search space.
- Swarm: The collective group of particles.
- Global Optimum: The 'treasure' or target value being searched for.
Particles move through the search space based on two main influences:
- Cognitive desire: The particle's tendency to search individually based on its own experience.
- Social influence: The collective action of the group or its neighbors, where particles move towards the best-known positions found by the swarm.
A key advantage of PSO is that it is a gradient-free optimizer, meaning the objective functions being optimized do not need to be differentiable.
Use Continuous Single-Objective Optimizers
masterPySwarms provides several Particle Swarm Optimization (PSO) algorithms for continuous search spaces. You can choose between different topologies depending on how particles interact:
pyswarms.single.global_best: A classic global-best PSO with a star-topology. Each particle compares its performance against the single best-performing particle in the entire swarm.pyswarms.single.local_best: A classic local-best PSO with a ring-topology. Each particle only compares itself with its nearest neighbors based on a distance metric.pyswarms.single.general_optimizer: A flexible PSO that allows for custom topologies. You can pass any topology defined in thepyswarms.backendmodule as an argument.
Understand the PySwarms backend module
masterThe
pyswarms.backendmodule serves as the core engine of the library. It provides the primitive methods and classes required to build custom swarm implementations. High-level Particle Swarm Optimization (PSO) algorithms provided by the library, such asGlobalBestPSOandLocalBestPSO, are constructed using these backend components. Developers looking to extend the library or create specialized swarm behaviors should interact with the following sub-modules:pyswarms.backend: Core primitives.pyswarms.handlers: Logic for handling swarm updates and constraints.pyswarms.topology: Definitions for how particles communicate (e.g., global vs. local topologies).pyswarms.swarms: Implementation of swarm structures.
Understand the pyswarms.topology package
masterThe
pyswarms.topologypackage provides various swarm topology implementations that can be used to build custom swarm algorithms. A topology defines how particles interact and share information within a swarm. Specifically, a topology implementation is capable of:- Determining the best particle in a given swarm.
- Computing the next position based on the current swarm position.
- Computing velocities based on a specific swarm configuration.
How to write a custom optimization loop
masterTo implement a custom swarm algorithm, you can bypass the high-level API and write your own optimization loop using the
pyswarms.backendmodule. This approach requires you to manually manage the interaction between two core components: the Swarm class and a Topology class.In each iteration of your loop, you retrieve the current state from the
Swarminstance and update its attributes using methods provided by aTopologyclass or thepyswarms.backend.operatorsmodule. This allows for a highly flexible, black-box implementation of different swarm behaviors.Core design principles and data conventions in PySwarms
masterPySwarms is designed to balance ease-of-use with ease-of-experimentation. To ensure repeatability and a consistent framework, the library follows specific data conventions for swarm representations:
- Swarm Representation: A swarm $\mathcal{S}$ is defined as an $m \times n$ matrix, where $m$ is the number of particles and $n$ is the number of dimensions in the search space.
- Fitness Representation: Fitness is expressed as an $m$-dimensional array, containing one value for each particle.
This convention allows the library to provide a high-level declarative interface while maintaining a consistent API for custom implementations.
Use the Swarm class to build custom swarm implementations
masterThepyswarms.swarms.Swarmclass is aDataClassdesigned to facilitate the creation of custom swarm implementations. It serves as a container for information regarding the particles generated during each timestep of the optimization process. Developers can use this class as a flexible foundation to build and manage their own swarm logic and particle data.