use crate::{comparison::BetterThanOperator, fitness::FitnessFunction}; #[derive(Clone, Debug)] pub struct Population { pub population: Vec } #[derive(Clone, Debug)] pub struct EvaluatedChromosome { pub chromosome: TChromosome, pub evaluation: TResult, } #[derive(Clone, Debug)] pub struct EvaluatedPopulation { pub population: Vec> } impl Population { pub fn empty() -> Self { Self { population: vec![] } } pub fn from_vec(vec: Vec) -> Self { Self { population: vec } } pub fn from_iterator(iter: impl Iterator) -> Self { Self::from_vec(iter.collect()) } pub fn evaluate>(self, func: &T) -> Result, T::Err> { EvaluatedPopulation::evaluate( self.population, func ) } pub fn join(&mut self, mut offsprings: Population) { self.population.append(&mut offsprings.population); } pub fn into_iter(self) -> impl Iterator { self.population.into_iter() } pub fn iter(&self) -> impl Iterator { self.population.iter() } pub fn iter_mut(&mut self) -> impl Iterator { self.population.iter_mut() } } impl EvaluatedChromosome { pub fn deconstruct(self) -> (TInput, TResult) { (self.chromosome, self.evaluation) } } impl EvaluatedPopulation { pub fn empty() -> Self { Self { population: vec![] } } pub fn new() -> Self { Self { population: vec![] } } pub fn evaluate>(chromosomes: Vec, func: &T) -> Result { func.fit_population(chromosomes) .map(|population| Self::from_vec(population)) } pub fn from_vec(vec: Vec>) -> Self { Self { population: vec } } pub fn best_candidate(&self, better_than: &impl BetterThanOperator) -> &EvaluatedChromosome { let mut best_so_far = &self.population[0]; for individual in self.population.iter().skip(1) { if better_than.better_than(&individual.evaluation, &best_so_far.evaluation) { best_so_far = individual; } } best_so_far } pub fn split_at(self, len: usize) -> (Self, Self) { let mut left = self.population; let right = left.split_off(len); ( Self::from_vec(left), Self::from_vec(right), ) } pub fn add(&mut self, c: EvaluatedChromosome) { self.population.push(c) } pub fn deconstruct(self) -> Vec> { self.population } pub fn nonevaluated(self) -> Population { Population::from_vec(self.deconstruct() .into_iter() .map(|individual| individual.chromosome) .collect::>()) } pub fn join(&mut self, mut offsprings: EvaluatedPopulation) { self.population.append(&mut offsprings.population); } pub fn iter(&self) -> impl Iterator> { self.population.iter() } pub fn iter_mut(&mut self) -> impl Iterator> { self.population.iter_mut() } } impl EvaluatedPopulation { pub fn evaluations_vec(&self) -> Vec { self.population .iter() .map(|individual| individual.evaluation) .collect() } }