# An Optimization Example Using the Particle Swarm Optimization Algorithm

This article is the companion of An Optimization Example Using the Differential Evolution Algorithm. It takes exactly the same problem (the same objective, the same constraint, the same penalty, the same budget of function evaluations) and solves it with another famous metaheuristic, particle swarm optimization (PSO). The goal is twofold: to explain every step of the algorithm in a clear and simple way, with a practical example, code and results, and to see, on equal terms, how the two methods behave. As we shall see, they have rather different personalities.




Introduction to Particle Swarm Optimization



  Particle Swarm Optimization (PSO) is a population-based, stochastic optimizer introduced by James Kennedy and
  Russell Eberhart in 1995. It was inspired by the collective movement of bird flocks and fish schools. Like differential evolution it
  minimizes a real-valued objective function over a continuous search space and needs no derivatives: it only ever
  evaluates the objective, which makes it a natural choice for black-box problems.




  The method is popular because it is simple (the core is a few dozen lines of code), has only a few control parameters (the swarm size $N$, the inertia weight $w$ and the two acceleration
  coefficients $c_1$ and $c_2$) and finds a good region of the search space remarkably fast. In return, the basic algorithm gives no mathematical guarantee of reaching the
  global optimum, may converge prematurely, and, as in every metaheuristic, constraints need explicit handling. For a smooth, convex
  problem a gradient-based method is far cheaper.





  The key idea. A swarm of particles flies through the search space. Every particle keeps part of its previous motion (inertia) and is pulled
  towards two places: the best point it has found itself (its memory) and the best point the whole swarm has found so far (the social information).
  Exploration is controlled by the balance of these three influences, and the swarm gradually contracts around a good solution.





  Problem statement. Given an objective $f:\mathbb{R}^D \to \mathbb{R}$, bounds
  $L_j \le x_j \le U_j$ and, optionally, constraints $g_k(\mathbf{x}) \le 0$, find
  $$\mathbf{x}^{*} = \arg\min_{\mathbf{x}} f(\mathbf{x}) \quad \text{subject to} \quad g_k(\mathbf{x}) \le 0,\; k = 1,\dots,K.$$









Algorithm Overview



  PSO maintains a swarm of $N$ particles. Each particle $i$ has a position $\mathbf{x}_i\in\mathbb{R}^D$ (a candidate solution), a
  velocity $\mathbf{v}_i\in\mathbb{R}^D$ and a memory: its personal best position $\mathbf{p}_i$, the best point it has visited so far.
  The swarm as a whole remembers the global best position $\mathbf{g}$. In every iteration $t$ the swarm evaluates the objective, updates the bests, accelerates every
  particle towards them, and moves it.









1
Initialize
N particles
random x and v




2
Evaluate
update pi and g
remember the bests




3
Accelerate
v = w·v + c1r1(p − x)
+ c2r2(g − x)




4
Move
x = x + v
then clip to bounds


repeat for t = 1 … Tmax, or until the swarm has converged

Fig. 1 – One iteration of particle swarm optimization. After the random initialization, every iteration evaluates the
swarm, updates the personal and global bests, computes new velocities and moves the particles.






1

Initialization – sample $N$ positions uniformly inside the bounds, give each particle a small random velocity and evaluate the swarm.




2

Evaluation – update each personal best $\mathbf{p}_i$ if the new position is better, then update the global best $\mathbf{g}$.




3

Velocity update – combine the old velocity with the pulls towards $\mathbf{p}_i$ and $\mathbf{g}$.




4

Move – add the velocity to the position and bring any particle that left the search box back to its boundary.




5

Repeat steps 2–4 until $T_{\max}$ iterations have run, or the swarm has converged.





