Solving Optimization Problems

Particle Swarm Optimization (PSO) for Constrained Optimization Problems

Hello everyone and welcome. I’m going to show you a simple but effective Particle Swarm Optimization or PSO algorithm for solving constrained optimization problems.

In this video, first, I run the PSO algorithm to show its performance in solving an optimization problem involving both linear and non-linear constraints. And then I will show you the Python code of the PSO algorithm, which you can download and customize to solve optimization problems in your fields.

By the way, my YouTube channel plans to cover various stochastic optimization algorithms in both Matlab and Python. There are many stochastic optimization algorithms such as Genetic algorithm, Particle swarm optimization,Ant colony optimization, Artificial bee colony, Cuckoo search, Greedy algorithm, Harmony search, Pattern search, Simulated annealing, Stochastic tunneling, Tabu search, etc. So, check it out if you need it.

For more videos like this, check my YouTube channel here.

import random
import time
import matplotlib.pyplot as plt
# ------------------------------------------------------------------------------
def objective_function(O):
    x = O[0]
    y = O[1]
    nonlinear_constraint = (x - 1) ** 3 - y + 1
    linear_constraint = x + y - 2
    if nonlinear_constraint > 0:
        penalty1 = 1
    else:
        penalty1 = 0
 
    if linear_constraint > 0:
        penalty2 = 1
    else:
        penalty2 = 0
 
    z = (1 - x) ** 2 + 100 * (y - x ** 2) ** 2 + penalty1 + penalty2
    return z
 
bounds = [(-1.5, 1.5), (-0.5, 2.5)]  # upper and lower bounds of variables
nv = 2  # number of variables
mm = -1  # if minimization problem, mm = -1; if maximization problem, mm = 1
 
# PARAMETERS OF PSO
particle_size = 120  # number of particles
iterations = 200  # max number of iterations
w = 0.8  # inertia constant
c1 = 1  # cognative constant
c2 = 2  # social constant
 
# Visualization
fig = plt.figure()
ax = fig.add_subplot()
fig.show()
plt.title('Evolutionary process of the objective function value')
plt.xlabel("Iteration")
plt.ylabel("Objective function")
# ------------------------------------------------------------------------------
class Particle:
    def __init__(self, bounds):
        self.particle_position = []  # particle position
        self.particle_velocity = []  # particle velocity
        self.local_best_particle_position = []  # best position of the particle
.
.
.
 
    def evaluate(self, objective_function):
        self.fitness_particle_position = objective_function(self.particle_position)
        if mm == -1:
            if self.fitness_particle_position < self.fitness_local_best_particle_position:
                self.local_best_particle_position = self.particle_position  # update the local best
                self.fitness_local_best_particle_position = self.fitness_particle_position  # update the fitness of the local best
.
.
.
 
    def update_velocity(self, global_best_particle_position):
        for i in range(nv):
            r1 = random.random()
            r2 = random.random()
 
            cognitive_velocity = c1 * r1 * (self.local_best_particle_position[i] - self.particle_position[i])
            social_velocity = c2 * r2 * (global_best_particle_position[i] - self.particle_position[i])
            self.particle_velocity[i] = w * self.particle_velocity[i] + cognitive_velocity + social_velocity
 
    def update_position(self, bounds):
        for i in range(nv):
            self.particle_position[i] = self.particle_position[i] + self.particle_velocity[i]
 
            # check and repair to satisfy the upper bounds
            if self.particle_position[i] > bounds[i][1]:
                self.particle_position[i] = bounds[i][1]
            # check and repair to satisfy the lower bounds
            if self.particle_position[i] < bounds[i][0]:
                self.particle_position[i] = bounds[i][0]
 
class PSO:
    def __init__(self, objective_function, bounds, particle_size, iterations):
        fitness_global_best_particle_position = initial_fitness
        global_best_particle_position = []
        swarm_particle = []
        for i in range(particle_size):
            swarm_particle.append(Particle(bounds))
.
.
.
Sorry! This is only a half of the code.

Notice: It’s possible to watch the video and re-type the Python code yourself – that would take you from 1 to 3 hours; or with just €1.99 (the cost of a cup of coffee), you can download/copy the whole Python code within 2 minutes. It’s your choice to make.

Original price is €4.99 but today it’s only €1.99 (save €3 today – available for a limited time only)

Download the whole Python code here (Membership Code ID: 011)

