0% found this document useful (0 votes)
51 views7 pages

Particle Swarm Optimization

Particle swarm optimization is a population-based algorithm where particles move throughout a region to find the best solution. The particles are attracted to the best locations found by themselves and other particles in the swarm. The algorithm initializes particles randomly and iteratively updates their positions and velocities based on these attractions until a stopping criteria is met.

Uploaded by

adlinreshma654
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views7 pages

Particle Swarm Optimization

Particle swarm optimization is a population-based algorithm where particles move throughout a region to find the best solution. The particles are attracted to the best locations found by themselves and other particles in the swarm. The algorithm initializes particles randomly and iteratively updates their positions and velocities based on these attractions until a stopping criteria is met.

Uploaded by

adlinreshma654
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

What Is Particle Swarm Optimization?

Particle swarm is a population-based algorithm. In this respect it is similar to the genetic algorithm. A collection of
individuals called particles move in steps throughout a region. At each step, the algorithm evaluates the objective
function at each particle. After this evaluation, the algorithm decides on the new velocity of each particle. The
particles move, then the algorithm reevaluates.

The inspiration for the algorithm is flocks of birds or insects swarming. Each particle is attracted to some degree
to the best location it has found so far, and also to the best location any member of the swarm has found. After
some steps, the population can coalesce around one location, or can coalesce around a few locations, or can
continue to move.

The particleswarm function attempts to optimize using a Particle Swarm Optimization Algorithm.

Particle Swarm Optimization Algorithm


Algorithm Outline
The particle swarm algorithm begins by creating the initial particles, and assigning them
initial velocities.
It evaluates the objective function at each particle location, and determines the best (lowest)
function value and the best location.
It chooses new velocities, based on the current velocity, the particles' individual best
locations, and the best locations of their neighbors.
It then iteratively updates the particle locations (the new location is the old one plus the
velocity, modified to keep particles within bounds), velocities, and neighbors.
Iterations proceed until the algorithm reaches a stopping criterion.
Here are the details of the steps.
Initialization
By default, particleswarm creates particles at random uniformly within bounds. If there is an
unbounded component, particleswarm creates particles with a random uniform distribution
from –1000 to 1000. If you have only one bound, particleswarm shifts the creation to have
the bound as an endpoint, and a creation interval 2000 wide. Particle i has position x(i),
which is a row vector with nvars elements. Control the span of the initial swarm using
the InitialSwarmSpan option.
Similarly, particleswarm creates initial particle velocities v uniformly within the range [-
r,r], where r is the vector of initial ranges. The range of component i is the ub(i) - lb(i),
but for unbounded or semi-unbounded components the range is the InitialSwarmSpan option.
particleswarm evaluates the objective function at all particles. It records the current
position p(i) of each particle i. In subsequent iterations, p(i) will be the location of the best
objective function that particle i has found. And b is the best over all particles: b =
min(fun(p(i))). d is the location such that b = fun(d).

particleswarm initializes the neighborhood size N to minNeighborhoodSize =


max(1,floor(SwarmSize*MinNeighborsFraction)) .
particleswarm initializes the inertia W = max(InertiaRange), or if InertiaRange is negative,
it sets W = min(InertiaRange).
particleswarm initializes the stall counter c = 0.
For convenience of notation, set the variable y1 = SelfAdjustmentWeight, and y2 =
SocialAdjustmentWeight, where SelfAdjustmentWeight and SocialAdjustmentWeight are
options.
Iteration Steps
The algorithm updates the swarm as follows. For particle i, which is at position x(i):
1. Choose a random subset S of N particles other than i.
2. Find fbest(S), the best objective function among the neighbors, and g(S), the position of
the neighbor with the best objective function.
3. For u1 and u2 uniformly (0,1) distributed random vectors of length nvars, update the
velocity
v = W*v + y1*u1.*(p-x) + y2*u2.*(g-x).
This update uses a weighted sum of:
 The previous velocity v
 The difference between the current position and the best position the particle has seen p-x
 The difference between the current position and the best position in the current
neighborhood g-x
4. Update the position x = x + v.
5. Enforce the bounds. If any component of x is outside a bound, set it equal to that bound.
6. Evaluate the objective function f = fun(x).
7. If f < fun(p), then set p = x. This step ensures p has the best position the particle has
seen.
8. If f < b, then set b = f and d = x. This step ensures b has the best objective function in
the swarm, and d has the best location.
9. If, in the previous step, the best function value was lowered, then set flag = true.
Otherwise, flag = false. The value of flag is used in the next step.
10. Update the neighborhood. If flag = true:
a. Set c = max(0,c-1).
b. Set N to minNeighborhoodSize.
c. If c < 2, then set W = 2*W.
d. If c > 5, then set W = W/2.
e. Ensure that W is in the bounds of the InertiaRange option.
If flag = false:
f. Set c = c+1.
g. Set N = min(N + minNeighborhoodSize,SwarmSize).
Stopping Criteria
particleswarm iterates until it reaches a stopping criterion.

Stopping Option Stopping Test

