use crate::{
core::{context::Context, population::Population},
fitness::FitnessEvaluator,
operators::GeneticOperator,
};
#[derive(Debug)]
pub struct State<G, F> {
population: Population<G, F>,
generation: usize,
}
impl<G, F> State<G, F> {
pub fn new(population: Population<G, F>, generation: usize) -> Self {
Self {
population,
generation,
}
}
pub fn with_population(&self, population: Population<G, F>) -> Self {
Self::new(population, self.generation)
}
pub fn population(&self) -> &Population<G, F> {
&self.population
}
pub fn generation(&self) -> usize {
self.generation
}
pub(crate) fn inc_generation(&mut self) {
self.generation += 1;
}
pub(crate) fn apply_operators<Fe, R, C>(
&mut self,
ctx: &mut Context<Fe, R, C>,
ops: &mut impl GeneticOperator<G, F, Fe, R, C>,
) where
Fe: FitnessEvaluator<G, F>,
{
self.population = ops.apply(self, ctx).into();
}
pub fn into_population(self) -> Population<G, F> {
self.population
}
}