A research toolkit for particle swarm optimization in Python
This minor release contains multiple documentation and CI/CD improvements. Thank you for everyone who helped out in this version! I apologize for this very late release--life happened:
This new version adds support for parallel particle evaluation, better documentation, multiple fixes, and updated build dependencies.
This is the first major release of PySwarms. Starting today, we will be adhering to a better semantic versioning guidelines. We will be updating the project wikis shortly after. The maintainers believe that PySwarms is mature enough to merit a version 1, this would also help us release more often (mostly minor releases) and create patch releases as soon as possible.
Also, we will be maintaining a quarterly release cycle, where the next minor release (v.1.1.0) will be on June. All enhancements and new features will be staged on the development
branch, then will be merged back to the master
branch at the end of the cycle. However, bug fixes and documentation errors will merit a patch release, and will be merged to master
immediately.
Reporter
module - #227@cost
decorator which automatically scales to the whole swarm - #226Optimizers
- #232environments
module was removed - #217We're proud to present the release of PySwarms version 0.3.0! Coinciding with this, we would like to welcome Aaron Moser (@whzup) as one of the project's maintainers! v.0.3.0 includes new topologies, a static
option to configure a particle's neighbor/s, and a revamped plotters
module. We would like to thank our contributors for helping us with this release.
pyswarms.backend
module - #142, #151, #155, #177GeneralOptimizerPSO
class. The GeneralOptimizerPSO
class has an additional attribute for the topology used in the optimization - #151plotters
module for swarm visualization. The environments
module is now deprecated - #135, #172setup.py
not running on Windows - #175GeneralOptimizerPSO
ClassNew topologies were added to improve the ability to customize how a swarm behaves during optimization. In addition, a GeneralOptimizerPSO
class was added to enable switching-out various topologies. Check out the description below!
static attribute
The newly added topologies expand on the existing ones (Star and Ring topology) and increase the built-in variety of possibilities for users that want to build their custom swarm implementation from the pyswarms.backend
module. The new topologies include:
- Pyramid
topology: Computes the neighbours using a Delaunay triangulation of the particles.
- Random
topology: Computes the neighbours randomly, but systematically.
- VonNeumann
topology: Computes the neighbours using a Von Neumann topology (inherited from the Ring topology)
With these new topologies, the ability to change the behaviour of the topologies was added in form of a static
argument that is passed when initializing a Topology
class. The static
parameter is a boolean that decides whether the neighbours in the topologies are computed every iteration (static=False
) or only in the first one (static=True
). It is passed as a parameter at the initialization of the topology and is False
by default. Additionally, the LocalBestPSO
now also takes a static
parameter to pass this information to its Ring topology. For an example see below.
GeneralOptimizerPSO
classThe new topologies can also be easily used in the new GeneralOptimizerPSO
class which extends the collection of optimizers. In addition to the parameters used in the GlobalBestPSO
and LocalBestPSO
classes, the GeneralOptimizerPSO
uses a topology
argument. This argument passes a Topology
class to the GeneralOptimizerPSO
.
from pyswarms.single import GeneralOptimizer
from pyswarms.backend.topology import Random
options = {"w": 1, "c1": 0.4, "c2": 0.5, "k": 3}
topology = Random(static=True)
optimizer = GeneralOptimizerPSO(n_particles=20, dimensions=4, options=options, bounds=bounds, topology=topology)
plotters
moduleThe environments module is now deprecated. Instead, we have a plotters module that takes a property of the optimizer and plots it with minimal effort. The whole module is built on top of matplotlib
.
import pyswarms as ps
from pyswarms.utils.functions import single_obj as fx
from pyswarms.utils.plotters import plot_cost_history
# Set-up optimizer
options = {'c1':0.5, 'c2':0.3, 'w':0.9}
optimizer = ps.single.GlobalBestPSO(n_particles=50, dimensions=2, options=options)
optimizer.optimize(fx.sphere_func, iters=100)
# Plot the cost
plot_cost_history(optimizer.cost_history)
plt.show()
We can also plot the animation...
from pyswarms.utils.plotters.formatters import Mesher
from pyswarms.utils.plotters.formatters import Designer
from pyswarms.utils.plotters import plot_contour, plot_surface
# Plot the sphere function's mesh for better plots
m = Mesher(func=fx.sphere_func)
# Adjust figure limits
d = Designer(limits=[(-1,1), (-1,1), (-0.1,1)],
label=['x-axis', 'y-axis', 'z-axis'])
In 2D,
plot_contour(pos_history=optimizer.pos_history, mesher=m, mark=(0,0))
Or in 3D!
pos_history_3d = m.compute_history_3d(optimizer.pos_history) # preprocessing
animation3d = plot_surface(pos_history=pos_history_3d,
mesher=m, designer=d,
mark=(0,0,0))
pyswarms.backend
module for custom swarm algorithms. Users can now use some primitives provided in this module to write their own optimization loop, providing a more "white-box" approach in swarm intelligence - #119, #115, #116, #117The new backend module exposes some swarm optimization primitives so that users can create their custom swarm implementations without relying too much on our base classes. There are two main components for the backend, the Swarm
class and the Topology
base class. Using these classes, you can construct your own optimization loop like the one below:
This class acts as a data class that holds all necessary attributes in a given swarm. The idea is to continually update the attributes located there. You can easily initialize this class by providing the initial position and velocity matrices.
The topology class abstracts away common operations in swarm optimization: (1) determining the best particle in the swarm, (2) computing the next position, and (3) computing the velocity matrix. As of now, we only have the Ring
and Star
topologies implemented. Hopefully, we can add more in the future.