tpt-math-optimize-general
General numerical optimisation: in-house closure-driven minimisers for smooth,
unconstrained problems (steepest descent, nonlinear conjugate gradient, Newton,
L-BFGS, Nelder–Mead), plus Levenberg–Marquardt least squares, curve fitting and
a small dense linear-programme solver. Everything is built directly on
tpt-math-linalg-dense's DVector/DMatrix. A cost function and a starting
point are enough to get an answer, with no external optimisation framework in
the dependency graph.
Part of tpt-math
Part of the tpt-math workspace —
the numeric substrate for tpt-science, tpt-engineering, and tpt-formal.
It is the general-purpose half of the optimisation layer (the convex/QP half is
tpt-math-optimize-convex), and is re-exported by the tpt-math-optimize
umbrella crate. Parameters are plain tpt-math-linalg-dense DVector<f64>s from the same
in-house storage that tpt-math-linalg wraps.
Features
This crate has no optional features: default = [] and everything described
here is always available. It is not #![no_std], so it needs std.
The dependency set is fixed rather than feature-gated: tpt-math-linalg,
tpt-math-linalg-dense, tpt-math-numeric and tpt-math-autodiff-rev (the
last one backs minimize_lbfgs_autodiff).
Quick start
[]
= "0.2"
Minimise f(x, y) = (x - 3)² + (y - 2)² by gradient descent:
use DVector;
use minimize_gradient_descent;
let cost = ;
let grad = ;
let best = minimize_gradient_descent.unwrap;
assert!;
assert!;
The three convenience solvers:
| Function | Method | Needs |
|---|---|---|
minimize_gradient_descent |
steepest descent + More-Thuente line search | cost, gradient |
minimize_conjugate_gradient |
nonlinear CG (Polak–Ribière+, with periodic restarts) + More-Thuente line search | cost, gradient |
minimize_newton |
Newton's method (full step, analytic Hessian) | cost, gradient, Hessian |
minimize_lbfgs |
limited-memory BFGS | cost, gradient |
minimize_nelder_mead |
Nelder–Mead simplex (derivative-free) | cost only |
minimize_lbfgs_autodiff |
L-BFGS with gradients from tpt-math-autodiff-rev |
cost only |
Other entry points:
levenberg_marquardtandcurve_fit(inleast_squares) for nonlinear least squares, with optional box bounds throughLeastSquaresOptions.linear_program::maximizeformax cᵀxsubject toA x ≤ b, returning anLpSolutionor anLpError.
Each minimize_* function has a *_with variant where one exists.
Each has a *_with variant that takes Options and returns a Solution
reporting the parameter vector, the cost re-evaluated there, the iteration
count, whether the gradient tolerance was met, and a human-readable
termination reason:
use DVector;
use ;
let cost = ;
let grad = ;
let options = new.with_gradient_tolerance;
let solution = minimize_conjugate_gradient_with.unwrap;
assert!;
assert!;
Notes
- Nothing is hidden:
tpt_math_linalgandtpt_math_linalg_denseare both re-exported, so you can build on the same parameter types without a second, possibly version-skewed, dependency on them. - Errors are flattened to a
Stringmessage rather than a typed error type. - Every run stops at
max_itersor earlier once the gradient's L2 norm falls toOptions::gradient_tolerance(default1e-9). That early stop matters: line searches fail outright on a numerically zero gradient, so without it an already-converged run would report an error instead of its answer. - Optimisation is deliberately unit-less — a cost mixes every unit in the
problem — but unit-tagged
tpt_math_linalg::Vecvalues can be moved in and out withpoint_from_tagged/point_to_tagged(TaggedVec<U>). minimize_newtontakes no line search; the step is the full Newton step.- Empty, non-finite initial points and negative gradient tolerances are rejected up front with a clear message.
#![forbid(unsafe_code)].- This crate is dual-licensed
MIT OR Apache-2.0, matching the rest of the workspace, and depends only on crates from this workspace (tpt-math-linalg,tpt-math-linalg-dense,tpt-math-numeric,tpt-math-autodiff-rev) — no third-party optimisation framework.
License
Licensed under either of MIT or Apache-2.0 at your option.