From 5e2d04d4f4e19ece698b8411662d61ebe4a0332e Mon Sep 17 00:00:00 2001 From: Marc Mintel Date: Mon, 16 Nov 2026 12:36:29 +0100 Subject: [PATCH] POSTERIOR S2: Prior je Linie (GEGNER_LINIEN, Strasse/Topf/Barrels/Groesse/Art/PF-Aggressor, Rueckfall bis MIN_N 30); ohne Tabelle wie b91875a1. Live-Hand 178593300919 mit Probetabelle: Fold Co-Authored-By: Claude Opus 5.5 --- src/engine/glaube.rs | 33 ++-- src/engine/glaube_linie.rs | 250 +++++++++++++++++++++++++++++ src/engine/mod.rs | 1 + src/exploit/street_continuation.rs | 3 +- 4 files changed, 277 insertions(+), 10 deletions(-) create mode 100644 src/engine/glaube_linie.rs diff --git a/src/engine/glaube.rs b/src/engine/glaube.rs index 18df4069..1f96c386 100644 --- a/src/engine/glaube.rs +++ b/src/engine/glaube.rs @@ -24,6 +24,7 @@ //! street level already carries it. Board texture is what the formula misses. use crate::card::{deck, Card}; +use super::glaube_linie::{self, Line}; use crate::exploit::bluff_share::MAX_BLUFF_SHARE; use crate::exploit::call_floor::CALL_RUNG; use crate::exploit::profile::Profile; @@ -248,19 +249,33 @@ fn class_fold(f: f64, m_board: f64, m_ref: f64) -> f64 { } } -/// S2: the bluff share of a villain's bet as a Beta posterior. The prior mean is the share the -/// heuristic gives (`exploit::bluff_share` with the population prior), its weight `STREET_PRIOR` -/// bets, the same as the solver's read (`exploit::range_read::RangeRead::bluff_share`). The evidence -/// is the villain's shown river bets (`Profile::river_bluff_share` over `river_bluff_shown`), so only -/// the river moves; flop and turn keep the prior until showdowns are counted per street. Off: `prior`. -pub fn bluff(profile: Option<&Profile>, street: Street, prior: f64) -> f64 { - if !mode().is_some_and(|m| m.bluff) || street != Street::River { +/// S2: the bluff share of a villain's bet as a Beta posterior. +/// +/// * Prior of an UNREAD villain (no profile or the pool baseline): the population's air share on +/// this betting `line` (`engine::glaube_linie`, real showdowns per street, pot, barrels, size, +/// kind), `(air + 0,5)/(n + 1)`. Without a line table: on the river the pool mean `POOL_RIVER_AIR` +/// (never above the heuristic), on flop and turn the heuristic. +/// * Prior of a READ villain: the heuristic from his aggression (`exploit::bluff_share`). +/// * Evidence: his shown river bets (`Profile::river_bluff_share` over `river_bluff_shown`), weight +/// of the prior `STREET_PRIOR` bets as in the solver's read; flop and turn have no per-street +/// showdown count yet. +/// +/// Off: `prior` unchanged. +pub fn bluff(profile: Option<&Profile>, street: Street, line: Option<&Line>, prior: f64) -> f64 { + if !mode().is_some_and(|m| m.bluff) || street == Street::Preflop { return prior; } let ok = |x: Option| x.filter(|v| v.is_finite()); - // Hierarchy: an unread villain (or the pool baseline) starts at the pool, never above the heuristic. let unread = profile.is_none_or(|p| p.pool_baseline); - let prior = if unread { prior.min(POOL_RIVER_AIR) } else { prior }; + let pool = line.and_then(glaube_linie::lookup).map(|(air, n)| (air + 0.5) / (n + 1.0)); + let prior = match (unread, pool) { + (true, Some(m)) => m.min(MAX_BLUFF_SHARE), + (true, None) if street == Street::River => prior.min(POOL_RIVER_AIR), + _ => prior, + }; + if street != Street::River { + return prior; + } let Some(p) = profile else { return prior }; match (ok(p.river_bluff_share), ok(p.river_bluff_shown)) { (Some(share), Some(n)) if n > 0.0 => beta_mean(prior, STREET_PRIOR, share.clamp(0.0, 1.0), n).min(MAX_BLUFF_SHARE), diff --git a/src/engine/glaube_linie.rs b/src/engine/glaube_linie.rs new file mode 100644 index 00000000..7557dee7 --- /dev/null +++ b/src/engine/glaube_linie.rs @@ -0,0 +1,250 @@ +//! Bluff share per betting LINE for `POSTERIOR=bluff` (S2): how much air the population shows down +//! behind a bet of this kind, counted from real showdowns (gedaechtnis-voll, PHH/HandHQ). Format and +//! counting rules: side note `BLUFF-LINIEN-format-exploit-live.md`; the file comes from `GEGNER_LINIEN`. +//! +//! A line is street, pot type (highest preflop raise level), barrels (consecutive aggressive streets +//! of this player up to this one), size step (`lesen/linien.py stufe`), kind (bet / raise / all-in) +//! and whether the player was the last preflop raiser. The lookup sums the counts of every row that +//! matches and drops the finer fields one by one until `MIN_N` cases back the share. + +use crate::game::{ActionRecord, SeenAction, Spot, Street}; +use crate::json::Value; +use std::sync::OnceLock; + +/// Cases a share needs before it is used (else the next coarser line). +pub const MIN_N: f64 = 30.0; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Pot { + Limp, + Single, + ThreeBet, + FourBet, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Kind { + Bet, + Raise, + AllIn, +} + +/// One betting line (see the module text). `size`: 'k' | 'm' | 'g' | 'u'. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Line { + pub street: Street, + pub pot: Pot, + pub barrels: u8, + pub size: char, + pub kind: Kind, + pub pf_aggressor: bool, +} + +/// One row of the table: a line with optional fields (missing = any) and its counts. +#[derive(Clone, Debug, PartialEq)] +struct Row { + street: Street, + pot: Option, + barrels: Option, + size: Option, + kind: Option, + pf_aggressor: Option, + air: f64, + n: f64, +} + +/// Size step of a bet `ratio` pots, as `lesen/linien.py stufe`. +pub fn step(ratio: f64) -> char { + if ratio <= 0.40 { + 'k' + } else if ratio <= 0.80 { + 'm' + } else if ratio <= 1.20 { + 'g' + } else { + 'u' + } +} + +/// The line of `seat`'s aggression on `street` in `spot` (its pressure `ratio`, `raised` when it +/// raised a bet). `None` without a known size. +pub fn line_of(spot: &Spot, seat: u8, street: Street, ratio: Option, raised: bool) -> Option { + let ratio = ratio.filter(|r| r.is_finite() && *r > 0.0)?; + let h: &[ActionRecord] = &spot.history; + let pre: Vec<&ActionRecord> = h.iter().filter(|r| r.street == Street::Preflop && r.action.is_aggressive()).collect(); + let pot = match pre.len() { + 0 => Pot::Limp, + 1 => Pot::Single, + 2 => Pot::ThreeBet, + _ => Pot::FourBet, + }; + let pf_aggressor = pre.last().is_some_and(|r| r.seat == seat); + let aggressive_on = |s: Street| h.iter().any(|r| r.street == s && r.seat == seat && r.action.is_aggressive()); + let mut barrels = 0u8; + for s in [Street::River, Street::Turn, Street::Flop] { + if (s as usize) > (street as usize) { + continue; + } + if !aggressive_on(s) { + break; + } + barrels += 1; + } + let all_in = h.iter().any(|r| r.street == street && r.seat == seat && r.action == SeenAction::AllIn); + let kind = if all_in { Kind::AllIn } else if raised { Kind::Raise } else { Kind::Bet }; + Some(Line { street, pot, barrels: barrels.max(1), size: step(ratio), kind, pf_aggressor }) +} + +fn text<'a>(v: &'a Value, key: &str) -> Option<&'a str> { + match v { + Value::Object(m) => m.iter().find(|(k, _)| k == key).and_then(|(_, x)| match x { + Value::String(s) => Some(s.as_str()), + _ => None, + }), + _ => None, + } +} + +fn number(v: &Value, key: &str) -> Option { + match v { + Value::Object(m) => m.iter().find(|(k, _)| k == key).and_then(|(_, x)| match x { + Value::Number(n) if n.is_finite() => Some(*n), + _ => None, + }), + _ => None, + } +} + +fn flag(v: &Value, key: &str) -> Option { + match v { + Value::Object(m) => m.iter().find(|(k, _)| k == key).and_then(|(_, x)| match x { + Value::Bool(b) => Some(*b), + _ => None, + }), + _ => None, + } +} + +fn row(v: &Value) -> Option { + let street = match text(v, "strasse")? { + "flop" => Street::Flop, + "turn" => Street::Turn, + "river" => Street::River, + _ => return None, + }; + let pot = match text(v, "topf") { + Some("limp") => Some(Pot::Limp), + Some("srp") => Some(Pot::Single), + Some("3bet") => Some(Pot::ThreeBet), + Some("4bet") => Some(Pot::FourBet), + _ => None, + }; + let kind = match text(v, "art") { + Some("bet") => Some(Kind::Bet), + Some("raise") => Some(Kind::Raise), + Some("allin") => Some(Kind::AllIn), + _ => None, + }; + let size = text(v, "groesse").and_then(|s| s.chars().next()).filter(|c| "kmgu".contains(*c)); + let (air, n) = (number(v, "luft")?, number(v, "n")?); + (n > 0.0 && (0.0..=n).contains(&air)).then_some(Row { + street, + pot, + barrels: number(v, "barrels").map(|b| b.clamp(1.0, 3.0) as u8), + size, + kind, + pf_aggressor: flag(v, "pf_aggressor"), + air, + n, + }) +} + +/// Rows of a table file's text. Unreadable rows are skipped. +fn parse_table(text: &str) -> Vec { + let Ok(root) = crate::json::parse(text) else { return Vec::new() }; + let rows = match &root { + Value::Object(m) => m.iter().find(|(k, _)| k == "zeilen").map(|(_, v)| v), + _ => None, + }; + match rows { + Some(Value::Array(a)) => a.iter().filter_map(row).collect(), + _ => Vec::new(), + } +} + +fn table() -> &'static [Row] { + static T: OnceLock> = OnceLock::new(); + T.get_or_init(|| { + std::env::var("GEGNER_LINIEN").ok().and_then(|p| std::fs::read_to_string(p).ok()).map_or_else(Vec::new, |t| parse_table(&t)) + }) +} + +/// Fallback levels: which fields of the line must match (size, pot, preflop aggressor, kind). +const LEVELS: [(bool, bool, bool, bool); 5] = [ + (true, true, true, true), + (false, true, true, true), + (false, false, true, true), + (false, false, false, true), + (false, false, false, false), +]; + +fn lookup_in(rows: &[Row], line: &Line) -> Option<(f64, f64)> { + for (size, pot, pf, kind) in LEVELS { + let (mut air, mut n) = (0.0, 0.0); + for r in rows { + let ok = r.street == line.street + && r.barrels.is_none_or(|b| b == line.barrels.min(3)) + && (!size || r.size.is_none_or(|s| s == line.size)) + && (!pot || r.pot.is_none_or(|p| p == line.pot)) + && (!pf || r.pf_aggressor.is_none_or(|a| a == line.pf_aggressor)) + && (!kind || r.kind.is_none_or(|k| k == line.kind)); + if ok { + air += r.air; + n += r.n; + } + } + if n >= MIN_N { + return Some((air, n)); + } + } + None +} + +/// Population (air, cases) behind `line`, from the `GEGNER_LINIEN` table. `None` without a table or +/// without `MIN_N` cases even on street and barrels alone. +pub fn lookup(line: &Line) -> Option<(f64, f64)> { + lookup_in(table(), line) +} + +#[cfg(test)] +mod tests { + use super::*; + + const T: &str = r#"{"zeilen": [ + {"strasse": "river", "topf": "3bet", "barrels": 3, "groesse": "m", "art": "allin", "pf_aggressor": true, "luft": 2, "n": 40}, + {"strasse": "river", "topf": "srp", "barrels": 3, "groesse": "m", "art": "bet", "pf_aggressor": true, "luft": 9, "n": 20}, + {"strasse": "river", "topf": "srp", "barrels": 1, "groesse": "k", "art": "bet", "pf_aggressor": false, "luft": 30, "n": 100}, + {"strasse": "turn", "luft": "x", "n": 5} + ]}"#; + + fn line(pot: Pot, barrels: u8, size: char, kind: Kind) -> Line { + Line { street: Street::River, pot, barrels, size, kind, pf_aggressor: true } + } + + #[test] + fn exact_lines_and_fallbacks() { + let rows = parse_table(T); + assert_eq!(rows.len(), 3, "the broken turn row is skipped"); + assert_eq!(lookup_in(&rows, &line(Pot::ThreeBet, 3, 'm', Kind::AllIn)), Some((2.0, 40.0))); + // 20 cases on the exact line are too few; dropping the size, pot and aggressor still + // separates the kind (bet): 20 + 100 = 120 cases do not match barrels 3 → only 20; at the + // last level (street + barrels) 2 + 9 = 11 air of 60. + assert_eq!(lookup_in(&rows, &line(Pot::Single, 3, 'm', Kind::Bet)), Some((11.0, 60.0))); + assert_eq!(lookup_in(&rows, &line(Pot::Single, 2, 'm', Kind::Bet)), None); + } + + #[test] + fn steps_match_the_python_counter() { + assert_eq!([0.4, 0.41, 0.8, 1.2, 1.3].map(step), ['k', 'm', 'm', 'g', 'u']); + } +} diff --git a/src/engine/mod.rs b/src/engine/mod.rs index 1459e051..9e720791 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -13,6 +13,7 @@ pub mod flop_spieler; pub mod fortsetzung; pub mod frist; pub mod glaube; +pub mod glaube_linie; pub mod from_plan; pub mod multiway; pub mod omaha_combos; diff --git a/src/exploit/street_continuation.rs b/src/exploit/street_continuation.rs index 366a313a..33f86391 100644 --- a/src/exploit/street_continuation.rs +++ b/src/exploit/street_continuation.rs @@ -260,7 +260,8 @@ fn on_street( .then(turn_raise_bluff) .flatten(); let bluff = with_pop_prior_on(villain.profile.as_ref(), street, sustained, lvl == 1, bluff); - let bluff = crate::engine::glaube::bluff(villain.profile.as_ref(), street, bluff); + let line = crate::engine::glaube_linie::line_of(spot, villain.seat, street, ratio, lvl == 1); + let bluff = crate::engine::glaube::bluff(villain.profile.as_ref(), street, line.as_ref(), bluff); let base = [BET_RUNG, RAISE_RUNG][lvl][slot]; ( rung(base, read_notch(villain, ratio) + style_rung(villain)),