14. Installation and First Solve
This chapter covers the build environment, the dependency declaration, a first runnable program and how to read results.
14.1 Environment
- Rust toolchain 1.88 or newer, edition 2024;
- Cargo with
cargo testavailable; - the build fetches
nalgebra,randandrand_chachafrom crates.io.
Check the toolchain versions:
rustc --version
cargo --version
14.2 Dependency declaration
Add nlopt4r to the application’s Cargo.toml from crates.io:
[dependencies]
nlopt4r = "1.0.1"
The crate page is https://crates.io/crates/nlopt4r, and the generated API reference for 1.0.1 is https://docs.rs/nlopt4r/1.0.1/nlopt4r/.
For a local checkout, use a path dependency instead (adjust the path):
[dependencies]
nlopt4r = { path = "../optimization/nlopt" }
The nlopt4r package version is 1.0.1 with rust-version = "1.88". The repository workspace contains four members: the solver, the benchmark tool, the native comparison tool and the extension example; applications depend on the solver package alone.
14.3 First program
The following program minimizes the quadratic
whose optimum is \((3,-2)\) with value 0:
use nlopt4r::{minimize, Method, Options, Vector};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let target = Vector::from_vec(vec![3.0, -2.0]);
let objective = (
|x: &Vector| 0.5 * (x - &target).norm_squared(),
|x: &Vector| x - &target,
);
let result = minimize(
&objective, &Vector::zeros(2), Method::Bfgs, &Options::default(),
)?;
if !result.converged() {
return Err("optimization exhausted its iteration budget".into());
}
println!("x = {}, f = {}", result.x, result.value);
Ok(())
}
Key points:
- the objective and analytic gradient enter as a closure tuple
(value, gradient);Vectoris an alias ofnalgebra::DVector<f64>; Options::default()supplies a 5000-iteration cap, gradient tolerance \(10^{-6}\) and Armijo parameters;result.converged()checks the convergence status; read this field before using the returned point.
14.4 Running the repository examples
Run these commands from a repository checkout; a downstream application that depends on the crates.io package does not include the examples.
cargo run --example rosenbrock # BFGS on the two-dimensional Rosenbrock function
cargo run --example global_search # DE/PSO comparison on a three-dimensional sphere
rosenbrock starts from \((-1.2,1)\) and checks an objective \(\le10^{-12}\) together with the coordinate error; global_search compares DE and PSO on \([-4,4]^3\) with a budget of 15000 calls, seed 7 for both and target \(10^{-8}\). Both examples return an error with a nonzero exit code when a check fails.
14.5 Reading results
Key Solution fields:
| Field | Meaning |
|---|---|
x, value | Returned point and objective value |
gradient_norm | Stationarity norm of the solver family (section 1.7) |
iterations | Outer iteration count |
status | Converged, MaxIterations, MaxEvaluations, Stagnated or Failed |
termination_reason | The specific criterion |
evaluations | Per-class objective/gradient/Hessian counts |
Standard reading flow: read status; with Converged, use x and value; with another status, read termination_reason and adjust budget or tolerances.
14.6 API documentation: docs.rs and local
The published 1.0.1 API reference is at https://docs.rs/nlopt4r/1.0.1/nlopt4r/. To generate the same documentation from a checkout:
cargo doc -p nlopt4r --no-deps --open
The documentation carries each module’s mathematical conventions, input shapes, defaults, diagnostic fields and runnable examples. Strict checks:
RUSTDOCFLAGS="-D warnings -D missing_docs" cargo doc -p nlopt4r --no-deps
cargo test -p nlopt4r --doc
Theory background
- Problem form and convergence quantities: see chapter 1.
- Quasi-Newton methods and BFGS: see chapter 4.
- Stopping and report fields: see chapter 12 and chapter 20.