Introduction
This book documents the mathematics, interface usage and measured results of nlopt4r, a pure-Rust nonlinear optimization library. The crate version is 1.0.1, the minimum Rust version is 1.88, and the edition is 2024. Every solver takes dynamically sized f64 vectors; the runtime dependencies are the Rust crates nalgebra, rand and rand_chacha.
The book describes the published 1.0.1 API. Code excerpts, defaults and command examples were checked against the 1.0.1 source.
Publication and API documentation
The crate is published on crates.io:
[dependencies]
nlopt4r = "1.0.1"
The generated API documentation is at https://docs.rs/nlopt4r/1.0.1/nlopt4r/. The help chapters link to the corresponding item pages on docs.rs.
Book structure
The book has two parts and 22 chapters:
- Part I: Theory (chapters 1–13) presents the mathematical models, update formulas, stopping criteria and reference baselines of each solver family.
- Part II: Software Help (chapters 14–22) presents function signatures, option defaults, report fields, error categories and runnable examples.
Chapters correspond one-to-one across the two parts by solver family. Each theory chapter ends with a “Software interface” section that lists the family’s entry points and links to the matching help chapter; each help chapter opens with a “Theory background” section that links back.
Solver overview
| Problem class | Entry points | Required derivatives |
|---|---|---|
| Smooth unconstrained, small/medium | minimize + Method | Gradient; Newton variants also a Hessian |
| Smooth unconstrained, large | local::lbfgs | Gradient |
| Hessian-vector products available | local::newton_cg, local::trust_region_cg | Gradient and HVP |
| Small dense trust region | trust_region::minimize | Gradient and Hessian |
| Finite box, gradient available | bounded::projected_gradient | Gradient |
| Finite box, derivative-free local search | derivative_free::{pattern_search, nelder_mead} | Objective values |
| Finite box, multimodal global search | global::{differential_evolution, particle_swarm} | Objective values |
| Residuals and Jacobians | least_squares::levenberg_marquardt_with_options | Jacobian |
| Convex quadratic programming | quadratic::minimize | Model data |
| Nonlinear constraints | constrained::sqp_with_restoration, constrained::augmented_lagrangian | Gradient and constraint Jacobians |
Chapters 16–19 contain the complete entry-point list, parameters and examples.
Notation
The book uses one notation set:
- \(x\in\mathbb{R}^n\) is the decision variable, \(f(x)\) the objective, \(g=\nabla f(x)\) the gradient and \(H=\nabla^2 f(x)\) the Hessian.
- The standard constraint form is \(h(x)=0\) (equalities) and \(g(x)\ge0\) (inequalities); each constraint occupies one Jacobian row.
- The Lagrangian is \(L(x,\mu,\lambda)=f(x)-\mu^\top h(x)-\lambda^\top g(x)\) with \(\lambda\ge0\).
- Least squares minimizes \(f(x)=\lVert r(x)\rVert^2/2\), with gradient \(g=J^\top r\) and Jacobian \(J\) of shape “residuals × variables”.
- Quadratic programming minimizes \(x^\top Hx/2+c^\top x\) subject to \(A_e x=b_e\) and \(A_i x\ge b_i\).
EvaluationCountsrecords objective, gradient, Hessian, constraint, residual, Jacobian and subproblem-model calls in separate fields.
Site and language switching
The Chinese book occupies the site root and the English book occupies /en/. The English button on the right of the menu bar opens the current chapter in English; the 中文 button on an English page returns to the matching Chinese page. Chapter files correspond one-to-one, with identical filenames.
Local build and deployment
The site builds with mdBook 0.5.4:
cargo install mdbook --version 0.5.4 --locked
mdbook build books/zh && mdbook build books/en
python3 -m http.server 8000 -d book # open http://localhost:8000/
Deployment uses a manual GitHub Actions run: Actions → Deploy nlopt4r book to GitHub Pages → Run workflow. The workflow builds both books and publishes book/. Chapter 22 documents the local wrkflw rehearsal of the build job.
Running the examples
The repository ships two runnable examples; run them from a repository checkout, because a downstream application that depends on the crates.io package does not provide cargo run --example. The measured benchmark tables in chapter 13 come from the separate nlopt4r-bench harness, not from these examples:
cargo run --example rosenbrock
cargo run --example global_search
rosenbrock minimizes the two-dimensional Rosenbrock function with BFGS; global_search compares DE and PSO on a three-dimensional sphere. Both examples check known solutions or target values and report a nonzero exit code when a check fails.
The published API reference is at https://docs.rs/nlopt4r/1.0.1/nlopt4r/; cargo doc -p nlopt4r --no-deps --open generates the same documentation locally.