MaxStallIterations and FunctionToleranc Relative change in the best objective function value g over the last
e thanFunctionTolerance.
Stopping Option Stopping Test

MaxIterations Number of iterations reaches MaxIterations.

OutputFcn or PlotFcn OutputFcn or PlotFcn can halt the iterations.

ObjectiveLimit Best objective function value g is less than or equal toObjectiveLi

MaxStallTime Best objective function value g did not change in the lastMaxStallT

MaxTime Function run time exceeds MaxTime seconds.

If particleswarm stops with exit flag 1, it optionally calls a hybrid function after it exits.

Particle Swarm Options


Specifying Options for particleswarm
Create options using the optimoptions function as follows.

options = optimoptions('particleswarm','Param1',value1,'Param2',value2,...);
For an example, see Optimize Using Particle Swarm.

Each option in this section is listed by its field name in options. For example, Display refers to the
corresponding field of options.

Swarm Creation
By default, particleswarm calls the @pswcreationuniform swarm creation function. This function works as
follows.

1. If an InitialSwarmMatrix option exists, @pswcreationuniform takes the first SwarmSize rows of


the InitialSwarmMatrix matrix as the swarm. If the number of rows of
theInitialSwarmMatrix matrix is smaller than SwarmSize, then @pswcreationuniform continues to
the next step.
2. @pswcreationuniform creates enough particles so that there are SwarmSize in
total. @pswcreationuniform creates particles that are randomly, uniformly distributed. The range for any
swarm component is -InitialSwarmSpan/2,InitialSwarmSpan/2 , shifted and scaled if necessary to
match any bounds.
After creation, particleswarm checks that all particles satisfy any bounds, and truncates components if
necessary. If the Display option is 'iter' and a particle needed truncation, thenparticleswarm notifies
you.

Custom Creation Function

Set a custom creation function using optimoptions to set the CreationFcn option to @customcreation,
where customcreation is the name of your creation function file. A custom creation function has this syntax.

swarm = customcreation(problem)
The creation function should return a matrix of size SwarmSize-by-nvars, where each row represents the
location of one particle. See problem for details of the problem structure. In particular, you can
obtain SwarmSize from problem.options.SwarmSize, and nvars from problem.nvars.

For an example of a creation function, see the code for pswcreationuniform.


edit pswcreationuniform
Display Settings
The Display option specifies how much information is displayed at the command line while the algorithm is
running.

 'off' or 'none' — No output is displayed.


 'iter' — Information is displayed at each iteration.
 'final' (default) — The reason for stopping is displayed.
iter displays:

 Iteration — Iteration number


 f-count — Cumulative number of objective function evaluations
 Best f(x) — Best objective function value
 Mean f(x) — Mean objective function value over all particles
 Stall Iterations — Number of iterations since the last change in Best f(x)
The DisplayInterval option sets the number of iterations that are performed before the iterative display
updates. Give a positive integer.

Algorithm Settings
The details of the particleswarm algorithm appear in Particle Swarm Optimization Algorithm. This section
describes the tuning parameters.

The main step in the particle swarm algorithm is the generation of new velocities for the swarm:

For u1 and u2 uniformly (0,1) distributed random vectors of length nvars, update the velocity

v = W*v + y1*u1.*(p-x) + y2*u2.*(g-x).

The variables W = inertia, y1 = SelfAdjustmentWeight, and y2 = SocialAdjustmentWeight.

This update uses a weighted sum of:

 The previous velocity v


 x-p, the difference between the current position x and the best position p the particle has seen
 x-g, the difference between the current position x and the best position g in the current neighborhood
Based on this formula, the options have the following effect:

 Larger absolute value of inertia W leads to the new velocity being more in the same line as the old, and with a
larger absolute magnitude. A large absolute value of W can destabilize the swarm. The value of W stays within the
range of the two-element vector InertiaRange.
 Larger values of y1 = SelfAdjustmentWeight make the particle head more toward the best place it has
visited.
 Larger values of y2 = SocialAdjustmentWeight make the particle head more toward the best place in the
current neighborhood.
Large values of inertia, SelfAdjustmentWeight, or SocialAdjustmentWeight can destabilize the swarm.

The MinNeighborsFraction option sets both the initial neighborhood size for each particle, and the minimum
neighborhood size; see Particle Swarm Optimization Algorithm. SettingMinNeighborsFraction to 1 has all
members of the swarm use the global minimum point as their societal adjustment target.

See Optimize Using Particle Swarm for an example that sets a few of these tuning options.

Hybrid Function
A hybrid function is another minimization function that runs after the particle swarm algorithm terminates. You
can specify a hybrid function in the HybridFcn option. The choices are

 [] — No hybrid function.
 fminsearch (@fminsearch) — Use the MATLAB® function fminsearch to perform unconstrained
minimization.
 patternsearch (@patternsearch) — Use a pattern search to perform constrained or unconstrained
minimization.
 fminunc (@fminunc) — Use the Optimization Toolbox™ function fminunc to perform unconstrained
minimization.
 fmincon (@fmincon) — Use the Optimization Toolbox function fmincon to perform constrained minimization.

