You are on page 1of 5

INTRODUCTION TO PROGRAMMING

2022/23

Leiden Institute of Advanced Computer Science (LIACS)

Assignment 3
Particle Swarm Optimisation algorithm:
Function minimisation

DEADLINE : 23/12/2022
DELIVERY: Brightspace (see “Delivery” for instructions)
GENERAL

For this assignment you will create a PSO algorithm that minimises the
following function:

2 2
(x-π) + (y-e) + sin(3*x+0.75) + cos(4*y-2.13)

(please notice that “e” refers to Euler’s number)

In order to do so, remember the update rule of PSO that you will use to “move”
the particles:

V = w * V + c1*r1*(pbest - X) + c2*r2*(gbest - X)
X=X+V

where:

• V = updating velocities array, containing the velocity of each particle


• w = inertia weight (to slightly “slow down” the velocity of the particles)
• c1 = cognitive coefficient (how much each particle remembers what it did)
• c2 = social coefficient (how much each particle is aware of what the others
are doing)
• r (1 and 2) = a degree of randomness in the update
• pbest = array containing the personal best position of each particle (shape
= ?)
• gbest = an array containing the current best position in the whole map
(shape = ?)

TIPS

• the function should be minimised which means that you are looking for the
smallest value(s)
• careful to avoid getting stuck in local minima, play with the parameters (w, c1
and c2) to confirm that you are getting to the absolute minimum
• be always aware of the shape of your arrays, you cannot use “+” between
differently shaped arrays. However, you can always reshape them...

STEPS

In case you need help to start, here are some useful steps. Try to tackle them
in order one at the time while keeping track of what the overall algorithm
does.

1 - Define the function to be minimised


• do that in such a way that you can use it throughout the code

2 - Initialise the particles:


• set the number of particles (20 should do the trick but feel free to experiment)
• generate (an) array(s) with the initial random position for X and Y set between
0 and 5
• generate an array with the initial random and normally distributed velocities
of the particles

3 - Initialise the ”best” parameters (personal and global):


• initialise the personal bests array (at what positions can you initialise it?)
• calculate the global best for the first time
4 - Initialise parameters:
• set both cognitive (c1) and social (c2) coefficients (experiment for best
values)
• set inertia (w) (experiment for best values)

5 - Iterations:
• create an array with two random numbers to drive the particles’ movement
(r)
• update the array of velocities following the rule described in ”Introduction”
• update the array of positions by using the array of velocities to ”move” the
particles
• calculate again the function values based on the new positions
• for each particle, if the new function value is better than the old one, update
pbest
• now select the new gbest from all the pbests

6 - After iterations:
• print final gbest and respective function value

GRADING

Functionality: the code uses PSO to find the global minimum with a good
approximation (~3.3, ~2.8). It could be easily used for another function. 6
points
Structure: the code is divided into functions, the amount of global code is
minimised. 1 point
Comments: the code is well commented (the right amount of information to
understand what the code does). 0.5 point
Numpy: the code uses numpy arrays and related functions instead of lists
(besides eventual array creation). 1 point
Values: the values used for c1, c2 and w are indeed optimised through
experimentation. 0.5 point
Visualization: the student visualises in a graph the location of the minimum in
the function (besides this, you are free to pick the type of representation). 1
point
DELIVERY

After you are sure about the result, compress the code into a .zip file.
Rename the .zip file using your student number + “_3” like in the following
example:

Student number: 123456 > 123456_3.zip


(no “s” at the beginning!)

Upload the resulting .zip file on Brightspace under “Assignments” > “Assignment
3 - PSO”

Deadline 23rd December 2022

You might also like