• Stars
    star
    249
  • Rank 162,987 (Top 4 %)
  • Language
    Python
  • License
    GNU General Publi...
  • Created about 8 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Learn about particle swarm optimization (PSO) through Python!

Particle Swarm Optimization with Python

gh-actions-ci GitHub license codecov

Particle swarm optimization (PSO) is amazing and I created a series of tutorials that cover the topic using Python. The first (pso-simple) is comprised of a bare bones implementation and is useful for anyone new to PSO and looking to get a good understanding of how it works. The tutorial can be found here: https://nathanrooy.github.io/posts/2016-08-17/simple-particle-swarm-optimization-with-python/

The second version (pso-advanced) is still a work in progress...

Installation

You can either download/clone this repo and use as is, or you can pip install it with the following command:

pip install git+https://github.com/nathanrooy/particle-swarm-optimization

Usage

particle swarm optimization - simple

Once you have completed the installation, usage is similar to that of other common optimization frameworks.

>>> from pso import pso_simple

Next, you need to specify a cost fucntion. I included the sphere function for example purposes, but you'll probably end up using your own.

>>> from pso.cost_functions import sphere

Next, let's specify some bounds and an initial starting location:

>>> initial=[5,5]               # initial starting location [x1,x2...]
>>> bounds=[(-10,10),(-10,10)]  # input bounds [(x1_min,x1_max),(x2_min,x2_max)...]

Lastly, lets minimize this thing!

>>> pso_simple.minimize(sphere, initial, bounds, num_particles=15, maxiter=30, verbose=True)

The output of which should look like this:

iter:    0, best solution:  -1.000000
iter:    1, best solution:  50.000000
iter:    2, best solution:  44.186379
iter:    3, best solution:  37.884043
iter:    4, best solution:  26.485279
iter:    5, best solution:  15.552986
iter:    6, best solution:   8.098333
iter:    7, best solution:   2.697282
iter:    8, best solution:   0.514359
iter:    9, best solution:   0.111682
iter:   10, best solution:   0.010832
iter:   11, best solution:   0.002607
iter:   12, best solution:   0.002607
iter:   13, best solution:   0.002607
iter:   14, best solution:   0.000507
iter:   15, best solution:   0.000507
iter:   16, best solution:   0.000507
iter:   17, best solution:   0.000507
iter:   18, best solution:   0.000507
iter:   19, best solution:   0.000507
iter:   20, best solution:   0.000507
iter:   21, best solution:   0.000415
iter:   22, best solution:   0.000268
iter:   23, best solution:   0.000194
iter:   24, best solution:   0.000064
iter:   25, best solution:   0.000064
iter:   26, best solution:   0.000018
iter:   27, best solution:   0.000013
iter:   28, best solution:   0.000001
iter:   29, best solution:   0.000001

FINAL SOLUTION:
   > [0.0010272779593734505, -0.00023254128511081273]
   > 1.109375455095469e-06

particle swarm optimization - advanced

(coming soon...)

More Repositories

1

evolving-simple-organisms

Evolving simple organisms using a genetic algorithm and deep learning from scratch with python
Python
144
star
2

rpi-urban-mobility-tracker

The easiest way to count pedestrians, cyclists, and vehicles on edge computing devices or live video feeds.
Jupyter Notebook
122
star
3

word2vec-from-scratch-with-python

A very simple, bare-bones, inefficient, implementation of skip-gram word2vec from scratch with Python
Python
101
star
4

landscapes

A dependency free library of standardized optimization test functions written in pure Python.
Python
52
star
5

differential-evolution-optimization

A simple, bare bones, implementation of differential evolution optimization.
Python
49
star
6

taxicab

Accurate routing for OpenStreetMap/OSMnx
Python
42
star
7

BlenderFOAM

Fluid based shape optimization with Blender and OpenFOAM
Python
34
star
8

simulated-annealing

A simple, bare bones, implementation of simulated annealing optimization algorithm.
Python
28
star
9

spatial-analysis

A collection of algorithms I use for the analysis of geospatial data. Written in C, wrapped in Python.
C
21
star
10

Clean-and-Simple-Jekyll-Theme

A simple, clean, and easy to modify Jekyll theme.
HTML
20
star
11

ped-net

Object detection optimized specifically for pedestrians, cyclists, and vehicles on edge/mobile hardware. PedNet = Pedestrian network
20
star
12

ahmed-bluff-body-cfd

Ahmed bluff body geometry (CAD + STL) and OpenFOAM (v7) validation cases
C++
15
star
13

p-area

The easiest way to compute the frontal/projected area of an STL geometry file. Useful for aerodynamic/CFD applications.
Python
14
star
14

aerodynamic-shape-optimization

Python scripts related to aerodynamic analysis and shape optimization
Jupyter Notebook
13
star
15

nathanrooy.github.io

HTML
10
star
16

bwo

Black Widow Optimization implemented in pure Python.
Python
10
star
17

turboshtein

Possibly the fastest way of computing the Levenshtein distance between two fairly short ASCII strings in Pythonโ€ฆ
C
4
star
18

PFOIL

Fortran
1
star
19

dash-vtk-cfd

Python
1
star
20

aerodynamic-panel-codes

Numerical panel codes for calculating the aerodynamic properties of 2D airfoils
Python
1
star
21

ternary-map-legend

Interpolate between three colors in the shape of a variable density equilateral triangle (useful for ternary map legends...)
Python
1
star
22

foillib

A minimal, dependency free utility for generating NACA and Bezier airfoils
Python
1
star