Type: Package
Title: Track Numerical Optimization
Version: 0.1.0
Description: Tracks parameter value, gradient, and Hessian at each iteration of numerical optimizers. Useful for analyzing optimization progress, diagnosing issues, and studying convergence behavior.
License: GPL (≥ 3)
Encoding: UTF-8
RoxygenNote: 7.3.2
Imports: checkmate, cli, ggplot2, oeli (≥ 0.7.2), optimizeR (≥ 1.2.0), stats, tibble, utils
Suggests: testthat (≥ 3.0.0)
Depends: R (≥ 4.1.0)
Config/testthat/edition: 3
URL: https://github.com/loelschlaeger/trackopt
BugReports: https://github.com/loelschlaeger/trackopt/issues
NeedsCompilation: no
Packaged: 2025-05-08 16:57:59 UTC; Lennart Oelschläger
Author: Lennart Oelschläger [aut, cre]
Maintainer: Lennart Oelschläger <oelschlaeger.lennart@gmail.com>
Repository: CRAN
Date/Publication: 2025-05-12 08:20:02 UTC

trackopt: Track Numerical Optimization

Description

logo

Tracks parameter value, gradient, and Hessian at each iteration of numerical optimizers. Useful for analyzing optimization progress, diagnosing issues, and studying convergence behavior.

Author(s)

Maintainer: Lennart Oelschläger oelschlaeger.lennart@gmail.com

See Also

Useful links:


Track numerical optimization

Description

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
)

## 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 be optimized, returning a single numeric value.

The first argument of f should be a numeric of the same length as p, optionally followed by any other arguments specified by the ... argument.

If f is to be optimized over an argument other than the first, or more than one argument, this has to be specified via the target argument.

p

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

target

[character() | NULL]
The name(s) of the argument(s) over which f gets optimized.

This can only be numeric arguments.

Can be NULL (default), then it is the first argument of f.

npar

[integer()]
The length(s) of the target argument(s).

Must be specified if more than two target arguments are specified via the target argument.

Can be NULL if there is only one target argument, in which case npar is set to be length(p).

gradient

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

The function call of gradient must be identical to f.

hessian

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

The function call of hessian must be identical to f.

...

Additional arguments to be passed to f (and gradient, hessian if specified).

iterations_max

[integer(1)]
The maximum number of iterations before termination.

tolerance

[numeric(1)]
The minimum allowed absolute change in function value between two iterations before termination.

typsize, fscale, ndigit, stepmax, steptol

Arguments passed on to nlm.

minimize

[logical(1)]
Minimize?

verbose

[logical(1)]
Print progress?

lower, upper

[numeric() | NULL]
Optionally lower and upper parameter bounds.

method, control

Arguments passed on to optim.

Elements trace and maxit are ignored in control.

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, see https://bookdown.org/yihui/rmarkdown-cookbook/animation.html#ref-R-animation.

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

A tibble with iterations in rows.

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)
ggplot2::autoplot(track)