use crate::ProbabilityWheelIterator;
use radiate_core::{Chromosome, Objective, Phenotype, Select};
#[derive(Debug, Default)]
pub struct RankSelector;
impl RankSelector {
pub fn new() -> Self {
RankSelector
}
}
impl<C: Chromosome> Select<C> for RankSelector {
fn select(&self, population: &[Phenotype<C>], _: &Objective, count: usize) -> Vec<usize> {
let n = population.len();
if n == 0 || count == 0 {
return Vec::new();
}
let rank_sum = (1..=n).map(|i| i as f32).sum::<f32>();
let mut probabilities = Vec::with_capacity(n);
for i in 0..n {
probabilities.push((n as f32 - i as f32) / rank_sum);
}
ProbabilityWheelIterator::new(probabilities, count).collect()
}
}