File Exchange

image thumbnail

Particle Swarm Optimization Toolbox

version (745 KB) by Brian Birge
With Trelea, Common, and Clerc types along with ...


Updated 20 Mar 2006

View License

Robust Particle Swarm toolbox implementing Trelea, Common, and Clerc types along with an alpha version of change detection.

This toolbox is designed for researchers in Computational Intelligence as well as application developers, students, and classroom labs. It is robust enough that several papers have been developed using it but it is also in constant development and very easy to hack. Users of MATLAB's Optimization Toolbox should feel right at home but even if you don't use that toolbox this will be easy to figure. Extensive help is included.

Anyone from serious AI researchers to beginning students should get something out of this.

This toolbox is constantly evolving and I welcome suggestions.

Current feature set:
* Trelea, Clerc, and Common Types
* Vectorized and non-vectorized versions, you can investigate thousands of particles at once with the proper cost function.
* custom plotting plug ins
* neural net trainer (requires Matlab's neural net toolbox)
* PDF Particle Swarm Introduction
* Alpha version of change detection, will track dynamic environments
* includes suite of test functions commonly used in the CI/optimization community.

Cite As

Brian Birge (2021). Particle Swarm Optimization Toolbox (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (162)


Excellent work

salwa elsayed


Excellent work. I have used this code in a GUI based toolbox for surface electromyography (sEMG) onset/offset detection. Available here:

hongta jiang

Hello, Sam. When I run the program I find that the result of the operation is beyond the bounds I set. How can I fix it?
x = pso(fitnessfcn,nvars,Aineq,bineq,Aeq,beq,LB,UB);


The value of the fitness function did not improve in the last 50 generations and maximum constraint violation is less than 1e-06, after 52 generations.

Final best point: [-0.83575 0.14343]

Ondrej Panek

Wemyu Wang


Sorry, I didn't find the non-vectorized version, can you tell me that?

ghazally mustafa

Jorge Hernandez tapia

Anyway to use this to image segmentation?

EvenApril Cui

Adamu Halilu Jabire

Mr jjjjjjjjjjjjjjjj is my email address please can you send me some help related to what you said through my email?


Hi every body
I'm working in feature selection with BPSO but i have faced problem with no matlab code
can any one help me pleas..


For any optimization technique, you can apply it in order to tune the PID controller. Firstly, you have to investigate the objective time, which is the minimization of the system error in time domain.


can anyone pls put some snapshots of how to compile the codes im getting error...

omar makram

how can i implement PSO into simulink for tuning PID controller?

mahdi abdelhassan


Department of

Please, how can i apply PSO to segment retinal blood vessel images?


Backtracking Search Optimization Algorithm is more simpler than PSO but its is too effective.

Monique Bakker

Hi, is there a way to use pso.m but with integer variables only?

Fatih Falcone

hi all,

I have objection function with constraint functions. But I dont understand to apply my problem into this toolbox. Can you please help me? Thank you in advance.

aanchal jain

hello everyone
i am facing problem in generating matlab code for generation cost minimization using PSO method. i need matlab program urgently for my work.

if anybody can send me the matlab program of the same it will be highly appreciable.
please email the mfile at

Rejeki Tambun

Hello everyone,
I want to use this to search the best placement and the capacity of DG in distribution system.
Is there anyone can help me?

Xueji Zhang


Hi every body
I'm working in feature selection with BPSO using Mutual information (By Hanchuan Peng April 16, 2003)
and i have faced problem with code
Subscript indices must either be real positive integers or logicals.

Error in BPSO (line 47)
can any one help me pleas..

the files in:

or directly in:

thanks advanced

Peggy Lin

Hi...really new to PSO here...does anyone have any suggestion on the issue Hiren experienced with multiple solutions found? I am also finding that some of my functions get trapped into two distinct local minima and local maximas...tweaked most of the PSO parameters to give it more time for convergence, make the swarm bigger, played with the velocity and inertia parameters...with no luck...

Peng tao

so good

ding eagle

I'm very sorry to everyone who want more detials about this PSO toolbox, becouse I almost forget the user name of this website and the password, and the original document I have is missing. I changed my study deriction, so I forgot the detials and I need time to get it again. Thanks for some good persons ,like Brian Birge, Guillermo and so on, offer help to others!

Hiren Patel

i am getting different answer each time as i run the code for my problem.

also Constantine satisfaction is not take place during the problem here is my function and constraint file for two design variables.

function y=7.4563.*10.^-4.*x(2).^2.*(11.*x(2)+0.06.*x(1))+0.5796.*10.^-4.*x(2).*x(1).^2 ;

constraint file

function [c,ceq] = mynonlcon(x)
% Non-linear inequality constraints
c(1) = 164165.04.*((6+0.00314.*x(:,1))./((x(:,2).^2.*x(:,1))))-78;19820.03./(x(:,2).^0.5.*x(:,1))-159;32509.75-((x(:,1)+11.*x(:,2)).^1.7);16289.11.*(6+0.00314.*x(:,1))-8.25.*(x(:,1).^2.*x(:,2));40-1.041.*(x(:,1)./x(:,2));3.14.*(((11.*x(:,2)+0.06.*x(:,1)).^3)./(5400.45.*x(:,1).*x(:,2).^4))-(0.01.*x(:,2));4-x(:,2);x(:,2)-9;200-x(:,1);x(:,1)-1000;
% Non-linear equality constraints
ceq = [];

please help me

bounthanh banhthasit

for education

mu ting

Brian Birge

@Sayan, the NaN as an error goal just tells the program to have no goal, in other words just minimize as much as possible with no target in mind. The commented code line right before that isn't a comment explaining the code, it's code that was commented out. Sorry, sloppy I know, and this is due for a major update.

Ahmad Taher


This is a great PSO toolbox. But can anyone please help me out in the folowing problem:

In DemoPSOBehavior.m file at lines 89 - 95 I see

"if minmax==1
% errgoal=0.97643183; % max for f6 function (close enough for termination)
% errgoal=0; % min
errgoal=NaN; <------ Will it be 0 ?

ABIN joy

Sir,i want to apply optimization technique to the intencity values taken from the it possble to apply PSO to that?


I can't even get the demo to run...

>> pso_Trelea_vectorized('f6',2)
Error: File: pso_Trelea_vectorized.m Line: 105 Column: 1
Function definitions are not permitted in this context.



Error using plot3
Vectors must be the same lengths.

Error in goplotpso (line 23)

Error in pso_Trelea_vectorized (line 295)
eval(plotfcn); % defined at top of script


Undefined function 'normmat' for input arguments of type 'double'? Help

Marcio H Zuchini



I want to apply PSO to fit kinetic parameters of a system of ODEs. I got the following error when I ran the program. Does anybody know how to fit it? Thank you in advance!

[val] = pso_Trelea_vectorized(@fitness, 2);

-- error message
PSO: 1/2000 iterations, GBest = 1895760000.0007648468.
Error using plot3
Vectors must be the same lengths.
Error in goplotpso (line 23)
Error in pso_Trelea_vectorized (line 344)
eval(plotfcn); % defined at top of script
Error in driver_fitness (line 5)
[val] = pso_Trelea_vectorized(@fitness, 2);


I was trying to use this program with a function handle input (instead of an .m file), but I get error. I was wondering, the only way to use this is to define the objective function in a separate .m file?



Hong Nhung Phan


Please, Can any body tell me way to simulation PSO-C from Matlab PSO toolbox.




my comment (see below) actually refers to another PS-optimizer. Sorry for that.



I think I found a crucial bug: in my program I write down the test parameters if they fit better than in the iterations before. In my test PSO stopped after fulfilling a break condition and gave me a final parameter set, which fitted less than an intermediate result I wrote down before.



Can this toolbox works with multiobjective optimization?

Sabb Aquarians

Phan HaNhut

Hi, Bui Hong Phong, I am living in Binh Duong, and you? 0167 3493 081

Joe Ajay

Hi Brian Brige, just an info needed...Were u able to update the toolbox for Discrete problems. I got a problem in which my variables are integers. Say for ex:-3 to 3 with step size of 1


Very helpful package.



plz can any body tell me which files are necessary for simple there GPSO and how to get only those files which run simple PSO??


@Fabian You could always rewrite your objective function to take into account function constraints by adding penalties.


Is there still no update for constraints? Or I#m blind enough to not find the option?

IBRAHIM MTOLERA to create switch onoff_Constraints in the control panel to add objective function in PSOt box


doesn't work for me


Phong Bui Hong

i could not read this file. Pls help me

ahmed albakri


yap ee chiet

Hi Brian Bierge,
may I know is the matlab code for multi-objectives PSO available?

Thank you.


hello every 1..i need a help pso method...hw do i can relate this method into solar tracking system..i am doing a simulation on solar mppt based on pso method..can anybody guide me hw to related this method into my project...
this is my email


good job


I think this is a good package because one can get started quickly and has good graphics. I hope it will be updated soon, and that the option of constrained optimization will be added.

A few comments:
* the code is easily modifiable to support anonymous functions as objective. This is neccessary to pass extra parameters to objective without using global parameters. The only modification needed is to remove the use of the function name as a string in the plotting routine.
* There is an error in the doc of pso_Trelea_vectorized.m: the explaination for input variable minmax value of 2 says that error is targeted to P(12), but the correct is P(11)
* The options should be moved from an array to a structure, so that only those one wants to let override the defaults need to be given.
* there is an error in line 441, where the variable 'sqrerr1' is misspelt as 'sqerr1'

nilam nini

good...does anybody here work on Multiobjective PSO using matlab..where can i get the source code



been trying to run demoPSOnet
getting the following error:

??? Error using ==> trainpso at 124
Unrecognized code.

Error in ==> network.subsasgn>setTrainFcn at 2025
net.trainParam = feval(trainFcn,'defaultParam');

Error in ==> network.subsasgn>network_subsasgn at 462
if isempty(err), [net,err]=setTrainFcn(net,trainFcn); end

Error in ==> network.subsasgn at 11
net = network_subsasgn(net,subscripts,v,netname);

Error in ==> newff>new_5p0 at 301
net.trainfcn = btf;

Error in ==> newff>create_network at 128
net = new_5p0(varargin{:});

Error in ==> newff at 102
out1 = create_network(varargin{:});

Error in ==> demoPSOnet at 34
net = newff([min(P,[],2),max(P,[],2)],... % input parameter ranges

reham hassen


Aditya Gourav


moh moh

good day for all users

Tricia Rambharose

Good day users,

A PSO toolbox which can be used for NN trainig, and works with the latests versions of MATLAB, is now available on MathWorks.

The PSO toolbox is found at:

The NN add-in for this PSO toolbox is found at:




Thank you very much, with these, I can use pso more quickly.

Tricia Rambharose

Good day everyone,

For those of you who emailed me asking for assistance in using PSO for NN training, and also for the general public, I found another PSO toolbox to be very helpful to me. The toolbox I now use is found at

I have added functionality for this PSO toolbox to be used as a training algorithm for a NN. This version is currently being tested and will then be made available.

sugith sewdass

I am currently working on a research project.
I am trying to train a neural network using PSO.
When using the PSO toolbox with Matlab version

demoPSOnet' does not work. I get the following error :
??? Error using ==> trainpso at 124
Unrecognized code.
If I can find a simple way to train the Neural Network using the PSO toolbox it will be of great help to me.

Please can anyone assist me in anyway.



Erdal Bizkevelci

Shawkat hamdan

hi Brian
i want to ask you if you have program in which we can make tuning the parameters of the pso like w, c1, c2 to make the pso more effective as results
thank you very much to your help and cooperation

this is my

my best regards


syed musthafa

dear all,
i need pso coding for OPIMAL LOCATION OF FACTS DEVICES..i also need ur valuable suggestions to do this project..kindly send me the codings/suggestions...

Brian Birge

Hey everyone, sorry for taking so long to update this, it will be updated shortly and contain discrete capability as well as multi-objective optimization built in and more flexible constraints (linear or nonlinear). If you wish to do multi-objective opt "lite" right now you can, just write your cost function so it has a weighted sum of your objectives as the output, a standard way to do it. More complex methods will be added with the update.

Tusi Bai

How to use it to multi-objective optimization problems?

Zacharias Voulgaris

I am also interested in discrete PSO for TSP. If anyone has something in Matlab, please contact me. Thanks.

Mohammed Ahmed Saeed

i prepare for my Master in optimal relay coordination and if you please i need your help to provide me with a swarm code to determine the optimal relay settings ( i can not do the fitness function for each particle )


Hi, everyone,
I am interested in PSO with both linear and nonlinear constraints for high-dimention problem, but I found this package can not handle it.
Is there any other PSO toolbox to handle the problems with nonlinear and linear constraints? Please let me know: I am waiting for your reply.



Saeed and Vishal,
You need to manually add the subdirectories to your path, e.g. /hiddenutils and /testfunctions for the demo.

Anyway, I'm still learning how to use the PSO tool - so far so good! I'll come back with a rating once I've explored it further.

saeed gol Golian

hi everyone
I encountered with the same error as vishal mehra

?? Undefined function or method 'normmat' for input arguments of type 'double'.

Error in ==> pso_Trelea_vectorized at 252
pos(1:ps,1:D) = normmat(rand([ps,D]),VR',1);

can anyone give me a hand?

vishal mehra

can anyone help me?
when i run the command at command window for testing the fn f6 i got the error as below.

out = pso_Trelea_vectorized('f6',2)
??? Undefined function or method 'normmat' for input arguments of type 'double'.

Error in ==> pso_Trelea_vectorized at 252
pos(1:ps,1:D) = normmat(rand([ps,D]),VR',1);

can anyone tell me now what i have to do?


Please who can help me for the choice of parameters trainpso
thank you

Shree Guha

I found this helpful. I had to modify the function to fit my problem description. I have a lot more to work on it. But i like it so far! Thanks again.

Ali Sabbagh

Dear jalla
would you please help me how it work?
i spend too much time to understand but i was not successfull
thank you

Hi friend,
I am a PhD Research Scholar in Indian Institute of Technology, Roorkee, India.
After working for 4 hours continuously, i am able to run train the Neural network program using this PSO toolbox.
I am vary thankful to you. Great job.
I can help, if anybody is in need of my help.

Tricia Rambharose

I am a research student at the University of the West Indies.

A collegue of mine used trainpso and newff to train a neural network. He used Matlab version 5 and it worked well. I am using Matlab version 7.6.0 and neither trainpso nor newff works.

With this toolbox 'DemoPSOBehavior' and pso_Trelea_vectorized('f6',2) works with my Matlab version, but 'demoPSOnet' does not work. I get this root error :

??? Error using ==> trainpso at 124
Unrecognized code.

Is there any other way to train a Neural Network by PSO using this toolbox?


this doesn't work with version 5.1 of neural network




I want to use Trainpso but i have a probleme .
matlab error message "Unrecognized code".
Please can you help me

Kanwardeep Singh

A Good Work

Gui Xiaolin

ilike it very much!

necla OZKAYA

thangjam singh


asd adsf

very good!I hope the new version can add local pso !

Masoud Shariati-Rad

Masoud Shariati

Lin Wang

mat yusof

sahbi boubaker

there's someone who have worked with pso to optimize hybrid systems please mail me if...

sahbi boubaker

good work

Ahmed Saleh

This is good to apply pso in optimization problem

sanny sihombing

is there anyone know a research paper that explore PSO for Neural Network training, using this toolbox? please e-mail me about this, i really need it for studying. thanks!

Maurice Clerc

It would be interesting to implement the new version (Standard PSO 2007, available on the Particle Swarm Central

nadda chawalarat

Dhilip Kumaar

This tool is very nice to use. I have done certain works using this tool for Vehicle Routing Problem.

abdelwahhab Boudjellal

i am etidient algerienne mgister can you help me by Matlab traitement image please
i d'nt space english good

Dhilip Kumaar

Its very good to use this zip file. It helps me inlot to use PSO toolbox in MATLAB

elham asadi

Where can I find a matlab code of discrete PSO or to solve TSP by PSO?Is anyone konw?If you konw,please mail me. Thanks a lot.

morteza eslamian

this program is very good

toumi lyazid

very happy

Mohammad Zakermoshfegh

lin si

Great job!

John Q

Any idea how to use for training neural nets with custom non-differentiable cost functions?
Please help...

saleh mohseni

saleh mohseni

m saberan

please add a perfect help to this file,because it is very hard to use it without help.i can not use trainpso for my problem.
thanks for your attention

Pragati Tiwary

very vague description of how to use the toolbox. I am not able to run a simple optimization problem. Please try to come up with some illustrative and explanatory User manual.

mm rezaei

wu di

Huynh Chau Duy

Documents and paper about Particle Swarm Optimization

li zhen

Where can I find a matlab code to solve TSP by PSO?Is anyone konw?If you konw,please mail me,thx a lot.

zhaojia Wang

Thanks a lot:)

Muhammad Shakil

Does not work with matlab 2006a

trace water

thank you the dear author!great work!

robert rino

ehsan nasr

babak zarei

Sirichai S

I'm graduate student of Thammasat University, Thailand.
I need to support my study.
Thank a lot

potter harryhfy

i am a student of computer and technologe department.

lan FA

thank you ,I need it for my project of ant model

ali alizade

please send for me

Dingyu Xue

Nice code.
Is it possible to provide the non-vectorized version as well? thanks

Ranjit singh chauhan

Excellent tool.

sachin soni

it's an excellent toolbox for the researchers and university student. it is helping me alot in preparing my research paper and i hope that the other user's will also find it a boon to them.

jagdish bansal

very good

aa ss


ish kakkar

it is very good and some more friendly can be made...

sankar karan


weidong wu

Ran Ren

safiye turgay

Omar Vicencio

dan lak


dan lak

dana lak

some democanbe added

Rafael Rodriguez

Yuan Liguo

Thanks,I downloaded it. It is helpful for me to learn PSO.

John D'Errico

I like the cleanup that was done, so I thought it was time to revisit this (nice, sophisticated) tool. Great graphics, at least where this is possible. If I want to complain about anything, its the fact that PSOt REQUIRES its function in the form of an m-file. Neither inline or anonymous functions are supported. Its a silly constraint, because my guess is the only reason is to include the function name on the generated graphics. Otherwise feval should be insensitive to these alternatives. The advantages that anonymous functions (especially) offer suggests fixing this. On the other hand, this is a really nice piece of work, so I'll raise my rating to a 5 anyway.

shark spear

I am wondering if this PSO can deal with discrete parameter optimization problems. Thanks a lot!


John D'Errico

A nice, sophisticated PSO toolbox. The only flaw is it feels like the interface & documentation needs some attention - apparent defaults that did not seem to default as the documentation implies. A simple test crashed for no apparent reason. I noted that the documentation did not even tell you the correct name of the function used to drive the optimizer. Once you get past these minor points and learn how to drive it properly, I think this is a nice toolbox. I'd like to give it about a 3.75 rating.

sione palu

Brian, could you please give a more examples. I find it excellent for a new learner like me about the subject.

Kurt Lewis

I don't know much about theis, but if the program won't run on the example included in the help, then I think I can't use it:

> out = pso_Trelea('f6',2)
??? Error using ==> f6
Too many input arguments.

Error in ==> pso_Trelea at 203
out(j)=eval(evstrg); % evaluate desired function with particle j

zhuohua duan

MATLAB Release Compatibility
Created with R14SP1
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!