//! `BUDGET=baum`: the work of every postflop solve is a deterministic function of its tree size, not of the clock. //! (Calibration helpers: `BAUM_LOG=1` prints one stderr line per finished solve loop.) /// `BUDGET=baum` is set (read once). Off: nothing in this module is consulted and every path is the clock's. pub fn aktiv() -> bool { static AN: std::sync::OnceLock = std::sync::OnceLock::new(); *AN.get_or_init(|| std::env::var("BUDGET").as_deref() == Ok("baum")) } /// Calibration log `BAUM_LOG=1`: `BAUM_LOG setup_ms loop_ms iters` on stderr. pub fn log(art: &str, feats: &str, setup_ms: f64, loop_ms: f64, done: u64) { static LOG: std::sync::OnceLock = std::sync::OnceLock::new(); if *LOG.get_or_init(|| std::env::var("BAUM_LOG").is_ok_and(|v| !v.is_empty() && v != "0")) { eprintln!("BAUM_LOG\t{art}\t{feats}\t{setup_ms:.1}\t{loop_ms:.1}\t{done}"); } } // --------------------------------------------------------------------------------------------------------------------- // The work of a solve as a function of its tree size. Constants: least-squares fits of CPU ms per iteration over the 238 // fixture decisions, measured with fixed iterations (`SOLVER_ITER`, `BAUM_LOG=1`; messung/solver_kern/BUDGET_BAUM.md), relative // error 12 to 16 % (HU) and 4 to 12 % (MW). A solve gets what the live clock would give it on an idle core // (`TURN_BUDGET_MS` etc.), but the iterations, the river/turn cards and the multiway stage are computed from these predictions // BEFORE the solve, so the same spot gets the same work whatever the machine load. // --------------------------------------------------------------------------------------------------------------------- use crate::engine::frist::Art; use std::cell::Cell; thread_local! { /// Predicted CPU ms the solves of this decision have used so far (the deterministic stand-in of `frist::elapsed_ms`). static KONTO: Cell = const { Cell::new(0.0) }; } /// New decision (called by `frist::begin`). pub fn neu() { KONTO.with(|k| k.set(0.0)); } /// Predicted CPU ms spent on solves in this decision so far. pub fn konto() -> f64 { KONTO.with(Cell::get) } /// Book predicted CPU ms. pub fn buche(ms: f64) { KONTO.with(|k| k.set(k.get() + ms)); } /// Setup (ranges, tree, equity tables) of a heads-up flop/turn/river solve, CPU ms (the measured ticks are 0 to 60). pub const HU_SETUP_MS: f64 = 15.0; /// CPU ms of one heads-up river iteration. pub fn hu_river_iter_ms(c0: usize, c1: usize, nodes: usize) -> f64 { 7.0e-6 * nodes as f64 * (c0 + c1) as f64 } /// CPU ms of one heads-up turn iteration with `k` river cards. pub fn hu_turn_iter_ms(c0: usize, c1: usize, nodes: usize, k: usize) -> f64 { k as f64 * (3.8e-7 * (c0 * c1) as f64 + 6.4e-6 * nodes as f64 * (c0 + c1) as f64) } /// CPU ms of one heads-up flop iteration with `k` turn cards. pub fn hu_flop_iter_ms(c0: usize, c1: usize, nodes: usize, k: usize) -> f64 { k as f64 * 7.5e-6 * nodes as f64 * (c0 + c1) as f64 } /// CPU ms of one multiway iteration (MCCFR samples one path: the cost follows players x combos, not the node count; /// fit `ms = k x players x sum(combos)`, relative error ~20 %, the small solves are limited by the 15.6 ms clock tick). pub fn mw_iter_ms(art: Art, players: usize, combos: &[usize]) -> f64 { let k = match art { Art::MwFlop => 4.2e-5, Art::MwTurn => 4.8e-5, _ => 1.75e-5, }; k * players as f64 * combos.iter().sum::() as f64 } /// CPU ms to set up a multiway solve of `nodes` tree nodes (locks and rows). pub fn mw_setup_ms(nodes: usize) -> f64 { 1.6e-3 * nodes as f64 } /// `x` rounded down to a multiple of `step`, between `lo` and `hi` (the live clock checks every `step` iterations too). pub fn iters_aus(x: f64, step: u32, lo: u32, hi: u32) -> u32 { let n = if x.is_finite() && x > 0.0 { (x as u32) / step * step } else { 0 }; n.clamp(lo, hi.max(lo)) } /// Iterations of a solve that must fit `kappe` CPU ms with `setup` ms already gone and `p` ms per iteration. pub fn iters_fuer(kappe: f64, setup: f64, p: f64, step: u32, lo: u32, hi: u32) -> u32 { iters_aus((kappe - setup) / p.max(1e-9), step, lo, hi) } /// Cards of a heads-up turn/flop solve so that `min_iters` iterations (x 1.3 slack, as the live check) fit `kappe`: /// the live `eng` rule (scale by `(kappe - setup) / (p x min_iters x 1.5)`, at most three times), with the predicted cost in /// place of two measured iterations. `iter_ms(k)` = predicted ms per iteration with `k` cards. pub fn karten_fuer(k0: usize, kmin: usize, kappe: f64, min_iters: u32, iter_ms: &dyn Fn(usize) -> f64) -> usize { let mut k = k0; for _ in 0..3 { let p = iter_ms(k); if p * min_iters as f64 * 1.3 <= kappe - HU_SETUP_MS || k <= kmin { break; } let skala = ((kappe - HU_SETUP_MS) / (p * min_iters as f64 * 1.5)).clamp(0.05, 0.9); k = ((k as f64 * skala) as usize).max(kmin); } k } /// Measurement only (`BAUM_KARTEN=`, under `BUDGET=baum`): force the river/turn card count of every heads-up turn/flop solve /// (48 = all), to compare the chosen count with the full one. pub fn karten_vorgabe() -> Option { static K: std::sync::OnceLock> = std::sync::OnceLock::new(); *K.get_or_init(|| std::env::var("BAUM_KARTEN").ok().and_then(|v| v.trim().parse().ok()).filter(|n: &usize| *n > 0)) } /// Measurement only (`BAUM_WAHL=schritt`, under `BUDGET=baum`): the old suit-sorted stride card selection instead of the balanced one, /// to tell what a difference to the clock/`SOLVER_ITER` runs owes to the card choice. pub fn wahl_schritt() -> bool { static W: std::sync::OnceLock = std::sync::OnceLock::new(); *W.get_or_init(|| std::env::var("BAUM_WAHL").as_deref() == Ok("schritt")) }