A particle is a candidate solution in particle swarm optimization (PSO). Its position supplies one value for each dimension of a search space. Picture a hiker moving across a landscape: the hiker’s location is a candidate answer, while the landscape’s height indicates how good that answer is. A swarm uses many such searchers to look for good maximums or minimums without checking every possible point. section 7.2 section 7.3
A drone-design search
Suppose a drone design varies by its amounts of aluminum and plastic. A particle’s two position values represent those amounts. A fitness function scores the resulting design; that score is not a third search dimension. The goal here is to find a position with a low score, representing less drag and wobble. section 7.3 section 7.7
Each particle needs enough state to move and remember what worked:
- Position records its current values in every search dimension. section 7.4
- Velocity records its current direction and speed of movement. section 7.4 section 7.8
- Best position and best fitness preserve the best result that particular particle has found so far; current fitness scores its present position. section 7.4 section 7.7
class Particle:
def __init__(self, x, y, inertia,
➥cognitive_constant, social_constant):
self.x = x #A
self.y = y #A
self.fitness = math.inf
self.velocity = (0.0, 0.0) #B
self.best_x = x
self.best_y = y #C
self.best_fitness = math.inf #D
self.inertia = inertia
self.cognitive_constant = cognitive_constant
self.social_constant = social_constant
The Python class makes that state concrete with x and y, velocity, current fitness, and saved best values. section 7.4
Start particles at random positions spread across the allowed search space. More particles can cover more ground but require more computation. An initial velocity can be zero; small random velocities also give particles different directions immediately. After initialization, score every position and save each particle’s first score as its personal best. section 7.6 section 7.7
Repeat the search by adjusting velocity and then position. Three influences guide a particle: inertia carries its existing movement forward, its personal best draws it toward its own promising find, and the swarm’s best draws it toward a shared find. Higher inertia favors exploring new regions; lower inertia favors refining nearby solutions. Score the new positions, update saved bests when results improve, and stop after a suitable number of iterations or when improvement stagnates. Return the best position found by the swarm. section 7.8 section 7.7 section 7.9 section 7.5
Figure 7.20 The movement of particles over five iterations
The movement diagram traces particles across five iterations, making repeated position updates easier to picture. section 7.8

