particle swarm options
specifying options for particleswarm
create options
using the 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.
if an
initialswarmmatrix
option exists,'pswcreationuniform'
takes the firstswarmsize
rows of theinitialswarmmatrix
matrix as the swarm. if the number of rows of theinitialswarmmatrix
matrix is smaller thanswarmsize
, then'pswcreationuniform'
continues to the next step.'pswcreationuniform'
creates enough particles so that there areswarmsize
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, then particleswarm
notifies you.
custom creation function
set a custom creation function using optimoptions
to set
the creationfcn
option to
@
, where
customcreation
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 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 numberf-count
— cumulative number of objective function evaluationsbest f(x)
— best objective function valuemean f(x)
— mean objective function value over all particlesstall iterations
— number of iterations since the last change inbest 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 . 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 positionx
and the best positionp
the particle has seenx-g
, the difference between the current positionx
and the best positiong
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 ofw
can destabilize the swarm. the value ofw
stays within the range of the two-element vectorinertiarange
.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 . setting
minneighborsfraction
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'
— use the matlab® function to perform unconstrained minimization.'patternsearch'
— use a pattern search to perform constrained or unconstrained minimization.'fminunc'
— use the optimization toolbox™ function to perform unconstrained minimization.'fmincon'
— use the optimization toolbox function to perform constrained minimization.
note
ensure that your hybrid function accepts your problem constraints. otherwise,
particleswarm
throws an error.
you can set separate options for the hybrid function. use for fminsearch
, or for fmincon
,
patternsearch
, or fminunc
. for
example:
hybridopts = optimoptions('fminunc',... 'display','iter','algorithm','quasi-newton');
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. see .
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 as a cell array of function handles:
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
.
the lone built-in plot function 'pswplotbestf'
plots the best
objective function value against iterations. to specify it,
options = optimoptions(@particleswarm,'plotfcn','pswplotbestf')
to create a custom plot function, write a function with syntax specified in structure of the output function or plot function. to specify a custom plot function, use a function handle. if you have several plot functions, pass them as a cell array of function handles:
options = optimoptions(@particleswarm,... 'plotfcn',{@plotfun1,@plotfun2,@plotfun3})
for an example of a custom output function, see .
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.'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.'iter'
— the solver is proceeding with its iterations. typically, this is where your output function or plot function performs its work.'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.
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 number of rows is equal to the swarm size. |
swarmfvals | vector containing the objective function values of particles
in the swarm. for particle i ,
swarmfvals(i) = fun(swarm(i,:)) , where
fun is the objective 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
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. for patternsearch
details, see .
stopping criteria
particleswarm
stops iterating when any of the following
occur.
stopping option | stopping test | exit flag |
---|---|---|
maxstalliterations and
functiontolerance | relative change in the best objective function value
g over the last
maxstalliterations iterations is less than
functiontolerance . | 1 |
maxiterations | number of iterations reaches
maxiterations . | 0 |
outputfcn or
plotfcn | outputfcn or plotfcn can
halt the iterations. | -1 |
objectivelimit | best objective function value g of a feasible
point is less than objectivelimit . | -3 |
maxstalltime | best objective function value g did not change
in the last maxstalltime seconds. | -4 |
maxtime | function run time exceeds maxtime
seconds. | -5 |
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.