psoptim | R Documentation |
General implementation of particle swarm optimization usable as a direct
replacement for optim
.
psoptim(par, fn, gr = NULL, ..., lower = -1, upper = 1, control = list())
par |
Vector with length defining the dimensionality of the
optimization problem. Providing actual values of |
fn |
A function to be minimized (or maximized), with first argument the vector of parameters over which minimization is to take place. It should return a scalar result. |
gr |
A function to return the gradient if local search is BFGS.
If it is |
... |
Further arguments to be passed to |
lower |
Lower bounds on the variables. |
upper |
Upper bounds on the variables. |
control |
A list of control parameters. See “Details”. |
By default this function performs minimization using a particle swarm
algorithm, but it will maximize if control$fnscale
is negative.
The default control arguments implies that the algorithm follows the Standard PSO 2007 implementation by Maurice Clerc, but the code also provides support for PSO 2011, clamping the maximal velocity, restarting when all particles converge to a single area and using BFGS as the local search direction.
The control
argument is a list that can supply any of the
following components:
Non-negative integer. If positive, tracing information on
the progress of the optimization is produced. Defaults to 0
.
An overall scaling to be applied to the value of fn
and gr
(if used) during optimization. If negative, turns the problem
into a maximization problem. Optimization is performed on
fn(par)/fnscale
. Defaults to 1
.
The maximum number of iterations. Defaults to 1000
.
The maximum number of function evaluations (not considering any
performed during numerical gradient computation). Defaults to Inf
.
The absolute convergence tolerance. The method converges once the
best fitness obtained is less than or equal to
abstol
. Defaults to -Inf
.
The tolerance for restarting. Once the maximal distance between the
best particle and all other particles is less than reltol*d
the algorithm restarts. Defaults to 0
which disables the
check for restarting.
The frequency for reports if control$trace
is
positive. Defaults to 10
.
Logical; if TRUE
statistics at every
reporting step are collected and returned. Defaults to FALSE
.
The swarm size. Defaults to floor(10+2*sqrt(length(par)))
unless type
is “SPSO2011” in which case the default is 40
.
The exponent for calculating number of informants. Defaults to 3
.
The average percentage of informants for each particle. A value of
1
implies that all particles are fully informed. Defaults to
1-(1-1/s)^k
.
The exploitation constant. A vector of length 1
or
2
. If the length is two, the actual constant used is gradially
changed from w[1]
to w[2]
as the number of iterations or
function evaluations approach the limit provided.
Defaults to 1/(2*log(2))
.
The local exploration constant. Defaults to .5+log(2)
.
The global exploration constant. Defaults to .5+log(2)
.
The diameter of the search space. Defaults to the euclidean distance
between upper
and lower
.
The maximal (euclidean) length of the velocity vector. Defaults to NA
which disables clamping of the velocity. However, if specified the
actual clamping of the length is v.max*d
.
Logical; if TRUE
the particles are processed in random
order. If vectorize
is TRUE
then the value of
rand.order
does not matter. Defaults to TRUE
.
The maximum number of restarts. Defaults to Inf
.
The maximum number of iterations without improvement.
Defaults to Inf
.
Logical; if TRUE
the particles are processed in a vectorized
manner. This reduces the overhead associated with iterating over
each particle and may be more time efficient for cheap function
evaluations. Defaults to FALSE
.
If true, each normal PSO position update is followed by an
L-BFGS-B search with the provided position as initial guess. This
makes the implementation a hybrid approach. Defaults to
FALSE
which disables BFGS for the local search. Note that
no attempt is done to control the maximal number of function
evaluations within the local search step (this can be done
separately through hybrid.control
) but the number of
function evaluations used by the local search method counts
towards the limit provided by maxf
AFTER the local search
returns. To support a broader class of hybrid approaches a
character vector can also be supplied with “off” being
equivalent to false, “on” equivalent to true, and
“improved” implying that the local search will only be
performed when the swarm finds an improvement.
List with any additional control parameters to pass on to
optim
when using L-BFGS-B for the local search.
Defaults to NULL
.
Character vector which describes which reference implementation of SPSO is followed. Can take the value of “SPSO2007” or “SPSO2011”. Defaults to “SPSO2007”.
A list, compatible with the output from optim
, with components:
par |
The best set of parameters found. |
value |
The value of |
counts |
A three-element vector containing the number of function evaluations, the number of iterations, and the number of restarts. |
convergence |
An integer code.
|
message |
A descriptive message of the reason for termination. |
If trace
is positive and trace.stats
is TRUE
additionally the component:
stats |
A list of statistics collected at every reporting step with the following components:
|
Default parameters follow:
Clerc, M. (2011) https://hal.archives-ouvertes.fr/hal-00764996/document. Notice that the SPSO 2011 implementation does not include any of the bells and whistles from the implementation by M. Clerc et al. and effectively only differes from the SPSO 2007 implementation in the default swarm size, how velocities are initiated and the update of velocities/positions which in the SPSO 2011 implementation are invariant to rotation.
The gradual change of w
and clamping the maximal velocity is
described in:
Parsopoulos, K.E. and Vrahatis M.N. (2002) Recent approaches to global optimization problems through Particle Swarm Optimization. Natural Computing 1: 235-306.
The restart (provided through reltol
) is similar to:
Evers G.I. and Ghalia M.B. Regrouping Particle Swarm Optimization: A New Global Optimization Algorithm with Improved Performance Consistency Across Benchmarks. https://bee22.com/resources/Evers%202009.pdf
The hybrid approach is similar to:
Qin J., Yin Y. and Ban X. (2010) A Hybrid of Particle Swarm Optimization and Local Search for Multimodal Functions. Lecture Notes in Computer Science, Volume 6145/2010, 589-596, DOI: 10.1007/978-3-642-13495-1_72
optim
, test.problem
.
set.seed(1) ## Rastrigin function psoptim(rep(NA,2),function(x) 20+sum(x^2-10*cos(2*pi*x)), lower=-5,upper=5,control=list(abstol=1e-8)) set.seed(1) ## Rastrigin function - local refinement with L-BFGS-B on improvements psoptim(rep(NA,2),function(x) 20+sum(x^2-10*cos(2*pi*x)), lower=-5,upper=5,control=list(abstol=1e-8,hybrid="improved")) ## Griewank function psoptim(rep(NA,2),function(x) sum(x*x)/4000-prod(cos(x/sqrt(1:2)))+1, lower=-100,upper=100,control=list(abstol=1e-2)) set.seed(1) ## Rastrigin function with reporting o <- psoptim(rep(NA,2),function(x) 20+sum(x^2-10*cos(2*pi*x)), lower=-5,upper=5,control=list(abstol=1e-8,trace=1,REPORT=1, trace.stats=TRUE)) ## Not run: plot(o$stats$it,o$stats$error,log="y",xlab="It",ylab="Error") points(o$stats$it,sapply(o$stats$f,min),col="blue",pch=2) ## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.