From 872f91797d5c768e9b6898942aca45d4cec82392 Mon Sep 17 00:00:00 2001 From: Anders Olsson Date: Fri, 8 May 2026 15:29:39 +0200 Subject: [PATCH] refactor(time_slice): add convergence field, rename iterate_to_convergence TimeSlice gains a pub(crate) convergence: ConvergenceOptions field set at construction. TimeSlice::new now takes it as a third parameter (breaking change to the pub constructor, acceptable in 0.1.x). History::add_events_with_prior passes self.convergence so the propagated value reaches every TimeSlice. The pre-existing convergence-the-method is renamed to iterate_to_convergence to disambiguate from the new convergence-the-field. The field is wired but not yet read by inference -- the three Game::*_with_arena callsites in time_slice.rs still hardcode ConvergenceOptions::default(). Task 2 changes that. Bit-equal because the propagated value equals the hardcoded value end-to-end. Also updated benches/batch.rs which has a fourth TimeSlice::new callsite (not enumerated in the plan -- only src/ files were). --- benches/batch.rs | 6 +++--- src/history.rs | 2 +- src/time_slice.rs | 26 ++++++++++++++++---------- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/benches/batch.rs b/benches/batch.rs index e480b9d..b87dc06 100644 --- a/benches/batch.rs +++ b/benches/batch.rs @@ -1,7 +1,7 @@ use criterion::{Criterion, criterion_group, criterion_main}; use trueskill_tt::{ - BETA, Competitor, EventKind, GAMMA, KeyTable, MU, P_DRAW, Rating, SIGMA, TimeSlice, - drift::ConstantDrift, gaussian::Gaussian, storage::CompetitorStore, + BETA, Competitor, ConvergenceOptions, EventKind, GAMMA, KeyTable, MU, P_DRAW, Rating, SIGMA, + TimeSlice, drift::ConstantDrift, gaussian::Gaussian, storage::CompetitorStore, }; fn criterion_benchmark(criterion: &mut Criterion) { @@ -35,7 +35,7 @@ fn criterion_benchmark(criterion: &mut Criterion) { let kinds = vec![EventKind::Ranked; composition.len()]; - let mut time_slice = TimeSlice::new(1, P_DRAW); + let mut time_slice = TimeSlice::new(1, P_DRAW, ConvergenceOptions::default()); time_slice.add_events(composition, results, weights, kinds, &agents); criterion.bench_function("Batch::iteration", |b| { diff --git a/src/history.rs b/src/history.rs index 4680838..91c72c9 100644 --- a/src/history.rs +++ b/src/history.rs @@ -594,7 +594,7 @@ impl, O: Observer, K: Eq + Hash + Clone> History { pub(crate) skills: SkillStore, pub(crate) time: T, p_draw: f64, + #[allow(dead_code)] + pub(crate) convergence: crate::ConvergenceOptions, arena: ScratchArena, pub(crate) color_groups: ColorGroups, } impl TimeSlice { - pub fn new(time: T, p_draw: f64) -> Self { + pub fn new(time: T, p_draw: f64, convergence: crate::ConvergenceOptions) -> Self { Self { events: Vec::new(), skills: SkillStore::new(), time, p_draw, + convergence, arena: ScratchArena::new(), color_groups: ColorGroups::new(), } @@ -444,7 +447,10 @@ impl TimeSlice { } #[allow(dead_code)] - pub(crate) fn convergence>(&mut self, agents: &CompetitorStore) -> usize { + pub(crate) fn iterate_to_convergence>( + &mut self, + agents: &CompetitorStore, + ) -> usize { let epsilon = 1e-6; let iterations = 20; @@ -643,7 +649,7 @@ mod tests { ); } - let mut time_slice = TimeSlice::new(0i64, 0.0); + let mut time_slice = TimeSlice::new(0i64, 0.0, crate::ConvergenceOptions::default()); time_slice.add_events( vec![ @@ -690,7 +696,7 @@ mod tests { epsilon = 1e-6 ); - assert_eq!(time_slice.convergence(&agents), 1); + assert_eq!(time_slice.iterate_to_convergence(&agents), 1); } #[test] @@ -720,7 +726,7 @@ mod tests { ); } - let mut time_slice = TimeSlice::new(0i64, 0.0); + let mut time_slice = TimeSlice::new(0i64, 0.0, crate::ConvergenceOptions::default()); time_slice.add_events( vec![ @@ -752,7 +758,7 @@ mod tests { epsilon = 1e-6 ); - assert!(time_slice.convergence(&agents) > 1); + assert!(time_slice.iterate_to_convergence(&agents) > 1); let post = time_slice.posteriors(); @@ -800,7 +806,7 @@ mod tests { ); } - let mut time_slice = TimeSlice::new(0i64, 0.0); + let mut time_slice = TimeSlice::new(0i64, 0.0, crate::ConvergenceOptions::default()); time_slice.add_events( vec![ @@ -814,7 +820,7 @@ mod tests { &agents, ); - time_slice.convergence(&agents); + time_slice.iterate_to_convergence(&agents); let post = time_slice.posteriors(); @@ -848,7 +854,7 @@ mod tests { assert_eq!(time_slice.events.len(), 6); - time_slice.convergence(&agents); + time_slice.iterate_to_convergence(&agents); let post = time_slice.posteriors(); @@ -898,7 +904,7 @@ mod tests { ); } - let mut ts = TimeSlice::new(0i64, 0.0); + let mut ts = TimeSlice::new(0i64, 0.0, crate::ConvergenceOptions::default()); ts.add_events( vec![