nimOptim: Nimble wrapper around R's builtin 'optim'.

View source: R/nimbleFunction_Rexecution.R

nimOptimR Documentation

Nimble wrapper around R's builtin optim.

Description

Nimble wrapper around R's builtin optim.

Usage

nimOptim(
  par,
  fn,
  gr = "NULL",
  ...,
  method = "Nelder-Mead",
  lower = -Inf,
  upper = Inf,
  control = nimOptimDefaultControl(),
  hessian = FALSE
)

Arguments

par

Initial values for the parameters to be optimized over.

fn

A function to be minimized (or maximized), with first argument the vector of parameters over which minimization is to take place. It should return a scalar result.

gr

A function to return the gradient for the "BFGS", "CG" and "L-BFGS-B" methods.

...

IGNORED

method

The method to be used. See 'Details' section of optim. One of: "Nelder-Mead", "BFGS", "CG", "L-BFGS-B". Note that the R methods "SANN", "Brent" are not supported.

lower

Vector or scalar of lower bounds for parameters.

upper

Vector or scalar of upper bounds for parameters.

control

A list of control parameters. See Details section of optim.

hessian

Logical. Should a Hessian matrix be returned?

Value

optimResultNimbleList

See Also

optim

Examples

## Not run: 
objectiveFunction <- nimbleFunction(
    run = function(par = double(1)) {
        return(sum(par) * exp(-sum(par ^ 2) / 2))
        returnType(double(0))
    }
)
optimizer <- nimbleFunction(
    run = function(method = character(0), fnscale = double(0)) {
        control <- optimDefaultControl()
        control$fnscale <- fnscale
        par <- c(0.1, -0.1)
        return(optim(par, objectiveFunction, method = method, control = control))
        returnType(optimResultNimbleList())
    }
)
cOptimizer <- compileNimble(optimizer)
cOptimizer(method = 'BFGS', fnscale = -1)

## End(Not run)

nimble documentation built on July 9, 2023, 5:24 p.m.