The function optimizer() creates an Optimizer object and is
equivalent to Optimizer$new().
The function objective() creates an Objective object and is
equivalent to Objective$new().
Arguments
- which
[
character(1)]
Either:one of
optimizer_dictionary$keys(the package of the optimizer must be installed)or
"custom", in which case$definition()must be used to define the optimizer details.
- ...
For
optimizer(), named arguments to pass to the optimizer algorithm. Additionally,.verbose = FALSEcan be set to suppress the status message forwhich = "custom".For
objective(), optionally additional function arguments that are fixed during the optimization.- f
[
function]
Afunctionto be optimized.- target
[
character()]
The argument name(s) to optimize.If
NULL(default), the first function argument is selected.- npar
[
integer()]
The length of each target argument.
Examples
### the Rosenbrock function
rosenbrock <- function(x) 100 * (x[2] - x[1]^2)^2 + (1 - x[1])^2
### minimize with 'stats::nlm'
nlm <- optimizer("stats::nlm", iterlim = 200)
nlm$minimize(rosenbrock, initial = c(-1.2, 1))
#> $value
#> [1] 3.973765e-12
#>
#> $parameter
#> [1] 0.999998 0.999996
#>
#> $seconds
#> [1] 0.01898527
#>
#> $initial
#> [1] -1.2 1.0
#>
#> $error
#> [1] FALSE
#>
#> $gradient
#> [1] -6.539235e-07 3.335976e-07
#>
#> $code
#> [1] 1
#>
#> $iterations
#> [1] 23
#>
### evaluate the objective
rosenbrock_objective <- objective(rosenbrock, npar = 2)
rosenbrock_objective$evaluate(c(1, 1))
#> [1] 0
