radiate_engines/builder/
population.rs

1use crate::GeneticEngineBuilder;
2use radiate_core::{Chromosome, Ecosystem, Population};
3
4#[derive(Clone)]
5pub struct PopulationParams<C: Chromosome> {
6    pub population_size: usize,
7    pub max_age: usize,
8    pub ecosystem: Option<Ecosystem<C>>,
9}
10
11impl<C, T> GeneticEngineBuilder<C, T>
12where
13    C: Chromosome + PartialEq + Clone,
14    T: Clone + Send,
15{
16    /// Set the population size of the genetic engine. Default is 100.
17    pub fn population_size(mut self, population_size: usize) -> Self {
18        self.add_error_if(|| population_size < 3, "population_size must be at least 3");
19
20        self.params.population_params.population_size = population_size;
21        self
22    }
23
24    /// Set the maximum age of an individual in the population. Default is 25.
25    pub fn max_age(mut self, max_age: usize) -> Self {
26        self.add_error_if(|| max_age < 1, "max_age must be greater than 0");
27
28        self.params.population_params.max_age = max_age;
29        self
30    }
31
32    /// Set the population of the genetic engine. This is useful if you want to provide a custom population.
33    /// If this is not set, the genetic engine will create a new population of `population_size` using the codec.
34    pub fn population(mut self, population: impl Into<Population<C>>) -> Self {
35        self.params.population_params.ecosystem = Some(Ecosystem::new(population.into()));
36        self
37    }
38
39    pub fn ecosystem(mut self, ecosystem: impl Into<Ecosystem<C>>) -> Self {
40        self.params.population_params.ecosystem = Some(ecosystem.into());
41        self
42    }
43}