use alloc::vec::Vec;
use rand_core::Rng;
use crate::cost::{compare, ranking};
use crate::rng::{below, unit_f64};
pub trait Selection {
fn select<R: Rng + ?Sized>(
&mut self,
costs: &[f64],
count: usize,
rng: &mut R,
out: &mut Vec<usize>,
);
}
impl<S: Selection + ?Sized> Selection for &mut S {
fn select<R: Rng + ?Sized>(
&mut self,
costs: &[f64],
count: usize,
rng: &mut R,
out: &mut Vec<usize>,
) {
(**self).select(costs, count, rng, out);
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Tournament {
pub size: usize,
}
impl Default for Tournament {
fn default() -> Self {
Self { size: 2 }
}
}
impl Selection for Tournament {
fn select<R: Rng + ?Sized>(
&mut self,
costs: &[f64],
count: usize,
rng: &mut R,
out: &mut Vec<usize>,
) {
for _ in 0..count {
let mut winner = below(rng, costs.len());
for _ in 1..self.size.max(1) {
let contestant = below(rng, costs.len());
if compare(costs[contestant], costs[winner]).is_lt() {
winner = contestant;
}
}
out.push(winner);
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Truncation {
pub fraction: f64,
}
impl Default for Truncation {
fn default() -> Self {
Self { fraction: 0.5 }
}
}
impl Selection for Truncation {
fn select<R: Rng + ?Sized>(
&mut self,
costs: &[f64],
count: usize,
rng: &mut R,
out: &mut Vec<usize>,
) {
let order = ranking(costs);
let eligible = fraction_count(self.fraction, costs.len());
for _ in 0..count {
out.push(order[below(rng, eligible)]);
}
}
}
fn fraction_count(fraction: f64, n: usize) -> usize {
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
clippy::cast_precision_loss,
reason = "a population fraction, clamped to 1..=n"
)]
let count = libm::ceil(fraction.clamp(0.0, 1.0) * n as f64) as usize;
count.clamp(1, n)
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum Sampling {
#[default]
Roulette,
Universal,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Proportional {
pub sampling: Sampling,
}
impl Selection for Proportional {
fn select<R: Rng + ?Sized>(
&mut self,
costs: &[f64],
count: usize,
rng: &mut R,
out: &mut Vec<usize>,
) {
let worst = costs
.iter()
.copied()
.filter(|c| c.is_finite())
.fold(f64::NEG_INFINITY, f64::max);
let weights: Vec<f64> = costs
.iter()
.map(|&c| if c.is_finite() { worst - c } else { 0.0 })
.collect();
sample(&weights, count, self.sampling, rng, out);
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct LinearRanking {
pub pressure: f64,
pub sampling: Sampling,
}
impl Default for LinearRanking {
fn default() -> Self {
Self {
pressure: 1.5,
sampling: Sampling::Roulette,
}
}
}
impl Selection for LinearRanking {
fn select<R: Rng + ?Sized>(
&mut self,
costs: &[f64],
count: usize,
rng: &mut R,
out: &mut Vec<usize>,
) {
let weights = ranking_weights(costs, self.pressure);
sample(&weights, count, self.sampling, rng, out);
}
}
pub(crate) fn ranking_weights(costs: &[f64], pressure: f64) -> Vec<f64> {
let n = costs.len();
let s = pressure.clamp(1.0, 2.0);
let mut weights = alloc::vec![0.0; n];
if n == 1 {
weights[0] = 1.0;
return weights;
}
#[expect(
clippy::cast_precision_loss,
reason = "population sizes are far below 2^52"
)]
let last = (n - 1) as f64;
for (rank, &index) in ranking(costs).iter().enumerate() {
#[expect(
clippy::cast_precision_loss,
reason = "population sizes are far below 2^52"
)]
let position = (last - rank as f64) / last;
weights[index] = 2.0 - s + 2.0 * (s - 1.0) * position;
}
weights
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Uniform;
impl Selection for Uniform {
fn select<R: Rng + ?Sized>(
&mut self,
costs: &[f64],
count: usize,
rng: &mut R,
out: &mut Vec<usize>,
) {
for _ in 0..count {
out.push(below(rng, costs.len()));
}
}
}
pub(crate) fn sample<R: Rng + ?Sized>(
weights: &[f64],
count: usize,
sampling: Sampling,
rng: &mut R,
out: &mut Vec<usize>,
) {
let total: f64 = weights.iter().sum();
if !(total > 0.0 && total.is_finite()) {
for _ in 0..count {
out.push(below(rng, weights.len()));
}
return;
}
match sampling {
Sampling::Roulette => {
for _ in 0..count {
out.push(spin(weights, total * unit_f64(rng)));
}
}
Sampling::Universal => universal(weights, total, count, rng, out),
}
}
fn spin(weights: &[f64], point: f64) -> usize {
let mut cumulative = 0.0;
let mut last_positive = 0;
for (index, &weight) in weights.iter().enumerate() {
if weight > 0.0 {
last_positive = index;
}
cumulative += weight;
if point < cumulative {
return index;
}
}
last_positive
}
fn universal<R: Rng + ?Sized>(
weights: &[f64],
total: f64,
count: usize,
rng: &mut R,
out: &mut Vec<usize>,
) {
if count == 0 {
return;
}
#[expect(
clippy::cast_precision_loss,
reason = "selection counts are far below 2^52"
)]
let spacing = total / count as f64;
let mut pointer = spacing * unit_f64(rng);
let mut cumulative = weights[0];
let mut index = 0;
for _ in 0..count {
while pointer >= cumulative && index + 1 < weights.len() {
index += 1;
cumulative += weights[index];
}
out.push(index);
pointer += spacing;
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::SplitMix64;
#[test]
fn big_tournament_prefers_best() {
let costs = [5.0, 1.0, 3.0, 4.0];
let mut out = Vec::new();
Tournament { size: 50 }.select(&costs, 100, &mut SplitMix64::new(3), &mut out);
assert!(out.iter().all(|&i| i == 1));
}
#[test]
fn truncation_stays_in_top() {
let costs = [5.0, 1.0, 3.0, 4.0];
let mut out = Vec::new();
Truncation { fraction: 0.5 }.select(&costs, 100, &mut SplitMix64::new(3), &mut out);
assert!(out.iter().all(|&i| i == 1 || i == 2));
}
#[test]
fn ranking_weights_are_linear() {
let w = ranking_weights(&[3.0, 1.0, 2.0], 1.2);
assert!((w.iter().sum::<f64>() - 3.0).abs() < 1e-12);
assert!((w[1] - 1.2).abs() < 1e-12 && (w[0] - 0.8).abs() < 1e-12);
}
#[test]
fn universal_has_minimal_spread() {
let weights = [1.0, 2.0, 3.0, 4.0];
let mut out = Vec::new();
sample(
&weights,
100,
Sampling::Universal,
&mut SplitMix64::new(9),
&mut out,
);
for (index, expected) in [10, 20, 30, 40].into_iter().enumerate() {
let got = out.iter().filter(|&&i| i == index).count();
assert!(got.abs_diff(expected) <= 1, "index {index}: {got}");
}
}
#[test]
fn proportional_skips_worst() {
let mut out = Vec::new();
Proportional::default().select(
&[1.0, 9.0, 2.0, f64::INFINITY],
200,
&mut SplitMix64::new(4),
&mut out,
);
assert!(out.iter().all(|&i| i == 0 || i == 2));
}
}