Thread-Safe C++ Trust Region Optimizers

Thread-safe C++ trust region optimizers, each exposed both as an R function and as a C-callable entry point (with function pointers registered for other packages to call without linking against this package's shared library) so they can be used from parallel C++ code such as in 'nlmixr2est'. Includes a port of the two-derivative trust region algorithm in the 'trust' package by Charles J. Geyer; a port of the Steihaug truncated conjugate-gradient trust region method from the 'basin' Rust crate (Steihaug (1983) ); and ports of M. J. D. Powell's derivative-free BOBYQA (Powell (2009), Report DAMTP 2009/NA06) and NEWUOA (Powell (2006) ) Fortran 77 code < https://github.com/libprima/prima/tree/main/fortran/original>, as modified and distributed in the 'minqa' package, whose R interface is reproduced.


RcppTrust

Codecov testcoverage R-CMD-check CRANversion CRANdownloads

RcppTrust is a thread-safe C++ port of the trust-region optimizer in Charles J. Geyer’s CRAN package trust, built for use in nlmixr2est. It implements the exact same algorithm – same Newton / easy-easy / hard-easy / hard-hard trust-region subproblem, same termination criteria – and exposes it three ways:

  • trust(), a drop-in R replacement for trust::trust(), with the same arguments, defaults, and return value.
  • trust_solve_c(), a thread-safe C entry point (a C objective function pointer plus a small options struct, no R API calls) safe to call from parallel C++ code such as an OpenMP loop.
  • A header-only, positionally-indexed function-pointer table (inst/include/RcppTrust.h), so another package can call trust_solve_c() without linking against this package’s shared library – the same pattern rxode2/n1qn1/lbfgsb3c use across the nlmixr2 ecosystem.

It also includes three more trust-region optimizers built the same way, each with an R function and a thread-safe C entry point:

R function Ported from Needs C entry point
steihaug() basin (Rust) gradient + Hessian or Hessian-vector products steihaug_solve_c()
bobyqa() Powell’s BOBYQA Fortran, via minqa function values only; box bounds bobyqa_solve_c()
newuoa() Powell’s NEWUOA Fortran, via minqa function values only newuoa_solve_c()

bobyqa()/newuoa() are drop-in replacements for their minqa counterparts and give bitwise-identical results; steihaug() reproduces basin’s iterates bit for bit. See vignette("trust-region-methods").

See vignette("RcppTrust") for what’s the same as upstream trust, what’s different, and a worked example of the C interface and the registration pattern.

Note this package was generated with the help of AI (Claude/Gemini).

Installation

You can install the development version of RcppTrust from GitHub with:

# install.packages("pak")
pak::pak("nlmixr2/RcppTrust")

Example

trust() is a straight substitute for trust::trust():

library(RcppTrust)

# Rosenbrock's function, the example from ?trust::trust
objfun <- function(x) {
  f <- expression(100 * (x2 - x1^2)^2 + (1 - x1)^2)
  g1 <- D(f, "x1"); g2 <- D(f, "x2")
  h11 <- D(g1, "x1"); h12 <- D(g1, "x2"); h22 <- D(g2, "x2")
  x1 <- x[1]; x2 <- x[2]
  list(
    value = eval(f), gradient = c(eval(g1), eval(g2)),
    hessian = rbind(c(eval(h11), eval(h12)), c(eval(h12), eval(h22)))
  )
}

out <- trust(objfun, c(3, 1), 1, 5)
out[c("value", "argument", "converged", "iterations")]
#> $value
#> [1] 5.165437e-15
#> 
#> $argument
#> [1] 1 1
#> 
#> $converged
#> [1] TRUE
#> 
#> $iterations
#> [1] 21

Authors

  • Charles J. Geyer – original algorithm and R implementation (trust)
  • M. J. D. Powell – BOBYQA and NEWUOA algorithms and original Fortran 77 code
  • Douglas Bates, Katharine M. Mullen, John C. Nash, Ravi Varadhan – minqa, the R distribution of Powell’s code this port follows
  • Johan Larsson and basin contributors – basin’s Steihaug trust-region implementation
  • Matthew Fidler – thread-safe C++ ports (RcppTrust)

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("RcppTrust")

0.2.0 by Matthew Fidler, 4 days ago


https://github.com/nlmixr2/RcppTrust/, https://nlmixr2.github.io/RcppTrust/, http://www.stat.umn.edu/geyer/trust/


Browse source code at https://github.com/cran/RcppTrust


Authors: Charles J. Geyer [aut, cph] (ORCID: , Matthew Fidler [aut, cre] (ORCID: , Michael J. D. Powell [aut] (BOBYQA and NEWUOA algorithms and original Fortran 77 code) , Douglas Bates [ctb, cph] (minqa) , Katharine M. Mullen [ctb, cph] (minqa) , John C. Nash [ctb, cph] (minqa) , Ravi Varadhan [ctb, cph] (minqa) , Johan Larsson [ctb, cph] (basin trust-region / Steihaug implementation)


Documentation:   PDF Manual  


GPL-2 license


Imports Rcpp

Suggests testthat, stats, knitr, rmarkdown, trust, minqa, microbenchmark

Linking to Rcpp, RcppArmadillo, BH

System requirements: C++17


Imported by nlmixr2est.


See at CRAN