Skip to main content

radiate_selectors/
random_selector.rs

1use radiate_core::{Chromosome, Objective, Population, Select, random_provider};
2
3#[derive(Debug, Default)]
4pub struct RandomSelector;
5
6impl RandomSelector {
7    pub fn new() -> Self {
8        RandomSelector
9    }
10}
11
12impl<C: Chromosome + Clone> Select<C> for RandomSelector {
13    fn select(&self, population: &Population<C>, _: &Objective, count: usize) -> Population<C> {
14        let mut selected = Vec::with_capacity(count);
15
16        for _ in 0..count {
17            let idx = random_provider::range(0..population.len());
18            let member = population.get(idx).expect("Index in range");
19            selected.push(member.clone());
20        }
21
22        Population::from(selected)
23    }
24}