Track numerical optimization trackopt website

CRAN status R-CMD-check Codecov test coverage

If you’re wondering which path a numerical optimizer takes from the starting values to the optimum, you might find the {trackopt} package useful. This package records the parameter values, function values, gradients, and Hessians along the path of any optimizer in R. The path can be summarized and, for functions of one or two parameters, plotted. This can be useful for analyzing optimization progress, diagnosing issues, and studying convergence behavior.

Installation

You can install the released package version from CRAN with:

install.packages("trackopt")

How to track an optimizer?

For demonstration, say we want to see how stats::nlminb() minimizes Himmelblau’s function when it starts at the origin. To this end, we define the optimizer via the {optimizeR} package and pass it to trackopt():

library("trackopt")
himmelblau <- function(x) (x[1]^2 + x[2] - 11)^2 + (x[1] + x[2]^2 - 7)^2
track <- trackopt(
  f = himmelblau,
  p = c(0, 0),
  optimizer = optimizeR::Optimizer$new("stats::nlminb")
)
print(track)
#> # A tibble: 10 × 7
#>    iteration    value      step parameter gradient hessian  seconds
#>  *     <dbl>    <dbl>     <dbl> <list>       <dbl>   <dbl>    <dbl>
#>  1         0 1.7 e+ 2  0        <dbl [2]>       NA      NA 0       
#>  2         1 5.18e+ 1 -1.18e+ 2 <dbl [2]>       NA      NA 0.000573
#>  3         2 1.28e+ 1 -3.90e+ 1 <dbl [2]>       NA      NA 0.000692
#>  4         3 2.99e+ 0 -9.83e+ 0 <dbl [2]>       NA      NA 0.000854
#>  5         4 3.24e- 1 -2.67e+ 0 <dbl [2]>       NA      NA 0.000985
#>  6         5 9.75e- 3 -3.14e- 1 <dbl [2]>       NA      NA 0.00113 
#>  7         6 1.18e- 4 -9.63e- 3 <dbl [2]>       NA      NA 0.00128 
#>  8         7 2.16e- 6 -1.16e- 4 <dbl [2]>       NA      NA 0.00142 
#>  9         8 2.56e-12 -2.16e- 6 <dbl [2]>       NA      NA 0.00158 
#> 10         9 1.53e-17 -2.56e-12 <dbl [2]>       NA      NA 0.00172

The track contains one row for the starting point and one row for each iteration of the optimizer. Note that the columns gradient and hessian are NA here, since we did not supply functions for the gradient and the Hessian via the arguments gradient and hessian. The summary() method returns an overview of the track and ggplot2::autoplot() plots the path:

summary(track)
#> Iterations: 9
#> Function improvement: 170 -> 1.533e-17
#> Computation time: 0.01024 seconds
#> Initial parameter: 0, 0
#> Final parameter: 3, 2
ggplot2::autoplot(track)

Contour plot of Himmelblau's function with the path of the optimizer from the starting point to a minimum.

And what about other optimizers?

In general, trackopt() tracks any optimizer that is available as an Optimizer object. For example, the following lines define the Nelder-Mead algorithm from the {pracma} package:

nelder_mead <- optimizeR::Optimizer$new("custom", .verbose = FALSE)
nelder_mead$definition(
  algorithm = pracma::nelder_mead,
  arg_objective = "fn",
  arg_initial = "x0",
  out_value = "fmin",
  out_parameter = "xmin",
  direction = "min"
)

However, the iterations can only be tracked if the optimizer has an argument that limits its iterations. For all other optimizers, like the one we just defined, trackopt() records the evaluations of the function instead. As an example, we minimize the Rosenbrock function:

rosenbrock <- function(x) 100 * (x[2] - x[1]^2)^2 + (1 - x[1])^2
track <- trackopt(f = rosenbrock, p = c(-1.2, 1), optimizer = nelder_mead)
summary(track)
#> Iterations: 77
#> Evaluations: 193
#> Function improvement: 24.2 -> 7.033e-13
#> Computation time: 0.03579 seconds
#> Initial parameter: -1.2, 1
#> Final parameter: 1, 1
ggplot2::autoplot(track)

Contour plot of the Rosenbrock function with the points at which the Nelder-Mead algorithm found a new best function value.

By the way, are you surprised to see that the track only has 77 iterations, although its last point is evaluation number 193? The reason is that the track only contains the evaluations that improved the function value, i.e., the points at which the optimizer found a new best function value.

The same works for global optimizers, for example differential evolution (DEoptim::DEoptim()), which searches for the minimum of Himmelblau’s function between parameter bounds:

set.seed(1)
track <- trackopt(
  f = himmelblau,
  p = c(0, 0),
  optimizer = optimizeR::Optimizer$new("DEoptim::DEoptim"),
  lower = -5,
  upper = 5
)
ggplot2::autoplot(track)

Contour plot of Himmelblau's function with the points at which differential evolution found a new best function value.

Shortcuts for stats::nlm() and stats::optim()

For these two optimizers, the functions nlm_track() and optim_track() save you from defining an Optimizer object. The following example tracks stats::nlm() while it minimizes Himmelblau’s function:

track <- nlm_track(f = himmelblau, p = c(0, 0))
summary(track)
#> Iterations: 8
#> Function improvement: 170 -> 1.755e-10
#> Computation time: 0.01386 seconds
#> Initial parameter: 0, 0
#> Final parameter: 3, 2
ggplot2::autoplot(track)

Contour plot of Himmelblau's function with the path of the optimizer from the starting point to a minimum.

The next example tracks the BFGS method of stats::optim() while it minimizes the Rosenbrock function:

track <- optim_track(f = rosenbrock, p = c(-1.2, 1), method = "BFGS")
print(track)
#> # A tibble: 37 × 6
#>    iteration value    step parameter hessian       seconds
#>  *     <dbl> <dbl>   <dbl> <list>    <list>          <dbl>
#>  1         0 24.2    0     <dbl [2]> <dbl [1]>     0      
#>  2         1 20.2   -3.97  <dbl [2]> <dbl [2 × 2]> 0.00163
#>  3         2  8.61 -11.6   <dbl [2]> <dbl [2 × 2]> 0.00358
#>  4         3  3.12  -5.48  <dbl [2]> <dbl [2 × 2]> 0.00236
#>  5         4  2.83  -0.292 <dbl [2]> <dbl [2 × 2]> 0.00299
#>  6         5  2.63  -0.196 <dbl [2]> <dbl [2 × 2]> 0.00329
#>  7         6  2.01  -0.628 <dbl [2]> <dbl [2 × 2]> 0.00360
#>  8         7  1.89  -0.117 <dbl [2]> <dbl [2 × 2]> 0.00379
#>  9         8  1.52  -0.370 <dbl [2]> <dbl [2 × 2]> 0.00564
#> 10         9  1.37  -0.150 <dbl [2]> <dbl [2 × 2]> 0.00426
#> # ℹ 27 more rows
ggplot2::autoplot(track)

Contour plot of the Rosenbrock function with the path of the optimizer from the starting point to the minimum.

Contact

If you have questions, find a bug, or need a feature, please file an issue on GitHub.