Particle swarm optimization (PSO) is a population-based, stochastic way to search for a good solution. Each particle represents a candidate solution and moves through the search space. Rather than replacing weaker candidates, the swarm keeps its particles and lets them use successful positions found by themselves and others. section 9.2 section 9.0
The intuition comes from bird flocking and fish schooling: individuals respond to shared information without a central controller. In PSO, a particle remembers its own best-known position while the swarm tracks its best-known position. Movement toward successful positions helps the group search together. section 9.1 section 9.8
A practical PSO run follows a repeating pattern:
- Initialize the swarm and its parameters. section 9.1
- Evaluate candidates, identify the best positions found so far, and update each particle using its own experience and information from the swarm or its neighbors. section 9.1 section 9.8
- Repeat until the stopping criterion is met. Tune inertia and the cognitive and social coefficients to balance exploration against exploitation. section 9.1 section 9.5
A 20-city route
For a traveling salesman problem, a particle’s position can be converted into a city order by sorting its indices. A cost function sums the distances between successive cities and the return to the start. A Python example supplies that cost function to a PySwarms optimizer, then prints the best route and its length. This is a search result, not a guarantee of an optimal tour. section 9.6 section 2.2
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.
The optimizer listing shows the inertia, cognitive, and social settings, particle count, iteration count, and conversion of the returned solution into a route. section 9.6
PSO began with continuous variables, but binary and permutation-based variants extend it to discrete and combinatorial problems. Because its search is stochastic, progress need not be steady and a run can miss the optimum; parameter tuning matters. section 9.8 section 2.2


