optim_cache | R Documentation |
optim
Optimization Function with a Cached
GradientRun the optim
function from
the stats package with the objective and the gradient
defined in one function. The result is global
minimization of a real-valued function of a numeric vector
with length d to be provided by fn
. The R
function fn
must provide the values of the objective
and its gradient.
The formal arguments of this function and their default values
are those of the optim
function at the
time when this function is written, based on version
4.1.2
of R. The optim
function is unlikely
to be changed in the future.
optim_cache( par, fn, gr = NULL, ..., method = c("BFGS", "CG", "L-BFGS-B"), lower = -Inf, upper = Inf, control = list(), hessian = FALSE )
par |
The initial values for the parameters to be optimized
over as in |
fn |
The function to be minimized. As a major difference with
|
gr |
Can not be used. This argument is for compatibility with
|
... |
Further arguments to be passed to
|
method |
As in |
lower, upper, hessian, control |
See |
A list with the same structure as that returned by
optim
. It contains par
and
value
giving the best set of parameters found and to
the value of fn
corresponding to par
.
## Note that in this example, gradient caching would not be worth it. ## emulate a costly-to-evaluate-alone gradient ## =========================================== braninDer <- function(x) { Sys.sleep(0.01) branin_with_grad(x)$gradient } ## separate objective and gradient functions ## ========================================= te <- system.time(res <- optim(par = c(0.5, 0.5), fn = branin, gr = braninDer)) ## gradient "cached" ## ================ teCache <- system.time(resCache <- optim_cache(par = c(0.5, 0.5), fn = branin_with_grad)) rbind("optim" = te, "optim_cache" = teCache) c("optim" = res$value, "optim_cache" = resCache$value) ## Check the use of ... ## ==================== braninShift <- function(x, shift = 0) { res <- branin_with_grad(x) res$objective <- res$objective + shift res } resShifted <- optim_cache(par = c(0.5, 0.5), fn = braninShift, shift = 100) c("optim_cache" = resCache$value, "optimShifted" = resShifted$value - 100)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.