Skip to main content

lbfgsb

Function lbfgsb 

Source
pub unsafe fn lbfgsb(
    n: c_int,
    m: c_int,
    x: *mut f64,
    l: *mut f64,
    u: *mut f64,
    nbd: *mut c_int,
    fmin: *mut f64,
    fn_: optimfn,
    gr: optimgr,
    fail: *mut c_int,
    ex: *mut c_void,
    factr: f64,
    pgtol: f64,
    fncount: *mut c_int,
    grcount: *mut c_int,
    maxit: c_int,
    msg: *mut c_char,
    trace: c_int,
    nreport: c_int,
)
Expand description

Bound-constrained limited-memory BFGS minimiser: lbfgsb in R_ext/Applic.h, the routine behind optim(method = "L-BFGS-B").

§Arguments

  • n: number of parameters. With n = 0 it calls fn_(0, u, ex) once and returns fail = 0, fncount = 1, grcount = 0 and the message "NOTHING TO DO".
  • m: number of correction pairs kept (optim’s lmm, default 5).
  • x: n starting values; overwritten with the solution.
  • l, u: n lower and upper bounds, read where nbd says so.
  • nbd: n bound codes: 0 unbounded, 1 lower bound only, 2 both bounds, 3 upper bound only. Code 2 with l[i] == u[i] fixes parameter i.
  • fmin: receives the last objective value computed, the one at the returned x on convergence. It is not meaningful when the input checks fail (fail = 52 before any evaluation).
  • fn_, gr: objective and gradient callbacks. Both are required (R calls them unconditionally).
  • fail: receives 0 (converged), 1 (maxit reached; the message is then "NEW_X"), 51 (a "WARNING: ..." from the line search) or 52 (an "ERROR: ...", such as an invalid nbd code or l[i] > u[i] under code 2).
  • ex: passed unchanged to both callbacks.
  • factr: relative-reduction tolerance in units of machine epsilon (optim’s default 1e7).
  • pgtol: projected-gradient tolerance (with optim’s default 0 only an exactly zero projected gradient stops on it).
  • fncount, grcount: receive the evaluation counts (R stores the same count in both, except with n = 0: see n above).
  • maxit: iteration limit.
  • msg: caller-owned buffer of at least 60 bytes (C char msg[60]); receives a NUL-terminated status such as "CONVERGENCE: REL_REDUCTION_OF_F <= FACTR*EPSMCH".
  • trace: 0 is silent; 1 to 6 print progress through Rprintf.
  • nreport: progress interval in iterations. It must be at least 1 even when trace = 0.

The routine keeps a limited-memory approximation of the inverse Hessian internally and does not return it. That approximation is a search device, not an inferential covariance: compute the Hessian at the optimum (as stats::optimHess does) for standard errors.

§Safety

  • x, l, u and nbd must be valid for n elements, msg must be writable for 60 bytes, and the other out-pointers must be valid. ex must stay valid for the whole call: the callbacks receive it on every evaluation.
  • lbfgsb raises R errors itself (an R longjmp): when nreport <= 0, and when the objective returns a non-finite value (“L-BFGS-B needs finite values of ‘fn’”). Call it where such a longjmp is caught: a #[miniextendr] function body, or with_r_unwind_protect. #[r_ffi_checked] gives thread routing and checking only, not an unwind guard.
  • The callbacks must not let a Rust panic unwind through lbfgsb’s frames (see optimfn).
  • The workspace comes from R_alloc / S_alloc, R’s transient allocation stack. R reclaims it when the enclosing .Call returns; outside .Call, bracket the call with vmaxget / vmaxset. Checked wrapper for lbfgsb. Calls lbfgsb_unchecked and routes through with_r_thread. Generated from source location line 1894, column 12. Generated from source file miniextendr-api/src/sys.rs.