Mechanics A Swarm of randomly distributed particles are evaluated, then updated based on the best position stored in memory of the particle and globally best position stored in swarm memory. Each particle has a position, and a memory of the best position discovered by the particle. The Swarm manages all the particles and has a memory of the best position found globally of all particles.

The Algorithm - Digitized







Prerequisite Vocabulary A particles position vector(array) represents the parameters of the objective function you are optimizing. The velocity vector represents the change of each parameter on every update. The search-space is just the maximum and minimum bounds of the parameters. Initialize Particle Population A population of particles known as the swarm are created and distributed randomly within the search-space, population size ranges from 20 to 150 particles dependent on the area of the search-space and runtime resources required for the evaluation function to execute. The population keeps track of the best solution found - known as the Swarm best position.



Coders Insight The population will hold the collection of particles, keeping track of the best solution found so far and pass that information to each particle on every iteration.



Evaluate Particles The Evaluation Function, also known as the Objective Function is the function that is being optimized, it takes a particles position as its input parameters and outputs a fitness score representing the particles performance used for particle comparison. Optimization can either be to maximize or minimize the result.



Coders Insight Return of the Evaluation Function should be comparable such as a distance-metric, reward based system or penalty count.

Update Particles Position Particles update their velocity vector according to the Swarms best position and the Particles best position, factoring in random jumping factors(interpolation), aiding in exploration and escaping local optimums. Here's the famed equation: The Evaluation Function, also known as the Objective Function is the function that is being optimized, it takes a particles position as its input parameters and outputs a fitness score representing the particles performance used for particle comparison. Optimization can either be to maximize or minimize the result.Particles update their velocity vector according to the Swarms best position and the Particles best position, factoring in random jumping factors(interpolation), aiding in exploration and escaping local optimums. Here's the famed equation:







w - Inertia(Velocity decay) factor

Pvel - Particle Velocity vector

R1, R2 - Random numbers 0 to 1

C1, C2 - Maximum Jump Constants

Pbest - Particle best position found vector

Ppos - Particle current position vector

Sbest - best position vector found during the algorithm



(Pbest - Ppos) Distance and direction toward the particles best past known position, C1 determines maximum jump factor, value of 2 would jump twice the vector length. R1 is a random number from 0 to 1 determining how far along it will jump the distance vector. C1 defaults to 1.5 to 2 for search space exploration reasons.



(Sbest - Ppos) Distance and direction toward the swarms best position, C2 determines the maximum jump factor, C2 normally is the same as C1. R2 is a random number from 0 to 1 determining how far along to jump the distance vector. R2 is not the same as R1.



w*Pvel keeps particles heading in the same direction, and improving resilience to local optimums. w is the inertia factor, allowing the velocity to be influenced in future updates. w is usually a number in the range of 0 to 1, where 1 is no decay; the default is 0.72.





Coder Insight

Particles have position and velocity vectors(arrays), particles keep track of the best position found. Here`s the equation written for coders:



P.vel[i] = w*P.vel[i] + rand()*c1*(P.best[i] - P.pos[i]) + rand()*c2*(S.best[i] - P.pos[i])

P.pos[i] = P.pos[i] + P.vel[i]



Done When the Swarms best position reaches a satisfactory score, or when the rate of progression becomes permissible.





Ending Note I hope this article serves as a seed to great innovations, good luck my programmer brothers.