Note: Ensure that your hybrid function accepts your problem constraints. Otherwise, particleswarm throw
You can set separate options for the hybrid function. Use optimset for fminsearch,
or optimoptions for fmincon, patternsearch, or fminunc. For example:

hybridopts = optimoptions('fminunc','Display','iter','Algorithm','quasi-
newton');

Include the hybrid options in the particleswarm options as follows:

options = optimoptions(options,'HybridFcn',{@fminunc,hybridopts});

hybridopts must exist before you set options.

For an example that uses a hybrid function, see Optimize Using Particle Swarm.

Output Function and Plot Function


Output functions are functions that particleswarm calls at each iteration. Output functions can
halt particleswarm, or can perform other tasks. To specify an output function,

options = optimoptions(@particleswarm,'OutputFcn',@outfun)
where outfun is a function with syntax specified in Structure of the Output Function or Plot Function. If you have
several output functions, pass them in a cell array:

options = optimoptions(@particleswarm,'OutputFcn',{@outfun1,@outfun2,@outfun3})
Similarly, plot functions are functions that particleswarm calls at each iteration. The difference between an
output function and a plot function is that a plot function has built-in plotting enhancements, such as buttons that
appear on the plot window to pause or stop particleswarm. To specify a plot function,

options = optimoptions(@particleswarm,'PlotFcn',@plotfun)
where plotfun is a function with syntax specified in Structure of the Output Function or Plot Function. If you
have several plot functions, pass them in a cell array:

options = optimoptions(@particleswarm,'PlotFcn',{@plotfun1,@plotfun2,@plotfun3})
The lone built-in plot function @pswplotbestf plots the best objective function value against iterations.

For an example of a custom output function, see Particle Swarm Output Function.

Structure of the Output Function or Plot Function

An output function has the following calling syntax:

stop = myfun(optimValues,state)

If your function sets stop to true, iterations end. Set stop to false to have particleswarm continue to
calculate.

The function has the following input arguments:

 optimValues — Structure containing information about the swarm in the current iteration. Details are
in optimValues Structure.
 state — String giving the state of the current iteration.
o 'init' — The solver has not begun to iterate. Your output function or plot function can use this state to open
files, or set up data structures or plots for subsequent iterations.
o 'iter' — The solver is proceeding with its iterations. Typically, this is where your output function or plot
function performs its work.
o 'done' — The solver reached a stopping criterion. Your output function or plot function can use this state to
clean up, such as closing any files it opened.
Passing Extra Parameters in the Optimization Toolbox documentation explains how to provide additional
parameters to output functions or plot functions.

optimValues Structure

particleswarm passes the optimValues structure to your output functions or plot functions.
The optimValues structure has the following fields.

Field Contents

funccount Total number of objective function evaluations.

bestx Best solution point found, corresponding to the best objective function value bestfval.

bestfval Best (lowest) objective function value found.

iteration Iteration number.

meanfval Mean objective function among all particles at the current iteration.

stalliterations Number of iterations since the last change in bestfval.

swarm Matrix containing the particle positions. Each row contains the position of one particle, and the n

swarmfvals Vector containing the objective function values of particles in the swarm. For particle i, swarmfv
function.

Parallel or Vectorized Function Evaluation


For increased speed, you can set your options so that particleswarm evaluates the objective function for the
swarm in parallel or in a vectorized fashion. You can use only one of these options. If you
set UseParallel to true and UseVectorized to true, then the computations are done in a vectorized
fashion, and not in parallel.

 Parallel particleswarm
 Vectorized particleswarm
Parallel particleswarm

If you have a Parallel Computing Toolbox™ license, you can distribute the evaluation of the objective functions to
the swarm among your processors or cores. Set the UseParallel option to true.

Parallel computation is likely to be faster than serial when your objective function is computationally expensive, or
when you have many particles and processors. Otherwise, communication overhead can cause parallel
computation to be slower than serial computation.

For details, see Parallel Computing.

Vectorized particleswarm

If your objective function can evaluate all the particles at once, you can usually save time by setting
the UseVectorized option to true. Your objective function should accept an M-by-N matrix, where each row
represents one particle, and return an M-by-1 vector of objective function values. This option works the same way
as the patternsearch and ga UseVectorized options. Forpatternsearch details, see Vectorize the
Objective and Constraint Functions.

Stopping Criteria
particleswarm stops iterating when any of the following occur.

Stopping Option Stopping Test

MaxStallIterations and FunctionToleranc Relative change in the best objective function value g over the last
e thanFunctionTolerance.

MaxIterations Number of iterations reaches MaxIterations.

OutputFcn or PlotFcn OutputFcn or PlotFcn can halt the iterations.

ObjectiveLimit Best objective function value g is less than or equal toObjectiveLi

MaxStallTime Best objective function value g did not change in the lastMaxStallT

MaxTime Function run time exceeds MaxTime seconds.

Also, if you set the FunValCheck option to 'on', and the swarm has particles with NaN, Inf, or complex
objective function values, particleswarm stops and issues an error.

You might also like