Control Parameters



  ParameterMeaningUsual rangeEffect


  
    $N$Swarm size$20$ to $50$ for small problemsLarger: more diversity and robustness, more evaluations per iteration


    $w$Inertia weight$0.4$ to $0.9$ (or $0.7298$, see below)Larger: particles keep moving and explore; smaller: they settle quickly


    $c_1$Cognitive coefficient (memory)$1.5$ to $2$Larger: stronger pull towards the particle's own best position


    $c_2$Social coefficient (swarm)$1.5$ to $2$Larger: stronger pull towards the best position of the swarm


    $T_{\max}$Number of iterationsproblem dependentSets the evaluation budget, $N \cdot T_{\max}$


  

The ranges are common rules of thumb from the PSO literature, not guarantees. The parameter study below shows
how sensitive the outcome can be, even on a two-dimensional problem.








Step 1 – Initialization



  The initial positions are drawn uniformly at random from the search box, so that the swarm covers the whole domain, and the velocities are small random numbers
  (here within ±10 % of the box width):




  $$x_{i,j,0} = L_j + \text{rand}_{[0,1]} \cdot (U_j - L_j), \qquad v_{i,j,0} = 0.2\,\bigl(\text{rand}_{[0,1]} - 0.5\bigr)(U_j - L_j)$$




  Each particle starts with $\mathbf{p}_i = \mathbf{x}_i$, and the best of them becomes the global best $\mathbf{g}$. A fixed random seed makes a run reproducible, which matters when you compare
  parameter settings.









Step 2 – The Velocity Update



  This single line is the heart of PSO. In every iteration each particle's new velocity is the sum of three terms:




  $$\mathbf{v}_i \;\leftarrow\; \underbrace{w\,\mathbf{v}_i}_{\text{inertia}} \;+\; \underbrace{c_1\,\mathbf{r}_1 \odot (\mathbf{p}_i - \mathbf{x}_i)}_{\text{memory (cognitive)}} \;+\; \underbrace{c_2\,\mathbf{r}_2 \odot (\mathbf{g} - \mathbf{x}_i)}_{\text{swarm (social)}}$$




  Here $\mathbf{r}_1$ and $\mathbf{r}_2$ are vectors of fresh uniform random numbers in $[0,1]$, one per coordinate, and $\odot$ is the component-wise product.
  The three terms have a simple physical meaning:




  Inertia $w\mathbf{v}_i$ is the momentum: the particle tends to continue in the direction in which it was already travelling. It prevents abrupt turns and carries particles
  across the search space.


  Memory $c_1\mathbf{r}_1\odot(\mathbf{p}_i-\mathbf{x}_i)$ points from the particle to the best place it has ever found itself. It lets every particle
  explore on its own.


  Swarm $c_2\mathbf{r}_2\odot(\mathbf{g}-\mathbf{x}_i)$ points from the particle to the best place found by anyone. It shares information and makes the swarm converge.




  Because of the random factors, the pull is not a fixed vector: it can be anywhere in the rectangle spanned by the particle and the target, scaled by $c$ (Fig. 2). On average $r = 0.5$,
  so the average pull of each term is $c/2$ of the distance to its target.



















0

2

4

6

8

10

2

4

6

8
x1
x2










p

g

xi (particle)

xnew
previous v

inertia  w·v

memory  c₁r₁(p − x)

swarm  c₂r₂(g − x)

new velocity

Fig. 2 – The velocity update in two dimensions (to scale). The particle $\mathbf{x}_i$ has the previous velocity $(1.2,\,0.5)$; its personal best is $\mathbf{p}$ and the swarm's best is $\mathbf{g}$. With
$w = 0.7298$, $c_1=c_2=1.49618$, $\mathbf{r}_1=(0.5,\,0.3)$ and $\mathbf{r}_2=(0.2,\,0.35)$ the three terms (purple, green, orange) add up to the new velocity $(2.70,\,3.73)$ (black), which carries the particle to $\mathbf{x}^{new}$.
The dashed rectangles show where the memory and swarm terms could have ended up for other values of $\mathbf{r}_1$ and $\mathbf{r}_2$.






Choosing $w$, $c_1$ and $c_2$: the constriction coefficients



  With $w=1$ and large $c$ the velocities can grow without limit and the swarm “explodes”. Two classical remedies are an inertia weight $w
