extern crate argmin;
extern crate argmin_testfunctions;
extern crate finitediff;
extern crate ndarray;
use argmin::prelude::*;
use argmin::solver::linesearch::MoreThuenteLineSearch;
use argmin::solver::quasinewton::LBFGS;
use argmin_testfunctions::rosenbrock;
use finitediff::*;
use ndarray::{array, Array1, Array2};
struct Rosenbrock {
a: f64,
b: f64,
}
impl ArgminOp for Rosenbrock {
type Param = Array1<f64>;
type Output = f64;
type Hessian = Array2<f64>;
type Jacobian = ();
type Float = f64;
fn apply(&self, p: &Self::Param) -> Result<Self::Output, Error> {
Ok(rosenbrock(&p.to_vec(), self.a, self.b))
}
fn gradient(&self, p: &Self::Param) -> Result<Self::Param, Error> {
Ok((*p).forward_diff(&|x| rosenbrock(&x.to_vec(), self.a, self.b)))
}
}
fn run() -> Result<(), Error> {
let cost = Rosenbrock { a: 1.0, b: 100.0 };
let init_param: Array1<f64> = array![-1.2, 1.0];
let linesearch = MoreThuenteLineSearch::new().c(1e-4, 0.9)?;
let solver = LBFGS::new(linesearch, 7);
let res = Executor::new(cost, solver, init_param)
.add_observer(ArgminSlogLogger::term(), ObserverMode::Always)
.max_iters(100)
.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);
}
}