Lock-Gewicht je Gegnerknoten w * n/(n+kappa) (Restricted Nash Response nach Datenstuetzung). n aus ranges_echt (Masse-gewichtet), wett_kurve (# n= je Punkt, Minimum der Nachbarn), BETTOR (Beiblatt <tabelle>.n aus bettor_n.py), Raise-Pool (Konstante). Gespielte Linie behaelt w_linie. Mehrweg (pool_sperren, river3/multi3) gleich. Aus = bitgleiche Rechnung. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
194 lines
7.7 KiB
Rust
194 lines
7.7 KiB
Rust
//! Messschalter `BETTOR=<Tabelle>`: what a player holds when he bets (or checks) at a decision where he
|
|
//! could bet, measured from real showdowns (`messung/familie_a/bettor.py`, PHH NL25/50 plus our NL2).
|
|
//!
|
|
//! The made-class floors of `street_continuation` read every bet the same way up to a size notch: a
|
|
//! river bet keeps top pair good kicker or better, a flop bet middle pair or better. Real NL2 does not
|
|
//! play like that: it stabs small with almost anything and overbets only strong. The model here is the
|
|
//! likelihood P(answer | board percentile) per decision node (street, preflop aggressor, own aggression
|
|
//! on the street before), answer `x` (check) or a size `k <= 0.40 < m <= 0.80 < g <= 1.20 < u` (pots),
|
|
//! `j` an all-in bet. The board percentile of a holding is the share of all two-card holdings its best
|
|
//! five on the current board beats (ties half). A hand stays in the range with probability `L(p) / max L`
|
|
//! (`engine::range_filter`): the posterior over the hands that were still in, by Bayes.
|
|
//!
|
|
//! Live 17.11.: against small bets (< 55 % pot) 50 % folds, two pair 24 %; against a 1.5-pot river bet
|
|
//! of a passive player KK on Ad7hAh7sJc called as "equity 0.66". Both are this missing size condition.
|
|
|
|
use crate::game::Street;
|
|
|
|
/// Bins of the likelihood (percentile 0..1).
|
|
pub const BINS: usize = 20;
|
|
|
|
/// Acceptance per bin, normalized to a maximum of 1.
|
|
pub type Likelihood = [f64; BINS];
|
|
|
|
/// Decision-node key: street, preflop aggressor, own aggression on the street before.
|
|
pub type Key = (Street, bool, bool);
|
|
|
|
fn table() -> Option<&'static Vec<(Key, char, Likelihood)>> {
|
|
use std::sync::OnceLock;
|
|
static T: OnceLock<Option<Vec<(Key, char, Likelihood)>>> = OnceLock::new();
|
|
T.get_or_init(|| {
|
|
let path = std::env::var("BETTOR").ok().filter(|p| !p.is_empty())?;
|
|
let text = std::fs::read_to_string(&path).ok()?;
|
|
let rows: Vec<_> = text.lines().filter_map(parse_row).collect();
|
|
(!rows.is_empty()).then_some(rows)
|
|
})
|
|
.as_ref()
|
|
}
|
|
|
|
fn parse_row(line: &str) -> Option<(Key, char, Likelihood)> {
|
|
let (key, answer, mut lk) = parse_raw(line)?;
|
|
let top = lk.iter().cloned().fold(0.0, f64::max);
|
|
if top <= 0.0 {
|
|
return None;
|
|
}
|
|
for slot in lk.iter_mut() {
|
|
*slot /= top;
|
|
}
|
|
Some((key, answer, lk))
|
|
}
|
|
|
|
/// A table row as written: `P(answer | percentile bin)` at the node (`sim::bettor_tor` samples from it).
|
|
pub fn parse_raw(line: &str) -> Option<(Key, char, Likelihood)> {
|
|
let line = line.trim();
|
|
if line.is_empty() || line.starts_with('#') {
|
|
return None;
|
|
}
|
|
let mut it = line.split_whitespace();
|
|
let street = match it.next()? {
|
|
"flop" => Street::Flop,
|
|
"turn" => Street::Turn,
|
|
"river" => Street::River,
|
|
_ => return None,
|
|
};
|
|
let pfa = it.next()? == "1";
|
|
let before = it.next()? == "1";
|
|
let answer = it.next()?.chars().next()?;
|
|
let mut lk = [0.0; BINS];
|
|
for slot in lk.iter_mut() {
|
|
*slot = it.next()?.parse::<f64>().ok().filter(|v| v.is_finite() && *v >= 0.0)?;
|
|
}
|
|
Some(((street, pfa, before), answer, lk))
|
|
}
|
|
|
|
/// Is `BETTOR` on (a readable table)?
|
|
pub fn enabled() -> bool {
|
|
table().is_some()
|
|
}
|
|
|
|
/// The size class of a bet of `ratio` pots (`None`: unknown size), or `j` for an all-in bet.
|
|
pub fn answer_of(ratio: Option<f64>, all_in: bool) -> Option<char> {
|
|
if all_in {
|
|
return Some('j');
|
|
}
|
|
let r = ratio.filter(|r| r.is_finite() && *r > 0.0)?;
|
|
Some(if r <= 0.40 {
|
|
'k'
|
|
} else if r <= 0.80 {
|
|
'm'
|
|
} else if r <= 1.20 {
|
|
'g'
|
|
} else {
|
|
'u'
|
|
})
|
|
}
|
|
|
|
/// The acceptance curve for `answer` at this node, if the table has it.
|
|
pub fn likelihood(street: Street, pfa: bool, before: bool, answer: char) -> Option<Likelihood> {
|
|
table()?.iter().find(|(k, a, _)| *k == (street, pfa, before) && *a == answer).map(|(_, _, lk)| *lk)
|
|
}
|
|
|
|
/// The row for `answer` at this node AS MEASURED (`P(answer | percentile bin)`, not normalized), so the
|
|
/// answers of one node can be compared with each other (`engine::solver_kern` locks the villain's whole
|
|
/// open decision to them). `None` without a table or without that row.
|
|
pub fn raw_likelihood(street: Street, pfa: bool, before: bool, answer: char) -> Option<Likelihood> {
|
|
use std::sync::OnceLock;
|
|
static RAW: OnceLock<Option<Vec<(Key, char, Likelihood)>>> = OnceLock::new();
|
|
RAW.get_or_init(|| {
|
|
let path = std::env::var("BETTOR").ok().filter(|p| !p.is_empty())?;
|
|
let text = std::fs::read_to_string(&path).ok()?;
|
|
let rows: Vec<_> = text.lines().filter_map(parse_raw).collect();
|
|
(!rows.is_empty()).then_some(rows)
|
|
})
|
|
.as_ref()?
|
|
.iter()
|
|
.find(|(k, a, _)| *k == (street, pfa, before) && *a == answer)
|
|
.map(|(_, _, lk)| *lk)
|
|
}
|
|
|
|
/// Observations behind the row for `answer` at this node (`STUETZUNG`): the sidecar `<BETTOR>.n`
|
|
/// (`messung/familie_a/bettor_n.py`, one line `street pfa before answer n`). `None` without the sidecar or the row.
|
|
pub fn raw_n(street: Street, pfa: bool, before: bool, answer: char) -> Option<f64> {
|
|
use std::sync::OnceLock;
|
|
static N: OnceLock<Option<Vec<(Key, char, f64)>>> = OnceLock::new();
|
|
N.get_or_init(|| {
|
|
let path = std::env::var("BETTOR").ok().filter(|p| !p.is_empty())?;
|
|
let text = std::fs::read_to_string(format!("{path}.n")).ok()?;
|
|
let rows: Vec<_> = text
|
|
.lines()
|
|
.filter(|l| !l.trim_start().starts_with('#'))
|
|
.filter_map(|l| {
|
|
let mut it = l.split_whitespace();
|
|
let street = match it.next()? {
|
|
"flop" => Street::Flop,
|
|
"turn" => Street::Turn,
|
|
"river" => Street::River,
|
|
_ => return None,
|
|
};
|
|
let pfa = it.next()? == "1";
|
|
let before = it.next()? == "1";
|
|
let answer = it.next()?.chars().next()?;
|
|
let n = it.next()?.parse::<f64>().ok().filter(|v| v.is_finite() && *v >= 0.0)?;
|
|
Some(((street, pfa, before), answer, n))
|
|
})
|
|
.collect();
|
|
(!rows.is_empty()).then_some(rows)
|
|
})
|
|
.as_ref()?
|
|
.iter()
|
|
.find(|(k, a, _)| *k == (street, pfa, before) && *a == answer)
|
|
.map(|(_, _, n)| *n)
|
|
}
|
|
|
|
/// Acceptance at board percentile `p` (linear between bin centres, flat at the ends).
|
|
pub fn accept(lk: &Likelihood, p: f64) -> f64 {
|
|
let x = (p.clamp(0.0, 1.0) * BINS as f64 - 0.5).clamp(0.0, (BINS - 1) as f64);
|
|
let i = x.floor() as usize;
|
|
let j = (i + 1).min(BINS - 1);
|
|
let t = x - i as f64;
|
|
lk[i] * (1.0 - t) + lk[j] * t
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn sizes_map_to_classes() {
|
|
assert_eq!(answer_of(Some(0.3), false), Some('k'));
|
|
assert_eq!(answer_of(Some(0.66), false), Some('m'));
|
|
assert_eq!(answer_of(Some(1.0), false), Some('g'));
|
|
assert_eq!(answer_of(Some(1.5), false), Some('u'));
|
|
assert_eq!(answer_of(Some(0.3), true), Some('j'));
|
|
assert_eq!(answer_of(None, false), None);
|
|
}
|
|
|
|
#[test]
|
|
fn rows_parse_and_normalize() {
|
|
let row = format!("river 0 1 u {}", (0..BINS).map(|i| format!("{}", i as f64 / 10.0)).collect::<Vec<_>>().join(" "));
|
|
let ((s, pfa, before), a, lk) = parse_row(&row).expect("row");
|
|
assert_eq!((s, pfa, before, a), (Street::River, false, true, 'u'));
|
|
assert!((lk[BINS - 1] - 1.0).abs() < 1e-12 && lk[0] == 0.0);
|
|
assert!(parse_row("# comment").is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn accept_interpolates() {
|
|
let mut lk = [0.0; BINS];
|
|
lk[BINS - 1] = 1.0;
|
|
assert_eq!(accept(&lk, 1.0), 1.0);
|
|
assert_eq!(accept(&lk, 0.0), 0.0);
|
|
assert!(accept(&lk, 1.0 - 0.75 / BINS as f64) > 0.0);
|
|
}
|
|
}
|