Optimisation aims to find the best solution from a set of feasible options under given constraints. In many machine learning and engineering problems, the search space is complex, non-linear and multimodal, where traditional gradient-based methods may be ineffective. This makes population-based optimization techniques more suitable.
Particle Swarm Optimization (PSO) is a stochastic population based optimization technique inspired by swarm intelligence in nature. It is designed to solve complex optimization problems where the search space is large, non-linear or unknown, where traditional deterministic methods are ineffective.
Each particle represents a potential solution and moves through the search space
Movement is guided by both individual experience and collective swarm knowledge
The algorithm iteratively improves solutions using a fitness function
Simple to implement with fast convergence and few control parameters
How Particle Swarm Optimization (PSO) Works
Particle Swarm Optimization (PSO) is an iterative, population based optimization algorithm. It works by moving a group of particles (candidate solutions) through the search space using simple mathematical rules based on personal and collective experience.
Randomly initialize N particles within the search space
Assign a random velocity to each particle
Evaluate the fitness value of each particle
pBest = current position gBest = best pBest among all particles
Step 2: Velocity Update
At each iteration the velocity of a particle is updated using:
where
: inertia weight (controls exploration)
: cognitive coefficient (self-learning)
: social coefficient (swarm learning)
: random values in [0,1]
Step 3: Position Update
After updating velocity the position is updated as:
If the new position goes outside [minx,maxx] clip it to the boundary.
Step 4: Update Best Positions
If current fitness is better than pBest, update pBest
If current fitness is better than gBest, update gBest
Step 5: Convergence
Repeat Steps 2–4 for a fixed number of iterations or until convergence
The swarm gradually moves toward the optimal solution
Step By Step Implementation
Here in this code we implements Particle Swarm Optimization (PSO) to find the global minimum of the Ackley function by iteratively updating a swarm of particles based on their personal best and the global best positions.
It simulates collective behavior to efficiently search the solution space and converge to an optimal solution.
Step 1: Import Required Libraries
Import numpy for numerical computations and vector operations
Import math for mathematical constants and functions