Particle swarm optimization (PSO) is a stochastic, population-based technique for searching for good solutions. Each candidate solution is a particle that moves through the search space using its own experience and information from other particles. The cooperative search is inspired by behaviors such as bird flocking and fish schooling. section 9.2 section 9.8
Think of a group searching for treasure: everyone keeps searching, but information from successful searchers helps guide the others. Unlike an approach that replaces poorer candidates, this cooperative picture retains the searchers and draws on better-performing ones. section 9.0
A basic PSO search follows this pattern:
- Initialize the swarm and its parameters; each particle represents a candidate solution. section 9.1 section 9.2
- Evaluate particles and track each particle’s best-known position and the swarm’s best-known position. section 9.1 section 9.8
- Adjust particle movement using individual and collective experience, then repeat until a stopping criterion is met. section 9.2 section 9.1
Inertia and cognitive and social coefficients influence the balance between exploration and exploitation. Tune them for the problem rather than assuming that one setting works everywhere. As a stochastic method, PSO can miss an optimum, and the time needed to find one cannot be predicted accurately. section 9.5 section 2.2
Continuous PSO was designed for continuous variables. Binary and permutation-based variants address discrete or combinatorial problems; adaptive PSO adjusts the exploration–exploitation trade-off. section 9.8 section 9.0
Planning a city tour
For a traveling salesman problem with 20 US cities, compute inter-city distances and score each particle by its tour distance. Sort the indices of a particle’s position to obtain a visiting order. A PySwarms example uses BinaryPSO with 100 particles, 150 iterations, and specified inertia, cognitive, social, and neighborhood settings; its best solution is converted into a named route. section 9.6
options = {'w': 0.79, 'c1': 2.05, 'c2': 2.05, 'k': 10, 'p': 2} #1
optimizer = ps.discrete.BinaryPSO(n_particles=100, dimensions=n_cities,
options=options) #2
cost, solution = optimizer.optimize(tsp_cost, iters=150, verbose=True,
distance=distance) #3
tour = np.argsort(solution) #4
city_names_tour = [city_names[i] for i in tour] #4
Route = " → ".join(city_names_tour) #5
print("Route:", Route) #5
print("Route length:", np.round(cost, 3)) #5
#1 Set up the PSO parameters.
#2 Instantiate the PSO optimizer.
#3 Solve the problem.
#4 Convert the best solution to a TSP tour.
#5 Print the best route and its length.
See the optimizer settings, run, and conversion of the best solution into a city route. section 9.6


