Particle Swarm Minimiser
This is an implementation of a particle swarm optimisation algorithm, which will find the minimum of two functions as well as report the corresponding input values. The two functions to minimise were
and
The algorithm is inspired by how for example bird flocks or fish schools behave. They are an interesting study, as there are no apparent leaders that the other members follow, yet the swarm behaves coherent.
In particle swarm optimisation, a common model is the Boids model (the name "Boids" is taken from "bird-like objects"), which is the model that was implemented in this program. In it, we let denote a swarm of
boids, hereafter called particles:
.
In the model, each particle only has a limited visual range, and is unable to perceive swarm mates that are far away. The so called visibility sphere
of particle
is introduced as
,
where is a constant. Each particle
has a position
, a velocity
and an acceleration
, and in each iteration of the algorithm, they are updated using standard Euler integration:
,
where is the time step (here set to 1). Each particle is influenced by three movement tendencies that together determine its acceleration. These tendencies are cohesion, alignment and separation.
-
Cohesion
This is the tendency of a particle to stay near the centre of the swarm. Letdenote the centre of density of the visibility sphere of particle
. If there are
particles in
, then
The steering vector representing cohesion is defined as
whereis a time constant. If there are no particles in
, then
-
Alignment
This is the tendency of particles to align their velocities with those of their nearby swarm mates. It is defined as
whereis a time constant and
are the number of particles in
. If there are no particles in
, then
.
-
Separation
Separation is needed in order to avoid collision with nearby swarm mates, and is defined as
where once againis a time constant. If there are no particles in
, then
.
The acceleration of particle is obtained by combining these steering vectors, by the following equation:
,
where ,
and
are constants between 0 and 1 that control the relative impact of the steering vectors.
The algorithm works as follows:
-
Initialisation
Initialise the positions and velocities of each particle. Letdenote the swarm size and
the number of variables in the problem to solve. The positions are randomly initialised, using uniform sampling in the range between
and
.
Initialise positions of all the particles as follows:
wheredenotes component
of the position of particle
and
is a random number in the range 0 to 1.
Initialise the velocities of all the particles as follows:
wheredenotes component
of the velocity of particle
,
is a random number between 0 and 1,
is a constant between 0 and 1, and
is the time step length. In the implementation,
and
are both set to 1.
-
Evaluation
The evaluation part consists of computing the objective function value for each. In other words, input
into the objective function, in this case either
or
-
Update the best positions
The objective of the algorithm is to reach optimal values of the objective function. Thus, the algorithm must keep track of the particles' performance. Both the best positionof particle
, and the best performance
of any particle in the swarm is stored. The former means comparing the position of particle
in the current iteration to how well it has previously performed, and updating the record if needed. The latter can be carried out in different ways, but in this implementation,
is the best-ever position of the whole swarm. The following equations capture this:
-
Update particle velocities and positions
Update the velocity of particleas follows, where
goes from 1 to
:
whereand
are uniform random numbers between 0 and 1,
is the so called inertia weight and
and
are positive constants. The
-term is sometimes called the cognitive component, and measures the degree of self-confidence of a particle, i.e. how much it trusts its own previous performance in order to get a better result. The
-term is sometimes called the social component, and measures how much a particle trusts others to find better candidate solutions.
The inertia weighthandles the trade off between exploration (
) and exploitation (
). Initially,
, and after each iteration,
is modified as follows:
,
whereis 0.99. For every iteration,
is decreased in this manner, until it reaches a minimum value of 0.35, where it will be kept fixed. This procedure makes the algorithm explorative in the early stages, and later on turns more and more into exploitation.
If the velocities are allowed to grow with no boundary, the swarm will cease to be coherent and will expand indefinitely. Therefore, a maximal velocityis introduced, restricting velocities by
where the sign-function is used to maintain the direction, and is defined as
The position of particleis then updated as follows, where
goes from 1 to
:
-
Repeat
Repeat from step 2, until the termination criterion has been reached.
The code is available at my GitHub account.
Screenshot of Matlab running the program: