Minimizing/Maximizing the value of a function.
Long story short: There are n particles moving in the domain of the target function. Each particle has a position and a velocity vector. The velocity of
each particle changes in regards to the point where the "best" value of the function was found globaly (among every particle), the point where the "best" value of the
function was meet by this particle, and inertia and a randomness. Each of these parameters has a constant multiplier too.
Here, I've tried to find the minimum of the below cost function:
It's rather a "simple" function as its domain is a subset of the 2 dimensional plane. For such functions, Google can easily find the minimum for you! But still, here's how the function looks like:
Function f is the cost function which using PSO, this code tries to minimize it.
Global Variables:
inertia: Inertia's constantpbc: Personal best constantgbc: Group best constantgbcor: Group best coordinationsgbv: Group best valuen: number of particlesmaxV: Maximum of velocity a particle can obtain
There's a class called Particles, which has the following variables:
cor, which stores the current coordinate of the particle.bestCor, which stores the "best" coordinate this particle ever had.best, which stores the value of the functionfat coordinatebestCor.v, which is the velocity vector of this particle.
For the above function, this is the positioning of the particles at different times of the calculation:
As can be observed from the image of the function, the potential candidates for the function's minimum is placed on the 4 corners of the x and y plane in range (-10, 10).
Outputs the minimum/maximum value of the function f that particles have found.


