use crate::{
core::{context::Context, offspring::Offspring, state::State},
operators::GeneticOperator,
};
#[derive(Debug, Clone)]
pub struct Conditional<A, B, P> {
a: A,
b: B,
predicate: P,
}
impl<A, B, P> Conditional<A, B, P> {
pub fn new(a: A, b: B, predicate: P) -> Self {
Self { a, b, predicate }
}
}
impl<G, F, Fe, R, C, A, B, P> GeneticOperator<G, F, Fe, R, C> for Conditional<A, B, P>
where
A: GeneticOperator<G, F, Fe, R, C>,
B: GeneticOperator<G, F, Fe, R, C>,
P: Fn(&State<G, F>) -> bool,
{
fn apply(&self, state: &State<G, F>, ctx: &mut Context<Fe, R, C>) -> Offspring<G, F> {
if (self.predicate)(state) {
self.a.apply(state, ctx)
} else {
self.b.apply(state, ctx)
}
}
fn transform(&self, state: State<G, F>, ctx: &mut Context<Fe, R, C>) -> Offspring<G, F> {
if (self.predicate)(&state) {
self.a.transform(state, ctx)
} else {
self.b.transform(state, ctx)
}
}
}