| Type: | Package |
| Title: | Track Numerical Optimization |
| Version: | 0.2.0 |
| Description: | Tracks the path of a numerical optimizer, i.e., the parameter values, function values, gradients, and Hessians on the way from the starting values to the optimum. Any optimizer that is defined via the 'optimizeR' package can be tracked, either via its iterations or via the evaluations of the objective function. The path can be summarized and, for functions of one or two parameters, visualized. |
| License: | GPL (≥ 3) |
| Encoding: | UTF-8 |
| Imports: | checkmate, cli, oeli (≥ 0.7.9), optimizeR (≥ 1.4.0), stats, tibble, utils |
| Suggests: | ao, DEoptim, GenSA, ggplot2, lbfgsb3c, pracma, testthat (≥ 3.0.0), ucminf |
| Depends: | R (≥ 4.1.0) |
| Config/testthat/edition: | 3 |
| URL: | https://github.com/loelschlaeger/trackopt |
| BugReports: | https://github.com/loelschlaeger/trackopt/issues |
| Config/roxygen2/version: | 8.1.0 |
| NeedsCompilation: | no |
| Packaged: | 2026-11-27 10:16:57 UTC; loelschlaeger |
| Author: | Lennart Oelschläger [aut, cre] |
| Maintainer: | Lennart Oelschläger <oelschlaeger.lennart@gmail.com> |
| Repository: | CRAN |
| Date/Publication: | 2026-11-27 10:40:02 UTC |
trackopt: Track Numerical Optimization
Description
Tracks the path of a numerical optimizer, i.e., the parameter values, function values, gradients, and Hessians on the way from the starting values to the optimum. Any optimizer that is defined via the 'optimizeR' package can be tracked, either via its iterations or via the evaluations of the objective function. The path can be summarized and, for functions of one or two parameters, visualized.
Author(s)
Maintainer: Lennart Oelschläger oelschlaeger.lennart@gmail.com
Authors:
Lennart Oelschläger oelschlaeger.lennart@gmail.com
See Also
Useful links:
Report bugs at https://github.com/loelschlaeger/trackopt/issues
Examples
himmelblau <- function(x) {
(x[1]^2 + x[2] - 11)^2 + (x[1] + x[2]^2 - 7)^2
}
# Example 1: Track the iterations of stats::nlminb() ------------------------
track <- trackopt(
f = himmelblau,
p = c(0, 0),
optimizer = optimizeR::Optimizer$new("stats::nlminb")
)
summary(track)
if (requireNamespace("ggplot2", quietly = TRUE)) {
ggplot2::autoplot(track)
}
# Example 2: Track the evaluations of the Nelder-Mead method ----------------
track <- trackopt(
f = himmelblau,
p = c(0, 0),
optimizer = optimizeR::Optimizer$new("stats::optim"),
track = "evaluations"
)
summary(track)
Track nlm and optim iterations
Description
Any other optimizer can be tracked via trackopt.
Usage
nlm_track(
f,
p,
target = NULL,
npar = NULL,
gradient = NULL,
hessian = NULL,
...,
iterations_max = 100,
tolerance = 1e-06,
typsize = rep(1, length(p)),
fscale = 1,
ndigit = 12,
stepmax = max(1000 * sqrt(sum((p/typsize)^2)), 1000),
steptol = 1e-06,
minimize = TRUE,
verbose = FALSE
)
optim_track(
f,
p,
target = NULL,
npar = NULL,
gradient = NULL,
...,
iterations_max = 100,
tolerance = 1e-06,
lower = NULL,
upper = NULL,
method = c("Nelder-Mead", "BFGS", "CG", "L-BFGS-B", "SANN", "Brent"),
control = list(),
minimize = TRUE,
verbose = FALSE
)
Arguments
f |
[ By default, the first argument of Use |
p |
[ |
target |
[ If |
npar |
[ Specify If |
gradient |
[ Its arguments must match the arguments of |
hessian |
[ Its arguments must match the arguments of |
... |
Additional arguments passed to |
iterations_max |
[ |
tolerance |
[ Can be |
typsize, fscale, ndigit, stepmax, steptol |
Arguments passed on to |
minimize |
[ |
verbose |
[ |
lower, upper |
[ |
method, control |
Arguments passed on to Elements |
Value
A tibble of class trackopt, see trackopt for its columns
and methods. The column gradient is only available for nlm_track().
Examples
himmelblau <- function(x) {
(x[1]^2 + x[2] - 11)^2 + (x[1] + x[2]^2 - 7)^2
}
track <- nlm_track(f = himmelblau, p = c(0, 0))
summary(track)
if (requireNamespace("ggplot2", quietly = TRUE)) {
ggplot2::autoplot(track)
}
optim_track(f = himmelblau, p = c(0, 0), method = "CG")
Track numerical optimization
Description
This function tracks the path of a numerical optimizer, i.e., the parameter values, function values, gradients, and Hessians on the way from the starting values to the optimum.
The following methods are available for the output:
-
summary(): summarize an optimization track -
autoplot(): visualize a track with one or two parameters
Usage
trackopt(
f,
p,
optimizer,
target = NULL,
npar = NULL,
gradient = NULL,
hessian = NULL,
...,
track = NULL,
iterations_arg = NULL,
iterations_max = 100,
tolerance = 1e-06,
lower = NULL,
upper = NULL,
minimize = TRUE,
verbose = FALSE
)
## S3 method for class 'trackopt'
summary(object, ...)
## S3 method for class 'trackopt'
autoplot(object, iteration = NULL, xlim = NULL, xlim2 = NULL, ...)
Arguments
f |
[ By default, the first argument of Use |
p |
[ |
optimizer |
[ |
target |
[ If |
npar |
[ Specify If |
gradient |
[ Its arguments must match the arguments of |
hessian |
[ Its arguments must match the arguments of |
... |
Additional arguments passed to |
track |
[
Can be |
iterations_arg |
[ The argument of the optimizer that limits its iterations, either:
Can be |
iterations_max |
[ |
tolerance |
[ Tracking stops when the absolute change in function value between two consecutive iterations is less than this value. Can be |
lower, upper |
[ |
minimize |
[ |
verbose |
[ |
object |
[ |
iteration |
[ If This option is useful for creating animations, e.g., via the chunk option
|
xlim, xlim2 |
[ If |
Value
trackopt() returns a tibble of class trackopt with one row for the
starting point and one row for each tracked iteration or evaluation. It
has the columns
-
iteration, the number of the row (0for the starting point), -
evaluation, the number of the evaluation off(only if evaluations are tracked), -
value, the function value, -
step, the change in function value compared to the previous row, -
parameter, the parameter values, -
gradient, the gradient, -
hessian, the Hessian, -
seconds, the computation time in seconds between the previous row and this row. If iterations are tracked, this is the time of the runs of the optimizer that were needed to get to this row. Since these runs start from the beginning, the times tend to increase along the track.
Gradient and Hessian are the values of the functions gradient and
hessian, or NA if these functions are not supplied. As an exception,
if iterations are tracked, the gradient and the Hessian for
nlm and the Hessian for optim
are the ones that the optimizer returns, and the functions gradient and
hessian are only used for the starting point. In this case, the track
for optim has no column gradient. Parameter,
gradient, and Hessian are stored in list columns if there is more than
one parameter.
summary() returns a list of class summary.trackopt with the number
of iterations, the function value and the parameter at the start and at
the end, and the total computation time. For a track of evaluations, it
also contains the number of the evaluation in the last row.
autoplot() returns a ggplot object. For more than two parameters, it
returns NULL with a warning.
Tracking iterations
Numerical optimizers usually only return their final result. To obtain the
points in between, the optimizer is run several times from the starting
values p, with an iteration limit of one, two, three, and so on. Since a
run with an iteration limit ends at a point that a single call of the
optimizer also passes, the track is the path of a single call of the
optimizer. Note that every run starts from the beginning, such that the
computation time grows quadratically with the number of iterations.
To this end, the argument of the optimizer that limits its iterations is required. It is known for the following optimizers:
-
nlm("iterlim"), -
nlminb(c("control", "iter.max")), -
optim(c("control", "maxit")), -
lbfgsb3c::lbfgsb3c(),lbfgsb3c::lbfgsb3(),lbfgsb3c::lbfgsb3f(), andlbfgsb3c::lbfgsb3x()(c("control", "maxit")), -
ucminf::ucminf()(c("control", "maxeval")), and
ao::ao()("iteration_limit").
For any other optimizer, it can be specified via iterations_arg. Note
that the limit must only stop the optimizer, i.e., a run with an iteration
limit must follow the path of a run without the limit.
A row is added to the track whenever a larger iteration limit leads to a new point. Tracking ends
when the optimizer stops before it reaches the iteration limit, which usually means that it has converged,
when the function value changes by less than
tolerancebetween two iterations,when the iteration limit
iterations_maxis reached,or when the optimizer fails, in which case a warning is issued and the track ends with the last iteration before the failure.
Tracking evaluations
Alternatively, any optimizer can be tracked via the evaluations of f,
which is the default if the argument that limits its iterations is neither
known nor specified. In this case, the optimizer is run only once and every
evaluation of f is recorded. The track then consists of the starting
point and the evaluations that improved the function value, i.e., the
points at which the optimizer found a new best function value, where the
column evaluation numbers the evaluations of f. Tracking ends when the
optimizer stops or when the track has iterations_max rows after the
starting point, in which case the optimizer is interrupted.
Examples
himmelblau <- function(x) {
(x[1]^2 + x[2] - 11)^2 + (x[1] + x[2]^2 - 7)^2
}
# Example 1: Track the iterations of stats::nlminb() ------------------------
track <- trackopt(
f = himmelblau,
p = c(0, 0),
optimizer = optimizeR::Optimizer$new("stats::nlminb")
)
summary(track)
if (requireNamespace("ggplot2", quietly = TRUE)) {
ggplot2::autoplot(track)
}
# Example 2: Track the evaluations of the Nelder-Mead method ----------------
track <- trackopt(
f = himmelblau,
p = c(0, 0),
optimizer = optimizeR::Optimizer$new("stats::optim"),
track = "evaluations"
)
summary(track)