Skip to main content

Crate tpt_math_optimize_general

Crate tpt_math_optimize_general 

Source
Expand description

General numerical optimization — in-house smooth, unconstrained minimizers.

This crate provides closure-driven entry points for the common smooth, unconstrained minimization cases, built directly on tpt_math_linalg_dense’s DVector/DMatrix (no argmin/faer dependency):

Convenience functionMethod
minimize_gradient_descentsteepest descent + More-Thuente line search
minimize_conjugate_gradientnonlinear CG (Polak–Ribière+) + More-Thuente line search, periodic restarts
minimize_newtonNewton’s method (full step, analytic Hessian)

Parameters are plain DVector<f64>s; unit-tagged tpt_math_linalg::Vec values move in and out with point_from_tagged / point_to_tagged. Optimization itself is deliberately unit-less: a cost mixes every unit in the problem, so there is no meaningful tag for a search direction or a step length.

§Examples

Minimize f(x, y) = (x - 3)² + (y - 2)² by gradient descent:

use tpt_math_optimize_general::{minimize_gradient_descent, tpt_math_linalg_dense::DVector};

let cost = |p: &DVector<f64>| (p[0] - 3.0).powi(2) + (p[1] - 2.0).powi(2);
let grad = |p: &DVector<f64>| DVector::from_vec(vec![2.0 * (p[0] - 3.0), 2.0 * (p[1] - 2.0)]);

let best = minimize_gradient_descent(cost, grad, DVector::zeros(2), 100).unwrap();

assert!((best[0] - 3.0).abs() < 1e-6);
assert!((best[1] - 2.0).abs() < 1e-6);

The *_with variants take Options and return a Solution, which also reports the cost, the iteration count and why the solver stopped:

use tpt_math_optimize_general::{minimize_newton, tpt_math_linalg_dense::{DMatrix, DVector}};

// f(x) = exp(x) - x, minimized at x = 0 with f(0) = 1.
let cost = |p: &DVector<f64>| p[0].exp() - p[0];
let grad = |p: &DVector<f64>| DVector::from_vec(vec![p[0].exp() - 1.0]);
let hess = |p: &DVector<f64>| DMatrix::from_vec(1, 1, vec![p[0].exp()]);

let best = minimize_newton(cost, grad, hess, DVector::from_vec(vec![1.0]), 50).unwrap();

assert!(best[0].abs() < 1e-6);

§Conventions

  • Every run stops at max_iters, or earlier once the gradient’s L2 norm falls to Options::gradient_tolerance. The early stop matters: a solver that has already reached its optimum would otherwise keep iterating and report a worse (or non-finite) point.
  • The reported Solution::param is the best parameter vector found, and Solution::cost is the user cost function re-evaluated there.
  • A non-finite or empty initial point, or a non-invertible Hessian (Newton), is reported as an Err with a human-readable message.

Re-exports§

pub use autodiff_opt::minimize_lbfgs_autodiff;
pub use least_squares::curve_fit;
pub use least_squares::levenberg_marquardt;
pub use least_squares::LeastSquaresOptions;
pub use least_squares::LeastSquaresSolution;
pub use least_squares::LsTermination;
pub use tpt_math_linalg;
pub use tpt_math_linalg::tpt_math_linalg_dense;

Modules§

autodiff_opt
Optimisation with gradients taken automatically by reverse-mode autodiff.
least_squares
Nonlinear least squares: Levenberg–Marquardt, optionally with box bounds, and a curve_fit convenience for models y ≈ f(x; p).
linear_program
Linear programming by the two-phase simplex method.

Structs§

Options
Knobs shared by every convenience minimizer.
Solution
The outcome of a minimization run.

Constants§

DEFAULT_GRADIENT_TOLERANCE
Default gradient L2 norm at or below which a run counts as converged.
DEFAULT_MAX_ITERS
Default iteration budget used by Options::default.

Functions§

minimize_conjugate_gradient
Minimize a smooth objective with nonlinear conjugate gradients (Polak–Ribière+ β update with periodic restarts, More-Thuente line search).
minimize_conjugate_gradient_with
minimize_conjugate_gradient with explicit Options, reporting a full Solution.
minimize_gradient_descent
Minimize a smooth objective by gradient descent with a More-Thuente line search.
minimize_gradient_descent_with
minimize_gradient_descent with explicit Options, reporting a full Solution.
minimize_lbfgs
Minimize a smooth objective with limited-memory BFGS (L-BFGS).
minimize_lbfgs_with
minimize_lbfgs with explicit Options, reporting a full Solution.
minimize_nelder_mead
Minimize a derivative-free objective with Nelder–Mead, returning only the best point. See minimize_nelder_mead_with.
minimize_nelder_mead_with
Minimize a derivative-free objective with the Nelder–Mead simplex method.
minimize_newton
Minimize a smooth objective with Newton’s method, using the analytic Hessian.
minimize_newton_with
minimize_newton with explicit Options, reporting a full Solution.
numerical_gradient
Central finite-difference approximation of the gradient of cost at x, with step h · max(1, |xᵢ|) in each coordinate. Use it when no analytic gradient exists.
point_from_tagged
Copy a unit-tagged tpt_math_linalg::Vec into a raw parameter vector.
point_to_tagged
Tag a raw parameter vector with the unit U, undoing point_from_tagged.

Type Aliases§

TaggedVec
A unit-tagged vector of f64s, as used by tpt_math_linalg.