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