Package {trackopt}


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

logo

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:

See Also

Useful links:

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

[function]
A function to optimize. It must return a single numeric value.

By default, the first argument of f is optimized. That argument should be a numeric vector of the same length as p. Additional arguments can be supplied through ....

Use target when f should be optimized over another argument or over multiple arguments.

p

[numeric()]
The starting parameter values for the target argument(s).

target

[character() | NULL]
The names of the numeric argument(s) to optimize.

If NULL (default), the first argument of f is used.

npar

[integer()]
The length of each target argument.

Specify npar when optimizing over multiple target arguments so p can be split correctly.

If target contains a single argument and npar is NULL, length(p) is used.

gradient

[function | NULL]
Optionally a function that returns the gradient of f.

Its arguments must match the arguments of f.

hessian

[function | NULL]
Optionally a function that returns the Hessian of f.

Its arguments must match the arguments of f.

...

Additional arguments passed to f, and to gradient and hessian when they are specified.

iterations_max

[integer(1)]
The largest iteration limit for the optimizer.

tolerance

[numeric(1)]
Tracking stops when the absolute change in function value between two consecutive iterations is less than this value.

Can be 0 for no value threshold.

typsize, fscale, ndigit, stepmax, steptol

Arguments passed on to nlm.

minimize

[logical(1)]
If TRUE, minimize f; otherwise maximize it.

verbose

[logical(1)]
If TRUE, print progress after each iteration.

lower, upper

[numeric() | NULL]
Optional lower and upper parameter bounds. Scalars are recycled to the number of parameters.

method, control

Arguments passed on to optim.

Elements trace and maxit are ignored in control because optim_track() controls tracing and iteration limits directly.

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:

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

[function]
A function to optimize. It must return a single numeric value.

By default, the first argument of f is optimized. That argument should be a numeric vector of the same length as p. Additional arguments can be supplied through ....

Use target when f should be optimized over another argument or over multiple arguments.

p

[numeric()]
The starting parameter values for the target argument(s).

optimizer

[Optimizer]
An Optimizer object, which can be created via Optimizer.

target

[character() | NULL]
The names of the numeric argument(s) to optimize.

If NULL (default), the first argument of f is used.

npar

[integer()]
The length of each target argument.

Specify npar when optimizing over multiple target arguments so p can be split correctly.

If target contains a single argument and npar is NULL, length(p) is used.

gradient

[function | NULL]
Optionally a function that returns the gradient of f.

Its arguments must match the arguments of f.

hessian

[function | NULL]
Optionally a function that returns the Hessian of f.

Its arguments must match the arguments of f.

...

Additional arguments passed to f, and to gradient and hessian when they are specified.

track

[character(1) | NULL]
Either:

  • "iterations" to track the iterations of the optimizer,

  • "evaluations" to track the evaluations of f that improved the function value.

Can be NULL (default), then iterations are tracked if iterations_arg is specified or known for optimizer, and evaluations otherwise.

iterations_arg

[character() | NULL]
Only relevant if iterations are tracked.

The argument of the optimizer that limits its iterations, either:

  • the name of the argument (e.g., "iterlim" for nlm),

  • the name of a list argument and the name of its element (e.g., c("control", "maxit") for optim).

Can be NULL (default) for the optimizers listed in the details.

iterations_max

[integer(1)]
The largest iteration limit for the optimizer (if iterations are tracked) or the maximum number of rows in the track after the starting point (if evaluations are tracked).

tolerance

[numeric(1)]
Only relevant if iterations are tracked.

Tracking stops when the absolute change in function value between two consecutive iterations is less than this value.

Can be 0 for no value threshold.

lower, upper

[numeric() | NULL]
Optional lower and upper parameter bounds. Scalars are recycled to the number of parameters.

minimize

[logical(1)]
If TRUE, minimize f; otherwise maximize it.

verbose

[logical(1)]
If TRUE, print progress after each iteration.

object

[trackopt]
A trackopt object.

iteration

[integer(1)]
The iteration to plot.

If NULL, the last iteration is plotted.

This option is useful for creating animations, e.g., via the chunk option fig.show = "animate" of {knitr}.

xlim, xlim2

[numeric(2)]
Ranges for the first and second parameter to plot.

If NULL, they are derived from the parameter ranges in object.

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

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:

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

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)