This function specifies the framework for a numerical optimizer.
Two wrappers for well-known optimizers are already available:
Usage
define_optimizer(
.optimizer,
.objective,
.initial,
.value,
.parameter,
.direction,
...,
.output_ignore = character(0),
.validate = FALSE,
.validation_settings = list(objective_test = TestFunctions::TF_ackley, objective_add =
list(), initial = round(stats::rnorm(2), 2), check_seconds = 10)
)
optimizer_nlm(
...,
.output_ignore = character(0),
.validate = FALSE,
.validation_settings = list()
)
optimizer_optim(
...,
.direction = "min",
.output_ignore = character(0),
.validate = FALSE,
.validation_settings = list()
)
Arguments
- .optimizer
A
function
, a numerical optimizer. Four conditions must be met:It must have an input named
.objective
for afunction
, the objective function which is optimized over its first argument.It must have an input named
.initial
for anumerical
vector, the initial parameter vector.It must have a
...
argument for additional parameters to the objective function.The output must be a named
list
, including the optimal function value and the optimal parameter vector.
- .objective
A
character
, the name of the function input ofoptimizer
.- .initial
A
character
, the name of the starting parameter values input ofoptimizer
.- .value
A
character
, the name of the optimal function value in the output list ofoptimizer
.- .parameter
A
character
, the name of the optimal parameter vector in the output list ofoptimizer
.- .direction
A
character
, indicates whether the optimizer minimizes ("min"
) or maximizes ("max"
).- ...
Additional arguments to be passed to the optimizer. Without specifications, the default values of the optimizer are used.
- .output_ignore
A
character
vector of element names in the output of.optimizer
that are not saved. The elements.value
and.parameter
are added automatically to.output_ignore
, because they are saved separately, see the output documentation ofapply_optimizer
.- .validate
A
logical
, set toTRUE
(FALSE
) to (not) validate theoptimizer
object. By default,.validate = FALSE
.- .validation_settings
Ignored if
.valdiate = FALSE
. Otherwise, alist
of validation settings:- objective_test
A
function
, the test function to be optimized. By default, it is the Ackley function.- objective_add
A
list
of additional arguments toobjective_test
(if any). By default,objective_add = list()
, because the default function forobjective_test
does not have additional arguments.- initial
A
numeric
vector, the initial values for the optimization ofobjective_test
. By default,initial = round(stats::rnorm(2), 2)
.- check_seconds
An
integer
, the maximum number of seconds before the test is aborted. The test call is considered to be successful if no error occurred withincheck_seconds
seconds. By default,check_seconds = 10
.
Format
An optimizer
object is a list
of six elements:
- optimizer
A
function
, the optimization algorithm.- optimizer_name
A
character
, the name ofoptimizer
.- optimizer_arguments
A named
list
, where each element is an additional function argument foroptimizer
.- optimizer_direction
Either
"min"
if the optimizer minimizes or"max"
if the optimizer maximizes.- optimizer_labels
A named
list
of fourcharacter
:- objective
the name of the function input of
optimizer
- initial
the name of the starting parameter values input of
optimizer
- value
the name of the optimal function value in the output list of
optimizer
- parameter
the name of the optimal parameter vector in the output list of
optimizer
.
- output_ignore
A
character
vector of element names in the outputlist
ofoptimizer
that are ignored. The elementsvalue
andparameter
are added automatically tooutput_ignore
, because they are saved separately, see the output documentation ofapply_optimizer
.
See also
Use apply_optimizer()
to apply an optimizer
object for numerical
optimization.
Examples
define_optimizer(
.optimizer = pracma::nelder_mead, # optimization function
.objective = "fn", # name of function input
.initial = "x0", # name of initial input
.value = "fmin", # name of value output
.parameter = "xmin", # name of parameter output
.direction = "min", # optimizer minimizes
.output_ignore = c("restarts", "errmess"), # ignore some outputs
tol = 1e-6, # additional optimizer argument
.validate = TRUE # validate the object
)
#> <optimizer 'pracma::nelder_mead'>