diff --git a/src/bin/diag_welt.rs b/src/bin/diag_welt.rs index 60108c37..52b80a74 100644 --- a/src/bin/diag_welt.rs +++ b/src/bin/diag_welt.rs @@ -13,7 +13,7 @@ use rechner::rng::Rng; use rechner::sim::memory::SessionMemory; use rechner::sim::opponent::Opponent; use rechner::sim::reader::{Rules, Style}; -use rechner::sim::table::{deal, play_hand_tracked_history, set_hero_gto, set_hero_why, take_hero_why, HandRead, HandSetup, TableCost}; +use rechner::sim::table::{deal, play_hand_tracked_history, set_hero_gto, set_hero_read_diff, set_hero_why, take_hero_read_diff, take_hero_why, HandRead, HandSetup, TableCost}; use rechner::sim::welt::{Persona, ARCHETYPES}; use std::collections::BTreeMap; @@ -23,6 +23,9 @@ const MICRO_DRAW_SEED: u64 = 0x31C0_D2A9_0000; const MICRO_SEED: u64 = 0x31C0_5EED; const STACK_BB: f64 = 100.0; const KLASSIK_SEED: u64 = 0x5EED_0001; +// The pool rows of `ab_zelle` (`pool-`, persona from `POOL_PERSONA`). +const POOL_DRAW_SEED: u64 = 0x9001_D2A9_0000; +const POOL_SEED: u64 = 0x9001_5EED; // The nemesis rules of `ab_zelle` (CMA-ES). const N1: Rules = Rules { call_margin: 0.215, raise_at: 0.587, bet_at: 0.548, open_at: 0.729, bluff: 0.068, size: 0.441 }; const N2: Rules = Rules { call_margin: 0.173, raise_at: 0.579, bet_at: 0.748, open_at: 0.800, bluff: 0.229, size: 0.996 }; @@ -118,6 +121,85 @@ fn arm(opp_of: &dyn Fn(u64) -> (Opponent, u64), deals: u32, draws: u64, bot: boo (by, total) } +/// `DIAG_READ=1`: where does the read move the engine's decision, and where does it fizzle? Every hand +/// is played twice on the same deal and seeds, with the Memory read and blind (`HandRead::Given(None)`); +/// the paths are identical up to the first decision where the two engines differ, so the hand's +/// difference (read − blind, luck-corrected) belongs to that node. Besides, every decision of the read +/// arm is also taken blind on the same spot (`set_hero_read_diff`): per node how often the read changes +/// the action or only the size. +fn read_arm(opp_of: &dyn Fn(u64) -> (Opponent, u64), deals: u32, draws: u64) { + let mut diff: BTreeMap = BTreeMap::new(); + let mut moved: BTreeMap = BTreeMap::new(); + let (mut hands, mut same, mut total) = (0u32, 0u32, 0.0); + for k in 0..draws { + let (opponent, seed) = opp_of(k); + let mut memory = SessionMemory::new(); + for d in 0..deals { + let (cards, _) = deal(2, Rng::seeded(mix(seed, d as u64))); + for hero_seat in 1..=2u8 { + let setup = |read| HandSetup { + deal: &cards, + hero_seat, + opponent, + stack_bb: STACK_BB, + seed: mix(seed ^ 0xA5A5, (d as u64) << 8 | hero_seat as u64), + read, + }; + set_hero_why(true); + set_hero_read_diff(true); + let (r, _) = play_hand_tracked_history(&setup(HandRead::Given(memory.read())), TableCost::FREE, Some(&mut memory)); + let why_r = take_hero_why(); + for (node, x) in take_hero_read_diff() { + let e = diff.entry(node).or_default(); + (0..3).for_each(|i| e[i] += x[i]); + } + set_hero_read_diff(false); + let (b, _) = play_hand_tracked_history(&setup(HandRead::Given(None)), TableCost::FREE, None); + let why_b = take_hero_why(); + let delta = (r.hero_net - r.luck) - (b.hero_net - b.luck); + hands += 1; + total += delta; + // First differing decision: node and choice (with size), not the equity or the reason. + let cut = |w: &String| { + let node = w.split(" pot ").next().unwrap_or(""); + let choice = w.split("-> ").nth(1).unwrap_or("").split(" | ").next().unwrap_or(""); + format!("{node} {choice}") + }; + match why_r.iter().zip(&why_b).find(|(x, y)| cut(x) != cut(y)) { + Some((x, y)) => { + let node = x.split(" pot ").next().unwrap_or("?").to_string(); + let act = |w: &String| w.split("-> ").nth(1).unwrap_or("").split([' ', '|']).next().unwrap_or("").to_string(); + let (a, b) = (act(y), act(x)); + let label = if a == b { format!("{node}: {a}, andere Groesse") } else { format!("{node}: blind {a} -> Read {b}") }; + let e = moved.entry(label).or_default(); + e.n += 1; + e.net += delta; + } + None => { + same += 1; + if why_r.len() != why_b.len() { + moved.entry("(Laenge verschieden)".into()).or_default().n += 1; + } + } + } + } + } + } + set_hero_why(false); + let per100 = |x: f64| 100.0 * x / hands.max(1) as f64; + println!("READ-WIRKUNG: {hands} Haende, Read minus blind gesamt {:+.2} bb/100; Pfad gleich in {:.1} % der Haende", per100(total), 100.0 * same as f64 / hands.max(1) as f64); + println!("-- je Knoten: Entscheidungen, Read aendert die Aktion, nur die Groesse --"); + for (node, x) in &diff { + println!(" {:<22} {:>7} Aktion {:>5.1} % Groesse {:>5.1} %", node, x[0], 100.0 * x[1] as f64 / x[0].max(1) as f64, 100.0 * x[2] as f64 / x[0].max(1) as f64); + } + println!("-- erste Abweichung: wo der Read Geld bewegt (Beitrag bb/100 Read minus blind) --"); + let mut v: Vec<_> = moved.iter().collect(); + v.sort_by(|a, b| b.1.net.abs().total_cmp(&a.1.net.abs())); + for (k, g) in v.iter().take(40) { + println!(" {:<52} {:>6} Haende {:>+8.2} ({:+.2} bb/Hand)", k, g.n, per100(g.net), g.net / g.n.max(1) as f64); + } +} + fn main() { let a: Vec = std::env::args().collect(); let name = a.get(1).expect("diag_welt [deals] [draws] [top]").clone(); @@ -127,6 +209,11 @@ fn main() { Box::new(|k| { (Opponent::Welt(Persona::draw_micro(Rng::seeded(MICRO_DRAW_SEED + k)).0), MICRO_SEED ^ (k + 1).wrapping_mul(0x9E37_79B9)) }) + } else if name.starts_with("pool-") { + let pfad = std::env::var("POOL_PERSONA").unwrap_or_else(|_| panic!("Zeile {name} braucht POOL_PERSONA")); + let text = std::fs::read_to_string(&pfad).unwrap_or_else(|e| panic!("POOL_PERSONA {pfad}: {e}")); + let m = Persona::from_pool_json(&text).unwrap_or_else(|e| panic!("{e}")); + Box::new(move |k| (Opponent::Welt(Persona::draw_pool(m, Rng::seeded(POOL_DRAW_SEED + k)).0), POOL_SEED ^ (k + 1).wrapping_mul(0x9E37_79B9))) } else if let Some(opp) = match name.as_str() { "station" => Some(Opponent::CallingStation), "fish" => Some(Opponent::Reader(Style::Fish)), @@ -145,6 +232,10 @@ fn main() { (Opponent::Welt(Persona::draw(i, Rng::seeded(DRAW_SEED + 1000 * i as u64 + k)).0), WELT_SEED ^ (k + 1).wrapping_mul(0x9E37_79B9)) }) }; + if std::env::var("DIAG_READ").as_deref() == Ok("1") { + read_arm(&*opp_of, deals, draws); + return; + } let (eng, te) = arm(&*opp_of, deals, draws, false); let (bot, tb) = arm(&*opp_of, deals, draws, true); let per100 = |x: f64, n: u32| 100.0 * x / n.max(1) as f64; // per 100 hands, as ab_zelle (deal mean over both seats) diff --git a/src/sim/table.rs b/src/sim/table.rs index 6fe13eb0..20aacb3d 100644 --- a/src/sim/table.rs +++ b/src/sim/table.rs @@ -49,6 +49,34 @@ pub struct Deal { thread_local! { static HERO_GTO: std::cell::Cell = const { std::cell::Cell::new(false) }; static HERO_WHY: std::cell::RefCell>> = const { std::cell::RefCell::new(None) }; + static HERO_READ_DIFF: std::cell::RefCell>> = const { std::cell::RefCell::new(None) }; +} + +/// Diagnostics only (`diag_welt DIAG_READ=1`): while on, every engine decision of hero is also taken +/// blind (villains without profile, same spot and seed) and counted per node (`node_tag`): +/// `[decisions, other action, same action other size]`. Off (default): nothing is computed. +pub fn set_hero_read_diff(on: bool) { + HERO_READ_DIFF.with(|c| *c.borrow_mut() = on.then(std::collections::BTreeMap::new)); +} + +pub fn take_hero_read_diff() -> std::collections::BTreeMap { + HERO_READ_DIFF.with(|c| c.borrow_mut().as_mut().map(std::mem::take).unwrap_or_default()) +} + +/// The node of a decision: street and what hero faces there (`frei` nothing to call, `vs-bet` one +/// aggression on the street, `vs-raise` two or more); preflop `vs-bet` is the blinds only. +pub fn node_tag(spot: &Spot) -> String { + let aggr = spot.history.iter().filter(|r| r.street == spot.street && r.action.is_aggressive()).count(); + let facing = if spot.to_call <= 0.0 { + "frei" + } else if spot.street == Street::Preflop { + ["vs-blind", "vs-open", "vs-3bet"][aggr.min(2)] + } else if aggr <= 1 { + "vs-bet" + } else { + "vs-raise" + }; + format!("{:?} {facing}", spot.street) } /// Diagnostics only (`diag_welt`): while on, every engine decision of hero appends @@ -279,11 +307,25 @@ impl Table<'_> { let choice = match check(self.spot(view)) { Ok(spot) => { let decision = decide_with(&spot, self.lage); + HERO_READ_DIFF.with(|c| { + if let Some(m) = c.borrow_mut().as_mut() { + let mut blind = spot.clone(); + blind.villains.iter_mut().for_each(|v| v.profile = None); + let b = decide_with(&blind, self.lage).choice; + let e = m.entry(node_tag(&spot)).or_default(); + e[0] += 1; + if std::mem::discriminant(&b) != std::mem::discriminant(&decision.choice) { + e[1] += 1; + } else if b != decision.choice { + e[2] += 1; + } + } + }); HERO_WHY.with(|c| { if let Some(v) = c.borrow_mut().as_mut() { v.push(format!( - "{:?} pot {:.1} call {:.1} eq {} -> {:?} | {}", - spot.street, + "{} pot {:.1} call {:.1} eq {} -> {:?} | {}", + node_tag(&spot), spot.pot, spot.to_call, decision.equity.map(|e| format!("{e:.2}")).unwrap_or("-".into()),