radiate_engines/builder/
selectors.rs1use crate::GeneticEngineBuilder;
2use radiate_core::{Chromosome, Select};
3use std::sync::Arc;
4
5#[derive(Clone)]
6pub struct SelectionParams<C: Chromosome> {
7 pub offspring_fraction: f32,
8 pub survivor_selector: Arc<dyn Select<C>>,
9 pub offspring_selector: Arc<dyn Select<C>>,
10}
11
12impl<C, T> GeneticEngineBuilder<C, T>
13where
14 C: Chromosome + PartialEq + Clone,
15 T: Clone + Send,
16{
17 pub fn offspring_fraction(mut self, offspring_fraction: f32) -> Self {
21 self.add_error_if(
22 || !(0.0..=1.0).contains(&offspring_fraction),
23 "offspring_fraction must be between 0.0 and 1.0",
24 );
25
26 self.params.selection_params.offspring_fraction = offspring_fraction;
27 self
28 }
29
30 pub fn boxed_survivor_selector(mut self, selector: Box<dyn Select<C>>) -> Self {
31 self.params.selection_params.survivor_selector = selector.into();
32 self
33 }
34
35 pub fn boxed_offspring_selector(mut self, selector: Box<dyn Select<C>>) -> Self {
36 self.params.selection_params.offspring_selector = selector.into();
37 self
38 }
39
40 pub fn survivor_selector<S: Select<C> + 'static>(mut self, selector: S) -> Self {
44 self.params.selection_params.survivor_selector = Arc::new(selector);
45 self
46 }
47
48 pub fn offspring_selector<S: Select<C> + 'static>(mut self, selector: S) -> Self {
51 self.params.selection_params.offspring_selector = Arc::new(selector);
52 self
53 }
54}