Skip to main content

spectrafit_trust_region/
report.rs

1//! Generic trust-region outcome types, shared by every method built on this
2//! framework (Levenberg–Marquardt, dogleg, Newton-CG/Steihaug, …).
3//!
4//! A method's driver constructs a [`Report`] with a [`Termination`] reason; the
5//! framework owns these so consumers speak one vocabulary regardless of which
6//! method ran.
7
8/// Why the solve stopped.
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum Termination {
11    /// `‖Jᵀr‖_∞ ≤ gtol` — first-order optimality. For a bounded problem the
12    /// LM driver also reports it when the projected gradient vanishes: every
13    /// component is held at a bound with an outward gradient, or the
14    /// active-set projected step is fully clamped by the box (a step that
15    /// merely rounds away against the parameters does not count).
16    Gtol,
17    /// Relative cost decrease `≤ ftol`.
18    Ftol,
19    /// Relative step size `≤ xtol`.
20    Xtol,
21    /// Residuals are (numerically) zero.
22    ResidualsZero,
23    /// Exhausted the residual-evaluation budget.
24    MaxEval,
25    /// The trust-region control diverged without an accepted step.
26    NoImprovement,
27    /// A residual/Jacobian evaluation failed.
28    NumericalError,
29}
30
31impl Termination {
32    /// Whether the run reached a convergence criterion (vs. a budget/error stop).
33    pub fn was_successful(&self) -> bool {
34        matches!(
35            self,
36            Termination::Gtol | Termination::Ftol | Termination::Xtol | Termination::ResidualsZero
37        )
38    }
39}
40
41/// Outcome of a solve. The optimised parameters live in the `problem` (read via
42/// [`TrustRegionProblem::params`](crate::TrustRegionProblem::params)); this only
43/// carries diagnostics.
44///
45/// Not `Copy`: it carries the per-iteration convergence trajectory (`cost_history`
46/// / `gradient_norm_history`) as owned `Vec`s. These are observability only — they
47/// do not affect the optimisation and are recorded once per accepted point plus the
48/// terminal point.
49#[derive(Debug, Clone)]
50pub struct Report {
51    /// Why the loop stopped.
52    pub termination: Termination,
53    /// Accepted iterations.
54    pub n_iter: usize,
55    /// Residual evaluations.
56    pub n_residual_evals: usize,
57    /// Jacobian evaluations.
58    pub n_jacobian_evals: usize,
59    /// Final `½‖r‖²`.
60    pub cost: f64,
61    /// `‖Jᵀr‖_∞` **at the most recent point where the Jacobian was evaluated**
62    /// — the start of the final outer iteration.
63    ///
64    /// This is the definition for every termination reason, and it is what
65    /// makes the number comparable across them. It is deliberately *not*
66    /// "‖Jᵀr‖_∞ at the returned parameters": on an `Ftol`/`Xtol`/`MaxEval` stop
67    /// the loop exits after accepting a step, and no Jacobian has been
68    /// evaluated at that new point, so reporting a gradient there would mean
69    /// either paying for an extra Jacobian evaluation or pairing the stale `J`
70    /// with the new `r` — a product that is the gradient at neither point. The
71    /// drivers therefore report the last genuinely-evaluated gradient instead.
72    /// On a `Gtol` stop the two coincide, since that test fires before any
73    /// step is taken.
74    pub gradient_norm: f64,
75    /// `½‖r‖²` at each accepted point (index 0 = initial), ending at the terminal
76    /// cost. Empty only for an immediate pre-iteration numerical failure.
77    pub cost_history: Vec<f64>,
78    /// `‖Jᵀr‖_∞` recorded alongside each `cost_history` entry, under the same
79    /// "at the most recent Jacobian evaluation" definition as
80    /// [`gradient_norm`](Self::gradient_norm) — so the terminal entry repeats
81    /// the previous iteration's gradient when the loop stopped after an
82    /// accepted step.
83    pub gradient_norm_history: Vec<f64>,
84    /// The free-parameter vector `θ` at each accepted point, recorded alongside
85    /// each `cost_history` entry (same length and ordering). This is the raw
86    /// material for the convergence-to-truth metric `dₖ = ‖(θₖ − θ_true)/s‖₂`
87    /// on synthetic cases — observability only, it does not affect the solve.
88    /// Empty for solvers that do not track it (only the faer LM driver records
89    /// it today; the trust-region / dogleg / newton-cg drivers leave it empty).
90    pub params_history: Vec<Vec<f64>>,
91}