16. Local Solver Reference
This chapter lists the entries, option defaults and report fields of the local smooth solvers. The theory is in chapters 3–7.
16.1 Legacy dense methods: minimize and Method
pub fn minimize<P: Evaluator + ?Sized>(
problem: &P,
x0: &Vector<f64>,
method: Method,
options: &Options,
) -> Result<Solution, Error>
API pages (docs.rs): minimize, Method, Options, Solution.
Method values:
| Variant | Direction/model | Memory |
|---|---|---|
SteepestDescent | \(d=-g\) | \(O(n)\) |
NormalizedGradient | \(d=-g/\lVert g\rVert\) | \(O(n)\) |
FletcherReeves | Conjugate gradient, restart every \(n+1\) steps | \(O(n)\) |
DampedNewton | Dense Newton system + Armijo | \(O(n^2)\) |
ModifiedNewton | Positive-definite shifted Hessian | \(O(n^2)\) |
Bfgs | Dense inverse BFGS | \(O(n^2)\) |
Dfp | Dense inverse DFP | \(O(n^2)\) |
Sr1 | Symmetric rank-one inverse update | \(O(n^2)\) |
Broyden { phi } | Broyden convex family, 0 for DFP and 1 for BFGS | \(O(n^2)\) |
Options combines two settings:
pub struct Options {
pub stopping: StopOptions, // defaults in chapter 12
pub line_search: ArmijoOptions, // defaults in chapter 3
}
All legacy methods use Armijo backtracking; the dense Newton variants read evaluate_hessian and return an explicit error on None.
Call example (BFGS):
#![allow(unused)]
fn main() {
use nlopt4r::{minimize, Method, Options, Vector};
let problem = (
|x: &Vector| (x[0] - 1.0).powi(2) + (x[1] + 1.0).powi(2),
|x: &Vector| Vector::from_vec(vec![2.0 * (x[0] - 1.0), 2.0 * (x[1] + 1.0)]),
);
let result = minimize(&problem, &Vector::zeros(2), Method::Bfgs, &Options::default())?;
assert!(result.converged());
assert!(result.gradient_norm <= 1e-6);
Ok::<(), nlopt4r::Error>(())
}
16.2 Calling Armijo backtracking directly
pub fn armijo<P: Evaluator + ?Sized>(
problem: &P,
x: &Vector<f64>,
direction: &Vector<f64>,
options: &ArmijoOptions,
) -> Result<Step, Error>
API pages (docs.rs): armijo, ArmijoOptions, Step.
- one value/gradient request at the starting point, then value-only requests at trial points;
Stepfields:alpha(accepted step),value(trial value),evaluations(trial count, with the starting call counted separately);- the direction satisfies \(g^\top d<0\); exhausted backtracking returns
Error::LineSearchFailed { evaluations, last_step }.
ArmijoOptions defaults:
| Field | Default |
|---|---|
initial_step | 1 |
contraction | 0.5 |
sufficient_decrease | 0.2 |
max_iterations | 60 |
16.3 Bounded projected gradient
pub fn projected_gradient<P: Evaluator + ?Sized>(
problem: &P,
x0: &Vector,
bounds: &Bounds,
options: &Options,
) -> Result<Solution, Error>
API pages (docs.rs): projected_gradient, projected_gradient_norm, Bounds.
Properties:
- the initial point projects into the box, and every callback point is feasible;
- the direction is \(d=P(x-g)-x\), and backtracking tests \(x+\alpha d\) with every trial projected into the box;
Solution::gradient_normis the gradient-mapping norm \(\lVert x-P(x-g)\rVert\), zero at active boundary points;- uses the gradient tolerance of
Options::stoppingand the Armijo parameters ofOptions::line_search.
Computing the mapping norm separately:
pub fn projected_gradient_norm(
x: &Vector,
gradient: &Vector,
bounds: &Bounds,
) -> Result<f64, Error>
Example:
#![allow(unused)]
fn main() {
use nlopt4r::{Bounds, Options, Vector, bounded::projected_gradient};
let problem = (
|x: &Vector| (x[0] - 3.0).powi(2),
|x: &Vector| Vector::from_vec(vec![2.0 * (x[0] - 3.0)]),
);
let bounds = Bounds::uniform(1, -1.0, 1.0)?;
let result = projected_gradient(&problem, &Vector::zeros(1), &bounds, &Options::default())?;
assert!(result.converged());
assert!((result.x[0] - 1.0).abs() < 1e-6);
Ok::<(), nlopt4r::Error>(())
}
16.4 L-BFGS
pub fn lbfgs<P: Evaluator + ?Sized>(
problem: &P,
x0: &Vector,
options: &LbfgsOptions,
) -> Result<Solution, Error>
API pages (docs.rs): lbfgs, LbfgsOptions.
- stores
history_sizecorrection pairs with \(O(m n)\) memory; - outer steps use the strong Wolfe conditions, and the accepted point’s value/gradient is reused directly;
- correction pairs with curvature below \(s^\top y>10^{-10}\lVert s\rVert\lVert y\rVert\) are skipped;
- an anomalous direction resets the history and falls back to steepest descent.
LbfgsOptions defaults:
| Field | Default |
|---|---|
stopping | StopOptions::default() |
line_search | StrongWolfeOptions::default() |
history_size | 10 |
curvature_tolerance | 1e-10 |
Example (two-dimensional Rosenbrock):
#![allow(unused)]
fn main() {
use nlopt4r::{local::{lbfgs, LbfgsOptions}, Vector};
let rosenbrock = (
|x: &Vector| (1.0 - x[0]).powi(2) + 100.0 * (x[1] - x[0] * x[0]).powi(2),
|x: &Vector| Vector::from_vec(vec![
-2.0 * (1.0 - x[0]) - 400.0 * x[0] * (x[1] - x[0] * x[0]),
200.0 * (x[1] - x[0] * x[0]),
]),
);
let result = lbfgs(
&rosenbrock,
&Vector::from_vec(vec![-1.2, 1.0]),
&LbfgsOptions::default(),
)?;
assert!(result.converged());
Ok::<(), nlopt4r::Error>(())
}
evaluations.objective and evaluations.gradient record the actual callback counts; a budget interruption returns a non-converged report.
16.5 Newton-CG (matrix-free)
pub trait HessianVector: Evaluator {
fn hessian_vector(&self, x: &[f64], v: &[f64], product: &mut [f64]) -> Result<(), Error>;
}
pub fn newton_cg<P: HessianVector + ?Sized>(
problem: &P,
x0: &Vector,
options: &NewtonCgOptions,
) -> Result<Solution, Error>
API pages (docs.rs): HessianVector, newton_cg, NewtonCgOptions.
- the inner truncated CG caps iterations at
min(max_cg_iterations, n); - the inner termination condition is \(\lVert r\rVert\le\texttt{cg_tolerance}\cdot\lVert g\rVert\);
- a direction of curvature at or below zero ends the inner loop, and the outer loop runs a strong Wolfe search along the resulting direction;
- each Hessian-vector product counts in
evaluations.hessian.
NewtonCgOptions defaults:
| Field | Default |
|---|---|
stopping | StopOptions::default() |
line_search | StrongWolfeOptions::default() |
max_cg_iterations | 100 |
cg_tolerance | 0.1 |
Example:
#![allow(unused)]
fn main() {
use nlopt4r::{Error, Evaluator, Vector, local::{HessianVector, NewtonCgOptions, newton_cg}};
struct Sphere;
impl Evaluator for Sphere {
fn evaluate(&self, x: &[f64], gradient: Option<&mut [f64]>) -> Result<f64, Error> {
if let Some(g) = gradient {
for (out, &v) in g.iter_mut().zip(x) {
*out = 2.0 * v;
}
}
Ok(x.iter().map(|v| v * v).sum())
}
}
impl HessianVector for Sphere {
fn hessian_vector(&self, _: &[f64], v: &[f64], out: &mut [f64]) -> Result<(), Error> {
for (out, &v) in out.iter_mut().zip(v) {
*out = 2.0 * v;
}
Ok(())
}
}
let result = newton_cg(&Sphere, &Vector::from_element(10, 1.0), &NewtonCgOptions::default())?;
assert!(result.converged());
Ok::<(), Error>(())
}
16.6 Dense trust region
pub fn minimize<P: Evaluator + ?Sized>(
problem: &P,
x0: &Vector,
options: &TrustRegionOptions,
) -> Result<Solution, Error>
API pages (docs.rs): trust_region::minimize, subproblem, TrustRegionOptions, TrustRegionStep.
- each step solves the quadratic subproblem \(\min g^\top d+\tfrac12 d^\top B d\), \(\lVert d\rVert\le\Delta\), globally via eigendecomposition;
- the ratio \(\rho=\text{actual decrease}/\text{model decrease}\) above
acceptance_thresholdaccepts the step; - a subproblem solution on the radius boundary with ratio at least 0.75 doubles the radius, and a low ratio halves it.
TrustRegionOptions defaults:
| Field | Default |
|---|---|
stopping | StopOptions::default() |
initial_radius | 1 |
maximum_radius | 2 |
acceptance_threshold | 0.15 |
The subproblem is also callable on its own:
#![allow(unused)]
fn main() {
use nlopt4r::{Matrix, Vector, trust_region::subproblem};
let g = Vector::from_vec(vec![2.0, 0.0]);
let step = subproblem(&g, &Matrix::identity(2, 2), 1.0)?;
assert!((step.step.norm() - 1.0).abs() < 1e-8);
Ok::<(), nlopt4r::Error>(())
}
TrustRegionStep fields: step (displacement), value (model change \(g^\top d+\tfrac12d^\top Bd\)), multiplier (multiplier \(\lambda\ge0\) satisfying \((B+\lambda I)d=-g\)).
16.7 Steihaug matrix-free trust region
pub fn steihaug<F>(
g: &Vector,
radius: f64,
options: &SteihaugOptions,
multiply: F,
) -> Result<SteihaugStep, Error>
where
F: FnMut(&Vector) -> Result<Vector, Error>
pub fn trust_region_cg<P: HessianVector + ?Sized>(
problem: &P,
x0: &Vector,
options: &TrustRegionCgOptions,
) -> Result<Solution, Error>
API pages (docs.rs): steihaug, trust_region_cg, SteihaugOptions, TrustRegionCgOptions, SteihaugStep, SteihaugTermination.
SteihaugStep fields: step, value, iterations, hessian_products, termination: SteihaugTermination. Inner termination reasons:
| Variant | Condition |
|---|---|
ResidualTolerance | Residual norm reaches relative_tolerance times the starting norm |
Boundary | Positive-curvature CG reaches the trust-region boundary |
NegativeCurvature | A direction of nonpositive curvature continues to the better point on the boundary |
MaxIterations | Inner iteration cap |
SteihaugOptions defaults: max_iterations = 100, relative_tolerance = 0.1.
TrustRegionCgOptions defaults: initial radius 1, maximum radius 100, acceptance threshold 0.1, subproblem from SteihaugOptions::default().
16.8 Strong Wolfe utility
pub fn strong_wolfe<F>(
value: f64,
gradient: &Vector,
direction: &Vector,
options: &StrongWolfeOptions,
evaluate: F,
) -> Result<StrongWolfeStep, Error>
where
F: FnMut(f64) -> Result<(f64, Vector), Error>
API pages (docs.rs): strong_wolfe, StrongWolfeOptions, StrongWolfeStep.
The callback returns trial value and gradient for a step length; the accepted state StrongWolfeStep carries alpha, value and gradient for direct reuse. StrongWolfeOptions defaults: sufficient_decrease = 1e-4, curvature = 0.9, initial_step = 1, maximum_step = 1e20, max_evaluations = 80.
16.9 Scalar search
pub fn minimize_scalar<F: Fn(f64) -> f64>(
f: F,
bounds: (f64, f64),
tolerance: f64,
max_iterations: usize,
method: ScalarMethod,
) -> Result<ScalarSolution, Error>
pub fn minimize_scalar_with_options<F: Fn(f64) -> f64>(
f: F,
bounds: (f64, f64),
method: ScalarMethod,
options: &ScalarOptions,
) -> Result<ScalarSolution, Error>
API pages (docs.rs): minimize_scalar, minimize_scalar_with_options, ScalarMethod, ScalarOptions, ScalarSolution.
minimize_scalaris the convenience entry: one absolute interval tolerance and an iteration cap, with the relative tolerance fixed at \(\sqrt{\varepsilon_{\text{mach}}}\);minimize_scalar_with_optionstakes the fullScalarOptionsand a strict objective budget;ScalarMethod::GoldenSectionandScalarMethod::Parabolic(Brent-protected interpolation);- interval defaults: absolute \(10^{-8}\), relative \(\sqrt{\varepsilon_{\text{mach}}}\);
ScalarSolutionfields:x,value,bracket,iterations,status,termination_reason,evaluations.
#![allow(unused)]
fn main() {
use nlopt4r::line_search::{minimize_scalar_with_options, ScalarMethod, ScalarOptions};
let result = minimize_scalar_with_options(
|x| (x - 0.4).powi(2),
(-1.0, 2.0),
ScalarMethod::Parabolic,
&ScalarOptions::default(),
)?;
assert!((result.x - 0.4).abs() < 1e-6);
Ok::<(), nlopt4r::Error>(())
}
16.10 Selection guide
| Scenario | Entry |
|---|---|
| Small scale with analytic Hessian | Method::DampedNewton, Method::ModifiedNewton |
| Small scale, gradient only | Method::Bfgs, Method::Dfp, Method::Sr1 |
| Medium to large scale, gradient only | local::lbfgs |
| Large scale with computable Hessian-vector products | local::newton_cg, local::trust_region_cg |
| Variables with lower and upper bounds | bounded::projected_gradient |
| One-dimensional continuous unimodal | line_search::minimize_scalar* |
| Few variables, nonsmooth objective | derivative_free (chapter 17) |