optim_cache: Run the 'optim' Optimization Function with a Cached Gradient

View source: R/optim_cache.R

optim_cacheR Documentation

Run the optim Optimization Function with a Cached Gradient

Description

Run 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.

Usage

optim_cache(
  par,
  fn,
  gr = NULL,
  ...,
  method = c("BFGS", "CG", "L-BFGS-B"),
  lower = -Inf,
  upper = Inf,
  control = list(),
  hessian = FALSE
)

Arguments

par

The initial values for the parameters to be optimized over as in optim.

fn

The function to be minimized. As a major difference with optim, this function must return a list with two elements objective and gradient (vector).

gr

Can not be used. This argument is for compatibility with optim. It can be removed in future versions.

...

Further arguments to be passed to optim.

method

As in optim. Since we are always using the gradient in this function, only the methods "BFGS", "CG" "L-BFGS-B" can be used here.

lower, upper, hessian, control

See optim.

Value

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.

Examples

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


libKriging/dolka documentation built on April 14, 2022, 7:17 a.m.