From b0db23c78cb1b0291b645b87dae9d47faf9082ca Mon Sep 17 00:00:00 2001 From: Rutherther Date: Mon, 27 Oct 2025 20:45:35 +0100 Subject: [PATCH] feat: add best selection --- codes/eoa_lib/src/selection.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/codes/eoa_lib/src/selection.rs b/codes/eoa_lib/src/selection.rs index 48718b9d62374591cf51cae11eaa9aabadedf0dc..5485387628b8597cfc9a304eefd2bc61eb89ee2d 100644 --- a/codes/eoa_lib/src/selection.rs +++ b/codes/eoa_lib/src/selection.rs @@ -11,6 +11,30 @@ pub trait Selection { ) -> impl Iterator; } +pub struct BestSelection; +impl BestSelection { + pub fn new() -> Self { + Self + } +} + +impl Selection for BestSelection { + fn select(&mut self, + count: usize, + evaluations: &EvaluatedPopulation, + better_than: &dyn BetterThanOperator + ) -> impl Iterator { + let mut idxs = (0..evaluations.population.len()) + .collect::>(); + idxs.sort_unstable_by(|&i, &j| better_than.ordering( + &evaluations.population[i].evaluation, + &evaluations.population[j].evaluation) + ); + + idxs.into_iter().take(count) + } +} + pub struct TournamentSelection { rng: Box, p: f64,