PSOPy
A python implementation of Particle Swarm Optimization.
Introduction
PSOPy (pronounced “Soapy”) is a SciPy compatible super fast Python implementation for Particle Swarm Optimization. The codes are tested for standard optimization test functions (both constrained and unconstrained).
The library provides two implementations, one that mimics the interface to scipy.optimize.minimize and one that directly runs PSO. The SciPy compatible function is a wrapper over the direct implementation, and therefore may be slower in execution time, as the constraint and fitness functions are wrapped.
Installation
GitHub
To install this library from GitHub,
$ git clone https://github.com/jerrytheo/psopy.git
$ cd psopy
$ python setup.py install
In order to run the tests,
$ python setup.py test
PyPI
This library is available on the PyPI as psopy. If you have pip installed run,
$ pip install psopy
Examples
Unconstrained Optimization
Consider the problem of minimizing the Rosenbrock function, implemented as scipy.optimize.rosen using a swarm of 1000 particles.
>>> import numpy as np
>>> from psopy import minimize
>>> from scipy.optimize import rosen
>>> x0 = np.random.uniform(0, 2, (1000, 5))
>>> res = minimize(rosen, x0, options={'stable_iter': 50})
>>> res.x
array([1.00000003, 1.00000017, 1.00000034, 1.0000006 , 1.00000135])
Constrained Optimization
Next, we consider a minimization problem with several constraints. The intial positions for constrained optimization must adhere to the constraints imposed by the problem. This can be ensured using the provided function psopy.init_feasible. Note, there are several caveats regarding the use of this function. Consult its documentation for more information.
>>> # The objective function.
>>> fun = lambda x: (x[0] - 1)**2 + (x[1] - 2.5)**2
>>> # The constraints.
>>> cons = ({'type': 'ineq', 'fun': lambda x: x[0] - 2 * x[1] + 2},
... {'type': 'ineq', 'fun': lambda x: -x[0] - 2 * x[1] + 6},
... {'type': 'ineq', 'fun': lambda x: -x[0] + 2 * x[1] + 2},
... {'type': 'ineq', 'fun': lambda x: x[0]},
... {'type': 'ineq', 'fun': lambda x: x[1]})
>>> from psopy import init_feasible
>>> x0 = init_feasible(cons, low=0., high=2., shape=(1000, 2))
>>> res = minimize(fun, x0, constrainsts=cons, options={
... 'g_rate': 1., 'l_rate': 1., 'max_velocity': 4., 'stable_iter': 50})
>>> res.x
array([ 1.39985398, 1.69992748])
License
References
Metadata
Release files for psopy 0.2.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| psopy-0.2.4.tar.gz | 14.2 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| psopy-0.2.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 29.0 kB
Release files / psopy-0.2.4.tar.gz
| Download URL | psopy-0.2.4.tar.gz |
|---|---|
| Size | 14.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
988e5878f59e68c9482a5c65160418e5fdf5022b749cb7db195bc07c5ce1ac4f
|
|
BLAKE2b-256 checksum How to use checksums |
4da5729a934f706ae19c8913211047082ad284885d199f23e4ddcdf9e0c0e8f5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2
|
Release files / psopy-0.2.4-py3-none-any.whl
| Download URL | psopy-0.2.4-py3-none-any.whl |
|---|---|
| Size | 14.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8de5cdc9077f130f90e4a4afdc970a54eda2d3b94efded186d8e139d71821526
|
|
BLAKE2b-256 checksum How to use checksums |
d910f12cbdd5d621d7cba33d673c4655151561173bf2d48f99228ff2a662668f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2
|
Security for psopy 0.2.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.