This repository implements Particle Swarm Optimization (PSO), an optimization algorithm inspired by the social behavior of birds flocking or fish schooling. The implementation uses PSO to find the minimum of several well-known mathematical functions commonly used in optimization problems.
- Custom Functions: The code includes a custom objective function as well as several classic test functions:
- Ackley
- Rosenbrock
- Rastrigin
- Himmelblau
- McCormick
- Three-hump camel
- Particle Swarm Optimization: The PSO algorithm is implemented with customizable parameters, such as inertia weight (w), cognitive (c1), and social (c2) coefficients.
- Visualization: You can visualize the particles’ movement across the solution space with a real-time contour plot animation. This demonstrates how particles converge to the global minimum of the objective function.
To run the code, you need the following Python libraries:
- numpy
- matplotlib
Install them using:
pip install numpy matplotlib
To run the PSO algorithm, execute:
python main.py
This will apply PSO to the selected test functions and display the optimization process as an animation if show=True is set.
Each test function is defined to test the optimization algorithm’s effectiveness. These are classical functions in optimization research. A custom objective function f(x, y) is also provided.
You can view the optimization process for each function in the following videos:
- Ackley Function:
ackley.mp4
- Rosenbrock Function:
rosenbrock.mp4
- Rastrigin Function:
rastrigin.mp4
- Himmelblau Function:
himmelblau.mp4
- McCormick Function:
mccormick.mp4
- Three-Hump Camel Function:
three_hump_camel.mp4
You can adjust the PSO parameters in the ParticleSwarmOptimization class:
n_epoch: Number of epochs (iterations).n_particles: Number of particles.c1: Cognitive coefficient (influence of personal best).c2: Social coefficient (influence of global best).w: Inertia weight (velocity retention across iterations).
This project is licensed under the MIT License.