Title: | Unified Framework for Numerical Optimizers |
---|---|
Description: | Provides a unified object-oriented framework for numerical optimizers in R. Allows for both minimization and maximization with any optimizer, optimization over more than one function argument, measuring of computation time, setting a time limit for long optimization tasks. |
Authors: | Lennart Oelschläger [aut, cre] , Marius Ötting [ctb] |
Maintainer: | Lennart Oelschläger <[email protected]> |
License: | GPL (>= 3) |
Version: | 1.1.2 |
Built: | 2024-12-27 06:00:08 UTC |
Source: | https://github.com/loelschlaeger/optimizer |
optimizer
objectThis function performs numerical optimization using an optimizer
object.
apply_optimizer(optimizer = optimizer_nlm(), objective, initial, ...)
apply_optimizer(optimizer = optimizer_nlm(), objective, initial, ...)
optimizer |
An object of class |
objective |
A |
initial |
A |
... |
Additional arguments to be passed to |
A named list
, containing at least these four elements:
value
A numeric
, the value of the estimated optimum of
objective
.
parameter
A numeric
vector, the parameter vector where
the optimum of objective
is obtained.
seconds
A numeric
, the total optimization time in
seconds.
initial
A numeric
, the initial parameter values.
Appended are additional output elements of the optimizer (if not excluded by
the output_ignore
element via define_optimizer
).
define_optimizer()
for creating an optimizer
object.
apply_optimizer(optimizer_nlm(), function(x) x^4 + 3*x - 5, 2)
apply_optimizer(optimizer_nlm(), function(x) x^4 + 3*x - 5, 2)
This function specifies the framework for a numerical optimizer.
Two wrappers for well-known optimizers are already available:
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() )
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() )
.optimizer |
A
|
.objective |
A |
.initial |
A |
.value |
A |
.parameter |
A |
.direction |
A |
... |
Additional arguments to be passed to the optimizer. Without specifications, the default values of the optimizer are used. |
.output_ignore |
A |
.validate |
A |
.validation_settings |
Ignored if
|
An optimizer
object.
An optimizer
object is a list
of six elements:
A function
, the optimization algorithm.
A character
, the name of
optimizer
.
A named list
, where each element
is an additional function argument for optimizer
.
Either "min"
if the optimizer minimizes
or "max"
if the optimizer maximizes.
A named list
of four
character
:
the name of the function input of optimizer
the name of the starting parameter values input of
optimizer
the name of the optimal function value in the output list
of optimizer
the name of the optimal parameter vector in the
output list of optimizer
.
A character
vector of element
names in the output list
of optimizer
that are ignored.
The elements value
and parameter
are added automatically to
output_ignore
, because they are saved
separately, see the output documentation of apply_optimizer
.
Use apply_optimizer()
to apply an optimizer
object for numerical
optimization.
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 )
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 )
The Objective
object specifies the framework for an objective function
for numerical optimization.
An Objective
object.
objective_name
A character
, a label for the objective function.
fixed_arguments
A character
, the names of the fixed arguments (if any).
seconds
A numeric
, a time limit in seconds. Computations are interrupted
prematurely if seconds
is exceeded.
No time limit if seconds = Inf
(the default).
Note the limitations documented in setTimeLimit
.
hide_warnings
Either TRUE
to hide warnings when evaluating the objective function,
or FALSE
(default) if not.
verbose
Either TRUE
(default) to print status messages, or FALSE
to hide those.
npar
An integer
vector, defining the length of each target argument.
output_template
A template of the expected output value, used for the validate
method.
new()
Creates a new Objective
object.
Objective$new(f, target = NULL, npar, ...)
f
A function
to be optimized.
It is expected that f
has at least one numeric
argument.
Further, it is expected that the return value of f
is of the
structure numeric(1)
, i.e. a single numeric
value (although
this can be altered via the output_template
field).
target
A character
, the argument name(s) of f
that get optimized.
All target arguments must receive a numeric
vector
.
Can be NULL
(default), then it is the first argument of f
.
npar
A integer
of the same length as target
, defining the length
of the respective numeric
vector
argument.
...
Optionally additional arguments to f
that are fixed during
the optimization.
A new Objective
object.
set_argument()
Set a fixed function argument.
Objective$set_argument(..., overwrite = TRUE, verbose = self$verbose)
...
Optionally additional arguments to f
that are fixed during
the optimization.
overwrite
Either TRUE
(default) to allow overwriting, or FALSE
if not.
verbose
Either TRUE
(default) to print status messages, or FALSE
to hide those.
Invisibly the Objective
object.
get_argument()
Get a fixed function argument.
Objective$get_argument(argument_name, verbose = self$verbose)
argument_name
A character
, a name of an argument for f
.
verbose
Either TRUE
(default) to print status messages, or FALSE
to hide those.
The argument value.
remove_argument()
Remove a fixed function argument.
Objective$remove_argument(argument_name, verbose = self$verbose)
argument_name
A character
, a name of an argument for f
.
verbose
Either TRUE
(default) to print status messages, or FALSE
to hide those.
Invisibly the Objective
object.
validate()
Validate an Objective
object.
Objective$validate(.at)
.at
A numeric
of length sum(self$npar)
, the values for the target
arguments written in a single vector.
Invisibly the Objective
object.
evaluate()
Evaluate the objective function.
Objective$evaluate(.at, .negate = FALSE, ...)
.at
A numeric
of length sum(self$npar)
, the values for the target
arguments written in a single vector.
.negate
Either TRUE
to negate the numeric
return value of
f
, or FALSE
(default) else.
...
Optionally additional arguments to f
that are fixed during
the optimization.
The objective value.
print()
Print details of the Objective
object.
Objective$print()
Invisibly the Objective
object.
clone()
The objects of this class are cloneable with this method.
Objective$clone(deep = FALSE)
deep
Whether to make a deep clone.
### define log-likelihood function of Gaussian mixture model llk <- function(mu, sd, lambda, data) { sd <- exp(sd) lambda <- plogis(lambda) cluster_1 <- lambda * dnorm(data, mu[1], sd[1]) cluster_2 <- (1 - lambda) * dnorm(data, mu[2], sd[2]) sum(log(cluster_1 + cluster_2)) } ### the log-likelihood function is supposed to be optimized over the first ### three arguments, the 'data' argument is constant objective <- Objective$new( f = llk, target = c("mu", "sd", "lambda"), npar = c(2, 2, 1), data = faithful$eruptions ) ### evaluate the objective function at 1:5 (1:2 is passed to mu, 3:4 to sd, ### and 5 to lambda) objective$evaluate(1:5)
### define log-likelihood function of Gaussian mixture model llk <- function(mu, sd, lambda, data) { sd <- exp(sd) lambda <- plogis(lambda) cluster_1 <- lambda * dnorm(data, mu[1], sd[1]) cluster_2 <- (1 - lambda) * dnorm(data, mu[2], sd[2]) sum(log(cluster_1 + cluster_2)) } ### the log-likelihood function is supposed to be optimized over the first ### three arguments, the 'data' argument is constant objective <- Objective$new( f = llk, target = c("mu", "sd", "lambda"), npar = c(2, 2, 1), data = faithful$eruptions ) ### evaluate the objective function at 1:5 (1:2 is passed to mu, 3:4 to sd, ### and 5 to lambda) objective$evaluate(1:5)
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 numeric
vector, 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.
label
A character
, the label for the optimizer.
algorithm
A function
, the optimization algorithm.
arg_objective
A character
, the argument name for the objective function in
algorithm
.
arg_initial
A character
, the argument name for the initial values in
algorithm
.
out_value
A character
, the element name for the optimal function value in
the output list
of algorithm
.
out_parameter
A character
, the element name for the optimal parameters in the
output list
of algorithm
.
direction
Either "min"
(if the optimizer minimizes) or "max"
(if the optimizer maximizes).
arguments
A named list
of custom arguments for algorithm
. Defaults
are used for arguments that are not specified.
seconds
A numeric
, a time limit in seconds. Optimization is interrupted
prematurely if seconds
is exceeded.
No time limit if seconds = Inf
(the default).
Note the limitations documented in setTimeLimit
.
hide_warnings
Either TRUE
to hide warnings during optimization, or FALSE
(default) else.
output_ignore
A character
vector
of elements to ignore in the
optimization output.
new()
Initializes a new Optimizer
object.
Optimizer$new(which, ...)
which
A character
, either one of optimizer_dictionary$keys
or
"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.
A new Optimizer
object.
definition()
Defines an optimizer.
Optimizer$definition( algorithm, arg_objective, arg_initial, out_value, out_parameter, direction )
algorithm
A function
, the optimization algorithm.
arg_objective
A character
, the argument name for the objective function in
algorithm
.
arg_initial
A character
, the argument name for the initial values in
algorithm
.
out_value
A character
, the element name for the optimal function value in
the output list
of algorithm
.
out_parameter
A character
, the element name for the optimal parameters in the
output list
of algorithm
.
direction
Either "min"
(if the optimizer minimizes) or "max"
(if the optimizer maximizes).
Invisibly the Optimizer
object.
set_arguments()
Sets optimizer arguments.
Optimizer$set_arguments(...)
...
Optionally additional arguments to be passed to the optimizer algorithm. Without specifications, default values are used.
The Optimizer
object.
validate()
Validates the Optimizer
object. A time limit in seconds for
the optimization can be set via the $seconds
field.
Optimizer$validate( objective = optimizeR::test_objective, initial = round(stats::rnorm(2)), ..., direction = "min" )
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 a Objective
object for more
flexibility.
initial
A numeric
vector with starting parameter values for the optimization.
...
Optionally additional arguments to be passed to the optimizer algorithm. Without specifications, default values are used.
direction
Either "min"
for minimization or "max"
for maximization.
The Optimizer
object.
minimize()
Performing minimization.
Optimizer$minimize(objective, initial, ...)
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 a Objective
object for more
flexibility.
initial
A numeric
vector with starting parameter values for the optimization.
...
Optionally additional arguments to be passed to the optimizer algorithm. Without specifications, default values are used.
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 also counts as an error. In addition,
the flag time_out = TRUE
is appended.
Optimizer$new("stats::nlm")$ minimize(objective = function(x) x^4 + 3*x - 5, initial = 2)
maximize()
Performing maximization.
Optimizer$maximize(objective, initial, ...)
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 a Objective
object for more
flexibility.
initial
A numeric
vector with starting parameter values for the optimization.
...
Optionally additional arguments to be passed to the optimizer algorithm. Without specifications, default values are used.
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.
Optimizer$new("stats::nlm")$ maximize(objective = function(x) -x^4 + 3*x - 5, initial = 2)
optimize()
Performing minimization or maximization.
Optimizer$optimize(objective, initial, direction = "min", ...)
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 a Objective
object for more
flexibility.
initial
A numeric
vector with starting parameter values for the optimization.
direction
Either "min"
for minimization or "max"
for maximization.
...
Optionally additional arguments to be passed to the optimizer algorithm. Without specifications, default values are used.
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.
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")
print()
Prints the optimizer label.
Optimizer$print(...)
...
Optionally additional arguments to be passed to the optimizer algorithm. Without specifications, default values are used.
Invisibly the Optimizer
object.
clone()
The objects of this class are cloneable with this method.
Optimizer$clone(deep = FALSE)
deep
Whether to make a deep clone.
### 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")
### 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")
The optimizer_dictionary
object is a dictionary of currently implemented
numerical optimizer functions.
optimizer_dictionary
optimizer_dictionary
An R6
object of class Dictionary
.
This R6 object manages two related parameter spaces: the Optimization Space (for optimization) and the Interpretation Space (for easier interpretation).
In the Optimization Space, parameters are stored as a numeric
vector
, the standard format for numerical optimizers. Parameters in
this space are typically identified.
In the Interpretation Space, parameters are stored as a list
and can
take different formats (e.g., matrix
). Parameters here do not need to
be identified.
The user can define transformation functions (not necessarily bijective) to
switch between these two spaces via the $o2i()
and $i2o()
methods.
new()
Initializes a new ParameterSpaces
object.
ParameterSpaces$new(parameter_names, parameter_lengths_in_o_space)
parameter_names
[character()
]
Unique names for the parameters.
parameter_lengths_in_o_space
[integer()
]
The length of each parameter in the optimization space.
A new ParameterSpaces
object.
print()
Print an overview of the parameter spaces.
ParameterSpaces$print(show_transformer = FALSE)
show_transformer
[logical(1)
]
Show transformer functions in the output?
switch()
Switch between Optimization Space and Interpretation Space.
ParameterSpaces$switch(x, to = NULL)
x
[numeric()
| list()
]
The parameters, either as a numeric vector
(will be switched to
Interpretation Space), or as a list()
(will be switched to Optimization
Space).
to
[character(1)
| NULL
]
Explicitly switch to a specific space, either
"o"
: Optimization Space
"i"
: Interpretation Space
If NULL
, the function will switch to the other space.
o2i()
Define transformation functions when switching from Optimization Space to Interpretation Space.
ParameterSpaces$o2i(...)
...
[function
]
One or more transformation functions, named according to the parameters.
Transformers from Optimization Space to Interpretation Space (o2i)
must receive a numeric
. The default is the identity.
i2o()
Define transformation functions when switching from Interpretation Space to Optimization Space.
ParameterSpaces$i2o(...)
...
[function
]
One or more transformers functions, named according to the parameters.
Transformers from Interpretation Space to Optimization Space (i2o)
must return a numeric
. The default is as.vector()
.
clone()
The objects of this class are cloneable with this method.
ParameterSpaces$clone(deep = FALSE)
deep
Whether to make a deep clone.
### Log-likelihood function of two-class Gaussian mixture model with ### parameter vector `theta` that consists of ### - `mu`, mean vector of length 2 ### - `sd`, standard deviation vector of length 2, must be positive ### - `lambda`, class probability of length 1, must be between 0 and 1 normal_mixture_llk <- function(theta, data) { mu <- theta[1:2] sd <- exp(theta[3:4]) lambda <- plogis(theta[5]) c1 <- lambda * dnorm(data, mu[1], sd[1]) c2 <- (1 - lambda) * dnorm(data, mu[2], sd[2]) sum(log(c1 + c2)) } ### define parameter spaces ### - `mu` needs no transformation ### - `sd` needs to be real in optimization space and positive in ### interpretation space ### - `lambda` needs to be real and of length `1` in optimization space, and ### a probability vector of length `2` in interpretation space normal_mixture_spaces <- ParameterSpaces$ new( parameter_names = c("mu", "sd", "lambda"), parameter_lengths_in_o_space = c(2, 2, 1) )$ o2i( "mu" = function(x) x, "sd" = function(x) exp(x), "lambda" = function(x) c(plogis(x), 1 - plogis(x)) )$ i2o( "mu" = function(x) x, "sd" = function(x) log(x), "lambda" = function(x) qlogis(x[1]) ) ### switch between parameter spaces par <- list( # parameters in interpretation space "mu" = c(2, 4), "sd" = c(0.5, 1), "lambda" = c(0.4, 0.6) ) (x <- normal_mixture_spaces$switch(par)) # switch to optimization space normal_mixture_llk( theta = x, data = datasets::faithful$eruptions ) normal_mixture_spaces$switch(x) # switch back
### Log-likelihood function of two-class Gaussian mixture model with ### parameter vector `theta` that consists of ### - `mu`, mean vector of length 2 ### - `sd`, standard deviation vector of length 2, must be positive ### - `lambda`, class probability of length 1, must be between 0 and 1 normal_mixture_llk <- function(theta, data) { mu <- theta[1:2] sd <- exp(theta[3:4]) lambda <- plogis(theta[5]) c1 <- lambda * dnorm(data, mu[1], sd[1]) c2 <- (1 - lambda) * dnorm(data, mu[2], sd[2]) sum(log(c1 + c2)) } ### define parameter spaces ### - `mu` needs no transformation ### - `sd` needs to be real in optimization space and positive in ### interpretation space ### - `lambda` needs to be real and of length `1` in optimization space, and ### a probability vector of length `2` in interpretation space normal_mixture_spaces <- ParameterSpaces$ new( parameter_names = c("mu", "sd", "lambda"), parameter_lengths_in_o_space = c(2, 2, 1) )$ o2i( "mu" = function(x) x, "sd" = function(x) exp(x), "lambda" = function(x) c(plogis(x), 1 - plogis(x)) )$ i2o( "mu" = function(x) x, "sd" = function(x) log(x), "lambda" = function(x) qlogis(x[1]) ) ### switch between parameter spaces par <- list( # parameters in interpretation space "mu" = c(2, 4), "sd" = c(0.5, 1), "lambda" = c(0.4, 0.6) ) (x <- normal_mixture_spaces$switch(par)) # switch to optimization space normal_mixture_llk( theta = x, data = datasets::faithful$eruptions ) normal_mixture_spaces$switch(x) # switch back
This function is useful for testing or debugging the behavior of objective functions. It can throw a warning and / or an error on purpose.
test_objective( x, value = x, warning_prob = 0, error_prob = 0, warning_msg = "warning", error_msg = "error", call. = TRUE )
test_objective( x, value = x, warning_prob = 0, error_prob = 0, warning_msg = "warning", error_msg = "error", call. = TRUE )
x |
Any input. |
value |
The return value, any object. |
warning_prob |
The probability for throwing a warning. |
error_prob |
The probability for throwing an error. |
warning_msg |
The warning message. |
error_msg |
The error message. |
call. |
The argument value
.
This function is useful for testing or debugging the behavior of optimization functions. It can throw a warning and / or an error on purpose.
test_optimizer( objective = test_objective, initial = 1, ..., parameter = 1, value = objective(parameter), seconds = 0, warning_prob = 0, error_prob = 0, warning_msg = "warning", error_msg = "error", call. = TRUE )
test_optimizer( objective = test_objective, initial = 1, ..., parameter = 1, value = objective(parameter), seconds = 0, warning_prob = 0, error_prob = 0, warning_msg = "warning", error_msg = "error", call. = TRUE )
objective |
An objective |
initial |
The initial parameter vector. |
... |
Optionally additional arguments to be passed to |
parameter |
Defines the output |
value |
Defines the output |
seconds |
A delay in number of seconds. |
warning_prob |
The probability for throwing a warning. |
error_prob |
The probability for throwing an error. |
warning_msg |
The warning message. |
error_msg |
The error message. |
call. |
A list
with elements parameter
and value
.