scirs2_optimize/
result.rs

1//! Optimization result structures
2//!
3//! This module defines the common result structures for optimization algorithms.
4
5use ndarray::Array1;
6use std::fmt;
7
8/// A structure that contains the results of an optimization.
9///
10/// This structure is modeled after SciPy's `OptimizeResult` structure.
11#[derive(Clone, Debug)]
12pub struct OptimizeResults<T> {
13    /// The solution array
14    pub x: Array1<T>,
15
16    /// Value of objective function at the solution
17    pub fun: T,
18
19    /// Value of the gradient/jacobian at the solution
20    pub jac: Option<Vec<T>>,
21
22    /// Value of the Hessian at the solution
23    pub hess: Option<Vec<T>>,
24
25    /// Value of constraint functions at the solution (if applicable)
26    pub constr: Option<Array1<T>>,
27
28    /// Number of iterations performed
29    pub nit: usize,
30
31    /// Number of evaluations of the objective function
32    pub nfev: usize,
33
34    /// Number of evaluations of the gradient
35    pub njev: usize,
36
37    /// Number of evaluations of the Hessian
38    pub nhev: usize,
39
40    /// Maximum number of iterations exceeded flag
41    pub maxcv: usize,
42
43    /// Termination message
44    pub message: String,
45
46    /// Whether or not the optimizer exited successfully
47    pub success: bool,
48
49    /// Termination status code
50    pub status: i32,
51}
52
53impl<T: fmt::Display + fmt::Debug> fmt::Display for OptimizeResults<T>
54where
55    T: Copy,
56    Array1<T>: fmt::Debug,
57{
58    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59        writeln!(f, "Optimization Results:")?;
60        writeln!(f, "  success: {}", self.success)?;
61        writeln!(f, "  status: {}", self.status)?;
62        writeln!(f, "  message: {}", self.message)?;
63        writeln!(f, "  nfev: {}", self.nfev)?;
64        writeln!(f, "  njev: {}", self.njev)?;
65        writeln!(f, "  nhev: {}", self.nhev)?;
66        writeln!(f, "  nit: {}", self.nit)?;
67        writeln!(f, "  final value: {}", self.fun)?;
68        writeln!(f, "  solution: {:?}", self.x)?;
69        if let Some(ref jac) = self.jac {
70            writeln!(f, "  jacobian: {:?} (vector)", jac)?;
71        }
72        Ok(())
73    }
74}
75
76impl<T> Default for OptimizeResults<T>
77where
78    T: Default + Clone + num_traits::Zero,
79{
80    fn default() -> Self {
81        OptimizeResults {
82            x: Array1::<T>::zeros(0),
83            fun: T::default(),
84            jac: None,
85            hess: None,
86            constr: None,
87            nit: 0,
88            nfev: 0,
89            njev: 0,
90            nhev: 0,
91            maxcv: 0,
92            message: String::new(),
93            success: false,
94            status: 0,
95        }
96    }
97}