A tutorial on Optimization Algorithms, the example of Particle Swarm Optimization
First appeared here.
In this blog post, I’ll make a quick introduction to what is an optimization algorithm and then I’ll talk about one specific that is, Particle Swarm Optimization (PSO).
What is an optimization algorithm ?
Optimization algorithms are here to solve optimization problems where we want to find the best solution within a large set of possible solutions.
Optimization problems are everywhere. For instance, when we enter a super market, the goal is to purchase all the things we need in a short time and with little expenses. Doing so we are optimizing our time and our budget.
We can define an optimization problem by three things:
- The measure of success (technically the cost function) which will be maximized or minimized.
- Instances of the problem which are the possible inputs to the problem, expressed in terms of design variables. These Instances can be subject to some constraints, thus setting bounds on the possible values to explore.
- The set of possible solutions which define the space to explore in order to find the best solution.
Generally, exploring the set of solutions is computationally heavy and very time consuming, that is why there are various algorithms to tackle these problems and find an appropriate and acceptable solution in a reasonable time.
These algorithms are iterative, and according to some criteria, one can decide whether the solution found is good enough to stop the optimization process. When that is possible, we speak about the algorithm convergence.
One of these algorithms is the Particle Swarm Optimization (PSO), which is the subject of this post. First, I’ll try to explain how it works, then I’ll walk you through a Python implementation, and test it on a real example.
What is Particle Swarm Optimization (PSO)?
PSO is an iterative optimization algorithm which tries to simulate social behaviour. It was developped by Dr. Eberhart and Dr. Kennedy, back in 1995.
It is best known that working together in order to achieve a goal is more efficient than no team work at all. PSO leverages this social behaviour by prevailing the information sharing between a set of possible candidate solutions, each called a particle.
A particle is defined by:
- A position. For example when the design variables are limited to two (i.e plane), a particle is defined by its coordinate (x,y).
- A velocity. This is a value used to move the particle position toward the best solution.
The information sharing is performed in two ways, giving two variants of PSO:
- Local PSO, in which the information sharing is between every particle and its direct neighbors. Here a particle’s movement, at each iteration, is influenced by its local best known position. The particle and its neighbors form a ring data structure topology.
- Gloabl PSO, where the information sharing is between every particle and the best particle of all, defined by the best position. It is the best position which guides the movements of all the other particles, at each iteration. Here the particles are organized in a star data structure topology.
The Algorithm
We start by initializing randomly a population of particles in the search space. And through various iterations, each particle moves toward the best solution by following either the global best solution at each iteration or its locally best-known solution among its neighbors, depending on whether we consider local or global PSO.
Here is the pseudo-code for global PSO:
For each particle
Randomly initialize its position and velocity
END
Do
For each particle
Calculate its fitness value (measure of success)
If the fitness value is better than the best fitness value (pBest) in history
Set current value as the new pBest
ENDChoose the particle with the best fitness value of all the particles as the gBest
For each particle
Calculate particle velocity
Update particle position using the computed velocity
End
While maximum iterations or minimum error criteria is not attained
Python Implementation
The points to discuss here are the the initializations of the particle and their updates, both for the positions and the velocities.
- Particles are initialized randomly using a uniform distribution.
- The updates are performed using a slightly modified version of the initial paper, by the same authors (For further reading, go check this paper).
Here is the python code which tries to implement a simple PSO
Some points to discuss
- The initial point matters a lot in the performance of the optimization algorithm. As a rule of thumbs, as far as it is from the optmum, as long it takes the algorithm to converge (this is true for all optimization algorithms).
- PSO is a stochastic algorithm, the updates are performed using random processes. This can make it hard to track the solutions for multiple optimizatiion processes run. We can always use a seed for the random numbers though.
- It does not absolutely find the global optimal solution, but it does a good job finding one which is very close. For the sphere function, the global optimum is at (0, 0, 0), my implementation found another point which is not too bad.
- Depending on the number of particles, the convergence might take longer. Generally, it is better not to go beyong 50.
- For difficult functions, we might need more iterations before being able to find a good solution, between 500 and 1000 iterations.
This is the end of this long post. I hope it helps, I tried my best to keep it simple.
For a more advanced PSO, I highly recommend this open source python library Pyswarms. You can also find all the code on my github here.
Reference
- Swarm Intelligence website
- Initertia weight strategies in PSO paper.