File Exchange

## Particle Swarm Optimization (PSO)

version 1.5.0.2 (2.68 KB) by Pramit Biswas

### Pramit Biswas (view profile)

Searching/Tuning/Optimizing by Particle Swarm Optimization (PSO) method

Updated 04 Mar 2020

This is simple basic PSO function.
This function is well illustrated and analogically programed to understand and visualize Particle Swarm Optimization theory in better way and how it implemented.
To run this you also need to have a function MinMaxCheck.m(File Id: #43251)
PSO Description: http://en.wikipedia.org/wiki/Particle_swarm_optimization
Program Description:
%%
% INPUT VARIABLES
% Bird_in_swarm=Number of particle=agents=candidate
% Number_of_quality_in_Bird=Number of Variable
%
% MinMaxRange: jx2 matrix; jth row contains minimum and maximum values of the jth variable
% say you have a variable N1
% which can have maximum value M1 and minimum value m1
% then your matrix will be [m1 M1]
% for more:
% [m1 M1; m2 M2; mj Mj]
%
% Food_availability=Objective function with one input variable (for more than one variable you may use array)
% example for two variable
% function f = funfunc(array)
% a=array(1);
% b=array(2);
% f = a+b ;
% end
% Food_availability is a string, for above example : 'funfunc'
%
% availability_type is string 'min' or 'max' to check depending upon need to minimize or maximize the Food_availability
% velocity_clamping_factor (normally 2)
% cognitive_constant=c1=individual learning rate (normally 2)
% social_constant=c2=social parameter (normally 2)
% normally C1+C2>=4
%
% Inertia_weight=At the beginning of the search procedure, diversification is heavily weighted, while intensification is heavily weighted at the end of the search procedure.
% Min_Inertia_weight=min of inertia weight (normally 0.4)
% Max_Inertia_weight=max of inertia weight (normally 0.9)
% max_iteration=how many times readjust the position of the flock/swarm of birds its quest for food
%
%
% OUTPUT VARIABLE
% optimised_parameters : Optimal parameters

Required MATLAB function MinMaxCheck.m (File Id: #43251)
http://www.mathworks.in/matlabcentral/fileexchange/43251-bound-values-of-an-array

If the program helps you in any way in your seminar/project/research/thesis etc. work, then please cite our work (either this page or the paper).

Thank you.

### Cite As

Pramit Biswas (2020). Particle Swarm Optimization (PSO) (https://www.mathworks.com/matlabcentral/fileexchange/43541-particle-swarm-optimization-pso), MATLAB Central File Exchange. Retrieved .

@inproceedings{biswas2014pso, title={PSO based PID controller design for twin rotor MIMO system}, author={Biswas, Pramit and Maiti, Roshni and Kolay, Anirban and Sharma, Kaushik Das and Sarkar, Gautam}, booktitle={IEEE International Conference on Control, Instrumentation, Energy and Communication (CIEC)}, pages={56--60}, year={2014} }

Pramit Biswas, Roshni Maiti, Anirban Kolay, Kaushik Das Sharma, and Gautam Sarkar. "PSO based PID controller design for twin rotor MIMO system," IEEE International Conference on Control, Instrumentation, Energy and Communication (CIEC), pp. 56-60. 2014.

Shreyansh Nandania

### Shreyansh Nandania (view profile)

hello sir,
Can you please help me for pso code on minimum cost of generators in generator rescheduling method for congestion management
mail id : 16bee009@nirmauni.ac.in

denny

### denny (view profile)

The x variables will be out of the boundary. please add the following code to the par.

% Check bounds before proceeding
% ---------------------------------------------------------------------
if ~all([isempty([Aineq,bineq]), isempty([Aeq,beq]), ...
isempty([LB;UB]), isempty(nonlcon)])
state = boundcheckfcn(state,Aineq,bineq,Aeq,beq,LB,UB,nonlcon,...
options) ;
end % if ~isempty
% ---------------------------------------------------------------------

for i=1:n
state.Population(i,(state.Population(i,:)<LB))=LB(state.Population(i,:)<LB);
state.Population(i,(state.Population(i,:)>UB))=UB(state.Population(i,:)>UB);
end

Naresh kumar

Pramit Biswas

### Pramit Biswas (view profile)

@studentzhang: Number of variables are the parameters you want to optimise. Agent is a parameter of PSO itself, which defines the number of points you want to search in solution space in each iteration.

@Huda Diab Abdulgalil: Probably yes, you can.

Pramit Biswas

### Pramit Biswas (view profile)

http://dg-algorithm.blogspot.com/

studentzhang

### studentzhang (view profile)

% Bird_in_swarm=Number of particle=agents=candidate
% Number_of_quality_in_Bird=Number of Variable
I want to kown what's the difference bettwen Bird_in_swarm and Number_of_quality_in_Bird,my God.

Huda Diab Abdulgalil

### Huda Diab Abdulgalil (view profile)

Good work. I would ask if you can help in how I can use this method with deep belief network. I want to merge the DBN training with some optimization technique to optimize the number of nodes and hidden node in the designed DBN model . can u plz help me.

HD

### HD (view profile)

srikanta mohapatra

### srikanta mohapatra (view profile)

sir i have done a prediction model using NARNET. but now i want to merge the NARNET with some optimization technique. can u plz help me in this regards. plz sir

sawon pratiher

kate kong

### kate kong (view profile)

'MinMaxCheck'is undefined

MCC

MCC

### MCC (view profile)

please mr pramit Biswas, can you assist me with a working pso and genetic algorithm(GA) MPPT matlab code for the case of MPPT ....my email is lordfaraday@yahoo.com

Pramit Biswas

### Pramit Biswas (view profile)

@saam ryan, just update fitness function accordingly

saam ryan

### saam ryan (view profile)

hi
i have pso code to optimize production of 3 power generators with different cost functions.each generator has min and max power generation and when it works good when i run.but now i have to add a linear constraint says:the Total production of 3 generators should be 700 MEGA Watt.
so thanks

M N Alam

### M N Alam (view profile)

Dear All
A very simple PSO codes can be obtained with an example in MATLAB environment to the link given below;

https://www.researchgate.net/publication/296636431_Codes_in_MATLAB_for_Particle_Swarm_Optimization

M N Alam

youchan song

Pramit Biswas

### Pramit Biswas (view profile)

@Orccun Kor, PSO is a metaheuristic tool, which may give a different optimum result. You can give -Inf +Inf for no boundary

Orcun Kor

### Orcun Kor (view profile)

Hi there,
I'm trying to run PSO with single variable, however the code finds different optimum each time. Another problem is that my design variable does not respect lower and upper bounds.

GeoMath

### GeoMath (view profile)

see Backtracking Search Optimization Algorithm for more efficient PSO.

Rejeki Tambun

### Rejeki Tambun (view profile)

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

Aishwarya Routray

### Aishwarya Routray (view profile)

Hello sir, Can i have the c code for pso where input is objective function.

sarmistha datta

### sarmistha datta (view profile)

can i have the code for symbiotic organism search algorithm for 2 area automatic generation control.

sherin c

### sherin c (view profile)

can I apply this code to images for edge detection??if yes what modification is done in the function?

Selim

### Selim (view profile)

Thank you. First i wrote this code:

function [optimised_parameters] = Particle_Swarm_Optimization(500, 3, [0 5;0 5;0 5], 'tracklsq', 'min', 2, 0.12, 1.2, 0.4, 0.9, 30);

but i get the error:

invalid syntax at '500'...

Error: File: Particle_Swarm_Optimization.m Line: 37 Column: 52
Unexpected MATLAB expression

sitender malik

sitender malik

### sitender malik (view profile)

Dear sir i am working on Software Reliability Modelling and i need to optimize my objective function the problem is that my objective function is having four variables and when i use ur code availability(p,itr)= feval(objective,parameter)" saying that subscript dimensions does not match matched . plz help me what changes should i do in the code?

sitender malik

### sitender malik (view profile)

Dear sir i am working on Software Reliability Modelling and i need to optimize my objective function the problem is that my objective function is having four variables and when i use ur code availability(p,itr)= feval(objective,parameter)" saying that subscript dimensions does not match matched . plz help me what changes should i do in the code?

sitender malik

### sitender malik (view profile)

Dear sir i am working on Software Reliability Modelling and i need to optimize my objective function the problem is that my objective function is having four variables and when i use ur code availability(p,itr)= feval(objective,parameter)" saying that subscript dimensions does not match matched . plz help me what changes should i do in the code?

sitender malik

### sitender malik (view profile)

Dear sir i am working on Software Reliability Modelling and i need to optimize my objective function the problem is that my objective function is having four variables and when i use ur code availability(p,itr)= feval(objective,parameter)" saying that subscript dimensions does not match matched . plz help me what changes should i do in the code?

sitender malik

### sitender malik (view profile)

sorry for the multiple requests...

Pramit Biswas

### Pramit Biswas (view profile)

@Osenuk you can discuss your issues here.

Osenuk

sana

### sana (view profile)

Sir,
I am getting error in "availability(p,itr)= feval(objective,parameter)" when I am using two varible problem.

Hamza

### Hamza (view profile)

i have a question,
how to give input to the fitness function using the swarm members (particles).
which then will be used to optimize the fitness using the function you have provided here.

arudhra

### arudhra (view profile)

can any one help me how to give the avilability_type value in the main function

availability_type is string 'min' or 'max' to check depending upon need to minimize or maximize the Food_availability

Muhamed Muzaiyen S M Bijli

### Muhamed Muzaiyen S M Bijli (view profile)

@Pramit Biswas Following is my problem how can I use your code to optimize my objective function y.

function y= muza(x)
FAO=2.5; % FG504 % Avg FG Temp at AH Outlet
TGA=156; % KX022 % FG O2 at AH Outlet
TAA=35; % KY085 % Ambient Temp
TAAI=35; % KY062 % Air Temp at AH Inlet (Ambient Temp)
TGAI=x(2); % KY060 % Gas Temp at AH Inlet
TGAO=170; % KY061 % Gas Temp at AH Outlet
MLTFH=x(1); % CM555 % Total Coal Flow
LRC=0.5;

y =(100-0.0145-(((0.279-(0.0306*FAO)+(0.000363*FAO^2)-(0.00000573*FAO^3))*((TGAI*TGAO)+(TAA*TAAI)-(TAA*TGAO)-(TGAI*TAAI))*100)/(((0.0441-(0.0063*FAO)+(0.0002*FAO^2))*(TGAI-TAAI))*17835))-(1.4+(0.00106*TGA)-(0.00234*TAA))-(1.833+(0.00139*TGA)-(0.00307*TAA))-((((0.012*FAO)-(0.000571*FAO^2))*((1.9*TGA)-(1.825*TAA)-(4.54))*100))/(((0.21-(0.02*FAO))*17835))-(((0.18*TGA)-(0.18*TAA))/MLTFH)-LRC)
end

x(1)= min=120, max=150.
x(2)= min=330, max=370.

Pramit Biswas

### Pramit Biswas (view profile)

@mohammed abdullrahman: as I commented earlier, you can use this program for simulink model, you may take look on these commands: load_system, set_param, get_param, save_system, sim; to create your desired fitness function.

Pramit Biswas

### Pramit Biswas (view profile)

@gon: I think it is possible, if you can make an appropriate fitness function, which can handle multiple objective functions according to your need, then send back the single output to the PSO function.

Pramit Biswas

### Pramit Biswas (view profile)

@braa: I tried to explain everything, however i'll add more example and details in next update.
regarding the use of this program for simulink model, you may take look on these commands: load_system, set_param, get_param, save_system, sim; to create your desired fitness function.

Pramit Biswas

### Pramit Biswas (view profile)

@braa: I understood your need, i also add those features in future updates as soon as possible.

mohammed abdullrahman

### mohammed abdullrahman (view profile)

pls sir how can i use this code in simulink ?

gon

### gon (view profile)

This file is only able to optimize for a single fitness variable right? How do I code for multiple objective function that need to be optimize?

braa

### braa (view profile)

The program is giving me a single output value, i wish to see the whole swarm, in order to produce a Pareto Plot

braa

### braa (view profile)

hello, it's really frustrating that there is no example.
I need to optimize the output of a simulink model!

Pramit Biswas

### Pramit Biswas (view profile)

@Jordan: Good to hear that it is useful to you.
you can achieve initial Gbest by saving current Gbest into a file. and then load it at first iteration. (you may realize this by loading variable though)

Pramit Biswas

### Pramit Biswas (view profile)

@Jordan: Good to here that it is useful to you.
you can achieve initial Gbest by saving current Gbest into a file. and then load it at first iteration. (you may realize this by loading variable though)

Jordan

### Jordan (view profile)

Beautiful implementation, works great. Do you know how one can hardcode an initial global best in the optimization?

Pramit Biswas

### Pramit Biswas (view profile)

@Shaveta Arora, that depends upon ur objective function, u've to find out

Shaveta Arora

### Shaveta Arora (view profile)

pls tell for constrained optimization, how to set the objective function range

Pramit Biswas

### Pramit Biswas (view profile)

@dalia elkashef:
Kindly mail me details of your requirements.

dalia elkashef

### dalia elkashef (view profile)

i make project by pso (mppt of solar ), use power of pv as objective function and i take values of volt , current from pv when simulation run , and use duty cycle of converter as variable and need to write initial values and bounders for it , by consider power = volt*current thier vales taken from pv during prog run , duty cycle is position which updated to reach to max power , how can i write that ?

Pramit Biswas

### Pramit Biswas (view profile)

@dalia elkashef
the command used for finding optimal point

Sam

dalia elkashef

### dalia elkashef (view profile)

pBest_availability,index]=min(availability(p,:)

i need to understand the mean of (index) which used ?

Pramit Biswas

### Pramit Biswas (view profile)

@Eko
try to understand the philosophy of PSO then the algorithm. go through some paper,ppt to understand

Pramit Biswas

### Pramit Biswas (view profile)

@Andrej
1. food_availability is your objective function: a different matlab function of your own chioce which need to be max/minimized
2.Number_of_quality_in_Bird=Number of Variable means, the variables which you used in your objective function and by varying it, you are finding optimal solution

Eko

### Eko (view profile)

Sorry sir, I was just learning PSO, can you give an example of its application to the function's objectives?

Andrej

### Andrej (view profile)

Thank you for you work. Please could you give some example like :
[ optimised_parameters ] = Particle_Swarm_Optimization (5, 2, [1 10;1 10], Food_availability, 'min', 2,2, 2, 0.4, 0.9, 10) but one that is correct because this one which i have written is not good. I don't know what write right in food availability and what exactly means Number_of_quality_in_Bird=Number of Variable. SO please one correct Example

engineering design

### engineering design (view profile)

sir i want to do project in pso will u help me sir my id sandeepraja.be@gmail.com

Pramit Biswas

### Pramit Biswas (view profile)

@sakthi priya

1. food_availability is a matlab function, you may call it fitness function also

2. say you have a variable N1
which can have maximum value M1 and minimum value m1

then your matrix will be [m1 M1]
for more:

[m1 M1]
[m2 M2]
...
[mN MN]

sakthi priya

### sakthi priya (view profile)

Thank you very much for the code.Sir i want to know how to give input for food availability,min max range and availability type.

Pramit Biswas

### Pramit Biswas (view profile)

Good to hear that and thank you for your appreciation @Jose Araujo

Jose Araujo

### Jose Araujo (view profile)

A very good implementation. Due tried with peaks function of MATLAB.

Pramit Biswas

### Pramit Biswas (view profile)

link to the function MinMaxCheck.m (File Id: #43251)http://www.mathworks.in/matlabcentral/fileexchange/43251-bound-values-of-an-array

 4 Mar 2020 1.5.0.2 added citation 4 Mar 2020 1.5.0.1 Updated Description and Citation. 15 Jun 2016 1.5.0.0 8 May 2014 1.5.0.0 added link in program of MinMaxCheck.m (to many request for the function). the link also available in the description 10 Apr 2014 1.4.0.0 more illustration bug remove 11 Dec 2013 1.3.0.0 Inertia weight formula updated.
##### MATLAB Release Compatibility
Created with R2010a
Compatible with any release
##### Platform Compatibility
Windows macOS Linux