1#![doc = include_str!("../README.md")]
2
3mod cobyla;
4mod cobyla_solver;
5mod cobyla_state;
6pub use crate::cobyla_solver::*;
7pub use crate::cobyla_state::*;
8
9#[derive(Debug, Clone, Copy)]
11pub enum FailStatus {
12 Failure,
13 InvalidArgs,
14 OutOfMemory,
15 RoundoffLimited,
16 ForcedStop,
17 UnexpectedError,
18}
19
20#[derive(Debug, Clone, Copy)]
22pub enum SuccessStatus {
23 Success,
24 StopValReached,
25 FtolReached,
26 XtolReached,
27 MaxEvalReached,
28 MaxTimeReached,
29}
30
31#[derive(Debug, Clone, Default)]
42pub struct StopTols {
43 pub ftol_rel: f64,
45 pub ftol_abs: f64,
47 pub xtol_rel: f64,
49 pub xtol_abs: Vec<f64>,
51}
52
53pub enum RhoBeg {
56 All(f64),
58 Set(Vec<f64>),
60}
61
62#[cfg(test)]
63mod tests {
64 use crate::CobylaSolver;
65 use approx::assert_abs_diff_eq;
66 use argmin::core::{CostFunction, Error, Executor, State};
67
68 fn paraboloid(x: &[f64], _data: &mut ()) -> f64 {
70 10. * (x[0] + 1.).powf(2.) + x[1].powf(2.)
71 }
72
73 struct ParaboloidProblem;
75
76 impl CostFunction for ParaboloidProblem {
77 type Param = Vec<f64>;
78 type Output = Vec<f64>;
79
80 fn cost(&self, x: &Self::Param) -> Result<Self::Output, Error> {
81 let fx = paraboloid(x, &mut ());
82 Ok(vec![fx, x[0]])
83 }
84 }
85
86 #[test]
87 fn test_paraboloid() {
88 let problem = ParaboloidProblem;
89 let solver = CobylaSolver::new(vec![1., 1.]);
90
91 let res = Executor::new(problem, solver)
92 .timer(true)
93 .configure(|state| state.max_iters(100).iprint(0))
94 .run()
95 .unwrap();
96
97 assert_abs_diff_eq!(0., res.state().get_best_param().unwrap()[0], epsilon = 1e-2);
98 assert_abs_diff_eq!(0., res.state().get_best_param().unwrap()[1], epsilon = 1e-2);
99 assert_abs_diff_eq!(10., res.state().get_best_cost(), epsilon = 1e-2);
100 }
101}