Particle Swarm Optimization
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.
MaxStallIterations and FunctionToleranc Relative change in the best objective function value g over the last
e thanFunctionTolerance.
Stopping Option Stopping Test
MaxStallTime Best objective function value g did not change in the lastMaxStallT
If particleswarm stops with exit flag 1, it optionally calls a hybrid function after it exits.
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.
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.
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
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');
options = optimoptions(options,'HybridFcn',{@fminunc,hybridopts});
For an example that uses a hybrid function, see Optimize Using Particle Swarm.
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.
stop = myfun(optimValues,state)
If your function sets stop to true, iterations end. Set stop to false to have particleswarm continue to
calculate.
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
bestx Best solution point found, corresponding to the best objective function value bestfval.
meanfval Mean objective function among all particles at the current iteration.
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 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.
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.
MaxStallIterations and FunctionToleranc Relative change in the best objective function value g over the last
e thanFunctionTolerance.
MaxStallTime Best objective function value g did not change in the lastMaxStallT
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.