No need to build the Python code from scratch because it’s very time-consuming. My idol, Jim Rohn, once said: “Time is more value than money. You can get more money, but you cannot get more time”. If you think this code can be used in your research/teaching work, you should download it and then customize/modify/apply it to your work, without any obligation of citing the original source if you don’t want. However, redistribution (i.e., downloading the code/script here and then making it available on another site on the Internet) is strictly prohibited.

If you have any question or problem, please contact Dr. Panda by email: learnwithpanda2018@gmail.com

Thank you very much and good luck with your research!

Dr.Panda

View Comments

  • Dear Sir,

    Its a very nice explanation of PSO algorithm. I cannot find code of PSO in this webpage, on PC also its not visible. Please do help.

  • Hi,
    could you please explain why you choose penalties like in the code above??
    Shouldn't they be something like P = max(0,x+y-2 ) for linear constraint ??
    Looking forward to your reply :)

  • Can I use this code for N-queen problem by changing fitness function and initial particles as 2-dimensional Grids. ?????//?

  • But I have some confusion.

    My problem is to solve 8-queen problem.

    I create initial particles by 8-*8 grid that are randomly populated with queens.
    Our fitness function returns number of attacks that is a board or grid with less number of queen attacks is best solution.

    I create a grid as 2-d array randomly populated with 1s and 0s where 1s represent queens and 0s represents empty space.

    Does I correctly map the problem???

    In your code you talk about the constraint and penalty in objective ?? This thing confused me Sir ?? Does I changed the whole code ? or part of the code?? and what does" bounds " list represent in your code?? in your code where you initialize the particles ???? PLease help me as I am a student.
    I am waiting for your kind and helpful response.
    Thanks
    Best Regards :- Zoya

  • Sir could you explain my particles in N-queen problem???? Why can I write in bounds?? and where your particles are initialized in your code ?? What are desicion variables?? Could you elaborate the code ?? with explanation please??

    • Hi, each particle is one solution to your problem. Bounds as in line 23, for example, -1.5 < x -0.5 and 1.5 < y < 2.5. Particle variables are initialized in line 52 of the code. Decision variables in this problem are x and y. Is that clear?

      • ok Sir, in my N-queen problem,

        suppose here N is 4 then , first I randomly initialize the particles as populating 4*4 different grids. And each grid which is 2D array is one solution or particle. like,

        [[ 1 0 0 1], [0 1 01] [1 0 1 0 ][ 0 0 0 1] ] // one particle or one solution
        [[ 1 0 1 1], [0 1 01] [1 1 1 0 ][ 0 1 0 1] ] // 2nd particle or 2nd solution and son on.

        So each particle is a 2D array a numpy array.
        We need to find a 4*4 grid with 4 queens but no queen is in attack. So my objective function is less number of attacks.

        So sir, I write the objective function.. But I cannot understand what can I write in bounds and how can Icreate particles that is 2d arrays randomly initilizaed with 0s and 1s with 1 represent queen and 0 represent empty space ??

        • If i understand your problem correctly, your solution (2D binary array) does not have bounds. So, just remove the bounds from the code and randomly generate 2D binary array, and then calculate the objective function.

  • Yes Sir, as you guide me,
    I remove the bounds, from the code, and initilize a 2d arrys in line 52 of code and write my own objective function.
    But sir it does not run successfully. what is my "random position" and "random velocity" of particle?

  • In the whole code there is concept of bounds , there is bounds in "update velocity function" also.
    Sir I think i rewrite the whole code ????? Still i am soo confused???

Recent Posts

Adaptive Re-Start Hybrid Genetic Algorithm in Matlab

Hello everyone! In this post, I am going to show you my innovative version of…

8 months ago

Test Your Understanding About Genetic Algorithm (Test 2)

Hello everyone. Let’s take a test to check your understanding about genetic algorithm, with multiple…

8 months ago

Adaptive Restart Hybrid Genetic Algorithm

Hello everyone! In this post, I am going to show you my innovative version of…

8 months ago

Adaptive Re-Start Hybrid Genetic Algorithm (Test the Performance in Case Studies)

Hello everyone. In this post, I am going to show you my innovative version of…

1 year ago

Adaptive Re-Start Hybrid Genetic Algorithm in Matlab

Hello everyone! Let’s see how my innovative version of Genetic Algorithm, called Adaptive Re-start Hybrid…

1 year ago

Crypto Quiz (Test Your Knowledge About Cryptocurrency)

Hello everyone! Let’s take a short quiz, to test your knowledge about crypto-currency, or crypto.…

2 years ago