Skip to main content

echidna_optim/
result.rs

1use std::fmt;
2
3/// Result of an optimization run.
4#[derive(Debug, Clone)]
5pub struct OptimResult<F> {
6    /// Solution point.
7    pub x: Vec<F>,
8    /// Objective value at the solution.
9    pub value: F,
10    /// Gradient at the solution.
11    pub gradient: Vec<F>,
12    /// Norm of the gradient at the solution.
13    pub gradient_norm: F,
14    /// Number of outer iterations performed.
15    pub iterations: usize,
16    /// Total number of objective function evaluations.
17    pub func_evals: usize,
18    /// Reason for termination.
19    pub termination: TerminationReason,
20}
21
22/// Why the optimizer stopped.
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
24pub enum TerminationReason {
25    /// Gradient norm fell below tolerance.
26    GradientNorm,
27    /// Step size fell below tolerance.
28    StepSize,
29    /// Change in objective value fell below tolerance.
30    FunctionChange,
31    /// Reached the maximum number of iterations.
32    MaxIterations,
33    /// Line search could not find a sufficient decrease.
34    LineSearchFailed,
35    /// A numerical error occurred (e.g. singular Hessian, NaN).
36    NumericalError,
37}
38
39impl fmt::Display for TerminationReason {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        match self {
42            TerminationReason::GradientNorm => write!(f, "gradient norm below tolerance"),
43            TerminationReason::StepSize => write!(f, "step size below tolerance"),
44            TerminationReason::FunctionChange => write!(f, "function change below tolerance"),
45            TerminationReason::MaxIterations => write!(f, "maximum iterations reached"),
46            TerminationReason::LineSearchFailed => write!(f, "line search failed"),
47            TerminationReason::NumericalError => write!(f, "numerical error"),
48        }
49    }
50}