A Nop
object defines a Numerical Optimization Problem.
Value
A Nop
object, which is an R6 class that specifies the numerical
optimization problem, stores optimization results, and provides methods
for analyzing the results, see the details.
Getting Started
Step 1: Create a new Nop
object:
Call object <- Nop$new(f, npar, ...)
where
f
is the function to be optimized over its first argument,npar
is the length of the first argument off
,and
...
are additional arguments forf
.
Step 2: Specify numerical optimizer:
Call object$set_optimizer(<optimizer object>)
, where
<optimizer object>
is an object of class optimizer
, which can
be created with the define_optimizer
function from
the {optimizeR} package.
Two optimizer objects are already available:
Optimization
Call object$evaluate()
to evaluate the target function at some point.
Call object$optimize()
for optimization. See also the methods
object$standardize()
, object$reduce()
, and
object$continue()
for initialization strategies.
Analysis of the results
The following are methods for the analysis of optimization results, with different filter options for optimization runs, optimizers, and elements:
$results()
returns all saved optimization results,$summary()
summarizes the results,$optima()
returns a frequency table of identified optima,$plot()
visualizes the optimization time or value,$best_parameter()
returns the parameter vector at which the optimum value is obtained,$best_value()
returns the found optimum value off
,$closest_parameter()
returns parameter closest to a specified value.
Active bindings
f
The
function
to be optimized.f_name
The name of the
function
to be optimized.f_target
The name of the target argument, i.e., the argument over which
f
is optimized.npar
The length of the target argument, i.e., the argument over which
f
is optimized.arguments
A
list
of specified additional arguments forf
.true_value
The true
numeric
optimum value off
(if available).true_parameter
The true optimum
numeric
parameter vector of lengthnpar
(if available), i.e., the point wheref
obtains its optimum.minimized
A
logical
, set toTRUE
(default) to show best minimum in$best_value()
,$best_parameter()
, and$optima()
.optimizer
A
list
of specified optimizers.new_label
A
character
, a new optimization label that has not been used yet.
Methods
Method new()
Creates a new Nop
object.
Usage
Nop$new(f, npar, ...)
Method print()
Prints details of the numerical optimization problem.
Usage
Nop$print(digits = getOption("ino_digits", default = 2), ...)
Method set_optimizer()
Sets a numerical optimizer.
Arguments
optimizer
An object of class
optimizer
, which can be created with thedefine_optimizer
function from the {optimizeR} package.label
A
character
, a unique label for the optimizer. By defaultlabel = NULL
, in which case the default label saved insideoptimizer
is used.
Method remove_optimizer()
Removes numerical optimizer.
Usage
Nop$remove_optimizer(
which_optimizer,
verbose = getOption("ino_verbose", default = TRUE)
)
Arguments
which_optimizer
Select specified numerical optimizers. Either:
"all"
, all specified optimizers,"removed"
, all removed optimizers,"active"
, all active optimizers (i.e., not removed),a
character
(vector) of specified optimizer labels,a
numeric
(vector) of optimizer ids (see$print()
output).
verbose
A
logical
, which indicates whether progress/details should be printed. Set toTRUE
(FALSE
) to print (hide) such messages. The default isTRUE
.
Method evaluate()
Evaluates the function.
Usage
Nop$evaluate(at = rnorm(self$npar), time_limit = NULL, hide_warnings = FALSE)
Arguments
at
A
numeric
vector of lengthnpar
, the point where the function is evaluated. Per default,at = rnorm(self$npar)
, i.e., random values drawn from a standard normal distribution.time_limit
An
integer
, the time limit in seconds for computations. No time limit iftime_limit = NULL
(the default). This currently only works reliably under Windows.hide_warnings
A
logical
. Set toTRUE
(FALSE
) to hide (show) warning messages.
Method optimize()
Optimizes the function.
Usage
Nop$optimize(
initial = "random",
runs = 1,
which_optimizer = "all",
seed = NULL,
return_results = FALSE,
save_results = TRUE,
label = self$new_label,
ncores = getOption("ino_ncores", default = 1),
verbose = getOption("ino_verbose", default = TRUE),
simplify = TRUE,
time_limit = NULL,
hide_warnings = TRUE,
fail_bad_initial = TRUE
)
Arguments
initial
Specify the initial point where the optimizer should start. Either:
the
character
"random"
(the default) for random initial values drawn from a standard normal distribution,a
numeric
vector of lengthnpar
, the starting point for optimization,a
list
of such vectors (in this case,runs
is set to the length of thelist
),or a
function
without any arguments that returns anumeric
vector of lengthnpar
. In all these cases, the same initial values are used for each optimizer. For more flexibility, afuntion
forinitial
can have two arguments, where the first argument specifies the optimization run, and the second argument specifies the optimizer.
runs
An
integer
, the number of optimization runs. By default,runs = 1
.which_optimizer
Select specified numerical optimizers. Either:
"all"
, all specified optimizers,"removed"
, all removed optimizers,"active"
, all active optimizers (i.e., not removed),a
character
(vector) of specified optimizer labels,a
numeric
(vector) of optimizer ids (see$print()
output).
seed
An
integer
, passed toset.seed
for reproducibility. Can beNULL
for no seed, which is the default.return_results
A
logical
, which indicates whether the optimization results should be returned as alist
. By default,return_results = FALSE
.save_results
A
logical
, which indicates whether the optimization results should be saved inside theNop
object. By default,save_results = TRUE
.label
Only relevant if
save_results = TRUE
. In this case, an optionalcharacter
to specify a custom label of the optimization. By default,label = self$new_label
creates a new label. Labels can be useful to distinguish optimization runs later.ncores
An
integer
, the number of CPU cores for parallel computation. The default is1
. You can useparallel::detectCores()
to detect the number of available CPU cores.verbose
A
logical
, which indicates whether progress/details should be printed. Set toTRUE
(FALSE
) to print (hide) such messages. The default isTRUE
.simplify
Only relevant if
return_results = TRUE
andruns = 1
and/or only one optimizer is specified. In this case, ifsimplify = TRUE
, the nested list output of optimization results is flattened if possible.time_limit
An
integer
, the time limit in seconds for computations. No time limit iftime_limit = NULL
(the default). This currently only works reliably under Windows.hide_warnings
A
logical
. Set toTRUE
(FALSE
) to hide (show) warning messages.fail_bad_initial
Either
TRUE
to immediately fail ifinitial
contains any misspecifications (default), orFALSE
to include the failed runs in the results.
Returns
The return value depends on the value of return_results
:
if
return_results = FALSE
, invisibly theNop
object,if
return_results = TRUE
, a nestedlist
of optimization results. Each element corresponds to one optimization run and is alist
of results for each optimizer. The results for each optimizer is alist
, the output ofapply_optimizer
. Ifsimplify = TRUE
, the output is flattened if possible.
Method test()
Validates the configuration of a Nop
object.
Arguments
at
A
numeric
of lengthnpar
, the point at which the functionf
and the specified optimizer are tested. Per default,at = rnorm(self$npar)
, i.e., random values drawn from a standard normal distribution.which_optimizer
Select specified numerical optimizers. Either:
"all"
, all specified optimizers,"removed"
, all removed optimizers,"active"
, all active optimizers (i.e., not removed),a
character
(vector) of specified optimizer labels,a
numeric
(vector) of optimizer ids (see$print()
output).
time_limit
An
integer
, the time limit in seconds for computations. No time limit iftime_limit = NULL
(the default). This currently only works reliably under Windows.verbose
A
logical
, which indicates whether progress/details should be printed. Set toTRUE
(FALSE
) to print (hide) such messages. The default isTRUE
.digits
An
integer
, the number of shown decimal places. The default is2
.
Method standardize()
Standardizes the optimization problem.
Arguments
argument_name
A
character
, the name of the argument off
to be standardized. The argument must anumeric
vector
,matrix
, ordata.frame
.by_column
Only relevant if the argument
argument_name
is amatrix
or adata.frame
. In that case, eitherTRUE
to standardize column-wise (default) orFALSE
to standardize row-wise. Currently, onlyby_column = TRUE
is implemented.center
Passed to
scale
. Default isTRUE
.scale
Passed to
scale
. Default isTRUE
.ignore
A
integer
(vector) of column indices (or row indices ifby_column = FALSE
) to not standardize.verbose
A
logical
, which indicates whether progress/details should be printed. Set toTRUE
(FALSE
) to print (hide) such messages. The default isTRUE
.
Method reduce()
Reduces the optimization problem.
Arguments
argument_name
A
character
, the name of the argument off
to be reduced.by_row
Only relevant if the argument
argument_name
is amatrix
or adata.frame
. In that case, eitherTRUE
to reduce row-wise (default) orFALSE
to reduce column-wise. Currently, onlyby_row = TRUE
is implemented.how
A
character
, specifying how to reduce. Can be one of:"random"
(default), reduce at random"first"
, reduce to the first elements"last"
, reduce to the last elements"similar"
, reduce to similar elements"dissimilar"
, reduce to dissimilar elements Note that"similar"
and"dissimilar"
are based on k-means clustering viakmeans
. To apply these options, the argumentargument_name
must benumeric
.
proportion
A
numeric
between0
and1
, specifying the reduction proportion. By default,proportion = 0.5
.centers
Only relevant, if
how = "(dis)similar"
. In that case, passed tokmeans
. By default,centers = 2
.ignore
Only relevant, if
how = "(dis)similar"
. In that case ainteger
(vector) of row indices (or column indices ifby_row = FALSE
) to ignore for clustering.seed
An
integer
, passed toset.seed
for reproducibility. Can beNULL
for no seed, which is the default.verbose
A
logical
, which indicates whether progress/details should be printed. Set toTRUE
(FALSE
) to print (hide) such messages. The default isTRUE
.
Method reset_argument()
Resets an additional argument for f
after transformation with
$standardize()
or $reduce()
.
Usage
Nop$reset_argument(
argument_name,
verbose = getOption("ino_verbose", default = TRUE)
)
Method continue()
Continues optimization runs, e.g., with a transformed parameter.
Arguments
which_run
Select saved results of optimization runs. Either:
"all"
, all results,"last"
, the results from the last optimization,"failed"
, the results from failed optimization runs,a
character
(vector) of labels specified in$optimize()
,a
numeric
(vector) of run ids.
which_optimizer
Select specified numerical optimizers. Either:
"all"
, all specified optimizers,"removed"
, all removed optimizers,"active"
, all active optimizers (i.e., not removed),a
character
(vector) of specified optimizer labels,a
numeric
(vector) of optimizer ids (see$print()
output).
seed
An
integer
, passed toset.seed
for reproducibility. Can beNULL
for no seed, which is the default.return_results
A
logical
, which indicates whether the optimization results should be returned as alist
. By default,return_results = FALSE
.save_results
A
logical
, which indicates whether the optimization results should be saved inside theNop
object. By default,save_results = TRUE
.ncores
An
integer
, the number of CPU cores for parallel computation. The default is1
. You can useparallel::detectCores()
to detect the number of available CPU cores.verbose
A
logical
, which indicates whether progress/details should be printed. Set toTRUE
(FALSE
) to print (hide) such messages. The default isTRUE
.simplify
Only relevant if
return_results = TRUE
andruns = 1
and/or only one optimizer is specified. In this case, ifsimplify = TRUE
, the nested list output of optimization results is flattened if possible.time_limit
An
integer
, the time limit in seconds for computations. No time limit iftime_limit = NULL
(the default). This currently only works reliably under Windows.hide_warnings
A
logical
. Set toTRUE
(FALSE
) to hide (show) warning messages.
Method results()
Returns saved optimization results.
Usage
Nop$results(
which_run = "all",
which_optimizer = "all",
which_element = "all",
only_comparable = FALSE,
simplify = TRUE
)
Arguments
which_run
Select saved results of optimization runs. Either:
"all"
, all results,"last"
, the results from the last optimization,"failed"
, the results from failed optimization runs,a
character
(vector) of labels specified in$optimize()
,a
numeric
(vector) of run ids.
which_optimizer
Select specified numerical optimizers. Either:
"all"
, all specified optimizers,"removed"
, all removed optimizers,"active"
, all active optimizers (i.e., not removed),a
character
(vector) of specified optimizer labels,a
numeric
(vector) of optimizer ids (see$print()
output).
which_element
Select elements of saved optimization results. See
$elements_available()
for the names of all available elements. Either:"all"
, all elements,"basic"
, the elements"value"
, thenumeric
value of the found optimum,"parameter"
, the parametervector
at which the optimum value is obtained,
"default"
, the elements that are saved for all optimization runs by default, i.e."run"
, the run id,"optimizer"
, the label for the optimizer,"value"
and"parameter"
(see above),"seconds"
, the optimization time in seconds,"label"
, the label for the optimization run,
a
character
(vector), names of specific elements in the optimization output.
only_comparable
Either
TRUE
to show only comparable results (i.e., results obtained for the original optimization problem), orFALSE
to include all optimization results.simplify
Only relevant if
return_results = TRUE
andruns = 1
and/or only one optimizer is specified. In this case, ifsimplify = TRUE
, the nested list output of optimization results is flattened if possible.
Method number_runs()
Returns the number of saved optimization runs.
Arguments
which_run
Select saved results of optimization runs. Either:
"all"
, all results,"last"
, the results from the last optimization,"failed"
, the results from failed optimization runs,a
character
(vector) of labels specified in$optimize()
,a
numeric
(vector) of run ids.
which_optimizer
Select specified numerical optimizers. Either:
"all"
, all specified optimizers,"removed"
, all removed optimizers,"active"
, all active optimizers (i.e., not removed),a
character
(vector) of specified optimizer labels,a
numeric
(vector) of optimizer ids (see$print()
output).
only_comparable
Either
TRUE
to show only comparable results (i.e., results obtained for the original optimization problem), orFALSE
to include all optimization results.
Method elements_available()
Returns names of available elements per optimizer.
Arguments
which_optimizer
Select specified numerical optimizers. Either:
"all"
, all specified optimizers,"removed"
, all removed optimizers,"active"
, all active optimizers (i.e., not removed),a
character
(vector) of specified optimizer labels,a
numeric
(vector) of optimizer ids (see$print()
output).
Method clear()
Deletes optimization results.
Arguments
which_run
Select saved results of optimization runs. Either:
"all"
, all results,"last"
, the results from the last optimization,"failed"
, the results from failed optimization runs,a
character
(vector) of labels specified in$optimize()
,a
numeric
(vector) of run ids.
which_optimizer
Select specified numerical optimizers. Either:
"all"
, all specified optimizers,"removed"
, all removed optimizers,"active"
, all active optimizers (i.e., not removed),a
character
(vector) of specified optimizer labels,a
numeric
(vector) of optimizer ids (see$print()
output).
which_element
Select elements of saved optimization results. See
$elements_available()
for the names of all available elements. Either:"all"
, all elements,"basic"
, the elements"value"
, thenumeric
value of the found optimum,"parameter"
, the parametervector
at which the optimum value is obtained,
"default"
, the elements that are saved for all optimization runs by default, i.e."run"
, the run id,"optimizer"
, the label for the optimizer,"value"
and"parameter"
(see above),"seconds"
, the optimization time in seconds,"label"
, the label for the optimization run,
a
character
(vector), names of specific elements in the optimization output.
Method summary()
Provides an overview of the optimization runs.
Usage
Nop$summary(
which_element = "basic",
which_run = "all",
which_optimizer = "all",
digits = getOption("ino_digits", default = 2),
only_comparable = FALSE,
...
)
Arguments
which_element
Select elements of saved optimization results. See
$elements_available()
for the names of all available elements. Either:"all"
, all elements,"basic"
, the elements"value"
, thenumeric
value of the found optimum,"parameter"
, the parametervector
at which the optimum value is obtained,
"default"
, the elements that are saved for all optimization runs by default, i.e."run"
, the run id,"optimizer"
, the label for the optimizer,"value"
and"parameter"
(see above),"seconds"
, the optimization time in seconds,"label"
, the label for the optimization run,
a
character
(vector), names of specific elements in the optimization output.
which_run
Select saved results of optimization runs. Either:
"all"
, all results,"last"
, the results from the last optimization,"failed"
, the results from failed optimization runs,a
character
(vector) of labels specified in$optimize()
,a
numeric
(vector) of run ids.
which_optimizer
Select specified numerical optimizers. Either:
"all"
, all specified optimizers,"removed"
, all removed optimizers,"active"
, all active optimizers (i.e., not removed),a
character
(vector) of specified optimizer labels,a
numeric
(vector) of optimizer ids (see$print()
output).
digits
An
integer
, the number of shown decimal places. The default is2
.only_comparable
Either
TRUE
to show only comparable results (i.e., results obtained for the original optimization problem), orFALSE
to include all optimization results....
Optionally named expressions of elements. See
$elements_available()
for the names of all available elements. In addition,"true_value"
,"true_parameter"
,"best_value"
, and"best_parameter"
can be accessed As an example, you could adddistance = "sqrt(sum((parameter - true_parameter) ^ 2))"
for the euclidean distance between the estimated and true parameter vector.
Method optima()
Provides an overview of the identified optimum values.
Usage
Nop$optima(
digits = getOption("ino_digits", default = 2),
sort_by = "frequency",
which_run = "all",
which_optimizer = "all",
only_comparable = TRUE
)
Arguments
digits
An
integer
, the number of shown decimal places. The default is2
.sort_by
Either:
"frequency"
(default) to sort rows by frequency,"value"
to sort by value.
which_run
Select saved results of optimization runs. Either:
"all"
, all results,"last"
, the results from the last optimization,"failed"
, the results from failed optimization runs,a
character
(vector) of labels specified in$optimize()
,a
numeric
(vector) of run ids.
which_optimizer
Select specified numerical optimizers. Either:
"all"
, all specified optimizers,"removed"
, all removed optimizers,"active"
, all active optimizers (i.e., not removed),a
character
(vector) of specified optimizer labels,a
numeric
(vector) of optimizer ids (see$print()
output).
only_comparable
Either
TRUE
to show only comparable results (i.e., results obtained for the original optimization problem), orFALSE
to include all optimization results.
Method plot()
Visualizes the optimization time or value.
Arguments
which_element
Either:
"seconds"
to plot the optimization times (default)"value"
to plot the optimization values
by
Either:
"label"
to group by optimization label"optimizer"
to group by optimizerNULL
to not group (default)
relative
Only if
which_element = "seconds"
. In this case, set toTRUE
to plot relative time differences with respect to the overall median.which_run
Select saved results of optimization runs. Either:
"all"
, all results,"last"
, the results from the last optimization,"failed"
, the results from failed optimization runs,a
character
(vector) of labels specified in$optimize()
,a
numeric
(vector) of run ids.
which_optimizer
Select specified numerical optimizers. Either:
"all"
, all specified optimizers,"removed"
, all removed optimizers,"active"
, all active optimizers (i.e., not removed),a
character
(vector) of specified optimizer labels,a
numeric
(vector) of optimizer ids (see$print()
output).
only_comparable
Either
TRUE
to show only comparable results (i.e., results obtained for the original optimization problem), orFALSE
to include all optimization results.title
A
character
, the plot title.xlim
Passed on to
coord_cartesian
.
Returns
A ggplot
object.
Method deviation()
Visualizes deviation of parameters.
Arguments
reference
A
numeric
of lengthself$npar
, the reference parameters.which_element
Either:
"initial"
to compute deviations to the initial values (default)"parameter"
to compute deviations to the estimated parameters
which_run
Select saved results of optimization runs. Either:
"all"
, all results,"last"
, the results from the last optimization,"failed"
, the results from failed optimization runs,a
character
(vector) of labels specified in$optimize()
,a
numeric
(vector) of run ids.
which_optimizer
Select specified numerical optimizers. Either:
"all"
, all specified optimizers,"removed"
, all removed optimizers,"active"
, all active optimizers (i.e., not removed),a
character
(vector) of specified optimizer labels,a
numeric
(vector) of optimizer ids (see$print()
output).
only_comparable
Either
TRUE
to show only comparable results (i.e., results obtained for the original optimization problem), orFALSE
to include all optimization results.title
A
character
, the plot title.ylim
Passed on to
coord_cartesian
.parameter_labels
A
character
of lengthlength(reference)
with labels for the parameters.
Returns
A ggplot
object.
Method trace()
Capture trace of optimization with stats::nlm()
.
Arguments
initial
A
numeric
vector of lengthnpar
, the starting point for the optimization. By default,initial = stats::rnorm(self$npar)
, i.e., random initial values drawn from a standard normal distribution.iterations
A positive
integer
, the maximum number of iterations before termination. By default,interations = 100
.tolerance
A
numeric
, the minimum allowable absolute change in the function value before termination. By default,tolerance = 1e-6
.which_element
A
character
(vector) of elements to provide in the output, can be one or more of:value
(the current function value)parameter
(the current value of each parameter)gradient
(the current gradient value)hessian
(the current Hessian value)seconds
(the number of seconds for the current iteration)
...
Additional arguments passed on to
nlm
. The argumentsiterlim
andhessian
cannot be specified.
Method best_value()
Returns the best found numeric
value of f
.
Usage
Nop$best_value(
which_run = "all",
which_optimizer = "all",
only_comparable = TRUE,
digits = getOption("ino_digits", default = 2)
)
Arguments
which_run
Select saved results of optimization runs. Either:
"all"
, all results,"last"
, the results from the last optimization,"failed"
, the results from failed optimization runs,a
character
(vector) of labels specified in$optimize()
,a
numeric
(vector) of run ids.
which_optimizer
Select specified numerical optimizers. Either:
"all"
, all specified optimizers,"removed"
, all removed optimizers,"active"
, all active optimizers (i.e., not removed),a
character
(vector) of specified optimizer labels,a
numeric
(vector) of optimizer ids (see$print()
output).
only_comparable
Either
TRUE
to show only comparable results (i.e., results obtained for the original optimization problem), orFALSE
to include all optimization results.digits
An
integer
, the number of shown decimal places. The default is2
.
Method best_parameter()
Returns the best found numeric
parameter vector.
Usage
Nop$best_parameter(
which_run = "all",
which_optimizer = "all",
only_comparable = TRUE,
digits = getOption("ino_digits", default = 2)
)
Arguments
which_run
Select saved results of optimization runs. Either:
"all"
, all results,"last"
, the results from the last optimization,"failed"
, the results from failed optimization runs,a
character
(vector) of labels specified in$optimize()
,a
numeric
(vector) of run ids.
which_optimizer
Select specified numerical optimizers. Either:
"all"
, all specified optimizers,"removed"
, all removed optimizers,"active"
, all active optimizers (i.e., not removed),a
character
(vector) of specified optimizer labels,a
numeric
(vector) of optimizer ids (see$print()
output).
only_comparable
Either
TRUE
to show only comparable results (i.e., results obtained for the original optimization problem), orFALSE
to include all optimization results.digits
An
integer
, the number of shown decimal places. The default is2
.
Method closest_parameter()
Extracts the parameter vector that led to a function value closest
(in absolute value) to value
.
Note that this parameter vector is not necessarily unique.
Usage
Nop$closest_parameter(
value,
which_run = "all",
which_optimizer = "all",
only_comparable = TRUE,
digits = getOption("ino_digits", default = 2)
)
Arguments
value
A single
numeric
.which_run
Select saved results of optimization runs. Either:
"all"
, all results,"last"
, the results from the last optimization,"failed"
, the results from failed optimization runs,a
character
(vector) of labels specified in$optimize()
,a
numeric
(vector) of run ids.
which_optimizer
Select specified numerical optimizers. Either:
"all"
, all specified optimizers,"removed"
, all removed optimizers,"active"
, all active optimizers (i.e., not removed),a
character
(vector) of specified optimizer labels,a
numeric
(vector) of optimizer ids (see$print()
output).
only_comparable
Either
TRUE
to show only comparable results (i.e., results obtained for the original optimization problem), orFALSE
to include all optimization results.digits
An
integer
, the number of shown decimal places. The default is2
.
Examples
Nop$new(f = f_ackley, npar = 2)$
set_optimizer(optimizer_nlm())$
optimize(runs = 100, verbose = FALSE)$
optima()
#> value frequency
#> 1 0 39
#> 2 2.58 34
#> 3 3.57 12
#> 4 5.38 6
#> 5 4.88 5
#> 6 6.56 2
#> 7 6.88 2