Solving Optimization Problems

Particle Swarm Optimization (PSO) for Constrained Optimization Problems in Matlab

Hello everyone and welcome!

In this post, I am going to show you my Particle Swarm Optimization (PSO) algorithm for solving the constrained optimization problems. This version of Particle Swarm Optimization is coded in Matlab and its performance has been validated by comparing the obtained results with the well-known benchmark.

Here are the details of the constrained optimization problem to be solved in this video. As we can see, this is a minimum optimization problem with two constraints; one constraint is nonlinear and another one is linear. In addition, the proven global optimal solution is shown here and to be used as our benchmark to test our Particle Swarm Optimization algorithm.

Did you know that Particle Swarm Optimization is the second most popular stochastic optimization algorithm for solving the complex large-scale optimization problems as shown in this Figure. Only Genetic Algorithm is more popular, based on statistics data on Scopus database.

Here are some basic working principles of Particle Swarm Optimization. Particle Swarm Optimization solves an optimization problem by having a population of candidate solutions, called particles, and moving these particles around in the search-space of the optimization problem, using a simple mathematical formula related to the particle’s position and velocity. Each particle’s movement is influenced by its local best known position, but is also guided toward the best known positions in the search-space, which are updated as better positions are found by other particles. This is expected to move the swarm toward the best solutions. This working principle of Particle Swarm Optimization is illustrated by this simulation.

Source: https://en.wikipedia.org/wiki/Particle_swarm_optimization

Particle Swarm Optimization in Matlab:

Let’s see how my Particle Swarm Optimization (PSO) for constrained optimization problems works:

Matlab Code of PSO:

objective_function.m

function Z = objective_function(O)
    x = O(1);
    y = O(2);
    nonlinear_constraint = (x - 1)^3 -y +1; 
    linear_constraint = x + y -2;

    if nonlinear_constraint > 0 
        penalty1 = 1;
    else
        penalty1 = 0;
    end

    if linear_constraint > 0
        penalty2 = 1;
    else
        penalty2 = 0;
    end

Z = (1 - x)^2 + 100*(y - x^2)^2 + penalty1 + penalty2;

Main_PSO.m

clc
clear all
close all
%% Problem
nVar = 2; % number of variables
VarMin = [-1.5 -0.5]; % lower bound of variable
VarMax = [1.5 2.5]; % upper bound of varible
%% PSO parameters
MaxIter = 200; % max number of iterations
nPop = 100; % population size
w =  1; % inertia 
d = 0.9;  % damping ratio of the inertia
c1 = 2; % acceleration 1
c2 = 2; % acceleration 2
%% Initial
x0.position = [];
x0.velocity = [];
x0.fitness = [];
x0.best.position =[];
x0.best.fitness =[];

x = repmat(x0,nPop,1); % Make a population
global_best.fitness = inf;
% Generate initial population
for i = 1: nPop
    x(i).position = unifrnd(VarMin,VarMax,[1 nVar]); % generate random solutions
    x(i).velocity = zeros([1 nVar]); % initial velocity
    x(i).fitness = objective_function(x(i).position); %calculate the fitness
    x(i).best.position = x(i).position; % update the local best
.
.
.
Sorry! This is only a half of the Matlab code. 

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

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

Download the whole Matlab code here (Membership Code ID: 035)

No need to build the Matlab 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 and publications!

Dr.Panda

View Comments

  • I have a dataset with 4 input and 2 outpit. I want to maximize one output and minimize the other.
    how do I use directly the dataset in this code

    • Your problem is multi objective optimization problem. Please see other posts related to multi objective optimization on this website. Good luck!

  • Hi, I just made a payment contribution for Matlab Code of Particle Swarm Optimization (PSO) but I cannot access it yet.

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…

7 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…

7 months ago

Adaptive Restart Hybrid Genetic Algorithm

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

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

1 year ago