Method new()
Initializes a new Optimizer
object.
Usage
Optimizer$new(which, ..., .verbose = TRUE)
Arguments
which
[character(1)
]
Either:
one of optimizer_dictionary$keys
or "custom"
, in which case $definition()
must be used to
define the optimizer details.
...
[any
]
Optionally additional named arguments to be passed to the optimizer
algorithm. Without specifications, default values of the optimizer are used.
.verbose
[logical(1)
]
Print status messages?
Returns
A new Optimizer
object.
Method definition()
Defines an optimizer.
Usage
Optimizer$definition(
algorithm,
arg_objective,
arg_initial,
arg_lower = NA,
arg_upper = NA,
arg_gradient = NA,
arg_hessian = NA,
gradient_as_attribute = FALSE,
hessian_as_attribute = FALSE,
out_value,
out_parameter,
direction
)
Arguments
algorithm
[function
]
The optimization algorithm.
arg_objective
[character(1)
]
The argument name for the objective function in algorithm
.
arg_initial
[character(1)
]
The argument name for the initial values in algorithm
.
arg_lower
[character(1)
| NA
]
Optionally the argument name for the lower parameter bound in
algorithm
.
Can be NA
if not available.
arg_upper
[character(1)
| NA
]
Optionally the argument name for the upper parameter bound in
algorithm
.
Can be NA
if not available.
arg_gradient
[character(1)
| NA
]
Optionally the argument name for the gradient function in
algorithm
.
Can be NA
if not available.
arg_hessian
[character(1)
| NA
]
Optionally the argument name for the Hessian function in
algorithm
.
Can be NA
if not available.
gradient_as_attribute
[logical(1)
]
Only relevant if arg_gradient
is not NA
.
In that case, does algorithm
expect that the gradient is an attribute
of the objective function output (as for example in
nlm
)? In that case, arg_gradient
defines the
attribute name.
hessian_as_attribute
[logical(1)
]
Only relevant if arg_hessian
is not NA
.
In that case, does algorithm
expect that the Hessian is an attribute
of the objective function output (as for example in
nlm
)? In that case, arg_hessian
defines the
attribute name.
out_value
[character(1)
]
The element name for the optimal function value in the output list
of algorithm
.
out_parameter
[character(1)
]
The element name for the optimal parameters in the output list
of
algorithm
.
direction
[character(1)
]
Either "min"
(if the optimizer minimizes) or "max"
(if the optimizer maximizes).
Method set_arguments()
Sets optimizer arguments.
Usage
Optimizer$set_arguments(...)
Arguments
...
[any
]
Optionally additional named arguments to be passed to the optimizer
algorithm. Without specifications, default values of the optimizer are used.
Returns
The Optimizer
object.
Method minimize()
Performing minimization.
Usage
Optimizer$minimize(objective, initial, lower = NA, upper = NA, ...)
Arguments
objective
[function
| Objective
]
A function
to be optimized that
has at least one argument that receives a numeric
vector
and returns a single numeric
value.
Alternatively, it can also be an Objective
object for more
flexibility.
initial
[numeric()
]
Starting parameter values for the optimization.
lower
[NA
| numeric()
| numeric(1)
]
Lower bounds on the parameters.
If a single number, this will be applied to all parameters.
Can be NA
to not define any bounds.
upper
[NA
| numeric()
| numeric(1)
]
Upper bounds on the parameters.
If a single number, this will be applied to all parameters.
Can be NA
to not define any bounds.
...
[any
]
Optionally additional named arguments to be passed to the optimizer
algorithm. Without specifications, default values of the optimizer are used.
Returns
A named list
, containing at least these five elements:
value
A numeric
, the minimum function value.
parameter
A numeric
vector, the parameter vector
where the minimum is obtained.
seconds
A numeric
, the optimization time in seconds.
initial
A numeric
, the initial parameter values.
error
Either TRUE
if an error occurred, or FALSE
, 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 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, lower = NA, upper = NA, ...)
Arguments
objective
[function
| Objective
]
A function
to be optimized that
has at least one argument that receives a numeric
vector
and returns a single numeric
value.
Alternatively, it can also be an Objective
object for more
flexibility.
initial
[numeric()
]
Starting parameter values for the optimization.
lower
[NA
| numeric()
| numeric(1)
]
Lower bounds on the parameters.
If a single number, this will be applied to all parameters.
Can be NA
to not define any bounds.
upper
[NA
| numeric()
| numeric(1)
]
Upper bounds on the parameters.
If a single number, this will be applied to all parameters.
Can be NA
to not define any bounds.
...
[any
]
Optionally additional named arguments to be passed to the optimizer
algorithm. Without specifications, default values of the optimizer are used.
Returns
A named list
, containing at least these five elements:
value
A numeric
, the maximum function value.
parameter
A numeric
vector, the parameter vector
where the maximum is obtained.
seconds
A numeric
, the optimization time in seconds.
initial
A numeric
, the initial parameter values.
error
Either TRUE
if an error occurred, or FALSE
, 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)
Performing minimization or maximization.
Usage
Optimizer$optimize(
objective,
initial,
lower = NA,
upper = NA,
direction = "min",
...
)
Arguments
objective
[function
| Objective
]
A function
to be optimized that
has at least one argument that receives a numeric
vector
and returns a single numeric
value.
Alternatively, it can also be an Objective
object for more
flexibility.
initial
[numeric()
]
Starting parameter values for the optimization.
lower
[NA
| numeric()
| numeric(1)
]
Lower bounds on the parameters.
If a single number, this will be applied to all parameters.
Can be NA
to not define any bounds.
upper
[NA
| numeric()
| numeric(1)
]
Upper bounds on the parameters.
If a single number, this will be applied to all parameters.
Can be NA
to not define any bounds.
direction
[character(1)
]
Either "min"
for minimization or "max"
for maximization.
...
[any
]
Optionally additional named arguments to be passed to the optimizer
algorithm. Without specifications, default values of the optimizer are used.
Returns
A named list
, containing at least these five elements:
value
A numeric
, the maximum function value.
parameter
A numeric
vector, the parameter vector
where the maximum is obtained.
seconds
A numeric
, the optimization time in seconds.
initial
A numeric
, the initial parameter values.
error
Either TRUE
if an error occurred, or FALSE
, 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")
Prints the optimizer label.
Usage
Optimizer$print(...)
Arguments
...
[any
]
Optionally additional named arguments to be passed to the optimizer
algorithm. Without specifications, default values of the optimizer 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
deep
Whether to make a deep clone.