#![allow(dead_code)]
#![allow(unused_variables)]
#[allow(unused_imports)]
use cmaes::restart::{Local, RestartOptions, RestartStrategy};
use cmaes::DVector;
use std::f64::consts::PI;
use std::ops::RangeInclusive;
fn main() {
let rastrigin = (rastrigin, -5.0..=5.0, 30);
let weierstrass = (weierstrass, -0.5..=0.5, 30);
let eggholder = (eggholder, -512.0..=512.0, 2);
let (function, search_range, dim) = weierstrass;
let strategy = RestartStrategy::BIPOP(Default::default());
let restarter = RestartOptions::new(dim, search_range, strategy)
.fun_target(1e-10)
.max_time(std::time::Duration::from_secs(120))
.enable_printing(true)
.build()
.unwrap();
let results = restarter.run_parallel(|| function);
}
fn rastrigin(x: &DVector<f64>) -> f64 {
assert!(x.len() >= 2);
10.0 * x.len() as f64
+ x.iter()
.map(|xi| xi.powi(2) - 10.0 * (2.0 * PI * xi).cos())
.sum::<f64>()
}
fn weierstrass(x: &DVector<f64>) -> f64 {
assert!(x.len() >= 2);
let k_max = 20;
let a = 0.5f64;
let b = 3.0f64;
let y = x
.iter()
.map(|xi| {
(0..=k_max)
.map(|k| a.powi(k) * (2.0 * PI * b.powi(k) * (xi + 0.5)).cos())
.sum::<f64>()
})
.sum::<f64>()
- x.len() as f64
* (0..=k_max)
.map(|k| a.powi(k) * (2.0 * PI * b.powi(k) * 0.5).cos())
.sum::<f64>();
y + penalty(x, -0.5..=0.5, 1e6)
}
fn eggholder(x: &DVector<f64>) -> f64 {
assert!(x.len() == 2);
959.640636
- (x[1] + 47.0) * (x[1] + x[0] / 2.0 + 47.0).abs().sqrt().sin()
- x[0] * (x[0] - (x[1] + 47.0)).abs().sqrt().sin()
+ penalty(x, -512.0..=512.0, 1e6)
}
fn penalty(x: &DVector<f64>, bounds: RangeInclusive<f64>, penalty_factor: f64) -> f64 {
penalty_factor
* x.iter()
.map(|xi| (xi - bounds.end()).max(0.0).powi(2) + (bounds.start() - xi).max(0.0).powi(2))
.sum::<f64>()
}