gridSearch(fun, levels, ..., lower, upper, npar = 1L, n = 5L,
printDetail = TRUE,
method = c("loop", "multicore", "snow"),
mc.control = list(), cl = NULL,
keepNames = FALSE, asList = FALSE)
fun(x, ...)
, with x
being a numeric vector or a listfun
lower
and
upper
are to be expanded; see Details. Ignored if levels are explicitly specified.loop
(the default), multicore
or snow
. See Details.mclapply
if
method
is multicore
. Must be a list of named
elements; see the documentation of mclapply
in pkg{multicore}.NULL
. If method snow
is used, this must be a
cluster object or an integer (the number of cores).levels
be kept? Not implemented.fun
expect a list? Default is FALSE
.fun
.fun
.fun
was evaluated.x
(the first argument to fun
) increases, the
number of necessary function evaluations grows exponentially. Note that
gridSearch
will not warn about an unreasonable number of function
evaluations, but if printDetail
is TRUE
it will print the
required number of function evaluations.
In practice, grid search is often better than its reputation. If a
function takes only a few parameters, it is often a reasonable approach
to find expand.grid
to create
the list of parameter combinations for which fun
is evaluated; it
calls lapply
to evaluate fun
if
method == "loop"
(the default).
If method == "multicore"
, the function mclapply
from
package mclapply
can be passed through the list mc.control
. If multicore
is chosen but not available, then method
will be set to
loop
and a warning is issued. If method == "snow"
, the
function clusterApply
from package 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 not available or cl
is
not specified, then method
will be set to loop
and a
warning is issued.testFun <- function(x) x[1L] + x[2L]^2
sol <- gridSearch(fun = testFun, levels = list(1:2, c(2, 3, 5)))
sol$minfun
sol$minlevels
testFun2 <- function(x) sum(x^2)
sol <- gridSearch(fun = testFun2, lower = rep(1, 3), upper = c(2, 8, 4), n = 5L)
sol$minfun
sol$minlevels
Run the code above in your browser using DataLab