File Exchange

image thumbnail

Particle Swarm Optimization (PSO)

version 1.5.0.2 (2.68 KB) by Pramit Biswas
Searching/Tuning/Optimizing by Particle Swarm Optimization (PSO) method

77 Downloads

Updated 04 Mar 2020

View License

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.

Comments and Ratings (68)

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

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

%----------added by Linfei Yin
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
%----------added by Linfei Yin

Pramit Biswas

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

@Muhammad Raza: Please don't spam here.

Pramit Biswas

@Muhammad Raza: Please don't spam here.

Muhammad Raza

For Matlab Code, Visit link
http://dg-algorithm.blogspot.com/

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

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

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

kate kong

'MinMaxCheck'is undefined

MCC

MCC

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

@saam ryan, just update fitness function accordingly

saam ryan

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.
please help
so thanks

Pramit Biswas

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

Orcun Kor

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.
Help please :)

GeoMath

see Backtracking Search Optimization Algorithm for more efficient PSO.

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

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

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

sherin c

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

Selim

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

what should i do to fix it? Could you please help me? and i want to ask

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?

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?

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?

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?

sorry for the multiple requests...

Pramit Biswas

@Osenuk you can discuss your issues here.

Osenuk

could you please send me your email ??

sana

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

Hamza

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

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

@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

@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

@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

@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

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

pls sir how can i use this code in simulink ?

gon

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

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

braa

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

Pramit Biswas

@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

@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

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

Pramit Biswas

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

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

Pramit Biswas

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

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 ?
thanks in advance

Pramit Biswas

@dalia elkashef
the command used for finding optimal point

Sam

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

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

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

@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

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

Andrej

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

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

Pramit Biswas

Please ignore previous comment.
@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]

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

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

Jose Araujo

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

Updates

1.5.0.2

added citation

1.5.0.1

Updated Description and Citation.

1.5.0.0

https://cssanalytics.files.wordpress.com/2013/09/pso-graphic.png added

1.5.0.0

added link in program of MinMaxCheck.m (to many request for the function). the link also available in the description

1.4.0.0

more illustration
bug remove

1.3.0.0

Inertia weight formula updated.

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