This repository contains a Python code for implementing Particle Swarm Optimization (PSO) to solve a minimization problem using the PySwarms library. PSO is a population-based optimization algorithm inspired by the social behavior of birds and fish. In particle swarm optimization a population of potential solutions are represented by particles which move through the search space in of an optimization function in order to find the optimal solution to the function. Each particle moves through the search space based on its own experience and the experience of nearby particles.
These instructions will help you set up and run the PSO algorithm on your machine.
Make sure you have the required Python packages installed. You can install them using the provided requirements.txt file:
pip install -r requirements.txtYou can adjust the following parameters in the script to customize the optimization process:
- Number of genes (dimensions) in an individual (d)
- Size of the population (P)
- Number of generations (GENS)
- Range for the value of genes generated for the population (MIN and MAX)
The optimization algorithm is applied to the following test function defined in the script:
def test_function(ind):
n = len(ind.genes)
sum_part = sum((0.5 * i * ind.genes[i-1])**2 for i in range(1, n))
return sum(x ** 2 for x in ind.genes) + sum_partThe script will output the best position found by the PSO algorithm along with the corresponding fitness value over the specified numver of generations.
PySwarms library for PSO implementation by: @ljvmiranda921