Skip to main content

Module problem

Module problem 

Source
Expand description

Problem traits the user implements about their objective. Solvers bind on whichever subset they need (e.g. gradient descent requires CostFunction and Gradient; Nelder-Mead only needs CostFunction).

§Soft reject vs hard abort

Every problem trait method returns Result<_, Self::Error>. The two ways to signal “something went wrong” are deliberately distinct:

  • Soft reject (Ok(f64::INFINITY)): return +∞ from CostFunction::cost to reject a single point. Line searches treat it as worse and retreat; population solvers treat it as worst fitness. This is the right channel for “this x is outside my domain, but the solve should continue.”
  • Hard abort (Err(_)): return Err to terminate the entire solve. The error bubbles all the way out of Executor::run typed as Result<_, P::Error>. Use this when the failure is not about a particular x: a downstream service vanished, the user pressed cancel, an early-stopping criterion in the problem’s own state fired.

Problems that never fail in this way pick type Error = std::convert::Infallible; (or ! on nightly). Niche optimization collapses Result<f64, Infallible> to f64 layout, so the happy path stays zero-cost.

Structs§

EvalCounts
Per-kind evaluation counters carried by Problem.
Problem
Counting wrapper that solvers receive instead of &P directly.

Traits§

CostFunction
Scalar-valued objective f(x): Param → Output. The smallest problem trait: every solver binds at least on this.
Gradient
Analytic gradient ∇f(x): Param → Gradient. Required by first-order solvers (gradient descent, BFGS, …).
Hessian
Analytic Hessian H(x) = ∇²f(x): Param → Hessian for second-order solvers (Newton, trust-region-Newton). The associated Hessian matrix type lets solvers bound on the linear-algebra ops they need (LinearSolveSpd, SymmetricEigen, …) without baking in a backend.
HessianProduct
Matrix-free Hessian-vector products: v ↦ ∇²f(param) · v.
Jacobian
Analytic Jacobian J(x) = ∂r/∂x: Param → Jacobian for least-squares solvers (Gauss-Newton, LM, TRF). The associated Jacobian matrix type is what lets solvers bound on the linear-algebra ops they need (MatVec, LinearSolveSpd, …) without baking in a specific backend or assuming density.
MiniBatchGradient
Finite-sum gradient (1/|B|) Σ_{i ∈ B} ∇fᵢ(x) over a chosen subset B of component samples. Required by mini-batch stochastic solvers (Sgd), which call it once per step with a fresh batch of indices the solver drew from its own ChaCha8Rng.
Residual
Vector-valued residual r(x): Param → Output for least-squares problems. Required by Gauss-Newton, Levenberg-Marquardt, and any solver that minimizes ½‖r(x)‖².