In this video tutorial, implementation of Particle Swarm Optimization (PSO) in MATLAB is discussed in detail. In the first part, theoretical foundations of PSO is briefly reviewed. In the next two parts of this video tutorial, PSO is implemented line-by-line and from scratch, and every line of code is described in detail.
The video tutorial is available to watch online, via Yarpiz YouTube Channel, and to download as MP4 files. The project files of the video tutorial and lecture notes written by instructor during the course, are also available to download. The instructor of this course is Dr. S. Mostapha Kalami Heris, PhD of Control and Systems Engineering.
Video Files on YouTube
Part 1 of 3 - Theoretical Foundations:
https://youtu.be/sB1n9a9yxJk
Part 2 of 3 - Implementation in MATLAB:
https://youtu.be/xPkRL_Gt6PI
Part 3 of 3 - Improvements:
https://youtu.be/ICBYrKsFPqA
The course is also freely available via Udemy:
http://udemy.com/pso-in-matlab
For more information and download the video and project files and lecture notes for this tutorial, see: http://yarpiz.com/ytea101
After watching this video tutorial, you will be able to know what is PSO, and how it works, and how you can use it to solve your own optimization problems. Also, you will learn how to implement PSO in MATLAB programming language. If you are familiar with other programming languages, it is easy to translate the MATALB code and rewrite the PSO code in other programming languages.
Yarpiz (2021). Video Tutorial of Particle Swarm Optimization (PSO) in MATLAB (https://www.mathworks.com/matlabcentral/fileexchange/57286-video-tutorial-of-particle-swarm-optimization-pso-in-matlab), MATLAB Central File Exchange. Retrieved .
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Create scripts with code, output, and formatted text in a single executable document.
Hi, Please if it can get some help in adding 2 or more variables that a function will return and for each variable i want to set some specific limits. Thanks
John Cap Here is the PSO.m with the modification to plot the particles.
By mistake I shared the original pso1.m in my last message
%
% Copyright (c) 2016, Yarpiz (www.yarpiz.com)
% All rights reserved. Please read the "license.txt" for license terms.
%
% Project Code: YTEA101
% Project Title: Particle Swarm Optimization Video Tutorial
% Publisher: Yarpiz (www.yarpiz.com)
%
% Developer and Instructor: S. Mostapha Kalami Heris (Member of Yarpiz Team)
%
% Contact Info: sm.kalami@gmail.com, info@yarpiz.com
%
function out = PSO(problem, params)
%% Problem Definiton
CostFunction = problem.CostFunction; % Cost Function
nVar = problem.nVar; % Number of Unknown (Decision) Variables
VarSize = [1 nVar]; % Matrix Size of Decision Variables
VarMin = problem.VarMin; % Lower Bound of Decision Variables
VarMax = problem.VarMax; % Upper Bound of Decision Variables
range=[VarMin VarMax VarMin VarMax];
Ndiv=50;
dx=(range(2)-range(1))/Ndiv;
dy=(range(4)-range(3))/Ndiv;
[x,y] =meshgrid(range(1):dx:range(2),range(3):dy:range(4));
z=x.^2+y.^2;
figure(1);
surfc(x,y,z);
%% Parameters of PSO
MaxIt = params.MaxIt; % Maximum Number of Iterations
nPop = params.nPop; % Population Size (Swarm Size)
w = params.w; % Intertia Coefficient
wdamp = params.wdamp; % Damping Ratio of Inertia Coefficient
c1 = params.c1; % Personal Acceleration Coefficient
c2 = params.c2; % Social Acceleration Coefficient
% The Flag for Showing Iteration Information
ShowIterInfo = params.ShowIterInfo;
MaxVelocity = 0.2*(VarMax-VarMin);
MinVelocity = -MaxVelocity;
%% Initialization
% The Particle Template
empty_particle.Position = [];
empty_particle.Velocity = [];
empty_particle.Cost = [];
empty_particle.Best.Position = [];
empty_particle.Best.Cost = [];
% Create Population Array
particle = repmat(empty_particle, nPop, 1);
% Initialize Global Best
GlobalBest.Cost = inf;
% Initialize Population Members
for i=1:nPop
% Generate Random Solution
particle(i).Position = unifrnd(VarMin, VarMax, VarSize);
% Initialize Velocity
particle(i).Velocity = zeros(VarSize);
% Evaluation
particle(i).Cost = CostFunction(particle(i).Position);
% Update the Personal Best
particle(i).Best.Position = particle(i).Position;
particle(i).Best.Cost = particle(i).Cost;
xn(i)=particle(i).Position(1);
yn(i)=particle(i).Position(2);
% Update Global Best
if particle(i).Best.Cost < GlobalBest.Cost
GlobalBest = particle(i).Best;
end
end
% Array to Hold Best Cost Value on Each Iteration
BestCosts = zeros(MaxIt, 1);
%% Main Loop of PSO
for it=1:MaxIt
for i=1:nPop
% Update Velocity
particle(i).Velocity = w*particle(i).Velocity ...
+ c1*rand(VarSize).*(particle(i).Best.Position - particle(i).Position) ...
+ c2*rand(VarSize).*(GlobalBest.Position - particle(i).Position);
% Apply Velocity Limits
particle(i).Velocity = max(particle(i).Velocity, MinVelocity);
particle(i).Velocity = min(particle(i).Velocity, MaxVelocity);
% Update Position
particle(i).Position = particle(i).Position + particle(i).Velocity;
xn(i)=particle(i).Position(1);
yn(i)=particle(i).Position(2);
% Apply Lower and Upper Bound Limits
particle(i).Position = max(particle(i).Position, VarMin);
particle(i).Position = min(particle(i).Position, VarMax);
% Evaluation
particle(i).Cost = CostFunction(particle(i).Position);
%Se grafica el punto
figure(2);
contour(x,y,z,15);
hold on;
plot(xn,yn,'.','markersize',10,'markerfacecolor','g');
drawnow;
% Update Personal Best
if particle(i).Cost < particle(i).Best.Cost
particle(i).Best.Position = particle(i).Position;
particle(i).Best.Cost = particle(i).Cost;
% Update Global Best
if particle(i).Best.Cost < GlobalBest.Cost
GlobalBest = particle(i).Best;
end
end
hold off
end
% Store the Best Cost Value
BestCosts(it) = GlobalBest.Cost;
% Display Iteration Information
if ShowIterInfo
disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCosts(it))]);
end
% Damping Inertia Coefficient
w = w * wdamp;
end
out.pop = particle;
out.BestSol = GlobalBest;
out.BestCosts = BestCosts;
end
Nice videos. Well-documented code. This is the best source I've ever seen!
How can I populate my dataset into this code ?
I run the basic PSO with initiating wdamp but it was updating by BestCost, can you please help me out.
Thanks
clc;
clear;
close all;
%% Problem Definition
CostFunction = @(x) Sphere(x); % Cost Function
nVar = 5; % Number of Unknown (Deceision) Variables
VarSize = [1 nVar]; % Matrix Size of Decision Variables
VarMin = -10; % Lower bound of Decesion Variable
VarMax = 10; % Upper bound of Decesion Variable
%% Parameters of PSO
MaxIt = 100; % Maximum Number of Iterations
nPop = 50; % Population Size (Swarm Size)
wdamp = 0.99; % Damping Ratio of Inertia Coefficient
w = 1; % Inertia Coefficient
c1 = 2; % Personal Acceleration Coefficient
c2 = 2; % Social Acceleration Coefficient
%% Initialization
% The Particle Template
empty_particle.Position = [];
empty_particle.Velocity = [];
empty_particle.Cost = [];
empty_particle.Best.Position = [];
empty_particle.Best.Cost = [];
% Create Population Array
particle = repmat(empty_particle, nPop, 1);
% Initialize Global Best
GlobalBest.Cost = inf;
% Initialize Population Members
for i=1:nPop
% Generate Random Solution
particle(i).Position = unifrnd(VarMin, VarMax, VarSize);
% Initialize Velocity
particle(i).Velocity = zeros(VarSize);
% Evaluation
particle(i).Cost = CostFunction(particle(i).Position);
% Update the Personal Best
particle(i).Best.Position = particle(i).Position;
particle(i).Best.Cost = particle(i).Cost;
% Update Global Best
if particle(i).Best.Cost < GlobalBest.Cost
GlobalBest = particle(i).Best;
end
end
%
% Array to Hold Best Cost Value on Each Iteration
BestCosts = zeros(MaxIt, 1);
%% Main Loop of PSO
for it=1:MaxIt
for i=1:nPop
% Update Velocity
particle(i).Velocity = w*particle(i).Velocity ...
+ c1*rand(VarSize).*(particle(i).Best.Position - particle(i).Position) ...
+ c2*rand(VarSize).*(GlobalBest.Position - particle(i).Position);
% Update Position
particle(i).Postion = particle(i).Position + particle(i).Velocity;
% Evaluation
particle(i).Cost = CostFunction(particle(i).Position);
% Update Personal Best
if particle(i).Cost < particle(i).Best.Cost
particle(i).Best.Position = particle(i).Position;
particle(i).Best.Cost = particle(i).Cost;
% Update Global Best
if particle(i).Best.Cost < GlobalBest.Cost
GlobalBest = particle(i).Best;
end
end
end
% Store the Best Cost Value
BestCosts(it) = GlobalBest.Cost;
% Display Iteration Information
disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCosts(it))]);
% Damping Interia Coefficient
w = w * wdamp;
end
anyone who knows how to implement the visualization of the locations of all the particles every iteration?
excellent tutorial! simple and clear. I have a question in the velocity and position update, should'nt it be particl(i).position=particle(i-1).position+particle(i).Velocity? and same for velocity. According to the notes in video 1, this is how it should be, am I missing something? Thanks
can we use the stopping criteria with tolerance of error ?
with 'while' loop not based in its iteration ?
*****
https://dg-algorithm.blogspot.com/2016/04/
hello,i need nspso ,do you have link download ?,thank
very clear and easy to understand.
Nice lecture to understand PSO.Thanks
Thanks Yarpiz it is very use full to understand concept of PSO