#![doc = include_str!("../README.md")]
mod cobyla;
mod cobyla_solver;
mod cobyla_state;
pub use crate::cobyla_solver::*;
pub use crate::cobyla_state::*;
#[derive(Debug, Clone, Copy)]
pub enum FailStatus {
Failure,
InvalidArgs,
OutOfMemory,
RoundoffLimited,
ForcedStop,
UnexpectedError,
}
#[derive(Debug, Clone, Copy)]
pub enum SuccessStatus {
Success,
StopValReached,
FtolReached,
XtolReached,
MaxEvalReached,
MaxTimeReached,
}
#[derive(Debug, Clone, Default)]
pub struct StopTols {
pub ftol_rel: f64,
pub ftol_abs: f64,
pub xtol_rel: f64,
pub xtol_abs: Vec<f64>,
}
pub enum RhoBeg {
All(f64),
Set(Vec<f64>),
}
#[cfg(test)]
mod tests {
use crate::CobylaSolver;
use approx::assert_abs_diff_eq;
use argmin::core::{CostFunction, Error, Executor, State};
fn paraboloid(x: &[f64], _data: &mut ()) -> f64 {
10. * (x[0] + 1.).powf(2.) + x[1].powf(2.)
}
struct ParaboloidProblem;
impl CostFunction for ParaboloidProblem {
type Param = Vec<f64>;
type Output = Vec<f64>;
fn cost(&self, x: &Self::Param) -> Result<Self::Output, Error> {
let fx = paraboloid(x, &mut ());
Ok(vec![fx, x[0]])
}
}
#[test]
fn test_paraboloid() {
let problem = ParaboloidProblem;
let solver = CobylaSolver::new(vec![1., 1.]);
let res = Executor::new(problem, solver)
.timer(true)
.configure(|state| state.max_iters(100).iprint(0))
.run()
.unwrap();
assert_abs_diff_eq!(0., res.state().get_best_param().unwrap()[0], epsilon = 1e-2);
assert_abs_diff_eq!(0., res.state().get_best_param().unwrap()[1], epsilon = 1e-2);
assert_abs_diff_eq!(10., res.state().get_best_cost(), epsilon = 1e-2);
}
}