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. Withn = 0it callsfn_(0, u, ex)once and returnsfail = 0,fncount = 1,grcount = 0and the message"NOTHING TO DO".m: number of correction pairs kept (optim’slmm, default 5).x:nstarting values; overwritten with the solution.l,u:nlower and upper bounds, read wherenbdsays so.nbd:nbound codes:0unbounded,1lower bound only,2both bounds,3upper bound only. Code2withl[i] == u[i]fixes parameteri.fmin: receives the last objective value computed, the one at the returnedxon convergence. It is not meaningful when the input checks fail (fail = 52before any evaluation).fn_,gr: objective and gradient callbacks. Both are required (R calls them unconditionally).fail: receives0(converged),1(maxitreached; the message is then"NEW_X"),51(a"WARNING: ..."from the line search) or52(an"ERROR: ...", such as an invalidnbdcode orl[i] > u[i]under code2).ex: passed unchanged to both callbacks.factr: relative-reduction tolerance in units of machine epsilon (optim’s default1e7).pgtol: projected-gradient tolerance (withoptim’s default0only an exactly zero projected gradient stops on it).fncount,grcount: receive the evaluation counts (R stores the same count in both, except withn = 0: seenabove).maxit: iteration limit.msg: caller-owned buffer of at least 60 bytes (Cchar msg[60]); receives a NUL-terminated status such as"CONVERGENCE: REL_REDUCTION_OF_F <= FACTR*EPSMCH".trace:0is silent;1to6print progress throughRprintf.nreport: progress interval in iterations. It must be at least1even whentrace = 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,uandnbdmust be valid fornelements,msgmust be writable for 60 bytes, and the other out-pointers must be valid.exmust stay valid for the whole call: the callbacks receive it on every evaluation.lbfgsbraises R errors itself (an R longjmp): whennreport <= 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, orwith_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 (seeoptimfn). - The workspace comes from
R_alloc/S_alloc, R’s transient allocation stack. R reclaims it when the enclosing.Callreturns; outside.Call, bracket the call withvmaxget/vmaxset. Checked wrapper forlbfgsb. Callslbfgsb_uncheckedand routes throughwith_r_thread. Generated from source location line 1894, column 12. Generated from source fileminiextendr-api/src/sys.rs.