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!

64 Replies to “Particle Swarm Optimization (PSO) for Constrained Optimization Problems”

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

  2. 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 🙂

    1. Hi, there is no rule for selecting penalty. It depends on many factors like optimization problems, optimization algorithms, etc. For this problem, I used try-and-error method.

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

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

  5. 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??

    1. 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?

      1. 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 ??

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

  6. 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?

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

  8. Sir what is position of and velocity of particle means here ?? Suppose this is my
    soultion or particle which is 2D array [[1 0 0 01 ],[1 0 1 0 ] ,[1 1 0 1 ],[ 0 0 0 .1]]
    So what is initial position and of this particle and initial velocity ???

  9. Can I use this code for the incremental ELM optimization with some data set.
    If yes, what all changes would required.
    Please help me sir.

    1. Yes, it is possible. Try to understand the code first, and then you will know how to customize it to solve your problems

  10. hello sir , I am trying to optimize a problem using particle swarm optimization, but i don’t know how to write it on matlab. would you please help

    1. Hi, I don’t have background on that problem. But you can customize my PSO to solve your problems by updating the objective function and constraints.

  11. Hi Dr. Panda,

    Thanks for the code guidance. I have two questions regarding the constrained optimization model.
    1. It seems the decision variables in the model are continuous, what if integer or binary decision variables?
    2. Both x and y are scalar, what if X and Y are one-dimensional (vector) or two-dimensional (matrix)? Where and how to modify the source code?
    Thank you very much.

    1. For integers, we can use the “rounding” method. To solve a new problem, we need to update the objective function and constraints. The rest can be kept the same.

  12. Hello, Dr.Panda.
    I received the total code, but I can find the plot of iterations and minimum cost.
    The plot is not work, not plot graph now.
    please let me know the solution.

  13. Hi Dr. Panda
    May I have the matlab code of Particle Swarm Optimization (PSO) for Constrained Optimization Problems ,please?

    thank you

  14. Hello, I have my wireline log data and the core data. And I have to calculate mineral content such as volume of quartz, orthoclase, porosity, and volume of clay in the Shale reservoir using PSO. How can I go about it please? Can you be of help, please? Thank you, guys, as I await your responses.

  15. hi

    can we use this code for the deployment of WSN in forested region in 3D? I need to check the improvement of the cost, energy efficiency, coverage area etc using PSO algorithm

Leave a Reply

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