Implementing The Particle Swarm Optimization
Implementing The Particle Swarm Optimization
Iran Macedo
Dec 24, 2018
·
6 min read
·
Listen
Algorithm
At first, in the 2 for loops, it initializes the particles’ positions with a random
uniform distribution within a permissible range for all its dimensions (Some
problems require handling to several dimensions). After that, for each particle, it
calculates its fitness value and compared with his own best position (The p_best
value is the best position of that specific particle has ever been) and then it chooses
the best position of all particles in g_best.
Let’s take a closer look to the equation that defines the velocity of the next iteration
of a particle dimension:
Pᵢ is the best individual position and Pg is the best position of all particles. In
the equation, is measured the distance of each of these parameters to the
particle’s actual position.
rand₁ and rand₂ are random numbers where 0 ≤ rand ≤ 1 and they control the
influence of each value: Social and individual as shown below.
Google images
After that is calculated the new particle’s position until the number of iterations
specified or an error criteria be reached.
Implementation
Our goal is to find the minimum point of a certain function. In this case, the
function is f(x,y) = x² + y² + 1. Thus, the algorithm will work with 2 dimensions
positions arrays and the fitness value will be the Z-coordinate. Also, we know that
our target is to find the coordinates [0,0] which is the minimum of f(x,y).
To implement the algorithm in python was used an OOP (at this point it’s been
considered that you know the basics at it) to help us to implement and understand
all steps in code. Here, it’s used the numpy library (check more information here)
to handle array operations once we work with a multidimensional space.
Particle
Particle Class
Search Space
The Search Space is the entity which controls the algorithm routine. In this
implementation, it is responsible to keep all the particles, identify and set the
individuals best position values of all particles, manage the target error criteria,
calculate the best global value and set the best global position. In resume, it
encapsulate all principal steps.
Space State Class
The method move_particles calculate the new vector velocity to each particle in
each dimension as it was explained before.
Main loop
Main loop
At first, it’s initiated a Search Space with target at 1. This target represents the
target at fitness value, this means that the target is f(x,y) = 1. The algorithm then
will find which values of x and y gives a result equals 1 as shown before at the
function shape that we want to find the minimum. The Contour plot also show us
that the value we want to find out is [0,0]. The target error and the number of
particles (n_particles) it will be set by the user. Then with a list generator it’s
initiated all the particles and after initiated the iterations.
In all iterations at first, it’s found the best individual position and the best global
position to evaluate the target error criteria. If this criteria doesn’t achieve the
minimum error, the particles will calculate the velocity to move and then stop at a
new position until the number of iterations or the minimum error criteria be
reached. The last line just print out the best result found.
The values of W, C₁ and C₂ are chosen by hand so be careful with them because
they can get the longer.
Results
Using python interpreter we enter with the values of number of iterations, target
error criteria and number of particles into the swarm.
It’s an easy algorithm to implement and use. Hope you all enjoyed!