Skip to main content

An implementation of the Particle Swarm Optimization algorithm

Project description

psoa

An implementation of the Particle Swarm Optimization algorithm

The algorithm can be summarized as follows:

  • a swarm of moving particles are used to find the extreme value of an objective function
  • each particle has its own position and velocity
    • use the given function to evaluate particles' positions
    • results can be compared, depending on whether we want to maximize or minimize the objective function
  • In each step, a particle receives three forces, they are used to update the particle's velocity
    • a friction force that slows down the particle
    • a random attraction force towards the particle's previous best position
    • a random attraction force towards the swarm's previous best position
  • the particle's velocity is then used to update the particle's position
  • if boundary conditions are included, we consider them to be reflecting boundaries, i.e. if the particle moves beyond the boundary in one direction, we would
    • place the particle back onto the boundary
    • and reverse the particle's velocity in that direction
  • keep updating the particles' positions and velocities until
    • the swarm's best position converges
    • or until we reach the preset maximum number of steps

Installation:

pip install psoa

or

conda install -c wangxiangwen psoa

Example Usage:

>>> import psoa
>>> import numpy as np
>>> s = psoa.swarm()
>>> obj = lambda x: -((x[0] - 10) ** 2 + (x[1] - 25) ** 2)
>>> s.maximize(obj, dim=2)
([10.0, 25.0], -0.0)
>>> obj2 = lambda x: np.sum([xi ** 2 - 10 * np.cos(2 * np.pi * xi)
>>>                          for xi in x]) + 10 * len(x)
>>> s.minimize(obj2, dim=5, max_iteration=1e5,
>>>            boundaries=((-5.12, -5.12, -5.12, -5.12, -5.12),
>>>                        (5.12, 5.12, 5.12, 5.12, 5.12)))
([-2.8743161872992346e-10,
  2.579205368330527e-09,
  -4.79709591601136e-09,
  -1.0974277510973518e-09,
  4.4227337040162274e-10],
 0.0)

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

psoa-1.0.1.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

psoa-1.0.1-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file psoa-1.0.1.tar.gz.

File metadata

  • Download URL: psoa-1.0.1.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.6.10

File hashes

Hashes for psoa-1.0.1.tar.gz
Algorithm Hash digest
SHA256 4f2d9b900dec1a522d70b43e7aad8d86c43d80f681bf17825d4366f72c27461c
MD5 e6ef611cbb27e3cc24a6c47f51e65bd5
BLAKE2b-256 f226f6ac4be28994846912dffaf7c7c8923fa6922651de02df982c102f1b9648

See more details on using hashes here.

File details

Details for the file psoa-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: psoa-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.6.10

File hashes

Hashes for psoa-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b8aa6bccc55d619af6839801cbc457625a1e6d8399ebb2852740e11ac7f0d09c
MD5 7959e59dc29e11c9d395cd087b99836c
BLAKE2b-256 e184897355da2c0cb873fe33807a6437657612b33e3900dae4dfed96485d14c1

See more details on using hashes here.

Supported by

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