1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#[cfg(test)]
#[path = "../../../tests/unit/solver/selection/naive_selection_test.rs"]
mod naive_selection_test;
use super::*;
use std::iter::once;
pub struct NaiveSelection {
offspring_size: usize,
}
impl NaiveSelection {
pub fn new(offspring_size: usize) -> Self {
Self { offspring_size }
}
}
impl Selection for NaiveSelection {
fn select_parents(&self, refinement_ctx: &RefinementContext) -> Vec<InsertionContext> {
assert!(refinement_ctx.population.size() > 0);
let random = refinement_ctx.population.nth(0).unwrap().random.clone();
once(0_usize)
.chain(
(1..self.offspring_size)
.map(|_| random.uniform_int(0, refinement_ctx.population.size() as i32 - 1) as usize),
)
.take(self.offspring_size)
.filter_map(|idx| refinement_ctx.population.nth(idx))
.map(|individual| individual.deep_copy())
.collect()
}
}