This repository contains a Python implementation of Particle Swarm Optimization (PSO), a population-based stochastic optimization algorithm inspired by the social behavior of bird flocking or fish schooling. PSO is used to find optimal solutions in complex search spaces by iteratively improving a candidate solution with regard to a given measure of quality.
Particle Swarm Optimization simulates a swarm of particles moving through the search space. Each particle represents a potential solution and has:
- A position vector
- A velocity vector
- A personal best position (pbest) - the best solution it has found
- Knowledge of the global best position (gbest) - the best solution found by any particle in the swarm
At each iteration, particles update their velocities based on:
- Their current velocity (inertia)
- Attraction to their personal best position
- Attraction to the global best position
The velocity update formula is:
v_i = w * v_i + c1 * r1 * (pbest_i - x_i) + c2 * r2 * (gbest - x_i)
x_i = x_i + v_i
Where:
wis the inertia weightc1andc2are acceleration coefficientsr1andr2are random values between 0 and 1
This project implements PSO and tests it on the Sphere benchmark function, which is a simple convex optimization problem with the global minimum at the origin. The algorithm's performance is analyzed through convergence plots showing how the best fitness value improves over iterations.
particle-swarm-optimization/
├── src/
│ ├── __init__.py
│ ├── pso.py # PSO algorithm implementation
│ └── objective_functions.py # Benchmark functions (Sphere)
├── experiments/
│ ├── run_experiment.py # Script to run PSO experiment
│ └── plot_results.py # Script to generate convergence plots
├── results/ # Output directory for results
│ ├── output.txt # Best solution and fitness
│ ├── convergence.txt # Fitness values per iteration
│ └── convergence.png # Convergence plot
├── report/
│ └── PSO_Report.docx # Detailed analysis report
├── requirements.txt # Python dependencies
└── README.md # This file
- numpy: For numerical computations and array operations
- matplotlib: For plotting convergence curves
- Ensure Python 3.7+ is installed
- Install dependencies:
pip install -r requirements.txt
Execute the main experiment script:
python experiments/run_experiment.pyThis will:
- Initialize a swarm of 30 particles in 2D space
- Run PSO for 100 iterations with parameters: w=0.7, c1=1.5, c2=1.5
- Optimize the Sphere function
- Save results to
results/output.txtandresults/convergence.txt
After running the experiment, create the convergence plot:
python experiments/plot_results.pyThis generates results/convergence.png showing the best fitness value over iterations.
The PSO algorithm successfully optimizes the Sphere function, finding solutions very close to the global optimum [0, 0]. For example, a typical run might find:
- Best Solution: [0.00039, 0.00019]
- Best Value: 1.90e-07
The convergence plot demonstrates rapid initial improvement followed by fine-tuning, characteristic of effective PSO performance.
The current implementation uses standard PSO parameters:
- Swarm size: 30 particles
- Dimensions: 2
- Iterations: 100
- Inertia weight (w): 0.7
- Cognitive coefficient (c1): 1.5
- Social coefficient (c2): 1.5
- Initial position bounds: [-5, 5]
- Initial velocity bounds: [-1, 1]
To test on different functions or parameters:
- Add new objective functions in
src/objective_functions.py - Modify parameters in
experiments/run_experiment.py - Adjust plotting in
experiments/plot_results.pyif needed
- Kennedy, J., & Eberhart, R. (1995). Particle swarm optimization. Proceedings of ICNN'95 - International Conference on Neural Networks, 4, 1942-1948.
- Shi, Y., & Eberhart, R. (1998). A modified particle swarm optimizer. Proceedings of the IEEE International Conference on Evolutionary Computation, 69-73.