Particle swarm optimization: the simplest what and how

11 Nov

While there exist many introductory materials on Particle Swarm Optimization (PSO), it is best to have an intuitive example, simple, understandable at first reading, and illustrative. I personally do not like those examples that try to bring all intricacies together. Here comes the simplest example I guess: to find a minimum for the function:


It is know that the optimal solution is found when x=-1, and the minimum is f(x)=0;

The below video demonstrate the results of finding this optimal solution using PSO based approach:

As you can see, after some initial random movements of the design variables, the design variable gradually converges to x=-1 (the black curve) and the optimal value reaches 0.

The basic procedures to implement this PSO based approach are as follows:

[1] Create a collection of particles, each particle represents an instance of the design variable. In this case, the design variable is a scalar-valued single parameter:

%Initialize a particle swarm with nParticals
for i=1:nParticals
    x{i}=RandInRange(MinVars, MaxVars);

[2] For these particles, evaluate their performances:

for i=1:length(x)

[3] Find the best of these particles, which generates the minimum errors

ErrMat=cell2mat(err);   % Convert cell to array
minErr=min(ErrMat);     % Find the minimum
idx=find(ErrMat==minErr);   % Find which X is minimum

[4] The minimum value got so far is both the local best and the global best, keep them down for later use:

lBestX=x{idx};          % Local best
gBestX=x{idx};         % Global best
V=0;                             % Velocity of particles of initial movement

[5] For each particles, move them according to the formula:


alpha1=2.0;     % Local best influence;
alpha2=2.0;     % Global best influence;
Fai=0.9;             % Inertia Weight;
  for i=1:length(x)
       V=Fai*V + alpha1 * Gamma1 * (lBestX-x{i}) + alpha2 * Gamma2 * (gBestX-x{i}) ;  % update V;
      if(V>=VMax)         % To constrain the particles not to move too far…
      x{i}= x{i} + V;  

For details of these parameters and their physical meanings, Google “Particle Swarm Optimization”, a quick view of the result goes here

[6] Terminate when the some conditions are met:

while gBestY> 1e-6

Here, RunMoveOnce() refers to the process described in [5].

The Matlab files can be download it below. Enjoy!

v0.1  | v0.2  (Nov, 2010)


Posted by on November 11, 2010 in Maths, Programming


4 responses to “Particle swarm optimization: the simplest what and how

  1. hamid

    September 16, 2011 at 1:02 pm

    Thank You. It is the simplest explanation of PSO I have ever read.

  2. aloui mourad

    November 1, 2012 at 3:35 pm

    great work , i wish if you can help me and send me all the program my brother

  3. Panagiotis

    January 16, 2016 at 9:26 pm

    Hello, i cant seem to download the files u posted. Can u please check it out?
    Thanks in advance.

  4. mourad

    January 24, 2016 at 11:29 pm

    the file doesn’t exist !!!!!!!!!!!!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: