A research toolkit for particle swarm optimization in Python
MIT License
Bot releases are hidden (Show)
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:
Published by ljvmiranda921 over 5 years ago
This new version adds support for parallel particle evaluation, better documentation, multiple fixes, and updated build dependencies.
Published by ljvmiranda921 over 5 years ago
Published by ljvmiranda921 over 5 years ago
Published by ljvmiranda921 over 5 years ago
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.
Published by ljvmiranda921 over 5 years ago
Reporter
module - #227@cost
decorator which automatically scales to the whole swarm - #226Optimizers
- #232environments
module was removed - #217Published by ljvmiranda921 about 6 years ago
Published by ljvmiranda921 about 6 years ago
We'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))
Published by ljvmiranda921 over 6 years ago
Published by ljvmiranda921 over 6 years ago
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.
Published by ljvmiranda921 over 6 years ago
After three months, we are happy to present our next development release, version v.0.1.9! This release introduces non-breaking changes in the API and minor fixes adopting pylint
's and flake8
's strict conventions. This release would not have been possible without the help of @mamadyonline and our new Collaborator Siobhan K. Cronin! Thank you for all your help and support in maintaining PySwarms!
NEW: Ability to set the initial position of the swarm - #93
NEW: Ability to set a tolerance value to break the iteration - #93, #100
FIX: Fix for the Rosenbrock function returning the incorrect shape - #98
Before, the swarm particles were generated randomly with respect to a lower and upper bound that we set during initialization. Now, we have the ability to initialize our swarm particles around a particular location, just in case we have applications that require that feature.
Addtionally, we added a tolerance value to decrease optimization time. Usually, we just wait for a given number of iterations until the optimization finishes. We have now improved this and included a ftol
parameter that serves as a threshold whenever the difference in the costs are not as significant anymore.
Turns out that there is something wrong with our Rosenbrock function for it does not return a vector of shape (n_particles, )
. Don't worry, we have fixed that!
Published by ljvmiranda921 almost 7 years ago
Published by ljvmiranda921 about 7 years ago
local_best.py
and binary.py
not returning the best cost they have encountered in the optimization process - #34Published by ljvmiranda921 about 7 years ago
PlotEnvironment
PySwarms now implements a native version of GridSearch
and RandomSearch
to help you find the best hyperparameters in your swarm. To use this feature, simply call the RandomSearch
and GridSearch
classes from the pyswarms.utils.search
module.
import numpy as np
import pyswarms as ps
from pyswarms.utils.search import RandomSearch
from pyswarms.utils.functions import single_obj as fx
# Set-up choices for the parameters
options = {
'c1': (1,5),
'c2': (6,10),
'w': (2,5),
'k': (11, 15),
'p': 1
}
# Create a RandomSearch object
# n_selection_iters is the number of iterations to run the searcher
# iters is the number of iterations to run the optimizer
g = RandomSearch(ps.single.LocalBestPSO, n_particles=40,
dimensions=20, options=options, objective_func=fx.sphere_func,
iters=10, n_selection_iters=100)
best_score, best_options = g.search()
This then returns the best score found during optimization and the hyperparameter options that enabled it.
>>> best_score
1.41978545901
>>> best_options['c1']
1.543556887693
>>> best_options['c2']
9.504769054771
Most of the swarm classes now inherit the base class in order to demonstrate its extensibility. If you are a developer or a swarm researcher planning to implement your own algorithms, simply inherit from these Base Classes and implement the optimize()
method.
Published by ljvmiranda921 about 7 years ago
This new plotting environment makes it easier to plot the costs and swarm movement in 2-d or 3-d planes. The PlotEnvironment
class takes in the optimizer and its parameters as arguments. It then performs a fresh run to plot the cost and to create animations.
An example of usage can be seen below:
import pyswarms as ps
from pyswarms.utils.functions import single_obj as fx
from pyswarms.utils.environments import PlotEnvironment
# Set-up optimizer
options = {'c1':0.5, 'c2':0.3, 'w':0.9}
optimizer = ps.single.GlobalBestPSO(n_particles=10, dimensions=3, options=options)
# Initialize plot environment
plt_env = PlotEnvironment(optimizer, fx.sphere_func, 1000)
# Plot the cost
plt_env.plot_cost(figsize=(8,6));
plt.show()