View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Particle Swarm Optimization Toolbox

4.3 | 90 ratings Rate this file 296 Downloads (last 30 days) File Size: 745 KB File ID: #7506 Version: 1.0
image thumbnail

Particle Swarm Optimization Toolbox


Brian Birge (view profile)


22 Apr 2005 (Updated )

With Trelea, Common, and Clerc types along with ...

| Watch this File

File Information

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.


This file inspired Neural Network Add In For Psort, Optimal Power Flow By Vector Pso, Pso Solution To Economic Dispatch, Improved Pso Program To Solve Economic Dispatch, and Constrained Particle Swarm Optimization.

Required Products Neural Network Toolbox
MATLAB release MATLAB 7.0.1 (R14SP1)
Other requirements * current version works in Matlab 7 but has been in development since Matlab 5 so might work in the earlier versions too.
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (149)
09 Feb 2017 mostafa

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.

Comment only

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

Comment only
14 Dec 2016 omar makram

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

Comment only
27 Jul 2016 mahdi abdelhassan

10 Jul 2016 Hamza

Hamza (view profile)

10 May 2016 Department of

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

10 May 2016 M N Alam

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

Comment only
14 Mar 2016 Anka

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

Comment only
05 Mar 2016 Monique Bakker

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

25 Feb 2016 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.

Comment only
18 Feb 2016 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

Comment only
10 Feb 2016 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?

11 Nov 2015 Xueji Zhang

16 Aug 2015 mosab

mosab (view profile)

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

Comment only
30 Jun 2015 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...

Comment only
25 May 2015 Peng tao

so good

21 May 2015 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!

Comment only
19 May 2015 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

Comment only
23 Feb 2015 bounthanh banhthasit

for education

Comment only
07 Feb 2015 mu ting

23 Dec 2014 Brian Birge

Brian Birge (view profile)

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

Comment only
23 Nov 2014 Ahmad Azar

08 Nov 2014 Sayan

Sayan (view profile)

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 ?

Comment only
30 Jul 2014 ABIN joy

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

Comment only
28 Jul 2014 Jordan

Jordan (view profile)

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.

Comment only
28 Jul 2014 Jordan

Jordan (view profile)


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

Comment only
28 Jul 2014 Jordan

Jordan (view profile)

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

Comment only
20 Jun 2014 Marcio H Zuchini

19 Jun 2014 Wendy

Wendy (view profile)


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);

Comment only
15 May 2014 Talieh

Talieh (view profile)

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?


Comment only
04 May 2014 GUIZHOU

03 Feb 2014 Hong Nhung Phan


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


Comment only
31 Jan 2014 Matthias


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

17 Jan 2014 Matthias


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.

15 Jan 2014 homecat

29 Nov 2013 dab483

dab483 (view profile)

Can this toolbox works with multiobjective optimization?

01 Oct 2013 Sabb Aquarians

12 Sep 2013 Phan HaNhut

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

Comment only
03 Sep 2013 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

Comment only
19 Jul 2013 Muhmmad

Very helpful package.

07 May 2013 zhong

zhong (view profile)

06 May 2013 sehrish

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

Comment only
17 Apr 2013 Guillermo

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

Comment only
05 Apr 2013 geieraffe

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

Comment only
22 Jan 2013 IBRAHIM MTOLERA to create switch onoff_Constraints in the control panel to add objective function in PSOt box

07 Nov 2012 kevin

kevin (view profile)

doesn't work for me

Comment only
25 Oct 2012 Tom

Tom (view profile)

21 Jun 2012 Phong Bui Hong

i could not read this file. Pls help me

Comment only
07 Apr 2012 ahmed albakri


Comment only
26 Mar 2012 yap ee chiet

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

Thank you.

Comment only
19 Mar 2012 isma

isma (view profile)

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

Comment only
03 Mar 2012 sofiane

good job

24 Feb 2012 Erik

Erik (view profile)

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'

24 Jan 2012 nilam nini

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

19 Aug 2011 Raffaele

28 Jun 2011 ondrej

ondrej (view profile)

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

Comment only
12 Jun 2011 reham hassen


Comment only
08 May 2011 Aditya Gourav


22 Apr 2011 moh moh

good day for all users

Comment only
29 Nov 2010 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:



Comment only
08 Oct 2010 junjie

junjie (view profile)

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

Comment only
26 Sep 2010 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.

Comment only
26 Sep 2010 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.


Comment only
02 Sep 2010 Eric

Eric (view profile)

12 Jul 2010 Erdal Bizkevelci

