From 0047990b459b29545707dbf614b37ae3b083e492 Mon Sep 17 00:00:00 2001 From: Rutherther Date: Mon, 27 Oct 2025 20:45:10 +0100 Subject: [PATCH] fix: return proper indices from tournament --- codes/eoa_lib/src/selection.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/codes/eoa_lib/src/selection.rs b/codes/eoa_lib/src/selection.rs index ea03a8746ad6015151cb26b71d19bc03940e4093..48718b9d62374591cf51cae11eaa9aabadedf0dc 100644 --- a/codes/eoa_lib/src/selection.rs +++ b/codes/eoa_lib/src/selection.rs @@ -1,4 +1,5 @@ use rand::{seq::IteratorRandom, Rng, RngCore}; +use std::fmt::Debug; use crate::{comparison::BetterThanOperator, replacement::EvaluatedPopulation}; @@ -28,13 +29,13 @@ impl TournamentSelection { } } - fn tournament( + fn tournament( &mut self, idxs: &mut Vec, evaluations: &EvaluatedPopulation, better_than: &dyn BetterThanOperator ) -> usize { - idxs.sort_by(|&i, &j| better_than.ordering( + idxs.sort_unstable_by(|&i, &j| better_than.ordering( &evaluations.population[i].evaluation, &evaluations.population[j].evaluation) ); @@ -59,11 +60,11 @@ impl TournamentSelection { p_selector /= 1.0 - p; } - selected + idxs[selected] } } -impl Selection for TournamentSelection { +impl Selection for TournamentSelection { fn select( &mut self, count: usize, @@ -76,7 +77,8 @@ impl Selection for TournamentSelecti // Choose k. Do not care if already selected previously. (0..evaluations.population.len()).choose_multiple_fill(&mut self.rng, &mut k_selected_idxs); // Tournament between the k - self.tournament(&mut k_selected_idxs, evaluations, better_than) + let index = self.tournament(&mut k_selected_idxs, evaluations, better_than); + index }) } }