pub const MADSEN_DAMPING_CAP: f64 = 1e12;
pub const PLATEAU_DEFAULT_WINDOW: usize = 3;
pub const PLATEAU_DEFAULT_REL_TOL: f64 = 1e-8;
#[inline]
pub fn madsen_can_retry(damping: f64) -> bool {
damping.is_finite() && damping < MADSEN_DAMPING_CAP
}
#[inline]
pub fn madsen_retry_exhausted(damping: f64, attempts: usize, max_attempts: usize) -> bool {
attempts >= max_attempts || !damping.is_finite() || damping > MADSEN_DAMPING_CAP
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LoopVerdict {
Continue,
Plateaued,
Exhausted,
}
#[derive(Clone, Debug)]
pub struct FlatStreak {
window: usize,
streak: usize,
}
impl FlatStreak {
pub fn new(window: usize) -> Self {
Self {
window: window.max(1),
streak: 0,
}
}
pub fn note(&mut self, flat: bool) -> LoopVerdict {
if flat {
self.streak += 1;
if self.streak >= self.window {
return LoopVerdict::Plateaued;
}
} else {
self.streak = 0;
}
LoopVerdict::Continue
}
pub fn reset(&mut self) {
self.streak = 0;
}
pub fn streak(&self) -> usize {
self.streak
}
}
#[derive(Clone, Debug)]
pub struct PlateauDetector {
rel_tol: f64,
streak: FlatStreak,
last_merit: Option<f64>,
}
impl PlateauDetector {
pub fn new(window: usize, rel_tol: f64) -> Self {
Self {
rel_tol,
streak: FlatStreak::new(window),
last_merit: None,
}
}
pub fn standard() -> Self {
Self::new(PLATEAU_DEFAULT_WINDOW, PLATEAU_DEFAULT_REL_TOL)
}
pub fn note(&mut self, merit: f64) -> LoopVerdict {
if !merit.is_finite() {
self.streak.reset();
self.last_merit = None;
return LoopVerdict::Continue;
}
let verdict = match self.last_merit {
Some(prev) => {
let scale = prev.abs().max(merit.abs()).max(1.0);
self.streak
.note((prev - merit).abs() <= self.rel_tol * scale)
}
None => LoopVerdict::Continue,
};
self.last_merit = Some(merit);
verdict
}
}
#[derive(Clone, Debug)]
pub struct IterationBound {
used: usize,
max: usize,
}
impl IterationBound {
pub fn new(max: usize) -> Self {
Self {
used: 0,
max: max.max(1),
}
}
pub fn tick(&mut self) {
self.used += 1;
}
pub fn used(&self) -> usize {
self.used
}
pub fn max(&self) -> usize {
self.max
}
pub fn count_exhausted(&self) -> bool {
self.used >= self.max
}
pub fn exhausted_at(&self, damping: f64) -> bool {
madsen_retry_exhausted(damping, self.used, self.max)
}
pub fn verdict_at(&self, damping: f64) -> LoopVerdict {
if self.exhausted_at(damping) {
LoopVerdict::Exhausted
} else {
LoopVerdict::Continue
}
}
}
pub const MADSEN_INITIAL_REJECT_FACTOR: f64 = 2.0;
#[derive(Clone, Debug)]
pub struct RejectEscalator {
factor: f64,
rejects: usize,
}
impl Default for RejectEscalator {
fn default() -> Self {
Self::new()
}
}
impl RejectEscalator {
pub fn new() -> Self {
Self {
factor: MADSEN_INITIAL_REJECT_FACTOR,
rejects: 0,
}
}
pub fn escalate(&mut self, damping: &mut f64) {
*damping *= self.factor;
self.factor *= 2.0;
self.rejects += 1;
}
pub fn restart(&mut self) {
self.factor = MADSEN_INITIAL_REJECT_FACTOR;
self.rejects = 0;
}
pub fn rejects(&self) -> usize {
self.rejects
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reject_storm_exhausts_in_bounded_steps() {
let mut esc = RejectEscalator::new();
let mut damping = 1.0;
let mut steps = 0usize;
while madsen_can_retry(damping) {
esc.escalate(&mut damping);
steps += 1;
assert!(steps <= 64, "escalation must reach the damping cap");
}
assert!(steps <= 16, "escalation took {steps} steps");
assert_eq!(esc.rejects(), steps);
assert!(madsen_retry_exhausted(damping, 0, usize::MAX));
}
#[test]
fn continue_paths_without_rejects_still_exhaust_the_bound() {
let mut bound = IterationBound::new(5);
let esc = RejectEscalator::new();
let damping = 1e-6; let mut passes = 0usize;
while !bound.exhausted_at(damping) {
bound.tick();
passes += 1;
assert!(passes <= 5, "bound must stop a reject-free spin");
}
assert_eq!(passes, 5);
assert_eq!(esc.rejects(), 0, "no reject was ever recorded");
assert_eq!(bound.verdict_at(damping), LoopVerdict::Exhausted);
}
#[test]
fn escalations_do_not_double_count_iterations() {
let mut bound = IterationBound::new(10);
let mut esc = RejectEscalator::new();
let mut damping = 1.0;
bound.tick();
for _ in 0..3 {
esc.escalate(&mut damping);
}
assert_eq!(bound.used(), 1);
assert_eq!(esc.rejects(), 3);
assert!(!bound.count_exhausted());
}
#[test]
fn restart_rewinds_the_geometric_schedule() {
let mut esc = RejectEscalator::new();
let mut damping = 1.0;
esc.escalate(&mut damping); esc.escalate(&mut damping); assert_eq!(damping, 8.0);
esc.restart();
assert_eq!(esc.rejects(), 0);
let mut fresh = 1.0;
esc.escalate(&mut fresh);
assert_eq!(
fresh, MADSEN_INITIAL_REJECT_FACTOR,
"schedule restarts at ×2"
);
}
#[test]
fn plateau_fires_on_frozen_merit_and_not_during_descent() {
let mut det = PlateauDetector::new(3, 1e-8);
let mut merit = 100.0;
for _ in 0..50 {
assert_eq!(det.note(merit), LoopVerdict::Continue, "descent phase");
merit *= 0.9;
}
let mut fired_after = 0usize;
loop {
fired_after += 1;
assert!(fired_after <= 4, "plateau must fire within the window");
if det.note(merit) == LoopVerdict::Plateaued {
break;
}
}
assert!(fired_after >= 3, "must not fire before the streak window");
}
#[test]
fn plateau_resets_on_recovery_and_ignores_non_finite() {
let mut det = PlateauDetector::new(2, 1e-8);
det.note(10.0);
assert_eq!(det.note(10.0), LoopVerdict::Continue); assert_eq!(det.note(9.0), LoopVerdict::Continue); assert_eq!(det.note(9.0), LoopVerdict::Continue); assert_eq!(det.note(f64::NAN), LoopVerdict::Continue); assert_eq!(det.note(9.0), LoopVerdict::Continue); assert_eq!(det.note(9.0), LoopVerdict::Continue); assert_eq!(det.note(9.0), LoopVerdict::Plateaued); }
#[test]
fn flat_streak_pins_the_window_discipline() {
let mut streak = FlatStreak::new(3);
assert_eq!(streak.note(true), LoopVerdict::Continue); assert_eq!(streak.note(true), LoopVerdict::Continue); assert_eq!(streak.note(false), LoopVerdict::Continue); assert_eq!(streak.streak(), 0);
assert_eq!(streak.note(true), LoopVerdict::Continue); assert_eq!(streak.note(true), LoopVerdict::Continue); assert_eq!(streak.note(true), LoopVerdict::Plateaued); assert_eq!(streak.note(true), LoopVerdict::Plateaued); assert_eq!(streak.streak(), 4);
}
#[test]
fn plateau_detector_is_scale_invariant_on_large_magnitude_objective() {
let rel_tol = 1e-8;
let window = 3;
let big = 6.0e4_f64;
let mut det_big = PlateauDetector::new(window, rel_tol);
det_big.note(big);
let mut big_fired_at = None;
for k in 0..window {
let v = big - 6.0e-5 * (k as f64 + 1.0);
if det_big.note(v) == LoopVerdict::Plateaued {
big_fired_at = Some(k);
break;
}
}
assert!(
big_fired_at.is_some(),
"relative plateau detector must terminate on an O(1e4) flat valley \
within the window; instead it ran past it (the #1040 hang)"
);
let small = 1.0_f64;
let mut det_small = PlateauDetector::new(window, rel_tol);
det_small.note(small);
let mut small_fired_at = None;
for k in 0..window {
let v = small - 1.0e-9 * (k as f64 + 1.0);
if det_small.note(v) == LoopVerdict::Plateaued {
small_fired_at = Some(k);
break;
}
}
assert_eq!(
big_fired_at, small_fired_at,
"plateau detection must be invariant to objective magnitude"
);
let mut det_moving = PlateauDetector::new(window, rel_tol);
det_moving.note(big);
let mut moving = big;
for _ in 0..window {
moving -= 0.01 * moving;
assert_eq!(
det_moving.note(moving),
LoopVerdict::Continue,
"a 1%-per-cycle relative descent must never register as flat"
);
}
}
#[test]
fn policy_predicates_pin_the_reweight_semantics() {
assert!(madsen_can_retry(1e11));
assert!(!madsen_can_retry(MADSEN_DAMPING_CAP));
assert!(!madsen_can_retry(f64::INFINITY));
assert!(madsen_retry_exhausted(1.0, 5, 5));
assert!(madsen_retry_exhausted(f64::NAN, 0, 5));
assert!(madsen_retry_exhausted(1e13, 0, 5));
assert!(!madsen_retry_exhausted(1.0, 4, 5));
}
}