Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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 test available;
  • the build fetches nalgebra, rand and rand_chacha from 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

$$ f(x,y)=\tfrac12\bigl((x-3)^2+(y+2)^2\bigr), $$

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); Vector is an alias of nalgebra::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:

FieldMeaning
x, valueReturned point and objective value
gradient_normStationarity norm of the solver family (section 1.7)
iterationsOuter iteration count
statusConverged, MaxIterations, MaxEvaluations, Stagnated or Failed
termination_reasonThe specific criterion
evaluationsPer-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