Package: pyswarms
PySwarms is an extensible research toolkit for particle swarm optimization
(PSO) in Python.It is intended for swarm intelligence researchers, practitioners, and
students who prefer a high-level declarative interface for implementing PSO
in their problems. PySwarms enables basic optimization with PSO and
interaction with swarm optimizations. Check out more features below!Free software:MIT licenseDocumentation:https://pyswarms.readthedocs.io.Python versions:3.5 and aboveFeaturesHigh-level module for Particle Swarm Optimization. For a list of all optimizers, checkthis link.Built-in objective functions to test optimization algorithms.Plotting environment for cost histories and particle movement.Hyperparameter search tools to optimize swarm behaviour.(For Devs and Researchers): Highly-extensible API for implementing your own techniques.InstallationTo install PySwarms, run this command in your terminal:$pipinstallpyswarmsThis is the preferred method to install PySwarms, as it will always install
the most recent stable release.In case you want to install the bleeding-edge version, clone this repo:$gitclone-bdevelopmenthttps://github.com/ljvmiranda921/pyswarms.gitand then run$cdpyswarms
$pythonsetup.pyinstallRunning in a Vagrant BoxTo run PySwarms in a Vagrant Box, install Vagrant by going tohttps://www.vagrantup.com/downloads.htmland downloading the proper packaged from the Hashicorp website.Afterward, run the following command in the project directory:$vagrantprovision
$vagrantup
$vagrantsshNow you're ready to develop your contributions in a premade virtual environment.Basic UsagePySwarms provides a high-level implementation of various particle swarm
optimization algorithms. Thus, it aims to be user-friendly and customizable.
In addition, supporting modules can be used to help you in your optimization
problem.Optimizing a sphere functionYou can import PySwarms as any other Python module,importpyswarmsaspsSuppose we want to find the minima off(x) = x^2using global best
PSO, simply import the built-in sphere function,pyswarms.utils.functions.sphere(), and the necessary optimizer:importpyswarmsaspsfrompyswarms.utils.functionsimportsingle_objasfx# Set-up hyperparametersoptions={'c1':0.5,'c2':0.3,'w':0.9}# Call instance of PSOoptimizer=ps.single.GlobalBestPSO(n_particles=10,dimensions=2,options=options)# Perform optimizationbest_cost,best_pos=optimizer.optimize(fx.sphere,iters=100)This will run the optimizer for100iterations, then returns the best cost
and best position found by the swarm. In addition, you can also access
various histories by calling on properties of the class:# Obtain the cost historyoptimizer.cost_history# Obtain the position historyoptimizer.pos_history# Obtain the velocity historyoptimizer.velocity_historyAt the same time, you can also obtain the mean personal best and mean neighbor
history for local best PSO implementations. Simply calloptimizer.mean_pbest_historyandoptimizer.mean_neighbor_historyrespectively.Hyperparameter search toolsPySwarms implements a grid search and random search technique to find the
best parameters for your optimizer. Setting them up is easy. In this example,
let's try usingpyswarms.utils.search.RandomSearchto find the optimal
parameters forLocalBestPSOoptimizer.Here, we input a range, enclosed in tuples, to define the space in which the
parameters will be found. Thus,(1,5)pertains to a range from 1 to 5.importnumpyasnpimportpyswarmsaspsfrompyswarms.utils.searchimportRandomSearchfrompyswarms.utils.functionsimportsingle_objasfx# Set-up choices for the parametersoptions={'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 optimizerg=RandomSearch(ps.single.LocalBestPSO,n_particles=40,dimensions=20,options=options,objective_func=fx.sphere,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 enable it.>>>best_score1.41978545901>>>best_options['c1']1.543556887693>>>best_options['c2']9.504769054771Swarm visualizationIt is also possible to plot optimizer performance for the sake of formatting.
The plotters module is built on top ofmatplotlib, making it
highly-customizable.importpyswarmsaspsfrompyswarms.utils.functionsimportsingle_objasfxfrompyswarms.utils.plottersimportplot_cost_history,plot_contour,plot_surfaceimportmatplotlib.pyplotasplt# Set-up optimizeroptions={'c1':0.5,'c2':0.3,'w':0.9}optimizer=ps.single.GlobalBestPSO(n_particles=50,dimensions=2,options=options)optimizer.optimize(fx.sphere,iters=100)# Plot the costplot_cost_history(optimizer.cost_history)plt.show()We can also plot the animation...frompyswarms.utils.plotters.formattersimportMesher,Designer# Plot the sphere function's mesh for better plotsm=Mesher(func=fx.sphere,limits=[(-1,1),(-1,1)])# Adjust figure limitsd=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,designer=d,mark=(0,0))Or in 3D!pos_history_3d=m.compute_history_3d(optimizer.pos_history)# preprocessinganimation3d=plot_surface(pos_history=pos_history_3d,mesher=m,designer=d,mark=(0,0,0))ContributingPySwarms is currently maintained by a small yet dedicated team:Lester James V. Miranda (@ljvmiranda921)Siobhán K. Cronin (@SioKCronin)Aaron Moser (@whzup)Steven Beardwell (@stevenbw)And we would appreciate it if you can lend a hand with the following:Find bugs and fix themUpdate documentation in docstringsImplement new optimizers to our collectionMake utility functions more robust.We would also like to acknowledgeall our
contributors, past and
present, for making this project successful!If you wish to contribute, check out ourcontributing guide.
Moreover, you can also see the list of features that need some help in ourIssuespage.Most importantly, first-time contributors are welcome to join! I try my
best to help you get started and enable you to make your first Pull Request!
Let's learn from each other!CreditsThis project was inspired by thepyswarmmodule that performs PSO with
constrained support. The package was created withCookiecutterand theaudreyr/cookiecutter-pypackageproject template.Cite usAre you using PySwarms in your project or research? Please cite us!Miranda L.J., (2018). PySwarms: a research toolkit for Particle Swarm Optimization in Python.Journal of Open Source Software, 3(21), 433,https://doi.org/10.21105/joss.00433@article{pyswarmsJOSS2018,author={Lester James V. Miranda},title="{P}y{S}warms, a research-toolkit for {P}article {S}warm {O}ptimization in {P}ython",journal={Journal of Open Source Software},year={2018},volume={3},issue={21},doi={10.21105/joss.00433},url={https://doi.org/10.21105/joss.00433}}Projects citing PySwarmsNot on the list? Ping us in the Issue Tracker!Gousios, Georgios. Lecture notes for the TU Delft TI3110TU course Algorithms and Data Structures. Accessed May 22, 2018.http://gousios.org/courses/algo-ds/book/string-distance.html#sop-example-using-pyswarms.Nandy, Abhishek, and Manisha Biswas., "Applying Python to Reinforcement Learning."Reinforcement Learning. Apress, Berkeley, CA, 2018. 89-128.Benedetti, Marcello, et al., "A generative modeling approach for benchmarking and training shallow quantum circuits."arXiv preprint arXiv:1801.07686(2018).Vrbančič et al., "NiaPy: Python microframework for building nature-inspired algorithms." Journal of Open Source Software, 3(23), 613,https://doi.org/10.21105/joss.00613Häse, Florian, et al. "Phoenics: A Bayesian optimizer for chemistry."ACS Central Science.4.9 (2018): 1134-1145.Szynkiewicz, Pawel. "A Comparative Study of PSO and CMA-ES Algorithms on Black-box Optimization Benchmarks."Journal of Telecommunications and Information Technology4 (2018): 5.Mistry, Miten, et al. "Mixed-Integer Convex Nonlinear Optimization with Gradient-Boosted Trees Embedded." Imperial College London (2018).Vishwakarma, Gaurav.Machine Learning Model Selection for Predicting Properties of High Refractive Index PolymersDissertation. State University of New York at Buffalo, 2018.Uluturk Ismail, et al. "Efficient 3D Placement of Access Points in an Aerial Wireless Network."2019 16th IEEE Anual Consumer Communications and Networking Conference (CCNC)IEEE (2019): 1-7.Downey A., Theisen C., et al. "Cam-based passive variable friction device for structural control."Engineering StructuresElsevier (2019): 430-439.Thaler S., Paehler L., Adams, N.A. "Sparse identification of truncation errors."Journal of Computational PhysicsElsevier (2019): vol. 397Lin, Y.H., He, D., Wang, Y. Lee, L.J. "Last-mile Delivery: Optimal Locker locatuion under Multinomial Logit Choice Model"https://arxiv.org/abs/2002.10153Park J., Kim S., Lee, J. "Supplemental Material for Ultimate Light trapping in free-form plasmonic waveguide" KAIST, University of Cambridge, and Cornell Universityhttp://www.jlab.or.kr/documents/publications/2019PRApplied_SI.pdfPasha A., Latha P.H., "Bio-inspired dimensionality reduction for Parkinson's Disease Classification,"Health Information Science and Systems, Springer (2020).Carmichael Z., Syed, H., et al. "Analysis of Wide and Deep Echo State Networks for Multiscale Spatiotemporal Time-Series Forecasting,"Proceedings of the 7th Annual Neuro-inspired Computational ElementsACM (2019), nb. 7: 1-10https://doi.org/10.1145/3320288.3320303Klonowski, J. "Optimizing Message to Virtual Link Assignment in Avionics Full-Duplex Switched Ethernet Networks" ProquestHaidar, A., Jan, ZM. "Evolving One-Dimensional Deep Convolutional Neural Netowrk: A Swarm-based Approach,"IEEE Congress on Evolutionary Computation(2019)https://doi.org/10.1109/CEC.2019.8790036Shang, Z. "Performance Evaluation of the Control Plane in OpenFlow Networks," Freie Universitat Berlin (2020)Linker, F. "Industrial Benchmark for Fuzzy Particle Swarm Reinforcement Learning," Liezpic University (2020)Vetter, A. Yan, C. et al. "Computational rule-based approach for corner correction of non-Manhattan geometries in mask aligner photolithography," Optics (2019). vol. 27, issue 22: 32523-32535https://doi.org/10.1364/OE.27.032523Wang, Q., Megherbi, N., Breckon T.P., "A Reference Architecture for Plausible Thread Image Projection (TIP) Within 3D X-ray Computed Tomography Volumes"https://arxiv.org/abs/2001.05459Menke, Tim, Hase, Florian, et al. "Automated discovery of superconducting circuits and its application to 4-local coupler design," arxiv preprint:https://arxiv.org/abs/1912.03322OthersLike it? Love it? Leave us a star onGithubto show your appreciation!ContributorsThanks goes to these wonderful people (emoji key):Aaron🚧💻📖⚠️🤔👀Carl-K💻⚠️Siobhán K Cronin💻🚧🤔Andrew Jarcho⚠️💻Mamady💻Jay Speidell💻Eric🐛💻CPapadim🐛💻JiangHui💻Jericho Arcelao💻James D. Bohrman💻bradahoward💻ThomasCES💻Daniel Correia🐛💻fluencer💡📖miguelcocruz📖💡Steven Beardwell💻🚧📖🤔Nathaniel Ngo📖Aneal Sharma📖Chris McClure📖💡Christopher Angell📖Kutim🐛Jake Souter🐛💻Ian Zhang📖💡Zach📖Michel Lavoie🐛ewekam📖Ivyna Santino📖💡Muhammad Yasirroni📖Christian Kastner📖📦Nishant Rodrigues💻msat59💻🐛Diego📖Shaad Alaka📖Krzysztof Błażewicz🐛Jorge Castillo📖Philipp Danner💻Nikhil Sethi💻📖This project follows theall-contributorsspecification. Contributions of any kind welcome!