use argmin::core::observers::{ObserverMode, SlogLogger};
use argmin::core::{CostFunction, Error, Executor};
use argmin::solver::goldensectionsearch::GoldenSectionSearch;
struct TestFunc {}
impl CostFunction for TestFunc {
type Param = f32;
type Output = f32;
fn cost(&self, x: &Self::Param) -> Result<Self::Output, Error> {
Ok((x + 3.0) * (x - 1.0).powi(2))
}
}
fn main() -> Result<(), Error> {
let cost = TestFunc {};
let init_param = -0.5;
let solver = GoldenSectionSearch::new(-2.5, 3.0)?.with_tolerance(0.0001)?;
let res = Executor::new(cost, solver)
.configure(|state| state.param(init_param).max_iters(100))
.add_observer(SlogLogger::term(), ObserverMode::Always)
.run()
.unwrap();
println!("Result of golden section search:\n{res}");
Ok(())
}