| Optimizer {optimizeR} | R Documentation |
Specify numerical optimizer as R6 object
Description
A Optimizer R6 object defines a numerical optimizer based on an
optimization function implemented in R.
The main advantage of working with an Optimizer object instead of
using the optimization function directly lies in the standardized inputs and
outputs.
Any R function that fulfills the following four constraints can be defined as
an Optimizer object:
It must have an input for a
function, the objective function to be optimized.It must have an input for a
numericvector, the initial values from where the optimizer starts.It must have a
...argument for additional parameters passed on to the objective function.The output must be a named
list, including the optimal function value and the optimal parameter vector.
Active bindings
labelA
character, the label for the optimizer.algorithmA
function, the optimization algorithm.arg_objectiveA
character, the argument name for the objective function inalgorithm.arg_initialA
character, the argument name for the initial values inalgorithm.out_valueA
character, the element name for the optimal function value in the outputlistofalgorithm.out_parameterA
character, the element name for the optimal parameters in the outputlistofalgorithm.directionEither
"min"(if the optimizer minimizes) or"max"(if the optimizer maximizes).argumentsA named
listof custom arguments foralgorithm. Defaults are used for arguments that are not specified.secondsA
numeric, a time limit in seconds. Optimization is interrupted prematurely ifsecondsis exceeded.No time limit if
seconds = Inf(the default).Note the limitations documented in
setTimeLimit.hide_warningsEither
TRUEto hide warnings during optimization, orFALSE(default) else.output_ignoreA
charactervectorof elements to ignore in the optimization output.
Methods
Public methods
Method new()
Initializes a new Optimizer object.
Usage
Optimizer$new(which, ...)
Arguments
whichA
character, either one ofoptimizer_dictionary$keysor"custom"(in which case$definition()must be used to define the optimizer details)....Optionally additional arguments to be passed to the optimizer algorithm. Without specifications, default values are used.
Returns
A new Optimizer object.
Method definition()
Defines an optimizer.
Usage
Optimizer$definition( algorithm, arg_objective, arg_initial, out_value, out_parameter, direction )
Arguments
algorithmA
function, the optimization algorithm.arg_objectiveA
character, the argument name for the objective function inalgorithm.arg_initialA
character, the argument name for the initial values inalgorithm.out_valueA
character, the element name for the optimal function value in the outputlistofalgorithm.out_parameterA
character, the element name for the optimal parameters in the outputlistofalgorithm.directionEither
"min"(if the optimizer minimizes) or"max"(if the optimizer maximizes).
Returns
Invisibly the Optimizer object.
Method set_arguments()
Sets optimizer arguments.
Usage
Optimizer$set_arguments(...)
Arguments
...Optionally additional arguments to be passed to the optimizer algorithm. Without specifications, default values are used.
Returns
The Optimizer object.
Method validate()
Validates the Optimizer object. A time limit in seconds for
the optimization can be set via the $seconds field.
Usage
Optimizer$validate( objective = optimizeR::test_objective, initial = round(stats::rnorm(2)), ..., direction = "min" )
Arguments
objectiveA
functionto be optimized thathas at least one argument that receives a
numericvectorand returns a single
numericvalue.
Alternatively, it can also be a
Objectiveobject for more flexibility.initialA
numericvector with starting parameter values for the optimization....Optionally additional arguments to be passed to the optimizer algorithm. Without specifications, default values are used.
directionEither
"min"for minimization or"max"for maximization.
Returns
The Optimizer object.
Method minimize()
Performing minimization.
Usage
Optimizer$minimize(objective, initial, ...)
Arguments
objectiveA
functionto be optimized thathas at least one argument that receives a
numericvectorand returns a single
numericvalue.
Alternatively, it can also be a
Objectiveobject for more flexibility.initialA
numericvector with starting parameter values for the optimization....Optionally additional arguments to be passed to the optimizer algorithm. Without specifications, default values are used.
Returns
A named list, containing at least these five elements:
valueA
numeric, the minimum function value.parameterA
numericvector, the parameter vector where the minimum is obtained.secondsA
numeric, the optimization time in seconds.initialA
numeric, the initial parameter values.errorEither
TRUEif an error occurred, orFALSE, else.
Appended are additional output elements of the optimizer.
If an error occurred, then the error message is also appended as element
error_message.
If the time limit was exceeded, this also counts as an error. In addition,
the flag time_out = TRUE is appended.
Examples
Optimizer$new("stats::nlm")$
minimize(objective = function(x) x^4 + 3*x - 5, initial = 2)
Method maximize()
Performing maximization.
Usage
Optimizer$maximize(objective, initial, ...)
Arguments
objectiveA
functionto be optimized thathas at least one argument that receives a
numericvectorand returns a single
numericvalue.
Alternatively, it can also be a
Objectiveobject for more flexibility.initialA
numericvector with starting parameter values for the optimization....Optionally additional arguments to be passed to the optimizer algorithm. Without specifications, default values are used.
Returns
A named list, containing at least these five elements:
valueA
numeric, the maximum function value.parameterA
numericvector, the parameter vector where the maximum is obtained.secondsA
numeric, the optimization time in seconds.initialA
numeric, the initial parameter values.errorEither
TRUEif an error occurred, orFALSE, else.
Appended are additional output elements of the optimizer.
If an error occurred, then the error message is also appended as element
error_message.
If the time limit was exceeded, this also counts as an error. In addition,
the flag time_out = TRUE is appended.
Examples
Optimizer$new("stats::nlm")$
maximize(objective = function(x) -x^4 + 3*x - 5, initial = 2)
Method optimize()
Performing minimization or maximization.
Usage
Optimizer$optimize(objective, initial, direction = "min", ...)
Arguments
objectiveA
functionto be optimized thathas at least one argument that receives a
numericvectorand returns a single
numericvalue.
Alternatively, it can also be a
Objectiveobject for more flexibility.initialA
numericvector with starting parameter values for the optimization.directionEither
"min"for minimization or"max"for maximization....Optionally additional arguments to be passed to the optimizer algorithm. Without specifications, default values are used.
Returns
A named list, containing at least these five elements:
valueA
numeric, the maximum function value.parameterA
numericvector, the parameter vector where the maximum is obtained.secondsA
numeric, the optimization time in seconds.initialA
numeric, the initial parameter values.errorEither
TRUEif an error occurred, orFALSE, else.
Appended are additional output elements of the optimizer.
If an error occurred, then the error message is also appended as element
error_message.
If the time limit was exceeded, this also counts as an error. In addition,
the flag time_out = TRUE is appended.
Examples
objective <- function(x) -x^4 + 3*x - 5
optimizer <- Optimizer$new("stats::nlm")
optimizer$optimize(objective = objective, initial = 2, direction = "min")
optimizer$optimize(objective = objective, initial = 2, direction = "max")
Method print()
Prints the optimizer label.
Usage
Optimizer$print(...)
Arguments
...Optionally additional arguments to be passed to the optimizer algorithm. Without specifications, default values are used.
Returns
Invisibly the Optimizer object.
Method clone()
The objects of this class are cloneable with this method.
Usage
Optimizer$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Examples
### Task: compare minimization with 'stats::nlm' and 'pracma::nelder_mead'
# 1. define objective function and initial values
objective <- TestFunctions::TF_ackley
initial <- c(3, 3)
# 2. get overview of optimizers in dictionary
optimizer_dictionary$keys
# 3. define 'nlm' optimizer
nlm <- Optimizer$new(which = "stats::nlm")
# 4. define the 'pracma::nelder_mead' optimizer (not contained in the dictionary)
nelder_mead <- Optimizer$new(which = "custom")
nelder_mead$definition(
algorithm = pracma::nelder_mead, # the optimization function
arg_objective = "fn", # the argument name for the objective function
arg_initial = "x0", # the argument name for the initial values
out_value = "fmin", # the element for the optimal function value in the output
out_parameter = "xmin", # the element for the optimal parameters in the output
direction = "min" # the optimizer minimizes
)
# 5. compare the minimization results
nlm$minimize(objective, initial)
nelder_mead$minimize(objective, initial)
## ------------------------------------------------
## Method `Optimizer$minimize`
## ------------------------------------------------
Optimizer$new("stats::nlm")$
minimize(objective = function(x) x^4 + 3*x - 5, initial = 2)
## ------------------------------------------------
## Method `Optimizer$maximize`
## ------------------------------------------------
Optimizer$new("stats::nlm")$
maximize(objective = function(x) -x^4 + 3*x - 5, initial = 2)
## ------------------------------------------------
## Method `Optimizer$optimize`
## ------------------------------------------------
objective <- function(x) -x^4 + 3*x - 5
optimizer <- Optimizer$new("stats::nlm")
optimizer$optimize(objective = objective, initial = 2, direction = "min")
optimizer$optimize(objective = objective, initial = 2, direction = "max")