Skip to content

Wasif-Sadiq/particle-swarm-optimization

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Particle Swarm Optimization (PSO) Implementation

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.

What We Are Doing

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:

  1. Their current velocity (inertia)
  2. Attraction to their personal best position
  3. 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:

  • w is the inertia weight
  • c1 and c2 are acceleration coefficients
  • r1 and r2 are 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.

Project Structure

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

Dependencies

  • numpy: For numerical computations and array operations
  • matplotlib: For plotting convergence curves

Installation

  1. Ensure Python 3.7+ is installed
  2. Install dependencies:
    pip install -r requirements.txt

Usage

Running the Experiment

Execute the main experiment script:

python experiments/run_experiment.py

This 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.txt and results/convergence.txt

Generating Convergence Plots

After running the experiment, create the convergence plot:

python experiments/plot_results.py

This generates results/convergence.png showing the best fitness value over iterations.

Results

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.

Parameters

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]

Extending the Code

To test on different functions or parameters:

  1. Add new objective functions in src/objective_functions.py
  2. Modify parameters in experiments/run_experiment.py
  3. Adjust plotting in experiments/plot_results.py if needed

References

  • 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.

About

Implementation and analysis of Particle Swarm Optimization (PSO)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages