tensorlogic-train 0.1.0

Training loops, loss composition, and optimization schedules for TensorLogic
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
//! Early stopping monitor for training loops.
//!
//! Provides configurable early stopping based on metric monitoring,
//! multi-metric policies, plateau detection, and training progress tracking.

use std::collections::HashMap;

/// Configuration for early stopping.
#[derive(Debug, Clone)]
pub struct EarlyStoppingConfig {
    /// Number of epochs to wait after last improvement before stopping.
    pub patience: usize,
    /// Minimum change to qualify as an improvement.
    pub min_delta: f64,
    /// Whether to minimize (loss) or maximize (accuracy) the metric.
    pub mode: MonitorMode,
    /// If `Some`, the metric must beat this baseline to count as improvement.
    pub baseline: Option<f64>,
    /// Whether to signal restoring the best model state on stop.
    pub restore_best: bool,
    /// Minimum number of epochs before early stopping can trigger.
    pub min_epochs: usize,
}

impl Default for EarlyStoppingConfig {
    fn default() -> Self {
        Self {
            patience: 10,
            min_delta: 0.0,
            mode: MonitorMode::Minimize,
            baseline: None,
            restore_best: true,
            min_epochs: 1,
        }
    }
}

/// Whether we want to minimize (loss) or maximize (accuracy) the metric.
#[derive(Debug, Clone, PartialEq)]
pub enum MonitorMode {
    /// Improvement means the metric is decreasing (e.g. loss).
    Minimize,
    /// Improvement means the metric is increasing (e.g. accuracy).
    Maximize,
}

/// Decision returned by the early stopping monitor after each step.
#[derive(Debug, Clone, PartialEq)]
pub enum EarlyStoppingDecision {
    /// Keep training, no special event.
    Continue,
    /// Training should stop.
    Stop {
        /// Human-readable reason for stopping.
        reason: String,
    },
    /// A new best metric value was observed (training continues).
    NewBest {
        /// The new best metric value.
        value: f64,
        /// The epoch at which the new best was observed.
        epoch: usize,
    },
}

/// The early stopping monitor.
///
/// Tracks a single metric over epochs and decides when to stop training
/// based on patience, minimum delta, baseline, and minimum epoch constraints.
#[derive(Debug, Clone)]
pub struct EarlyStoppingMonitor {
    config: EarlyStoppingConfig,
    best_value: Option<f64>,
    best_epoch: usize,
    epochs_without_improvement: usize,
    current_epoch: usize,
    history: Vec<f64>,
    stopped: bool,
}

impl EarlyStoppingMonitor {
    /// Create a new monitor with the given configuration.
    pub fn new(config: EarlyStoppingConfig) -> Self {
        Self {
            config,
            best_value: None,
            best_epoch: 0,
            epochs_without_improvement: 0,
            current_epoch: 0,
            history: Vec::new(),
            stopped: false,
        }
    }

    /// Create a new monitor with default configuration.
    pub fn with_default() -> Self {
        Self::new(EarlyStoppingConfig::default())
    }

    /// Report the metric for the current epoch and return a decision.
    ///
    /// This advances the epoch counter, records the metric value,
    /// and evaluates whether training should continue or stop.
    pub fn step(&mut self, metric_value: f64) -> EarlyStoppingDecision {
        self.current_epoch += 1;
        self.history.push(metric_value);

        // Check baseline constraint: if a baseline is set and the metric
        // doesn't beat it, we don't consider it an improvement.
        if let Some(baseline) = self.config.baseline {
            let beats_baseline = match self.config.mode {
                MonitorMode::Minimize => metric_value < baseline,
                MonitorMode::Maximize => metric_value > baseline,
            };
            if !beats_baseline {
                self.epochs_without_improvement += 1;
                return self.evaluate_stop();
            }
        }

        // Check if this is an improvement over the best value seen so far.
        let is_new_best = match self.best_value {
            None => true,
            Some(best) => self.is_improvement(metric_value, best),
        };

        if is_new_best {
            self.best_value = Some(metric_value);
            self.best_epoch = self.current_epoch;
            self.epochs_without_improvement = 0;
            EarlyStoppingDecision::NewBest {
                value: metric_value,
                epoch: self.current_epoch,
            }
        } else {
            self.epochs_without_improvement += 1;
            self.evaluate_stop()
        }
    }

    /// Evaluate whether training should stop based on patience and min_epochs.
    fn evaluate_stop(&mut self) -> EarlyStoppingDecision {
        if self.current_epoch < self.config.min_epochs {
            return EarlyStoppingDecision::Continue;
        }

        if self.epochs_without_improvement >= self.config.patience {
            self.stopped = true;
            let best_str = self
                .best_value
                .map(|v| format!("{v:.6}"))
                .unwrap_or_else(|| "N/A".to_string());
            EarlyStoppingDecision::Stop {
                reason: format!(
                    "No improvement for {} epochs. Best value: {} at epoch {}.",
                    self.config.patience, best_str, self.best_epoch
                ),
            }
        } else {
            EarlyStoppingDecision::Continue
        }
    }

    /// Check if training should stop (without advancing epoch).
    pub fn should_stop(&self) -> bool {
        self.stopped
    }

    /// Best metric value seen so far.
    pub fn best_value(&self) -> Option<f64> {
        self.best_value
    }

    /// Epoch at which the best value was seen.
    pub fn best_epoch(&self) -> usize {
        self.best_epoch
    }

    /// Current epoch number.
    pub fn current_epoch(&self) -> usize {
        self.current_epoch
    }

    /// Number of epochs since last improvement.
    pub fn epochs_since_improvement(&self) -> usize {
        self.epochs_without_improvement
    }

    /// Full metric history.
    pub fn history(&self) -> &[f64] {
        &self.history
    }

    /// Reset the monitor to its initial state.
    pub fn reset(&mut self) {
        self.best_value = None;
        self.best_epoch = 0;
        self.epochs_without_improvement = 0;
        self.current_epoch = 0;
        self.history.clear();
        self.stopped = false;
    }

    /// Whether the current value is an improvement over the best.
    ///
    /// For `Minimize` mode: `current < best - min_delta`.
    /// For `Maximize` mode: `current > best + min_delta`.
    fn is_improvement(&self, current: f64, best: f64) -> bool {
        match self.config.mode {
            MonitorMode::Minimize => current < best - self.config.min_delta,
            MonitorMode::Maximize => current > best + self.config.min_delta,
        }
    }

    /// Return a human-readable summary of the monitor's current state.
    pub fn summary(&self) -> String {
        let best_str = self
            .best_value
            .map(|v| format!("{v:.6}"))
            .unwrap_or_else(|| "N/A".to_string());
        let mode_str = match self.config.mode {
            MonitorMode::Minimize => "minimize",
            MonitorMode::Maximize => "maximize",
        };
        format!(
            "EarlyStoppingMonitor(mode={}, epoch={}, best={} at epoch {}, \
             patience={}/{}, stopped={})",
            mode_str,
            self.current_epoch,
            best_str,
            self.best_epoch,
            self.epochs_without_improvement,
            self.config.patience,
            self.stopped,
        )
    }
}

/// Policy for combining decisions from multiple metric monitors.
#[derive(Debug, Clone, PartialEq)]
pub enum MultiMetricPolicy {
    /// Stop when ALL monitored metrics signal stop.
    All,
    /// Stop when ANY monitored metric signals stop.
    Any,
}

/// A multi-metric early stopping monitor.
///
/// Monitors multiple metrics simultaneously and applies a policy
/// (`All` or `Any`) to decide when to stop training.
#[derive(Debug, Clone)]
pub struct MultiMetricMonitor {
    monitors: Vec<(String, EarlyStoppingMonitor)>,
    policy: MultiMetricPolicy,
}

impl MultiMetricMonitor {
    /// Create a new multi-metric monitor with the given policy.
    pub fn new(policy: MultiMetricPolicy) -> Self {
        Self {
            monitors: Vec::new(),
            policy,
        }
    }

    /// Register a new metric to monitor.
    pub fn add_metric(&mut self, name: impl Into<String>, config: EarlyStoppingConfig) {
        let name = name.into();
        let monitor = EarlyStoppingMonitor::new(config);
        self.monitors.push((name, monitor));
    }

    /// Report values for all metrics and return a combined decision.
    ///
    /// Keys in `values` must match registered metric names.
    /// Metrics not present in `values` are skipped for that step.
    pub fn step(&mut self, values: &[(String, f64)]) -> EarlyStoppingDecision {
        let value_map: HashMap<&str, f64> = values.iter().map(|(k, v)| (k.as_str(), *v)).collect();

        let mut decisions = Vec::new();

        for (name, monitor) in &mut self.monitors {
            if let Some(&val) = value_map.get(name.as_str()) {
                let decision = monitor.step(val);
                decisions.push(decision);
            }
        }

        // Apply the policy to decide the combined outcome.
        let any_stop = decisions
            .iter()
            .any(|d| matches!(d, EarlyStoppingDecision::Stop { .. }));
        let all_stop = !decisions.is_empty()
            && decisions
                .iter()
                .all(|d| matches!(d, EarlyStoppingDecision::Stop { .. }));

        let should_stop = match self.policy {
            MultiMetricPolicy::All => all_stop,
            MultiMetricPolicy::Any => any_stop,
        };

        if should_stop {
            // Collect reasons from all monitors that signaled stop.
            let reasons: Vec<String> = decisions
                .into_iter()
                .filter_map(|d| {
                    if let EarlyStoppingDecision::Stop { reason } = d {
                        Some(reason)
                    } else {
                        None
                    }
                })
                .collect();
            EarlyStoppingDecision::Stop {
                reason: reasons.join("; "),
            }
        } else {
            // Check if any monitor found a new best.
            let new_best = decisions
                .iter()
                .find(|d| matches!(d, EarlyStoppingDecision::NewBest { .. }));
            match new_best {
                Some(EarlyStoppingDecision::NewBest { value, epoch }) => {
                    EarlyStoppingDecision::NewBest {
                        value: *value,
                        epoch: *epoch,
                    }
                }
                _ => EarlyStoppingDecision::Continue,
            }
        }
    }

    /// Get an individual monitor by name.
    pub fn get_monitor(&self, name: &str) -> Option<&EarlyStoppingMonitor> {
        self.monitors
            .iter()
            .find(|(n, _)| n == name)
            .map(|(_, m)| m)
    }

    /// Number of registered metrics.
    pub fn num_metrics(&self) -> usize {
        self.monitors.len()
    }

    /// Summary across all monitors.
    pub fn summary(&self) -> String {
        let mut parts = Vec::new();
        parts.push(format!(
            "MultiMetricMonitor(policy={:?}, metrics={})",
            self.policy,
            self.monitors.len()
        ));
        for (name, monitor) in &self.monitors {
            parts.push(format!("  {}: {}", name, monitor.summary()));
        }
        parts.join("\n")
    }
}

/// Plateau detector: detects when a metric has plateaued.
///
/// A plateau is detected when the variance of the most recent values
/// within a sliding window falls below a configurable threshold.
#[derive(Debug, Clone)]
pub struct PlateauDetector {
    /// Size of the sliding window.
    pub window_size: usize,
    /// Variance threshold below which a plateau is declared.
    pub variance_threshold: f64,
    history: Vec<f64>,
}

impl PlateauDetector {
    /// Create a new plateau detector.
    pub fn new(window_size: usize, variance_threshold: f64) -> Self {
        Self {
            window_size,
            variance_threshold,
            history: Vec::new(),
        }
    }

    /// Push a new metric value.
    pub fn push(&mut self, value: f64) {
        self.history.push(value);
    }

    /// Whether the metric has plateaued (window full and variance below threshold).
    pub fn is_plateau(&self) -> bool {
        if self.history.len() < self.window_size {
            return false;
        }
        match self.current_variance() {
            Some(var) => var < self.variance_threshold,
            None => false,
        }
    }

    /// Compute the variance of the values in the current window.
    ///
    /// Returns `None` if there are fewer values than the window size.
    pub fn current_variance(&self) -> Option<f64> {
        if self.history.len() < self.window_size {
            return None;
        }
        let window = self.values_in_window();
        let n = window.len() as f64;
        if n < 1.0 {
            return None;
        }
        let mean = window.iter().sum::<f64>() / n;
        let variance = window.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / n;
        Some(variance)
    }

    /// The values currently in the sliding window.
    pub fn values_in_window(&self) -> &[f64] {
        if self.history.len() < self.window_size {
            &self.history
        } else {
            &self.history[self.history.len() - self.window_size..]
        }
    }
}

/// Training progress tracker.
///
/// Combines epoch tracking with multi-metric recording and progress reporting.
#[derive(Debug, Clone)]
pub struct TrainingProgress {
    /// Total number of planned epochs.
    pub total_epochs: usize,
    /// Current epoch (0-indexed, incremented by `advance_epoch`).
    pub current_epoch: usize,
    /// Recorded metrics keyed by name, each with a history of values.
    pub metrics: HashMap<String, Vec<f64>>,
}

impl TrainingProgress {
    /// Create a new training progress tracker.
    pub fn new(total_epochs: usize) -> Self {
        Self {
            total_epochs,
            current_epoch: 0,
            metrics: HashMap::new(),
        }
    }

    /// Record a metric value for the current epoch.
    pub fn record(&mut self, metric_name: impl Into<String>, value: f64) {
        self.metrics
            .entry(metric_name.into())
            .or_default()
            .push(value);
    }

    /// Fraction of training completed (current / total).
    pub fn progress_fraction(&self) -> f64 {
        if self.total_epochs == 0 {
            return 0.0;
        }
        self.current_epoch as f64 / self.total_epochs as f64
    }

    /// Advance to the next epoch.
    pub fn advance_epoch(&mut self) {
        self.current_epoch += 1;
    }

    /// Get the latest recorded value for a metric.
    pub fn latest(&self, metric_name: &str) -> Option<f64> {
        self.metrics
            .get(metric_name)
            .and_then(|v| v.last().copied())
    }

    /// Get the best (min or max) recorded value for a metric.
    pub fn best(&self, metric_name: &str, mode: &MonitorMode) -> Option<f64> {
        self.metrics.get(metric_name).and_then(|values| {
            if values.is_empty() {
                return None;
            }
            match mode {
                MonitorMode::Minimize => values
                    .iter()
                    .copied()
                    .min_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)),
                MonitorMode::Maximize => values
                    .iter()
                    .copied()
                    .max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)),
            }
        })
    }

    /// Human-readable summary of training progress.
    pub fn summary(&self) -> String {
        let pct = self.progress_fraction() * 100.0;
        let mut parts = vec![format!(
            "TrainingProgress: epoch {}/{} ({:.1}%)",
            self.current_epoch, self.total_epochs, pct
        )];
        for (name, values) in &self.metrics {
            let latest = values.last().map(|v| format!("{v:.6}")).unwrap_or_default();
            parts.push(format!(
                "  {}: latest={}, entries={}",
                name,
                latest,
                values.len()
            ));
        }
        parts.join("\n")
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_early_stopping_config_default() {
        let config = EarlyStoppingConfig::default();
        assert_eq!(config.patience, 10);
        assert_eq!(config.min_delta, 0.0);
        assert_eq!(config.mode, MonitorMode::Minimize);
        assert!(config.baseline.is_none());
        assert!(config.restore_best);
        assert_eq!(config.min_epochs, 1);
    }

    #[test]
    fn test_monitor_new_best_on_first_step() {
        let mut monitor = EarlyStoppingMonitor::with_default();
        let decision = monitor.step(1.0);
        assert_eq!(
            decision,
            EarlyStoppingDecision::NewBest {
                value: 1.0,
                epoch: 1
            }
        );
    }

    #[test]
    fn test_monitor_continue_while_improving() {
        let config = EarlyStoppingConfig {
            patience: 3,
            ..Default::default()
        };
        let mut monitor = EarlyStoppingMonitor::new(config);

        // Decreasing loss values = improvement in Minimize mode.
        let d1 = monitor.step(1.0);
        assert!(matches!(d1, EarlyStoppingDecision::NewBest { .. }));

        let d2 = monitor.step(0.8);
        assert!(matches!(d2, EarlyStoppingDecision::NewBest { .. }));

        let d3 = monitor.step(0.6);
        assert!(matches!(d3, EarlyStoppingDecision::NewBest { .. }));

        let d4 = monitor.step(0.4);
        assert!(matches!(d4, EarlyStoppingDecision::NewBest { .. }));
    }

    #[test]
    fn test_monitor_stop_after_patience() {
        let config = EarlyStoppingConfig {
            patience: 3,
            ..Default::default()
        };
        let mut monitor = EarlyStoppingMonitor::new(config);

        // First step sets the best.
        monitor.step(1.0);

        // No improvement for 3 epochs.
        let d1 = monitor.step(1.5);
        assert_eq!(d1, EarlyStoppingDecision::Continue);

        let d2 = monitor.step(1.5);
        assert_eq!(d2, EarlyStoppingDecision::Continue);

        let d3 = monitor.step(1.5);
        assert!(matches!(d3, EarlyStoppingDecision::Stop { .. }));
        assert!(monitor.should_stop());
    }

    #[test]
    fn test_monitor_min_delta_threshold() {
        let config = EarlyStoppingConfig {
            patience: 2,
            min_delta: 0.1,
            ..Default::default()
        };
        let mut monitor = EarlyStoppingMonitor::new(config);

        // Best = 1.0
        monitor.step(1.0);

        // 0.95 is not < 1.0 - 0.1 = 0.9, so NOT an improvement.
        let d = monitor.step(0.95);
        assert_eq!(d, EarlyStoppingDecision::Continue);

        // 0.89 IS < 0.9, so it IS an improvement.
        let d = monitor.step(0.89);
        assert!(matches!(d, EarlyStoppingDecision::NewBest { .. }));
    }

    #[test]
    fn test_monitor_maximize_mode() {
        let config = EarlyStoppingConfig {
            patience: 3,
            mode: MonitorMode::Maximize,
            ..Default::default()
        };
        let mut monitor = EarlyStoppingMonitor::new(config);

        let d1 = monitor.step(0.5);
        assert!(matches!(d1, EarlyStoppingDecision::NewBest { .. }));

        let d2 = monitor.step(0.7);
        assert!(matches!(d2, EarlyStoppingDecision::NewBest { .. }));

        let d3 = monitor.step(0.9);
        assert!(matches!(d3, EarlyStoppingDecision::NewBest { .. }));

        // Metric goes down — not an improvement in Maximize mode.
        let d4 = monitor.step(0.8);
        assert_eq!(d4, EarlyStoppingDecision::Continue);
    }

    #[test]
    fn test_monitor_baseline_required() {
        let config = EarlyStoppingConfig {
            patience: 5,
            baseline: Some(0.5),
            mode: MonitorMode::Minimize,
            ..Default::default()
        };
        let mut monitor = EarlyStoppingMonitor::new(config);

        // 0.8 does NOT beat baseline of 0.5 (for Minimize, need < 0.5).
        let d = monitor.step(0.8);
        assert_eq!(d, EarlyStoppingDecision::Continue);
        assert!(monitor.best_value().is_none());

        // 0.4 DOES beat baseline.
        let d = monitor.step(0.4);
        assert!(matches!(d, EarlyStoppingDecision::NewBest { .. }));
    }

    #[test]
    fn test_monitor_min_epochs_prevents_early_stop() {
        let config = EarlyStoppingConfig {
            patience: 1,
            min_epochs: 5,
            ..Default::default()
        };
        let mut monitor = EarlyStoppingMonitor::new(config);

        // First step sets best.
        monitor.step(1.0);

        // No improvement, but we're under min_epochs (5), so Continue.
        let d = monitor.step(2.0);
        assert_eq!(d, EarlyStoppingDecision::Continue);

        let d = monitor.step(2.0);
        assert_eq!(d, EarlyStoppingDecision::Continue);

        let d = monitor.step(2.0);
        assert_eq!(d, EarlyStoppingDecision::Continue);

        // Epoch 5 — now min_epochs is satisfied, patience (1) exceeded.
        let d = monitor.step(2.0);
        assert!(matches!(d, EarlyStoppingDecision::Stop { .. }));
    }

    #[test]
    fn test_monitor_best_value_tracked() {
        let mut monitor = EarlyStoppingMonitor::with_default();
        monitor.step(1.0);
        monitor.step(0.5);
        monitor.step(0.8);
        assert_eq!(monitor.best_value(), Some(0.5));
        assert_eq!(monitor.best_epoch(), 2);
    }

    #[test]
    fn test_monitor_reset() {
        let mut monitor = EarlyStoppingMonitor::with_default();
        monitor.step(1.0);
        monitor.step(0.5);
        assert!(monitor.best_value().is_some());

        monitor.reset();
        assert!(monitor.best_value().is_none());
        assert_eq!(monitor.current_epoch(), 0);
        assert!(monitor.history().is_empty());
        assert!(!monitor.should_stop());
    }

    #[test]
    fn test_monitor_history() {
        let mut monitor = EarlyStoppingMonitor::with_default();
        monitor.step(1.0);
        monitor.step(0.8);
        monitor.step(0.6);
        assert_eq!(monitor.history().len(), 3);
        assert_eq!(monitor.history(), &[1.0, 0.8, 0.6]);
    }

    #[test]
    fn test_monitor_summary_nonempty() {
        let mut monitor = EarlyStoppingMonitor::with_default();
        monitor.step(1.0);
        let summary = monitor.summary();
        assert!(!summary.is_empty());
        assert!(summary.contains("minimize"));
    }

    #[test]
    fn test_multi_metric_any_policy() {
        let mut mm = MultiMetricMonitor::new(MultiMetricPolicy::Any);
        mm.add_metric(
            "loss",
            EarlyStoppingConfig {
                patience: 2,
                ..Default::default()
            },
        );
        mm.add_metric(
            "accuracy",
            EarlyStoppingConfig {
                patience: 100, // very high patience
                mode: MonitorMode::Maximize,
                ..Default::default()
            },
        );

        // Step 1: both get new best.
        let d = mm.step(&[("loss".to_string(), 1.0), ("accuracy".to_string(), 0.5)]);
        assert!(matches!(d, EarlyStoppingDecision::NewBest { .. }));

        // Step 2: loss not improving, accuracy not improving.
        let d = mm.step(&[("loss".to_string(), 1.5), ("accuracy".to_string(), 0.3)]);
        assert_eq!(d, EarlyStoppingDecision::Continue);

        // Step 3: loss patience exhausted (2 epochs without improvement) → Any triggers stop.
        let d = mm.step(&[("loss".to_string(), 1.5), ("accuracy".to_string(), 0.3)]);
        assert!(matches!(d, EarlyStoppingDecision::Stop { .. }));
    }

    #[test]
    fn test_multi_metric_all_policy() {
        let mut mm = MultiMetricMonitor::new(MultiMetricPolicy::All);
        mm.add_metric(
            "loss",
            EarlyStoppingConfig {
                patience: 2,
                ..Default::default()
            },
        );
        mm.add_metric(
            "accuracy",
            EarlyStoppingConfig {
                patience: 2,
                mode: MonitorMode::Maximize,
                ..Default::default()
            },
        );

        // Step 1: both new best.
        mm.step(&[("loss".to_string(), 1.0), ("accuracy".to_string(), 0.5)]);

        // Step 2-3: no improvement in either.
        mm.step(&[("loss".to_string(), 1.5), ("accuracy".to_string(), 0.3)]);

        // Step 3: loss has patience=2 exhausted, accuracy has patience=2 exhausted → All triggers.
        let d = mm.step(&[("loss".to_string(), 1.5), ("accuracy".to_string(), 0.3)]);
        assert!(matches!(d, EarlyStoppingDecision::Stop { .. }));
    }

    #[test]
    fn test_multi_metric_all_policy_no_stop_when_one_improving() {
        let mut mm = MultiMetricMonitor::new(MultiMetricPolicy::All);
        mm.add_metric(
            "loss",
            EarlyStoppingConfig {
                patience: 2,
                ..Default::default()
            },
        );
        mm.add_metric(
            "accuracy",
            EarlyStoppingConfig {
                patience: 2,
                mode: MonitorMode::Maximize,
                ..Default::default()
            },
        );

        // Step 1
        mm.step(&[("loss".to_string(), 1.0), ("accuracy".to_string(), 0.5)]);

        // Step 2: loss stagnant, accuracy improving.
        mm.step(&[("loss".to_string(), 1.5), ("accuracy".to_string(), 0.7)]);

        // Step 3: loss patience exhausted but accuracy still improving → All does NOT stop.
        let d = mm.step(&[("loss".to_string(), 1.5), ("accuracy".to_string(), 0.9)]);
        assert!(!matches!(d, EarlyStoppingDecision::Stop { .. }));
    }

    #[test]
    fn test_multi_metric_get_monitor() {
        let mut mm = MultiMetricMonitor::new(MultiMetricPolicy::Any);
        mm.add_metric("loss", EarlyStoppingConfig::default());
        mm.add_metric(
            "accuracy",
            EarlyStoppingConfig {
                mode: MonitorMode::Maximize,
                ..Default::default()
            },
        );

        assert!(mm.get_monitor("loss").is_some());
        assert!(mm.get_monitor("accuracy").is_some());
        assert!(mm.get_monitor("nonexistent").is_none());
        assert_eq!(mm.num_metrics(), 2);
    }

    #[test]
    fn test_multi_metric_summary() {
        let mut mm = MultiMetricMonitor::new(MultiMetricPolicy::Any);
        mm.add_metric("loss", EarlyStoppingConfig::default());
        let summary = mm.summary();
        assert!(!summary.is_empty());
        assert!(summary.contains("loss"));
    }

    #[test]
    fn test_plateau_detector_no_plateau() {
        let mut detector = PlateauDetector::new(3, 0.001);
        detector.push(1.0);
        detector.push(2.0);
        detector.push(3.0);
        assert!(!detector.is_plateau());
        assert!(detector.current_variance().is_some());
    }

    #[test]
    fn test_plateau_detector_plateau() {
        let mut detector = PlateauDetector::new(3, 0.001);
        detector.push(1.0);
        detector.push(1.0);
        detector.push(1.0);
        assert!(detector.is_plateau());
        assert_eq!(detector.current_variance(), Some(0.0));
    }

    #[test]
    fn test_plateau_detector_insufficient_data() {
        let mut detector = PlateauDetector::new(5, 0.001);
        detector.push(1.0);
        detector.push(1.0);
        assert!(!detector.is_plateau());
        assert!(detector.current_variance().is_none());
    }

    #[test]
    fn test_plateau_detector_window_slides() {
        let mut detector = PlateauDetector::new(3, 0.001);
        // Push varying values, then constant ones.
        detector.push(1.0);
        detector.push(5.0);
        detector.push(10.0);
        assert!(!detector.is_plateau()); // high variance

        detector.push(2.0);
        detector.push(2.0);
        detector.push(2.0);
        assert!(detector.is_plateau()); // window is [2.0, 2.0, 2.0]
    }

    #[test]
    fn test_training_progress_advance() {
        let mut progress = TrainingProgress::new(100);
        assert_eq!(progress.current_epoch, 0);
        progress.advance_epoch();
        assert_eq!(progress.current_epoch, 1);
        progress.advance_epoch();
        assert_eq!(progress.current_epoch, 2);
    }

    #[test]
    fn test_training_progress_best_minimize() {
        let mut progress = TrainingProgress::new(10);
        progress.record("loss", 1.0);
        progress.record("loss", 0.5);
        progress.record("loss", 0.8);

        let best = progress.best("loss", &MonitorMode::Minimize);
        assert_eq!(best, Some(0.5));
    }

    #[test]
    fn test_training_progress_best_maximize() {
        let mut progress = TrainingProgress::new(10);
        progress.record("accuracy", 0.6);
        progress.record("accuracy", 0.9);
        progress.record("accuracy", 0.7);

        let best = progress.best("accuracy", &MonitorMode::Maximize);
        assert_eq!(best, Some(0.9));
    }

    #[test]
    fn test_training_progress_latest() {
        let mut progress = TrainingProgress::new(10);
        progress.record("loss", 1.0);
        progress.record("loss", 0.5);
        assert_eq!(progress.latest("loss"), Some(0.5));
        assert_eq!(progress.latest("nonexistent"), None);
    }

    #[test]
    fn test_training_progress_fraction() {
        let mut progress = TrainingProgress::new(10);
        assert_eq!(progress.progress_fraction(), 0.0);
        progress.advance_epoch();
        progress.advance_epoch();
        progress.advance_epoch();
        assert!((progress.progress_fraction() - 0.3).abs() < 1e-10);
    }

    #[test]
    fn test_training_progress_summary() {
        let mut progress = TrainingProgress::new(10);
        progress.advance_epoch();
        progress.record("loss", 0.5);
        let summary = progress.summary();
        assert!(!summary.is_empty());
        assert!(summary.contains("loss"));
    }

    #[test]
    fn test_training_progress_zero_total_epochs() {
        let progress = TrainingProgress::new(0);
        assert_eq!(progress.progress_fraction(), 0.0);
    }
}