restartOpt {NMOF} | R Documentation |
Restart an Optimisation Algorithm
Description
The function provides a simple wrapper for the optimisation algorithms in the package.
Usage
restartOpt(fun, n, OF, algo, ...,
method = c("loop", "multicore", "snow"),
mc.control = list(), cl = NULL,
best.only = FALSE)
Arguments
fun |
the optimisation function: |
n |
the number of restarts |
OF |
the objective function |
algo |
the list |
... |
additional data that is passed to the particular optimisation function |
method |
can be |
mc.control |
a list containing settings that will be passed to |
cl |
default is |
best.only |
if |
Details
The function returns a list of lists. If a specific starting solution
is passed, all runs will start from this solution. If this is not
desired, initial solutions can be created randomly. This is done per
default in DEopt
, GAopt
and
PSopt
, but LSopt
and TAopt
require to specify a starting solution.
In case of LSopt
and TAopt
, the passed
initial solution algo$x0
is checked with is.function
: if
TRUE
, the function is evaluated in each single run. For
DEopt
, GAopt
and PSopt
, the
initial solution (which also can be a function) is specified with
algo$initP
.
The argument method
determines how fun
is
evaluated. Default is loop
. If method
is "multicore",
function mclapply
from package parallel is used. Further
settings for mclapply
can be passed through the list
mc.control
. If multicore
is chosen but the functionality
is not available, then method
will be set to loop
and a
warning is issued. If method == "snow"
, function
clusterApply
from package parallel is used. In this case,
the argument cl
must either be a cluster object (see the
documentation of clusterApply
) or an integer. If an integer, a
cluster will be set up via makeCluster(c(rep("localhost", cl)),
type = "SOCK")
, and stopCluster
is called when the function is
exited. If snow
is chosen but parallel is not available
or cl
is not specified, then method
will be set to
loop
and a warning is issued. In case that cl
is an
cluster object, stopCluster
will not be called automatically.
Value
If best.only
is FALSE
(the default), the function
returns a list of n
lists. Each of the n
lists stores
the output of one of the runs.
If best.only
is TRUE
, only the best restart is
reported. The returned list has the structure specific to the used
method.
Author(s)
Enrico Schumann
References
Gilli, M., Maringer, D. and Schumann, E. (2019) Numerical Methods and Optimization in Finance. 2nd edition. Elsevier. doi:10.1016/C2017-0-01621-X
Schumann, E. (2023) Financial Optimisation with R (NMOF Manual). http://enricoschumann.net/NMOF.htm#NMOFmanual
See Also
DEopt
, GAopt
, LSopt
,
PSopt
, TAopt
Examples
## see example(DEopt)
algo <- list(nP = 50L,
F = 0.5,
CR = 0.9,
min = c(-10, -10),
max = c( 10, 10),
printDetail = FALSE,
printBar = FALSE)
## choose a larger 'n' when you can afford it
algo$nG <- 100L
res100 <- restartOpt(DEopt, n = 5L, OF = tfTrefethen, algo = algo)
res100F <- sapply(res100, `[[`, "OFvalue")
algo$nG <- 200L
res200 <- restartOpt(DEopt, n = 5L, OF = tfTrefethen, algo = algo)
res200F <- sapply(res200, `[[`, "OFvalue")
xx <- pretty(c(res100F, res200F, -3.31))
plot(ecdf(res100F), main = "optimum is -3.306",
xlim = c(xx[1L], tail(xx, 1L)))
abline(v = -3.3069, col = "red") ## optimum
lines(ecdf(res200F), col = "blue")
legend(x = "right", box.lty = 0, , lty = 1,
legend = c("optimum", "100 generations", "200 generations"),
pch = c(NA, 19, 19), col = c("red", "black", "blue"))
## a 'best-of-N' strategy: given a sample x of objective
## function values, compute the probability that, after N draws,
## we have at least one realisation not worse than X
x <- c(0.1,.3,.5,.5,.6)
bestofN <- function(x, N) {
nx <- length(x)
function(X)
1 - (sum(x > X)/nx)^N
}
bestof2 <- bestofN(x, 2)
bestof5 <- bestofN(x, 5)
bestof2(0.15)
bestof5(0.15)
## Not run:
## with R >= 2.13.0 and the compiler package
algo$nG <- 100L
system.time(res100 <- restartOpt(DEopt, n = 10L, OF = tfTrefethen, algo = algo))
require("compiler")
enableJIT(3)
system.time(res100 <- restartOpt(DEopt, n = 10L, OF = tfTrefethen, algo = algo))
## End(Not run)