diff --git a/crates/solverforge-solver/src/heuristic/selector/scalar_neighborhood/cursor/ruin.rs b/crates/solverforge-solver/src/heuristic/selector/scalar_neighborhood/cursor/ruin.rs index e1644cec8..b7030ef90 100644 --- a/crates/solverforge-solver/src/heuristic/selector/scalar_neighborhood/cursor/ruin.rs +++ b/crates/solverforge-solver/src/heuristic/selector/scalar_neighborhood/cursor/ruin.rs @@ -21,7 +21,30 @@ pub(super) fn generate_ruin_batches( where S: PlanningSolution, { - let entity_count = slot.entity_count(solution); + ruin_batches( + slot.entity_count(solution), + min_ruin_count, + max_ruin_count, + moves_per_step, + rng, + ) +} + +/// Draws `moves_per_step` batches of distinct entity indexes, each the prefix +/// of a partial Fisher-Yates shuffle of the identity permutation. +/// +/// The permutation is restored to the identity after each batch by rewriting +/// only the positions the shuffle touched: the prefix, and the original +/// positions of the values the batch took. A value that enters the prefix +/// never moves again, so every other position still holds its own index. +/// Restoring costs O(ruin count) per batch instead of O(entity count). +fn ruin_batches( + entity_count: usize, + min_ruin_count: usize, + max_ruin_count: usize, + moves_per_step: usize, + rng: &mut SmallRng, +) -> Vec> { let min = min_ruin_count.min(entity_count); let max = max_ruin_count.min(entity_count); let mut permutation: Vec = (0..entity_count).collect(); @@ -30,9 +53,6 @@ where if entity_count == 0 { return Vec::new(); } - for (index, entity) in permutation.iter_mut().enumerate() { - *entity = index; - } let ruin_count = if min == max { min } else { @@ -42,11 +62,22 @@ where let swap_index = rng.random_range(index..entity_count); permutation.swap(index, swap_index); } - permutation[..ruin_count].to_vec() + let batch = permutation[..ruin_count].to_vec(); + for (index, entity) in permutation[..ruin_count].iter_mut().enumerate() { + *entity = index; + } + for &entity in &batch { + permutation[entity] = entity; + } + batch }) .collect() } +#[cfg(test)] +#[path = "ruin_tests.rs"] +mod tests; + struct RuinSnapshot { assigned: Vec, recreatable: Vec, diff --git a/crates/solverforge-solver/src/heuristic/selector/scalar_neighborhood/cursor/ruin_tests.rs b/crates/solverforge-solver/src/heuristic/selector/scalar_neighborhood/cursor/ruin_tests.rs new file mode 100644 index 000000000..28de4f08e --- /dev/null +++ b/crates/solverforge-solver/src/heuristic/selector/scalar_neighborhood/cursor/ruin_tests.rs @@ -0,0 +1,62 @@ +//! Ruin batch generation against the full-reset reference algorithm. + +use rand::rngs::SmallRng; +use rand::{RngExt, SeedableRng}; + +use super::ruin_batches; + +/// The reference: resets the whole permutation to the identity before every +/// batch. +fn full_reset_batches( + entity_count: usize, + min_ruin_count: usize, + max_ruin_count: usize, + moves_per_step: usize, + rng: &mut SmallRng, +) -> Vec> { + let min = min_ruin_count.min(entity_count); + let max = max_ruin_count.min(entity_count); + let mut permutation: Vec = (0..entity_count).collect(); + (0..moves_per_step) + .map(|_| { + if entity_count == 0 { + return Vec::new(); + } + for (index, entity) in permutation.iter_mut().enumerate() { + *entity = index; + } + let ruin_count = if min == max { + min + } else { + rng.random_range(min..=max) + }; + for index in 0..ruin_count { + let swap_index = rng.random_range(index..entity_count); + permutation.swap(index, swap_index); + } + permutation[..ruin_count].to_vec() + }) + .collect() +} + +#[test] +fn ruin_batches_match_the_full_reset_reference() { + for entity_count in 0..40 { + for (min, max) in [(0, 0), (1, 1), (1, 3), (2, 8), (5, 5), (0, 40), (39, 60)] { + for seed in 0..25u64 { + let mut fast = SmallRng::seed_from_u64(seed); + let mut reference = SmallRng::seed_from_u64(seed); + assert_eq!( + ruin_batches(entity_count, min, max, 7, &mut fast), + full_reset_batches(entity_count, min, max, 7, &mut reference), + "entity_count {entity_count}, ruin {min}..={max}, seed {seed}" + ); + assert_eq!( + fast.random::(), + reference.random::(), + "both consume the same random draws" + ); + } + } + } +}