use evolutionary_planning::diffusion::{Config, Diffusion};
use evolutionary_planning::ga::Operators;
use evolutionary_planning::operators::chain::{
Action, ActionKind, ActionModel, CutAndSplice, DeleteGene, InsertGene, ParamNewValue,
ParamShift, ParamSpec, RandomChain, SegmentInversion, SegmentShift,
};
use evolutionary_planning::operators::selection::LinearRanking;
use evolutionary_planning::operators::{Mutation, NoRepair, mix};
use evolutionary_planning::{Limits, SplitMix64};
fn execute(chain: &[Action]) -> (f64, f64) {
let (mut x, mut y, mut heading) = (0.0f64, 0.0f64, 0.0f64);
for action in chain {
match action.kind {
0 => {
x += action.params[0] * heading.to_radians().cos();
y += action.params[0] * heading.to_radians().sin();
}
_ => heading += action.params[0],
}
}
(x, y)
}
fn chain_mutation(model: &ActionModel) -> impl Mutation<Vec<Action>> {
mix(
mix(
ParamShift {
model: model.clone(),
},
4.0,
ParamNewValue {
model: model.clone(),
},
1.0,
),
3.0,
mix(
mix(
InsertGene {
genes: model.clone(),
max_len: 12,
},
1.0,
DeleteGene { min_len: 1 },
1.0,
),
1.0,
mix(
SegmentShift {
max_segment: Some(3),
},
1.0,
SegmentInversion {
max_segment: Some(3),
},
1.0,
),
1.0,
),
1.0,
)
}
#[test]
fn chain_program_reaches_target() {
let model = ActionModel::new(vec![
ActionKind::new(vec![ParamSpec::real(0.0, 3.0)]),
ActionKind::new(vec![ParamSpec::integer(-90.0, 90.0)]),
]);
let mutation = chain_mutation(&model);
let mut diffusion = Diffusion::new(
Config::default(),
Operators {
init: RandomChain {
genes: model.clone(),
min_len: 2,
max_len: 8,
},
selection: LinearRanking::default(),
crossover: CutAndSplice { max_len: 12 },
mutation,
repair: NoRepair,
},
);
let mut objective = |chain: &Vec<Action>| {
let (x, y) = execute(chain);
(x - 3.0).hypot(y - 4.0)
+ 0.001 * f64::from(u32::try_from(chain.len()).expect("short chains"))
};
let outcome = diffusion.run(
&mut objective,
&mut Limits::evaluations(30_000),
&mut (),
&mut SplitMix64::new(1),
);
let best = outcome.best_genome().expect("evaluated");
let (x, y) = execute(best);
assert!(
(x - 3.0).hypot(y - 4.0) < 0.1,
"reached ({x}, {y}) with {} actions",
best.len()
);
assert!(best.iter().all(|a| model.is_valid(a)));
}