Files
pokerfreak/src/sim/benchmark.rs
T
mmintelandClaude Opus 4.8 1492fb6a38 Synthetik-Labor: Population, Zwillings-Tor, adaptive Liga, Optimizer, Sim-to-Real
Ersetzt die irrefuehrende Oracle-Leiter durch eine ehrliche Messmethodik
(Oracle aus, Read via Memory/Posterior gegen eine gezogene Gegner-Population).

Stufen:
- 0 Exploitation-Luecke: +18.98 bb/100 gegen die gezogene Population
- 1 Kalibrierung an echte HUD-Daten (lesen/kalibrierung.py)
- 2 Zwillings-Tor (EGTA + Exploitability-Waechter): Offense +43.95,
  Boden gto -36.25 (verliert gegen Balancierte - die Oracle-verdeckte Wurzel)
- 3 Adaptive Liga L0-L3 (reaktiv / modellierend / Liga)
- 4 Sim-to-Real: HUD-Gap (sim::simreal) + Outcome-Abgleich (lesen/liveabgleich.py):
  real -266.75 vs Sim +43.95 => Gap -311; A/B-Pipeline + Runbook fertig,
  nur der geldechte Live-Lauf unter beiden Armen bleibt offen
- 5 Optimizer (Diagonal-CMA-ES, parallel): Nemesis -414.81 bb/100

Befunde und Architektur in docs/notizen/. Lab-Tests gruen
(Rust lib 92/0, python liveabgleich/kalibrierung/gegnersignal gruen).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-11 09:57:32 +01:00

222 lines
10 KiB
Rust

