Learn R Programming

NMOF (version 0.22-0)

restartOpt: 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)

Arguments

fun
the optimisation function: DEopt, GAopt, LSopt, TAopt or PSopt
n
the number of restarts
OF
the objective function
algo
the list algo that is passed to the particular optimisation function
...
additional data that is passed to the particular optimisation function
method
can be loop (the default), multicore or snow. See Details.
mc.control
a list containing settings that will be passed to mclapply if method is multicore. Must be a list of named elements. See the documentation of mclapply.
cl
default is NULL. If method snow is used, this must be a cluster object or an integer (the number of cores).

Value

  • Returns a list of n lists. Each of the n lists stores the output of one of the runs.

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 == "multicore", function mclapply from package multicore is used. Further settings for mclapply can be passed through the list mc.control. If multicore is chosen but the package is not available, then method will be set to loop and a warning is issued. If method == "snow", function clusterApply from package snow 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 the package 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.

References

Gilli, M., Maringer, D. and Schumann, E. (2011) Numerical Methods and Optimization in Finance. Elsevier. http://www.elsevierdirect.com/product.jsp?isbn=9780123756626 Schumann, E. (2011) Examples and Extensions for the NMOF Package. http://enricoschumann.net/files/NMOFex.pdf

See Also

DEopt, GAopt, LSopt, PSopt, TAopt

Examples

Run this code
## 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 = 10L, OF = tfTrefethen, algo = algo)
res100F <- sapply(res100, `[[`, "OFvalue")

algo$nG <- 300L
res300 <- restartOpt(DEopt, n = 10, OF = tfTrefethen, algo = algo)
res300F <- sapply(res300, `[[`, "OFvalue")

xx <- pretty(c(res100F, res300F, -3.31))
plot(ecdf(res100F), main = "optimum is -3.306", 
     xlim = c(xx[1], tail(xx,1)))
abline(v = -3.3069, col = "red")  ### optimum
lines(ecdf(res300F), col = "blue")
legend(x = "right", box.lty = 0, , lty = 1,
      legend = c("optimum", "100 generations", "300 generations"),
      pch = c(NA, 19, 19), col = c("red", "black", "blue"))

## 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))

Run the code above in your browser using DataLab