Hello everyone! I’m going to show you my Matlab code of Particle Swarm Optimization algorithm (PSO algorithm) for solving constrained optimization problems.
To validate the performance of this PSO algorithm, we use a benchmark problem namely constrained box volume optimization problem. Of course, you can download and customize my Matlab code of P-S-O algorithm to solve optimization problems in your field.
Here are the details of the benchmark problem.
Did you know that Particle Swarm Optimization algorithm and Genetic Algorithm are the most popular stochastic optimization algorithms for solving complex large-scale optimization problems.
Let’s see it.
For more videos like this, check my YouTube channel here.
objective_function.m
function Z = objective_function(X)
L = X(1);
W = X(2);
H = X(3);
% Constraint
P = 2*L*W + 2*L*H + 2*W*H - 10;
if P < 0
P = 0;
else
P = 1; % Penalty
end
Z = L*W*H*(1-P);
Main_PSO.m
clc
clear all
close all
%% Problem
nVar = 3; % number of variables
VarMin = [0 0 0]; % lower bound of variable
VarMax = [10 10 10]; % upper bound of varible
%% PSO parameters
MaxIter =300; % max number of iterations
nPop = 100; % population size
w = 1; % inertia
d = 0.99; % 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
% generate random solutions
for k = 1:nVar
x(i).position(k) = unifrnd(VarMin(k),VarMax(k));
end
x(i).velocity = zeros([1 nVar]); % initial velocity
x(i).fitness = objective_function(x(i).position);
x(i).best.position = x(i).position; % update the local best
x(i).best.fitness = x(i).fitness; % update the local best
.
.
.
Sorry! This is only a half of the code.
Notice: It’s possible to watch the video and re-type the Matlab 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 Matlab 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 Matlab code here (Membership Code ID: 013)
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!
Hello everyone! In this post, I am going to show you my innovative version of…
Hello everyone. Let’s take a test to check your understanding about genetic algorithm, with multiple…
Hello everyone! In this post, I am going to show you my innovative version of…
Hello everyone. In this post, I am going to show you my innovative version of…
Hello everyone! Let’s see how my innovative version of Genetic Algorithm, called Adaptive Re-start Hybrid…
Hello everyone! Let’s take a short quiz, to test your knowledge about crypto-currency, or crypto.…
View Comments
Thank you so much for your help
Good luck with your study and research!
hy sir, hope you are doing well...how can I use this code for minimization problem? where we have to made changes ?
Convert the objective function from maximum problem to minimum problem. Good luck!
yes I do.. I write the following objective function for minimization problem
z = sum(a*(b-c) + d*(g-f))
where a, and d are unknown variables and value of b, c, g and f are known. I also change global_best.fitness from -inf to +inf. however MATLAB produce error in update velocity equation. I also check the code multiple time code is absolutely right. now please help me in this problem I shall be very thankful to you
If i change the problem (example : gain control trajectory tracking mobile robot ), can i use this algoritm for n-constrain?
Yes, it is possible
can I used the above code for optimizing the switching angle of a multilevel inverter using selective harmonic elimination
Hi, I think it's possible but some modification/update must be done.
Sir, I need your help in our project and I can pay you for this. Do reach us.
Hi, how can I help?
Hi can used particle optimization in recommending the best product online?, thanks
Yes, it is possible
can you recommend the best algorithm for improving the recommending system? thans
Hello, I don't know the best algorithm for that.
Hello ,Dr hope you are doing great , thanks a lot for your help and clear explaination.
I have constraint from a form for a vector that its elements needs to be <= 0.4.
dx = [S(1) - 0.3, diff(S)] <=0.4 where the S is the decision variable .
I would be greatful sir,thanks a lot
Hello, many thanks for your interest. Sorry, I don't have the background to understand your constraints.