//! The benchmark: a fixed set of scenarios, run the same way for every version of the engine, so
//! that two runs can be compared deal by deal (`sim::paired`). The runs in text files; the
//! pure parts are here, the file and clock work in `src/bin/benchmark.rs`.
use super::opponent::Opponent;
use super::paired::{compare, Comparison, Verdict};
use super::reader::Style;
use super::session::{deal_samples, ReadMode, Setup};
use std::thread;
const SEED: u64 = 0x5EED_0001;
const STACK_BB: f64 = 100.0;
const HEADS_UP_DEALS: u32 = 1000;
const SIX_MAX_DEALS: u32 = 150;
/// The balanced opponents (gto, gto-small) run more deals: their leaks are small and concentrated in
/// a few high-variance river pots, so the paired interval must be narrow enough to prove a defensive
/// fix worth a handful of bb/100 (BEFUND-gto-restleck-river-overcall).
const BALANCED_DEALS: u32 = 3000;
pub struct Scenario {
pub name: &'static str,
pub setup: Setup,
}
const fn with_read(name: &'static str, players: u8, opponent: Opponent, read: ReadMode) -> Scenario {
let deals = if players == 2 { HEADS_UP_DEALS } else { SIX_MAX_DEALS };
Scenario { name, setup: Setup { players, opponent, deals, stack_bb: STACK_BB, seed: SEED, read } }
}
/// Read-given: the engine sees the opponent's converged HUD read (the exploit brain).
const fn scenario(name: &'static str, players: u8, opponent: Opponent) -> Scenario {
with_read(name, players, opponent, ReadMode::Oracle)
}
/// A heads-up balanced opponent (gto family) at the higher `BALANCED_DEALS` count, in any read mode.
const fn balanced(name: &'static str, opponent: Opponent, read: ReadMode) -> Scenario {
Scenario { name, setup: Setup { players: 2, opponent, deals: BALANCED_DEALS, stack_bb: STACK_BB, seed: SEED, read } }
}
/// Blind: the read is withheld, the no-read baseline a memory / probe must improve on. The gap to
/// the matching read-given scenario is the measured session payoff of a read (`tests/sim_blind`).
const fn blind(name: &'static str, players: u8, opponent: Opponent) -> Scenario {
with_read(name, players, opponent, ReadMode::Blind)
}
/// Memory: no handed read; the engine builds one from observed actions over the session
/// (`sim::memory`). Lands between the blind and read-given twins - the share of the prize that
/// observation alone recaptures.
const fn memory(name: &'static str, players: u8, opponent: Opponent) -> Scenario {
with_read(name, players, opponent, ReadMode::Memory)
}
/// Folder and random opponent are left out: the first is saturated at +75, the second's variance
/// would drown the total. `tests/sim_strength.rs` keeps watching both.
///
/// The last four are the two opponents the engine can actually exploit today (the calling station
/// and the fish, the classes `exploit::read_gate` lets through), each blind and memory: blind is the
/// no-read baseline, memory is the read built from observation. The ladder thus tracks both the
/// prize of a read and how much of it the session memory earns back.
pub const SCENARIOS: [Scenario; 19] = [
scenario("heads-up reg", 2, Opponent::Reader(Style::Reg)),
scenario("heads-up nit", 2, Opponent::Reader(Style::Nit)),
scenario("heads-up maniac", 2, Opponent::Reader(Style::Maniac)),
scenario("heads-up fish", 2, Opponent::Reader(Style::Fish)),
scenario("heads-up calling station", 2, Opponent::CallingStation),
balanced("heads-up gto", Opponent::Reader(Style::Gto), ReadMode::Oracle),
balanced("heads-up gto-small", Opponent::Reader(Style::GtoSmall), ReadMode::Oracle),
scenario("6-max reg", 6, Opponent::Reader(Style::Reg)),
scenario("6-max nit", 6, Opponent::Reader(Style::Nit)),
scenario("6-max maniac", 6, Opponent::Reader(Style::Maniac)),
scenario("6-max fish", 6, Opponent::Reader(Style::Fish)),
scenario("6-max calling station", 6, Opponent::CallingStation),
scenario("6-max gto", 6, Opponent::Reader(Style::Gto)),
balanced("heads-up gto (blind)", Opponent::Reader(Style::Gto), ReadMode::Blind),
balanced("heads-up gto-small (blind)", Opponent::Reader(Style::GtoSmall), ReadMode::Blind),
blind("heads-up fish (blind)", 2, Opponent::Reader(Style::Fish)),
blind("heads-up calling station (blind)", 2, Opponent::CallingStation),
memory("heads-up fish (memory)", 2, Opponent::Reader(Style::Fish)),
memory("heads-up calling station (memory)", 2, Opponent::CallingStation),
];
#[derive(Clone, Debug, PartialEq)]
pub struct ScenarioRun {
pub name: String,
pub deals: u32,
pub seed: u64,
pub decisions: u32,
pub abstains: u32,
/// Requests the guard refused: a simulator bug, must stay 0.
pub invalid: u32,
/// Hero's luck-corrected result per deal, bb.
pub samples: Vec<f64>,
}
impl ScenarioRun {
pub fn bb_per_100(&self) -> f64 {
100.0 * self.samples.iter().sum::<f64>() / self.samples.len().max(1) as f64
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Run {
/// Which version this is.
pub label: String,
pub scenarios: Vec<ScenarioRun>,
}
/// Plays every scenario, one thread each. The result does not depend on the threads.
pub fn run_all(label: &str) -> Run {
let scenarios = thread::scope(|scope| {
let running: Vec<_> = SCENARIOS
.iter()
.map(|s| {
scope.spawn(move || {
let (samples, m) = deal_samples(&s.setup);
ScenarioRun {
name: s.name.to_string(),
deals: s.setup.deals,
seed: s.setup.seed,
decisions: m.decisions,
abstains: m.abstains,
invalid: m.invalid,
samples,
}
})
})
.collect();
running.into_iter().map(|r| r.join().expect("a scenario panicked")).collect()
});
Run { label: label.to_string(), scenarios }
}
pub struct Report {
pub scenarios: Vec<(String, Comparison)>,
pub overall: Comparison,
pub invalid: u32,
}
impl Report {
/// Worse anywhere, even if the total hides it, or a request the guard refused.
pub fn has_regression(&self) -> bool {
self.invalid > 0
|| self.overall.verdict == Verdict::Worse
|| self.scenarios.iter().any(|(_, c)| c.verdict == Verdict::Worse)
}
}
/// The mean of independent scenarios, each counting the same; their variances add.
pub fn overall(comparisons: &[Comparison]) -> Comparison {
let k = comparisons.len().max(1) as f64;
let delta = comparisons.iter().map(|c| c.delta_bb_per_100).sum::<f64>() / k;
let ci95 = comparisons.iter().map(|c| c.ci95.powi(2)).sum::<f64>().sqrt() / k;
let identical = comparisons.iter().all(|c| c.verdict == Verdict::Identical);
Comparison {
delta_bb_per_100: delta,
ci95,
changed_deals: comparisons.iter().map(|c| c.changed_deals).sum(),
verdict: if identical { Verdict::Identical } else { Verdict::of(delta, ci95) },
}
}
/// Compares two runs. Runs under other conditions say nothing about the engine: refused.
pub fn verdict_of_run(base: &Run, new: &Run) -> Result<Report, String> {
if base.scenarios.len() != new.scenarios.len() {
return Err(format!("{} gegen {} Szenarien: Basislinie neu messen", base.scenarios.len(), new.scenarios.len()));
}
let mut scenarios = Vec::with_capacity(base.scenarios.len());
for (b, n) in base.scenarios.iter().zip(&new.scenarios) {
if b.name != n.name || b.deals != n.deals || b.seed != n.seed {
return Err(format!("andere Bedingungen: „{}“ gegen „{}“: Basislinie neu messen", b.name, n.name));
}
scenarios.push((b.name.clone(), compare(&b.samples, &n.samples)?));
}
let all: Vec<Comparison> = scenarios.iter().map(|(_, c)| *c).collect();
Ok(Report { overall: overall(&all), invalid: new.scenarios.iter().map(|s| s.invalid).sum(), scenarios })
}
/// The run as text. Floats are written so that they read back bit for bit.
pub fn render(run: &Run) -> String {
let mut text = format!("label {}\n", run.label);
for s in &run.scenarios {
let samples: Vec<String> = s.samples.iter().map(f64::to_string).collect();
text += &format!(
"scenario {}\ndeals {}\nseed {}\ncounts {} {} {}\nsamples {}\n",
s.name, s.deals, s.seed, s.decisions, s.abstains, s.invalid, samples.join(" ")
);
}
text
}
fn field<'a>(lines: &mut std::str::Lines<'a>, key: &str) -> Result<&'a str, String> {
let line = lines.next().ok_or_else(|| format!("„{key}“ fehlt"))?;
line.strip_prefix(key)
.and_then(|rest| rest.strip_prefix(' '))
.ok_or_else(|| format!("„{key}“ erwartet, „{line}“ gelesen"))
}
fn number<T: std::str::FromStr>(text: &str) -> Result<T, String> {
text.parse().map_err(|_| format!("keine Zahl: „{text}“"))
}
pub fn parse(text: &str) -> Result<Run, String> {
let mut lines = text.lines();
let label = field(&mut lines, "label")?.to_string();
let mut scenarios = Vec::new();
while lines.clone().next().is_some() {
let name = field(&mut lines, "scenario")?.to_string();
let deals: u32 = number(field(&mut lines, "deals")?)?;
let seed: u64 = number(field(&mut lines, "seed")?)?;
let counts = field(&mut lines, "counts")?.split(' ').map(number).collect::<Result<Vec<u32>, _>>()?;
let [decisions, abstains, invalid] = counts[..] else {
return Err(format!("„counts“ von „{name}“ hat {} Werte", counts.len()));
};
let samples = field(&mut lines, "samples")?.split(' ').map(number).collect::<Result<Vec<f64>, _>>()?;
if samples.len() != deals as usize {
return Err(format!("„{name}“: {deals} Austeilungen, {} Werte", samples.len()));
}
scenarios.push(ScenarioRun { name, deals, seed, decisions, abstains, invalid, samples });
}
Ok(Run { label, scenarios })
}