Particle Swarm Optimization (PSO) (original) (raw)

Last Updated : 10 Feb, 2026

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.

PSo100

Particle Swarm Optimization

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.

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.

Each particle in PSO has

Each particle stores:

frame_3315

PSO Workflow

Step 1: Initialization

pBest = current position
gBest = best pBest among all particles

Step 2: Velocity Update

At each iteration the velocity of a particle is updated using:

v_{i}^{t+1} = w \cdot v_{i}^{t} + c_{1} \cdot r_{1} \cdot (pBest_{i} - x_{i}^{t}) + c_{2} \cdot r_{2} \cdot (gBest - x_{i}^{t})

where

Step 3: Position Update

After updating velocity the position is updated as:

x_{i}^{t+1} = x_{i}^{t} + v_{i}^{t+1}

If the new position goes outside [minx,maxx] clip it to the boundary.

Step 4: Update Best Positions

Step 5: Convergence

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 as np import math import matplotlib.pyplot as plt

`

Step 2: Define the Cost (Objective) Function

def cost_function(x): a = 20 b = 0.2 c = 2 * math.pi d = len(x)

term1 = -a * np.exp(-b * np.sqrt(np.sum(x**2) / d))
term2 = -np.exp(np.sum(np.cos(c * x)) / d)

return term1 + term2 + a + math.e

`

Step 3: Define PSO Hyperparameters

DIMENSIONS = 2 POPULATION = 30 MAX_ITER = 100

MIN_BOUND = -5 MAX_BOUND = 5

w = 0.7 c1 = 1.5 c2 = 1.5 V_MAX = 0.5

`

Step 4: Define the Particle Class

class Particle: def init(self): self.position = np.random.uniform(MIN_BOUND, MAX_BOUND, DIMENSIONS) self.velocity = np.random.uniform(-V_MAX, V_MAX, DIMENSIONS) self.best_position = self.position.copy() self.best_fitness = cost_function(self.position) self.fitness = self.best_fitness

`

Step 5: Initialize the Swarm and Global Best

def particle_swarm_optimization(): swarm = [Particle() for _ in range(POPULATION)]

gbest_position = swarm[0].best_position.copy()
gbest_fitness = swarm[0].best_fitness

for particle in swarm:
    if particle.best_fitness < gbest_fitness:
        gbest_fitness = particle.best_fitness
        gbest_position = particle.best_position.copy()

`

Step 6: Update Velocity and Position of Particles

`

Step 7: Update Personal Best and Global Best

`

Step 8: Execute the PSO Algorithm

if name == "main": best_position, best_fitness = particle_swarm_optimization()

print("\nOptimal Solution Found:")
print("Best Position:", best_position)
print("Best Fitness:", best_fitness)

`

**Output:

PSO8

Output

You can download full code from here

Difference between Particle Swarm Optimization (PSO) and Genetic Algorithm (GA)

Here we compare Particle Swarm Optimization (PSO) and Genetic Algorithm(GA):

Parameter PSO Genetic Algorithm (GA)
Inspiration Based on social behavior of birds or fish Based on natural evolution and genetics
Search Strategy Particles move continuously through the search space Uses randomized population evolution
Population Update No creation or deletion particles only change position New individuals are created using crossover and mutation
Operators Used Velocity and position updates Selection, crossover and mutation
Variable Handling Best suited for continuous optimization Handles both continuous and discrete variables
Complexity and Speed Simple structure with faster convergence More complex and generally slower

Application

1. Health Care Applications

2. Environmental Applications

3. Industrial Applications

4 Smart City Applications

Advantages

Limitations