# Particle Swarm Optimization from Scratch with Python

Particle swarm optimization (PSO) is one of those rare tools that’s comically simple to code and implement while producing bizarrely good results. Developed in 1995 by Eberhart and Kennedy, PSO is a biologically inspired optimization routine designed to mimic birds flocking or fish schooling. I’ll occasionally use PSO for CFD based aerodynamic shape optimization, but more often than not, it’s for a machine learning project. PSO is not guaranteed to find the global minimum, but it does a solid job in challenging, high dimensional, non-convex, non-continuous environments. In this short introductory tutorial, I’ll demonstrate PSO in its absolute simplest form. At a later date, I’ll create another PSO tutorial featuring a more advanced implementation.

Below, are the only two equations that make up a bare bones PSO algorithm. As a heads up, “k” references the current iteration, therefore “k+1″ implies the next iteration.

Particle position:

Particle velocity:

Where:

Variable |
Definition |
---|---|

particle position | |

particle position | |

best individual particle position | |

best swarm position | |

constant inertia weight | |

cognitive and social parameters respectively | |

random numbers between 0 and 1 |

From the particle velocity equation, two important groups emerge:

- social term:
\( c_2 r_2 \left(p_k^g - x_k^i\right) \) - cognitive term:
\( c_1 r_1 \left(p_k^i - x_k^i\right) \)

Using these two simple equations, the basic flow structure of a PSO routine is as follows:

A) Initialize

- Set constants:
\( k_{max}, w_k, c_1, c_2 \) - Randomly initialize particle positions.
- Randomly initialize particle velocities.
- Set k=1 (iteration counter).

B) Optimize

- Evaluate cost function
\( f_k^i \) at each particle position\( x_k^i \) - If
\( f_k^i \le f_{best}^i \) then\( f_{best}^i = f_k^i \) and\( p_k^i = x_k^i \) . - If
\( f_k^i \le f_{best}^g \) then\( f_{best}^g = f_k^i \) and\( p_k^g = x_k^i \) . - If stopping condition is satisfied, go to C.
- Update all particle velocities.
- Update all particle positions.
- Increment k.
- Go to B(1).

C) Terminate

That’s it! It’s really is that simple. The main concept behind PSO, which is evident from the particle velocity equation above, is that there is a constant balance between three distinct forces pulling on each particle:

- The particles previous velocity (inertia)
- Distance from the individual particles’ best known position (cognitive force)
- Distance from the swarms best known position (social force)

These three forces are then weighted by

In vector form, these three forces can be seen below (vector magnitude represents the weight value of that specific force):

We can see in the above example that the weighting of the particles inertia and individual best overpower the swarms influence. In this scenario, the particle will continue exploring the search space rather than converge on the swarm. As another example below:

This time, the weighting assigned to the swarms influence overpowers the individual forces of the particle forcing it towards the swarm. This will result in a faster convergence, at the expense of not fully exploring the search space and potentially finding a better solution.

The implementation of a simple PSO routine in python is fairly straightforward. We are going to utilize some object-oriented programming and create a swarm of particles using a particle class. These particles will be monitored by a main optimization class. Below is the entire code:

I hope this was helpful! If you want, you can download the entire code from my GitHub (here). Check back later for my post on a more advanced particle swarm optimization routine.