//! Messschalter `VORFLOP_BAND=`: die Vorflop-Range eines Gegners im Topf Small Blind gegen Big Blind //! (alle anderen gefoldet) kommt aus gemessenen Frequenzen echter Spieler (`messung/familie_a/vorflop.py`, PHH NL25/50 //! Blind gegen Blind), nicht aus der Chart. //! //! Befund 17.11.: die Chart für den Small Blind ist spieltheoretisch breit (Open Top 62 % an sechs Plätzen, heads-up //! mal 1,5: alles; Big-Blind-Verteidigung 93 %), echte Spieler raisen dort 24 % und limpen 26 %, der Big Blind //! verteidigt 34 %. Die Lesung streckt die Chart dann noch relativ zur Sechser-Norm. Die Klassenböden der //! Basis schneiden den zu schwachen Prior weg; die Bayes-Lesung `BETTOR` reicht ihn durch und überschätzt die //! eigene Equity (Sim gegen tabellentreue Gegner: River nach Check-Einsatz 0,27 statt 0,13). //! //! Bänder (Perzentil, 0 = AA) monoton wie gemessen: Raise oben, Call darunter; Anteile der Range, die den Knoten //! erreicht. Mit vpip-Read gilt die Tabelle der vpip-Stufe des Gegners (`nach_vpip`: eine Station verteidigt das BB //! fast immer, ein Nit kaum), je Knoten ab `MIN_N` Fällen, sonst der Pool-Knoten; ohne Stufe streckt der Read das //! Pool-Band (`range_model`). Ungesetzt: aus, bitgleich. use super::preflop_role::PreflopRole; use crate::game::{SeenAction, Spot}; /// Fold, call, raise share and raise height (bb) of one node. #[derive(Clone, Copy, Debug, PartialEq)] pub struct Node { pub f: f64, pub c: f64, pub r: f64, pub hoehe: f64, } /// The six heads-up preflop nodes (see `vorflop.py`). #[derive(Clone, Copy, Debug, PartialEq)] pub struct Table { pub sb: Node, pub bb_limp: Node, pub sb_limp_r: Node, pub bb_open: Node, pub sb_3b: Node, pub bb_4b: Node, } /// Fewest cases for a node of a vpip tier to replace the pool node. pub const MIN_N: f64 = 300.0; /// The pool table and the tables per vpip tier `[lo, hi)`. #[derive(Clone, Debug, PartialEq)] pub struct Tables { pub pool: Table, pub stufen: Vec<(f64, f64, Table)>, } type Obj = Vec<(String, crate::json::value::Value)>; fn feld<'a>(o: &'a Obj, k: &str) -> Option<&'a crate::json::value::Value> { o.iter().find(|(n, _)| n == k).map(|(_, v)| v) } /// A table from a `knoten` object; with a `fallback`, a node missing or under `MIN_N` cases comes from it. fn table_of(knoten: &crate::json::value::Value, fallback: Option<&Table>) -> Result { use crate::json::value::Value; let Value::Object(knoten) = knoten else { return Err("vorflop: knoten ist kein Objekt".into()) }; let node = |name: &str, alt: Option| -> Result { let Some(Value::Object(f)) = feld(knoten, name) else { return alt.ok_or_else(|| format!("vorflop: Knoten {name} fehlt")); }; if let (Some(a), Some(Value::Number(n))) = (alt, feld(f, "n")) { if *n < MIN_N { return Ok(a); } } let zahl = |k: &str| match feld(f, k) { Some(Value::Number(x)) if x.is_finite() && *x >= 0.0 => Ok(*x), _ => Err(format!("vorflop: {name}.{k} fehlt")), }; Ok(Node { f: zahl("f")?, c: zahl("c")?, r: zahl("r")?, hoehe: zahl("hoehe_bb")? }) }; let fb = |pick: fn(&Table) -> Node| fallback.map(pick); Ok(Table { sb: node("sb", fb(|t| t.sb))?, bb_limp: node("bb_limp", fb(|t| t.bb_limp))?, sb_limp_r: node("sb_limp_r", fb(|t| t.sb_limp_r))?, bb_open: node("bb_open", fb(|t| t.bb_open))?, sb_3b: node("sb_3b", fb(|t| t.sb_3b))?, bb_4b: node("bb_4b", fb(|t| t.bb_4b))?, }) } /// The pool table (`knoten`) of a `vorflop.py` output. pub fn parse(text: &str) -> Result { Ok(parse_all(text)?.pool) } /// Pool table and vpip tiers (`nach_vpip`, optional) of a `vorflop.py` output. pub fn parse_all(text: &str) -> Result { use crate::json::value::Value; let v = crate::json::parse::parse(text).map_err(|e| format!("{e:?}"))?; let Value::Object(top) = v else { return Err("vorflop: object expected".into()) }; let pool = table_of(feld(&top, "knoten").ok_or("vorflop: knoten fehlt")?, None)?; let mut stufen = Vec::new(); if let Some(Value::Array(xs)) = feld(&top, "nach_vpip") { for x in xs { let Value::Object(o) = x else { continue }; let Some(Value::Array(b)) = feld(o, "vpip") else { continue }; let (Some(Value::Number(lo)), Some(Value::Number(hi))) = (b.first(), b.get(1)) else { continue }; let Some(k) = feld(o, "knoten") else { continue }; stufen.push((*lo, *hi, table_of(k, Some(&pool))?)); } } Ok(Tables { pool, stufen }) } fn tables() -> Option<&'static Tables> { use std::sync::OnceLock; static T: OnceLock> = OnceLock::new(); T.get_or_init(|| { let path = std::env::var("VORFLOP_BAND").ok().filter(|p| !p.is_empty())?; let text = std::fs::read_to_string(&path).ok()?; parse_all(&text).ok() }) .as_ref() } /// Is `VORFLOP_BAND` on (a readable table)? pub fn enabled() -> bool { tables().is_some() } /// The measured band of `seat` in role `role`, when the pot is small blind against big blind (every other seat /// folded or never acted preflop); `None` otherwise or with the switch off. The flag says the band comes from the /// villain's vpip tier (the read is in it already, no further stretch). pub fn band(spot: &Spot, seat: u8, role: PreflopRole, vpip: Option) -> Option<((f64, f64), bool)> { let t = tables()?; let stufe = vpip.filter(|v| v.is_finite()).and_then(|v| t.stufen.iter().find(|(lo, hi, _)| *lo <= v && v < *hi)); match stufe { Some((_, _, tab)) => band_in(tab, spot, seat, role).map(|b| (b, true)), None => band_in(&t.pool, spot, seat, role).map(|b| (b, false)), } } fn band_in(t: &Table, spot: &Spot, seat: u8, role: PreflopRole) -> Option<(f64, f64)> { let pre: Vec<_> = spot.history.iter().filter(|r| r.street == crate::game::Street::Preflop).collect(); let sb = pre.iter().find(|r| r.action == SeenAction::PostSmallBlind)?.seat; let bb = pre.iter().find(|r| r.action == SeenAction::PostBigBlind)?.seat; let posts = |a: SeenAction| matches!(a, SeenAction::PostSmallBlind | SeenAction::PostBigBlind | SeenAction::PostAnte); if pre.iter().any(|r| r.seat != sb && r.seat != bb && !posts(r.action) && r.action != SeenAction::Fold) { return None; } let sb_first = pre.iter().find(|r| r.seat == sb && !posts(r.action))?.action; let sb_open = sb_first.is_aggressive(); let (o, l) = (t.sb.r, t.sb.c); Some(match (seat == sb, seat == bb, role) { (true, _, PreflopRole::Opener) => (0.0, o), (true, _, PreflopRole::SmallBlindCompleter) => (o, o + l), (true, _, PreflopRole::ThreeBettor) if !sb_open => (o, o + l * t.sb_limp_r.r), (true, _, PreflopRole::CallsRaise) if !sb_open => (o + l * t.sb_limp_r.r, o + l * (t.sb_limp_r.r + t.sb_limp_r.c)), (true, _, PreflopRole::FourBettorPlus) if sb_open => (0.0, o * t.sb_3b.r), (true, _, PreflopRole::CallsThreeBet) if sb_open => (o * t.sb_3b.r, o * (t.sb_3b.r + t.sb_3b.c)), (_, true, PreflopRole::BigBlindCheck) => (t.bb_limp.r, 1.0), (_, true, PreflopRole::Opener) if !sb_open => (0.0, t.bb_limp.r), (_, true, PreflopRole::ThreeBettor) if sb_open => (0.0, t.bb_open.r), (_, true, PreflopRole::CallsRaise) if sb_open => (t.bb_open.r, t.bb_open.r + t.bb_open.c), (_, true, PreflopRole::FourBettorPlus) if sb_open => (0.0, t.bb_open.r * t.bb_4b.r), (_, true, PreflopRole::CallsThreeBet) if sb_open => (t.bb_open.r * t.bb_4b.r, t.bb_open.r * (t.bb_4b.r + t.bb_4b.c)), _ => return None, }) } #[cfg(test)] mod tests { use super::*; const TEXT: &str = r#"{"knoten": { "sb": {"n": 9000, "f": 0.5, "c": 0.26, "r": 0.24, "hoehe_bb": 3.7}, "bb_limp": {"n": 9000, "f": 0.0, "c": 0.82, "r": 0.18, "hoehe_bb": 4.5}, "sb_limp_r": {"n": 9000, "f": 0.45, "c": 0.52, "r": 0.03, "hoehe_bb": 17.6}, "bb_open": {"n": 9000, "f": 0.66, "c": 0.27, "r": 0.07, "hoehe_bb": 13.3}, "sb_3b": {"n": 9000, "f": 0.45, "c": 0.43, "r": 0.12, "hoehe_bb": 35.8}, "bb_4b": {"n": 9000, "f": 0.29, "c": 0.53, "r": 0.18, "hoehe_bb": 82.7}}, "nach_vpip": [{"vpip": [0.4, 0.55], "knoten": { "bb_open": {"n": 800, "f": 0.2, "c": 0.7, "r": 0.1, "hoehe_bb": 12.0}, "sb_3b": {"n": 20, "f": 0.9, "c": 0.1, "r": 0.0, "hoehe_bb": 30.0}}}]}"#; #[test] fn tier_nodes_replace_pool_only_when_counted() { let t = parse_all(TEXT).unwrap(); assert_eq!(t.stufen.len(), 1); let (lo, hi, tier) = &t.stufen[0]; assert_eq!((*lo, *hi), (0.4, 0.55)); assert_eq!(tier.bb_open.f, 0.2); // too few cases and missing nodes: the pool node assert_eq!(tier.sb_3b, t.pool.sb_3b); assert_eq!(tier.sb, t.pool.sb); assert_eq!(parse(TEXT).unwrap(), t.pool); } }