The Particle Swarm Optimizer (PSO) algorithm is a population-based stochastic algorithm. If you have not read the introduction lesson of PSO, please see here. In the last lessons, we tried to understand the inspiration and motivation for the Particle Swarm Optimizer (PSO) Algorithm. The various components of the PSO algorithm are discussed in detail. If you have already read the implementation details of the PSO algorithm, then this Python Code for PSO Algorithm will be like a treat for you. If you haven’t then go and check that out!

So, here we are presenting the Python Code for PSO Algorithm. You need to define your objective problem in the given code. The code is usable and can be implemented with slight modifications.

import numpy as np

# PSO Parameters
numParticles = 50  # Number of particles in the swarm
maxIterations = 100  # Maximum number of iterations
c1 = 2  # Cognitive coefficient
c2 = 2  # Social coefficient
w = 0.7  # Inertia weight

# Problem-specific parameters
Dim = 2  # Dimensionality of the problem
# Define your problem here, including the objective function and any constraints


# Define the PSO function
def pso():
    # Initialize the swarm
    position = np.random.rand(numParticles, Dim)  # Particle positions
    velocity = np.zeros((numParticles, Dim))  # Particle velocities
    personalBest = position.copy()  # Personal best positions
    personalBestFitness = np.zeros(numParticles) + np.inf  # Personal best fitness values
    globalBest = np.zeros(Dim)  # Global best position
    globalBestFitness = np.inf  # Global best fitness value

    # Main loop
    for iteration in range(maxIterations):
        # Evaluate fitness for each particle
        for i in range(numParticles):
            fitness = objective_function(position[i])

            # Update personal best if better fitness is found
            if fitness < personalBestFitness[i]:
                personalBest[i] = position[i]
                personalBestFitness[i] = fitness

            # Update global best if better fitness is found
            if fitness < globalBestFitness:
                globalBest = position[i]
                globalBestFitness = fitness

        # Update particle velocities and positions
        for i in range(numParticles):
            r1 = np.random.rand(Dim)
            r2 = np.random.rand(Dim)
            velocity[i] = w * velocity[i] \
                          + c1 * r1 * (personalBest[i] - position[i]) \
                          + c2 * r2 * (globalBest - position[i])
            position[i] = position[i] + velocity[i]

            # Apply any necessary constraints to the particle positions

            # Update fitness if necessary

            # Display current best fitness
            print(f'Iteration {iteration + 1}: Best Fitness = {globalBestFitness}')

    # Display final result
    print('Optimization Complete!')
    print(f'Best Fitness = {globalBestFitness}')
    print(f'Best Position = {globalBest}')


# Define your objective function
def objective_function(x):
    # Define your objective function here
    pass


# Run the PSO algorithm
pso()

If you are looking MATLAB implementation of Particle Swarm Optimizer (PSO) Algorithm, click here. Having any difficulty, leave your comment below. Happy Learning!

If you’re interested in reading original PSO paper for more details, check out this link.

Leave a Reply

Your email address will not be published. Required fields are marked *