Skip to content

Particle Swarm Optimization (PSO)

A swarm method for continuous problems and a simple first try.

PSO

PSO(*, w=0.9, w_damp=0.99, c1=2.0, c2=2.0, w_final=0.4, max_velocity=0.15, **kwargs)

Particle Swarm Optimization with inertia weight (global best topology).

Each particle moves by its velocity, which is pulled toward the particle's own best position and toward the best position of the swarm. A particle that leaves [0, 1] is clipped and the velocity components that left are reversed. The defaults, w from 0.9 to 0.4 with max_velocity=0.15, were chosen on benchmark tests. On random-key combinatorial problems (TSP or knapsack in the encoded space) use w=1, w_damp=0.99, w_final=None, max_velocity=None.

Parameters

  • w: inertia weight at the start (default 0.9)
  • w_damp: the inertia weight is multiplied by this after every iteration, used only when w_final is None
  • c1: personal acceleration coefficient
  • c2: global acceleration coefficient
  • w_final: if given, the inertia weight falls linearly from w to w_final over the run (by the share of the run used, progress()), and w_damp is ignored. The default is 0.4, the common 0.9 to 0.4 schedule from the later work of Shi and Eberhart. None damps w by w_damp instead.
  • max_velocity: if given, each velocity component is clamped to [-max_velocity, max_velocity] in code units (the search space is [0, 1], so 0.15 is 15% of the range). The default is 0.15. None gives no clamping.

References: J. Kennedy and R. Eberhart, "Particle swarm optimization", Proceedings of the IEEE International Conference on Neural Networks (ICNN), 1995. Y. Shi and R. Eberhart, "A modified particle swarm optimizer", IEEE World Congress on Computational Intelligence (WCCI), 1998. M. Clerc and J. Kennedy, "The particle swarm: explosion, stability, and convergence in a multidimensional complex space", IEEE Transactions on Evolutionary Computation 6(1), 2002, 58 to 73.

pso.use_constriction_coeffs(phi1=2.05, phi2=2.05) sets w, c1 and c2 to the constriction coefficients of Clerc and Kennedy (2002).

Example

import ypea

problem = ypea.Problem(
    ypea.Real("x", shape=20, lb=-10, ub=10),
    lambda sol: ypea.benchmarks.sphere(sol.x),
    vectorized=True,
    name="sphere",
)

alg = ypea.PSO(max_nfe=20_000)
result = alg.solve(problem, seed=1)
print(result.summary())
PSO on sphere
  best objective value: 0.00211644
  feasible: True (violation 0)
  evaluations: 20000, iterations: 199, time: 0.0184 s, stopped by: max_nfe

Differs from Python: MATLAB draws its own random numbers, so the best value differs from Python's.

problem = ypea.Problem( ...
    vars=ypea.Real("x", shape=20, lb=-10, ub=10), ...
    objective=@(sol) ypea.benchmarks.sphere(sol.x), ...
    vectorized=true, ...
    name="sphere");

alg = ypea.PSO(max_nfe=20000);
result = alg.solve(problem, seed=1);
disp(result.summary())
PSO on sphere
  best objective value: 0.000389909
  feasible: true (violation 0)
  evaluations: 20000, iterations: 199, time: 0.179 s, stopped by: max_nfe

Search on Rastrigin

PSO solutions on Rastrigin at four iterations, and its convergence
PSO with default settings, 50 iterations on Rastrigin in two dimensions (seed 1). The red points are the particles. The open diamond is the best solution so far, and the plus sign the global minimum.