25 Jun 2010 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


Comment only
23 May 2010 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...

Comment only
29 Apr 2010 Brian Birge

Brian Birge (view profile)

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.

Comment only
06 Apr 2010 Tusi Bai

Tusi Bai (view profile)

How to use it to multi-objective optimization problems?

Comment only
03 Mar 2010 Zacharias Voulgaris

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

Comment only
11 Jan 2010 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 )

Comment only
14 Dec 2009 tom

tom (view profile)

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.


13 Nov 2009 Eric

Eric (view profile)

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.

Comment only
13 Nov 2009 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?

Comment only
09 Sep 2009 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?

28 Apr 2009 Homri

Homri (view profile)

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

09 Apr 2009 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.

06 Apr 2009 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

Comment only
05 Apr 2009 jjjjjjjjjjjjjjjj jjjjjjjjjjjjjjjjjj

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.

14 Mar 2009 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?

Comment only
16 Feb 2009 NeuralDip

this doesn't work with version 5.1 of neural network

16 Feb 2009 NeuralDip

25 Jan 2009 Homri

Homri (view profile)

25 Jan 2009 Homri

Homri (view profile)

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

Comment only
05 Oct 2008 Kanwardeep Singh

A Good Work

28 Sep 2008 Gui Xiaolin

ilike it very much!

26 Sep 2008 necla OZKAYA

21 Sep 2008 thangjam singh


31 Jul 2008 asd adsf

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

09 Jul 2008 Masoud Shariati-Rad

09 Jul 2008 Masoud Shariati

12 Jun 2008 Lin Wang

29 Apr 2008 mat yusof

28 Apr 2008 sahbi boubaker

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

Comment only
23 Apr 2008 sahbi boubaker

good work

22 Apr 2008 Ahmed Saleh

This is good to apply pso in optimization problem

17 Apr 2008 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!

Comment only
10 Apr 2008 Maurice Clerc

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

Comment only
13 Feb 2008 nadda chawalarat

06 Feb 2008 Dhilip Kumaar

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

20 Jan 2008 abdelwahhab Boudjellal

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

20 Jan 2008 Dhilip Kumaar

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

11 Nov 2007 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.

Comment only
18 Oct 2007 morteza eslamian

this program is very good

22 Sep 2007 toumi lyazid

very happy

10 Sep 2007 Mohammad Zakermoshfegh

14 Aug 2007 lin si

Great job!

12 Aug 2007 John Q

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

04 Aug 2007 saleh mohseni

04 Aug 2007 saleh mohseni

13 Jul 2007 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

Comment only
06 Jul 2007 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.

01 Jul 2007 mm rezaei

29 May 2007 wu di

27 May 2007 Huynh Chau Duy

Documents and paper about Particle Swarm Optimization

Comment only
24 May 2007 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.

Comment only
11 May 2007 zhaojia Wang

Thanks a lot:)

17 Apr 2007 Muhammad Shakil

Does not work with matlab 2006a

30 Mar 2007 trace water

thank you the dear author!great work!

04 Mar 2007 robert rino

13 Feb 2007 ehsan nasr

10 Feb 2007 babak zarei

31 Jan 2007 Sirichai S

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

17 Dec 2006 potter harryhfy

i am a student of computer and technologe department.

Comment only
28 Nov 2006 lan FA

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

19 Nov 2006 ali alizade

please send for me

Comment only
11 Nov 2006 Dingyu Xue

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

18 Oct 2006 Ranjit singh chauhan

Excellent tool.

06 Oct 2006 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.

20 Sep 2006 jagdish bansal

very good

19 Sep 2006 aa ss


Comment only
13 Sep 2006 ish kakkar

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

13 Aug 2006 sankar karan


05 Aug 2006 weidong wu

04 Aug 2006 Ran Ren

03 Jul 2006 safiye turgay

18 Jun 2006 Omar Vicencio

09 Jun 2006 dan lak


09 Jun 2006 dan lak

09 Jun 2006 dana lak

some democanbe added

29 May 2006 Rafael Rodriguez

19 Apr 2006 Yuan Liguo

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

08 Apr 2006 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.

13 Mar 2006 shark spear

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

29 Jan 2006 KHALED ABO-AL-EZ

27 Jan 2006 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.

22 Nov 2005 sione palu

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

07 Oct 2005 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

08 Aug 2005 zhuohua duan

08 Mar 2006

Significantly updated PSO function itself as well as the plotting routines. Fixed DemoPSOBehavior, updated README, dropped non-vectorized code. This completely replaces previous version.

Contact us