Skip to main content

A SciPy compatible super fast Python implementation for Particle Swarm Optimization.

Project description

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_pso
>>> from scipy.optimize import rosen
>>> x0 = np.random.uniform(0, 2, (1000, 5))
>>> res = minimize_pso(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_pso(fun, x0, constrainsts=cons, options={
...     'g_rate': 1., 'l_rate': 1., 'max_velocity': 4., 'stable_iter': 50})
>>> res.x
array([ 1.39985398,  1.69992748])

Authors

License

Licensed under the BSD 3-Clause License.
Copyright 2018 Abhijit Theophilus, Snehanshu Saha, Suryoday Basak

References

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

psopy-0.2.2.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

psopy-0.2.2-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file psopy-0.2.2.tar.gz.

File metadata

  • Download URL: psopy-0.2.2.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for psopy-0.2.2.tar.gz
Algorithm Hash digest
SHA256 c21f8ecdd0435034cadfaf861fe42e9b78d7c1e42992038c87df816bcb9a6878
MD5 91b032d5e81a29cd31a8125e25ce9906
BLAKE2b-256 99d61832cbed0e5a88c20bcb4a6061e45ec672ffd90a1ba337480ca8854db717

See more details on using hashes here.

File details

Details for the file psopy-0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for psopy-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7244521d1b193d57208bbaf07774d8ed3d8cce8dfa5bdcf000e9a213810630f3
MD5 2f8f2115f332ef2b34d099cf8f9a84fd
BLAKE2b-256 c433268ff98320884be7379e5cf616df1242c338f54e67addadbad6185ffd2bd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page