use math_audio_differential_evolution::{
Crossover, DEConfigBuilder, NonlinearConstraintHelper, Strategy, differential_evolution,
};
use ndarray::Array1;
use std::str::FromStr;
use std::sync::Arc;
fn main() {
let himmelblau =
|x: &Array1<f64>| (x[0] * x[0] + x[1] - 11.0).powi(2) + (x[0] + x[1] * x[1] - 7.0).powi(2);
let bounds = [(-6.0, 6.0), (-6.0, 6.0)];
let fun =
Arc::new(|x: &Array1<f64>| Array1::from(vec![x[0] * x[0] + x[1] * x[1], x[0] + x[1]]));
let lb = Array1::from(vec![-f64::INFINITY, 1.0]);
let ub = Array1::from(vec![10.0, 1.0]);
let nlc = NonlinearConstraintHelper { fun, lb, ub };
let strategy = Strategy::from_str("best1exp").unwrap_or(Strategy::Best1Exp);
let mut cfg = DEConfigBuilder::new()
.seed(456)
.maxiter(800)
.popsize(30)
.strategy(strategy)
.recombination(0.9)
.crossover(Crossover::Exponential)
.build()
.expect("popsize must be >= 4");
nlc.apply_to(&mut cfg, 1e3, 1e3);
let rep = differential_evolution(&himmelblau, &bounds, cfg).expect("optimization failed");
println!(
"success={} message=\"{}\"\nbest f={:.6e}\nbest x={:?}",
rep.success, rep.message, rep.fun, rep.x
);
}