use cmaes::{CMAESOptions, DVector, ObjectiveFunction};
struct Rosenbrock {
a: f64,
b: f64,
counter: f64,
}
impl Rosenbrock {
fn new(a: f64, b: f64) -> Self {
Self { a, b, counter: 0.0 }
}
}
impl ObjectiveFunction for Rosenbrock {
fn evaluate(&mut self, x: &DVector<f64>) -> f64 {
assert!(x.len() == 2);
let y = (self.a - x[0]).powi(2) + self.b * (x[1] - x[0].powi(2)).powi(2);
self.counter += y;
y
}
}
impl<'a> ObjectiveFunction for &'a mut Rosenbrock {
fn evaluate(&mut self, x: &DVector<f64>) -> f64 {
ObjectiveFunction::evaluate(*self, x)
}
}
fn main() {
let mut function = Rosenbrock::new(5.0, 30.0);
let dim = 2;
let mut cmaes_state = CMAESOptions::new(vec![0.1; dim], 0.1)
.enable_printing(200)
.build(&mut function)
.unwrap();
let solution = cmaes_state.run();
let overall_best = solution.overall_best.unwrap();
println!(
"Solution individual has value {:e} and point {}",
overall_best.value, overall_best.point,
);
println!(
"Sum of objective function values outputted: {}",
function.counter
);
}