use evolutionary_planning::cmaes::{CmaEs, Config as CmaConfig};
use evolutionary_planning::de::rades::{Config as RadesConfig, Rades};
use evolutionary_planning::de::{Config as DeConfig, De};
use evolutionary_planning::problems::intersection::Intersection;
use evolutionary_planning::{Limits, Outcome, SplitMix64};
const SEEDS: core::ops::Range<u64> = 0..9;
const EVALUATIONS: u64 = 300;
fn main() {
let mut scene = Intersection::crossing(8);
let bounds = scene.bounds();
println!(
"8 robots, {EVALUATIONS} evaluations per run, {} seeds",
SEEDS.end - SEEDS.start
);
report(&mut scene, "RADES", &mut |scene, seed| {
Rades::new(RadesConfig::default(), bounds.clone()).run(
scene,
&mut Limits::evaluations(EVALUATIONS),
&mut (),
&mut SplitMix64::new(seed),
)
});
report(&mut scene, "DE", &mut |scene, seed| {
De::new(
DeConfig {
population_size: 10,
..DeConfig::default()
},
bounds.clone(),
)
.run(
scene,
&mut Limits::evaluations(EVALUATIONS),
&mut (),
&mut SplitMix64::new(seed),
)
});
report(&mut scene, "CMA-ES", &mut |scene, seed| {
CmaEs::new(
bounds.center(),
1.0,
CmaConfig {
bounds: Some(bounds.clone()),
..CmaConfig::default()
},
)
.minimize(
scene,
&mut Limits::evaluations(EVALUATIONS),
&mut (),
&mut SplitMix64::new(seed),
)
});
}
fn report(
scene: &mut Intersection,
name: &str,
optimise: &mut dyn FnMut(&mut Intersection, u64) -> Outcome<Vec<f64>>,
) {
let mut costs = Vec::new();
let mut best: Option<Outcome<Vec<f64>>> = None;
for seed in SEEDS {
let outcome = optimise(scene, seed);
costs.push(outcome.best_cost());
if best
.as_ref()
.is_none_or(|b| outcome.best_cost() < b.best_cost())
{
best = Some(outcome);
}
}
costs.sort_by(f64::total_cmp);
let best = best.expect("at least one seed");
let plan = scene
.plan(best.best_genome().expect("evaluated"))
.expect("feasible");
println!(
"{name:>7}: median {:8.2} best {:8.2} collisions {} makespan {}",
costs[costs.len() / 2],
best.best_cost(),
scene.collisions(&plan),
plan.iter().map(Vec::len).max().unwrap_or(0)
);
}