use argmin::core::observers::{ObserverMode, SlogLogger};
use argmin::core::{CostFunction, Error, Executor};
use argmin::solver::neldermead::NelderMead;
use argmin_testfunctions::rosenbrock;
use ndarray::{array, Array1};
struct Rosenbrock {
a: f64,
b: f64,
}
impl CostFunction for Rosenbrock {
type Param = Array1<f64>;
type Output = f64;
fn cost(&self, p: &Self::Param) -> Result<Self::Output, Error> {
Ok(rosenbrock(&p.to_vec(), self.a, self.b))
}
}
fn run() -> Result<(), Error> {
let cost = Rosenbrock { a: 1.0, b: 100.0 };
let solver = NelderMead::new(vec![
array![-1.0, 3.0],
array![2.0, 1.5],
array![2.0, -1.0],
])
.with_sd_tolerance(0.0001)?;
let res = Executor::new(cost, solver)
.configure(|state| state.max_iters(100))
.add_observer(SlogLogger::term(), ObserverMode::Always)
.run()?;
std::thread::sleep(std::time::Duration::from_secs(1));
println!("{res}");
Ok(())
}
fn main() {
if let Err(ref e) = run() {
println!("{e}");
std::process::exit(1);
}
}