File Exchange

image thumbnail

Particle Swarm Optimization (PSO)

version 1.0.0.0 (5.25 KB) by Yarpiz
A simple structured MATLAB implementation of PSO

195 Downloads

Updated 04 Sep 2015

View License

For more information, see the following link:
http://yarpiz.com/50/ypea102-particle-swarm-optimization

Cite As

Yarpiz (2021). Particle Swarm Optimization (PSO) (https://www.mathworks.com/matlabcentral/fileexchange/52857-particle-swarm-optimization-pso), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (19)

john mosxos

hongyu

ahmed hassan

shuai liu

Revanth Ram

Hello sir I want to minimise cos5x1+cos5x2+cos5x3+cos5x4=0
Cos7x1+cos7x2+cos7x3+cos7x4 =0
Cos11x1+cos11x2+cos11x3+cos11x4 =0
Where i have 4 unknown variables x1, x2, x3,x4 and condition is 0<x1<x2<x3<x4<pi/2.
Can you please help me about the code.
This is my engineering project. Thanking you sir..
Mail revanthroy507@gmail.com

Rahul Ghongade

Thank You.... And Coding is marvellous....Superb JOB..

RAMESH GUGULOTHU

SWAMINAIDU GUMMADI

Sir,
I need a some idea to develop a code for IIR filter design

pradeep shankar

I need pso program to eliminate harmonics in multilevel inverters for singlephase

vasu koneti

sir,
I tried to use your code for tunning the parameters of FOPID controller,iam getting error at objective function ,please help me sir.

hayet henchir

hello can you help me in this code
i will transfer this code in programming
pso -->psprogramming
function [fxmin, xmin, Swarm, history] = PSO(psoOptions)

%Globals
global psoFlags;
global psoVars;
global psoSParameters;
global notifications;

upbnd = 600; % Upper bound for init. of the swarm
lwbnd = 300; % Lower bound for init. of the swarm
GM = 0; % Global minimum (used in the stopping criterion)
ErrGoal = 1e-10; % Desired accuracy
%
%Initializations
if nargin == 0
psoOptions = get_psoOptions;
end
%For Displaying
if psoOptions.Flags.ShowViz
global vizAxes; %Use the specified axes if using GUI or create a new global if called from command window
vizAxes = plot(0,0, '.');
axis([-1000 1000 -1000 1000 -1000 1000]); %Initially set to a cube of this size
axis square;
grid off;
set(vizAxes,'EraseMode','xor','MarkerSize',15); %Set it to show particles.
pause(1);
end
%End Display initialization

% Initializing variables
success = 0; % Success Flag
iter = 0; % Iterations' counter
fevals = 0; % Function evaluations' counter

% Using params---
% Determine the value of weight change
w_start = psoOptions.SParams.w_start; %Initial inertia weight's value
w_end = psoOptions.SParams.w_end; %Final inertia weight
w_varyfor = floor(psoOptions.SParams.w_varyfor*psoOptions.Vars.Iterations); %Weight change step. Defines total number of iterations for which weight is changed.
w_now = w_start;
inertdec = (w_start-w_end)/w_varyfor; %Inertia weight's change per iteration

% Initialize Swarm and Velocity
SwarmSize = psoOptions.Vars.SwarmSize;
Swarm = rand(SwarmSize, psoOptions.Vars.Dim)*(psoOptions.Obj.ub-psoOptions.Obj.lb) + psoOptions.Obj.lb;
VStep = rand(SwarmSize, psoOptions.Vars.Dim);

f2eval = psoOptions.Obj.f2eval; %The objective function to optimize.

%Find initial function values.
fSwarm = feval(f2eval, Swarm);
fevals = fevals + SwarmSize;

% Initializing the Best positions matrix and
% the corresponding function values
PBest = Swarm;
fPBest = fSwarm;

% Finding best particle in initial population
[fGBest, g] = min(fSwarm);
lastbpf = fGBest;
Best = Swarm(g,:); %Used to keep track of the Best particle ever
fBest = fGBest;
history = [0, fGBest];

if psoOptions.Flags.Neighbor
% Define social neighborhoods for all the particles
for i = 1:SwarmSize
lo = mod(i-psoOptions.SParam.Nhood:i+psoOptions.SParam.Nhood, SwarmSize);
nhood(i,:) = [lo];
end
nhood(find(nhood==0)) = SwarmSize; %Replace zeros with the index of last particle.
end

if psoOptions.Disp.Interval & (rem(iter, psoOptions.Disp.Interval) == 0)
disp(sprintf('Iterations\t\tfGBest\t\t\tfevals'));
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% THE PSO LOOP %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
while( (success == 0) & (iter <= psoOptions.Vars.Iterations) )
iter = iter+1;

% Update the value of the inertia weight w
if (iter<=w_varyfor) & (iter > 1)
w_now = w_now - inertdec; %Change inertia weight
end


%%%%%%%%%%%%%%%%%
% The PLAIN PSO %

% Set GBest
A = repmat(Swarm(g,:), SwarmSize, 1); %A = GBest. repmat(X, m, n) repeats the matrix X in m rows by n columns.
B = A; %B wil be nBest (best neighbor) matrix

% use neighborhood model
% circular neighborhood is used
if psoOptions.Flags.Neighbor
for i = 1:SwarmSize
[fNBest(i), nb(i)] = min(fSwarm( find(nhood(i)) ));
B(i, :) = Swarm(nb(i), :);
end
end

% Generate Random Numbers
R1 = rand(SwarmSize, psoOptions.Vars.Dim);
R2 = rand(SwarmSize, psoOptions.Vars.Dim);

%% Calculate Velocity
if ~psoOptions.Flags.Neighbor %Normal
VStep = w_now*VStep + psoOptions.SParams.c1*R1.*(PBest-Swarm) + psoOptions.SParams.c2*R2.*(A-Swarm);
else %With neighborhood
R3 = rand(SwarmSize, psoOptions.Vars.Dim); %random nos for neighborhood
VStep = w_now*VStep + psoOptions.SParams.c1*R1.*(PBest-Swarm) + psoOptionsSParams.c2*R2.*(A-Swarm) + psoOptionsSParams.c3*R3.*(B-Swarm);
end
%%

% Apply Vmax Operator for v > Vmax
changeRows = VStep > psoOptions.SParams.Vmax;
VStep(find(changeRows)) = psoOptions.SParams.Vmax;
% Apply Vmax Operator for v < -Vmax
changeRows = VStep < -psoOptions.SParams.Vmax;
VStep(find(changeRows)) = -psoOptions.SParams.Vmax;

%% ::UPDATE POSITIONS OF PARTICLES::
Swarm = Swarm + psoOptions.SParams.Chi * VStep; % Evaluate new Swarm

fSwarm = feval(f2eval, Swarm);
fevals = fevals + SwarmSize;

%% Updating the best position for each particle
changeRows = fSwarm < fPBest;
fPBest(find(changeRows)) = fSwarm(find(changeRows));
PBest(find(changeRows), :) = Swarm(find(changeRows), :);

lastbpart = PBest(g, :);
% Updating index g
[fGBest, g] = min(fPBest);

%% %Update Best. Only if fitness has improved.
if fGBest < lastbpf
[fBest, b] = min(fPBest);
Best = PBest(b,:);
end

%% OUTPUT%%
if psoOptions.Save.Interval & (rem(iter, psoOptions.Save.Interval) == 0)
history((size(history,1)+1), :) = [iter, fBest];
end
if psoOptions.Disp.Interval & (rem(iter, psoOptions.Disp.Interval) == 0)
disp(sprintf('%4d\t\t\t%.5g\t\t\t%5d', iter, fGBest, fevals));
end
if psoOptions.Flags.ShowViz
[fworst, worst] = max(fGBest);
DrawSwarm(Swarm, SwarmSize, iter, psoOptions.Vars.Dim, Swarm(g,:), vizAxes);
end
%% TERMINATION%%
if abs(fGBest-psoOptions.Obj.GM) <= psoOptions.Vars.ErrGoal %GBest
success = 1;
elseif abs(fBest-psoOptions.Obj.GM)<=psoOptions.Vars.ErrGoal %Best
success = 1
else
lastbpf = fGBest; %To be used to find Best
end


end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% END OF PSO LOOP %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

[fxmin, b] = min(fPBest);
xmin = PBest(b, :);

history = history(:,1);
%Comment below line to Return Swarm. Uncomment to return previous best positions.
%Swarm = PBest;
%Return PBest

Jorge Hernandez tapia

Hello, how can i use this for image segmentation ?

sawon pratiher

Sowmith Nallu

Can you help me to find the reduction of side lobe of circular array antenna using pso algorithm
please help me
nalluseye@gmail.com

EB

I can't run this program. Error found in the costfunction. help me please

David Villeneuve

Very clear example of how to use Particle Swarm Optimization. I had it running on my 11-dimensional optimization problem in no time at all. Many thanks.

By moving the GlobalBest reference out of the main loop, it is possible to use a parfor loop.

Rejeki Tambun

Hello sir,
Can you help me to find the optimal placement and capacity of Distributed Generation by using PSO?
please help me
rejeki.tambun@yahoo.com

sherin c

MATLAB Release Compatibility
Created with R2012b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

YPEA102 Particle Swarm Optimization/PSO/