Particle swarm optimization (PSO) is an evolutionary-computation method that searches with a population of particles. Each particle is evaluated for fitness; fitter particles guide the swarm toward better regions of the solution space. The approach borrows survival-of-the-fittest and swarming behavior rather than relying on genetic operators. section 1.1.1 section 4.6
A particle represents one candidate solution as a vector. It begins at a position within configured bounds and has a speed. On each iteration, the algorithm evaluates every particle, retains each particle’s best-known position, identifies the swarm’s best particle, then updates positions by changing speeds toward those better candidates. Speed limits constrain each update. section 4.2.1
Listing 4.7 EDL_4_2_PSO.ipynb: Setting up the toolbox
toolbox = base.Toolbox()
toolbox.register("particle",
generate, size=2, pmin=-6,
pmax=6, smin=-3, smax=3) #1
toolbox.register("population",
tools.initRepeat, list,
toolbox.particle) #2
toolbox.register("update",
updateParticle, phi1=200, phi2=200) #3
toolbox.register("evaluate", evaluate) #4
Use the toolbox setup to see the four practical PSO operations: create particles, build a population, update movement, and evaluate fitness. section 4.2.1
Finding cannon-shot inputs
PSO can search for the initial velocity and firing angle that send a cannon ball to a target distance. Each particle is one velocity-and-angle guess; fitness is the squared difference between its calculated distance and the target. Repeated evaluation and swarming produce the best parameter pair. section 4.2 section 4.2.1
PSO is useful when a known function has unknown input parameters, and it extends to more-complex optimization problems. A further application is automatic hyperparameter optimization: map a particle vector into bounded hyperparameter values, train a model, use loss as fitness, and swarm toward lower-loss configurations. section 4.6 section 5.4.2
A practical DEAP workflow is:
- Define a particle generator with position and speed ranges. section 4.2.1
- Register particle, population, update, and evaluation functions in a toolbox. section 4.2.1
- Evaluate every particle and record personal and global best candidates. section 4.2.1
- Update particles toward the global best, then repeat for a chosen number of generations. section 4.2.1


