- hand::klasse: KLASSEN/klasse (aus sim::poolruf) und percentile (aus sim::bettor_tor) woertlich verschoben; sim re-exportiert. Live-Aufrufer (solver_kern, multi3_spieler, ranges_echt, kern_zeile) zeigen auf hand::klasse. - lib.rs: pub mod sim nur mit --features sim (Vorgabe aus); blueprint::welt_gegner ebenso. - Cargo.toml: 36 Sim-Bins required-features = ["sim"]; tests/sim_*.rs cfg(feature = "sim"). - rechner baut ohne Sim; jede uebrige Sim-Abhaengigkeit scheitert beim Bauen. - readme_staerken: Sim-Bloecke gegner/lbr nur noch als Hinweis ausgemustert, Matrix ohne Sim-Spalte, REFERENZ.txt nicht mehr gelesen; README entsprechend markiert. - messung/sim-raus/bitgleich.py: Vergleich neuer gegen Live-Kern auf hhDealer-Spots. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
443 lines
16 KiB
Rust
443 lines
16 KiB
Rust
//! Messschalter `RANGES_ECHT=<ranges.txt>`: gemessene Gegner-Ranges und -Reaktionen aus 12,4 Mio. echten
|
|
//! CoinPoker-NL2-Händen (heads-up nach dem Flop), als Eingabe für einen Postflop-Solver
|
|
//! (`messung/ranges_echt/FORMAT.md`, gebaut von `messung/ranges_echt/tabellen.py`). Ungesetzt: aus, nichts liest es.
|
|
//!
|
|
//! Drei Zeilenarten, alle je Knoten `Key` (Straße, Topf-Art, Vorflop-Aggressor, Position, Linie, Textur, Typ):
|
|
//! - `R`: die Range des Spielers an diesem Knoten als Verteilung über Zellen (Brett-Perzentil-Fach, auf Flop und
|
|
//! Turn mal Draw ja/nein),
|
|
//! - `A`: seine Aktionshäufigkeiten, wenn er dort am Zug ist (x k m g u j f c r),
|
|
//! - `L`: P(Aktion | Zelle) dort, die Strategie je Zelle (Fold als Rest aus R und A).
|
|
//!
|
|
//! Die Zelle einer Hand rechnet `zelle` hier, damit Messung und Kern dieselbe Größe meinen. Dünne Knoten fallen
|
|
//! über die Kette in `KETTE` auf gröbere zurück; die Tabelle ist schon hierarchisch geglättet (jede Zeile zu ihrem
|
|
//! Elternknoten geschrumpft), der erste vorhandene Schlüssel der Kette ist die Antwort.
|
|
|
|
use crate::card::Card;
|
|
use crate::hand::fast7::{mask, score};
|
|
|
|
/// Brett-Perzentil-Fächer.
|
|
pub const FAECHER: usize = 20;
|
|
/// Zellen auf Flop/Turn (Fach + 20 * Draw) und River (nur Fach).
|
|
pub const ZELLEN_FT: usize = 2 * FAECHER;
|
|
pub const ZELLEN_R: usize = FAECHER;
|
|
/// Aktionsalphabet der `A`-Zeilen in Spaltenreihenfolge.
|
|
pub const AKTIONEN: [char; 9] = ['x', 'k', 'm', 'g', 'u', 'j', 'f', 'c', 'r'];
|
|
|
|
const STRASSE_PLUS: u32 = 4 << 24; // fast7: Kategorie Straße und besser
|
|
|
|
/// Anteil aller Zwei-Karten-Hände (ohne Brett und eigene Karten), die `hole` mit der besten Fünf auf `board`
|
|
/// schlägt, Gleichstand halb (gleich `hand::klasse::percentile`, `exploit::bettor`).
|
|
pub fn perzentil(hole: &[Card], board: &[Card]) -> f64 {
|
|
crate::hand::klasse::percentile(hole, board)
|
|
}
|
|
|
|
/// Starker Draw (nur Flop und Turn): die Hand ist noch keine Straße oder besser, und mindestens 8 der nächsten
|
|
/// Karten machen sie dazu, ohne dass das Brett allein es schon ist.
|
|
pub fn draw(hole: &[Card], board: &[Card]) -> bool {
|
|
if board.len() >= 5 || board.len() < 3 {
|
|
return false;
|
|
}
|
|
let bm = mask(board);
|
|
let hm = mask(hole);
|
|
if score(hm | bm) >= STRASSE_PLUS {
|
|
return false;
|
|
}
|
|
let dead = bm | hm;
|
|
let mut outs = 0;
|
|
for s in 0..4 {
|
|
for r in 0..13 {
|
|
let c = 1u64 << (16 * s + r);
|
|
if dead & c != 0 {
|
|
continue;
|
|
}
|
|
let brett_allein = board.len() == 4 && score(bm | c) >= STRASSE_PLUS;
|
|
if !brett_allein && score(hm | bm | c) >= STRASSE_PLUS {
|
|
outs += 1;
|
|
}
|
|
}
|
|
}
|
|
outs >= 8
|
|
}
|
|
|
|
/// Fach eines Perzentils (0..FAECHER).
|
|
pub fn fach(p: f64) -> usize {
|
|
((p.clamp(0.0, 1.0) * FAECHER as f64) as usize).min(FAECHER - 1)
|
|
}
|
|
|
|
/// Zelle einer Hand auf dem Brett: Fach, auf Flop/Turn plus 20 bei starkem Draw.
|
|
pub fn zelle(hole: &[Card], board: &[Card]) -> usize {
|
|
let f = fach(perzentil(hole, board));
|
|
if board.len() < 5 && draw(hole, board) {
|
|
f + FAECHER
|
|
} else {
|
|
f
|
|
}
|
|
}
|
|
|
|
/// Textur des Bretts: `P` gepaart, `F` drei oder mehr einer Farbe, `S` Straße möglich (drei Ränge in einem
|
|
/// Fünferfenster, Ass auch unten), `-` nichts davon.
|
|
pub fn textur(board: &[Card]) -> String {
|
|
let mut r = [0u8; 13];
|
|
let mut s = [0u8; 4];
|
|
for c in board {
|
|
r[c.rank as usize] += 1;
|
|
s[c.suit as usize] += 1;
|
|
}
|
|
let mut t = String::new();
|
|
if r.iter().any(|&n| n >= 2) {
|
|
t.push('P');
|
|
}
|
|
if s.iter().any(|&n| n >= 3) {
|
|
t.push('F');
|
|
}
|
|
let mut bits = 0u32;
|
|
for (i, &n) in r.iter().enumerate() {
|
|
if n > 0 {
|
|
bits |= 1 << (i + 1);
|
|
}
|
|
}
|
|
if r[12] > 0 {
|
|
bits |= 1;
|
|
}
|
|
if (0..=9).any(|lo| ((bits >> lo) & 0x1F).count_ones() >= 3) {
|
|
t.push('S');
|
|
}
|
|
if t.is_empty() {
|
|
t.push('-');
|
|
}
|
|
t
|
|
}
|
|
|
|
/// Einsatz-Token einer Wette: Anteil am Topf davor; `j` all-in.
|
|
pub fn einsatz_token(einsatz: f64, topf_vorher: f64, all_in: bool) -> char {
|
|
if all_in {
|
|
return 'j';
|
|
}
|
|
let q = if topf_vorher > 0.0 { einsatz / topf_vorher } else { 1.0 };
|
|
if q <= 0.40 {
|
|
'k'
|
|
} else if q <= 0.80 {
|
|
'm'
|
|
} else if q <= 1.20 {
|
|
'g'
|
|
} else {
|
|
'u'
|
|
}
|
|
}
|
|
|
|
/// Linie gröber: Stufe 1 = frühere Straßen nur als letzter Aggressor (`O`/`I`/`X`), Stufe 2 = frühere Straßen `*`.
|
|
pub fn linie_stufe(l0: &str, stufe: u8) -> String {
|
|
if stufe == 0 {
|
|
return l0.to_string();
|
|
}
|
|
let teile: Vec<&str> = l0.split('/').collect();
|
|
let n = teile.len();
|
|
let mut aus: Vec<String> = Vec::with_capacity(n);
|
|
for (i, t) in teile.iter().enumerate() {
|
|
if i + 1 == n {
|
|
aus.push(t.to_string());
|
|
} else if stufe >= 2 {
|
|
aus.push("*".into());
|
|
} else {
|
|
aus.push(letzter_aggressor(t).to_string());
|
|
}
|
|
}
|
|
aus.join("/")
|
|
}
|
|
|
|
/// Wer auf einer abgeschlossenen Straße zuletzt gesetzt/erhöht hat: `O` (ausser Position), `I`, `X` (niemand).
|
|
pub fn letzter_aggressor(strasse: &str) -> char {
|
|
let mut w = 'X';
|
|
for (i, c) in strasse.chars().enumerate() {
|
|
if "kmgujr".contains(c) {
|
|
w = if i % 2 == 0 { 'O' } else { 'I' };
|
|
}
|
|
}
|
|
w
|
|
}
|
|
|
|
/// Schlüssel eines Knotens. `pfa`: '1' der Spieler war Vorflop-Aggressor, '0' der Gegner, '-' Limp-Topf;
|
|
/// `pos`: 'o' ausser Position, 'i' in Position; `topf`: 'L' 'S' '3' '4'; `strasse`: 'f' 't' 'r'.
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
|
pub struct Key {
|
|
pub strasse: char,
|
|
pub topf: char,
|
|
pub pfa: char,
|
|
pub pos: char,
|
|
pub linie: String,
|
|
pub textur: String,
|
|
pub typ: String,
|
|
}
|
|
|
|
impl Key {
|
|
fn mit(&self, linie: String, textur: &str, typ: &str) -> Key {
|
|
Key { linie, textur: textur.into(), typ: typ.into(), ..self.clone() }
|
|
}
|
|
}
|
|
|
|
/// Wer an der Linie am Zug ist: 'o' oder 'i' (jede Straße beginnt außer Position).
|
|
pub fn am_zug(linie: &str) -> char {
|
|
let cur = linie.rsplit('/').next().unwrap_or("");
|
|
if cur.chars().count() % 2 == 0 {
|
|
'o'
|
|
} else {
|
|
'i'
|
|
}
|
|
}
|
|
|
|
/// Steht der Spieler des Schlüssels gegen einen Einsatz am Zug, der Schlüssel ohne diesen Einsatz: der Gegner
|
|
/// kennt seine Hand nicht, die Range davor ist die Range danach (FORMAT.md §5).
|
|
pub fn vor_einsatz(k: &Key) -> Key {
|
|
let cur = k.linie.rsplit('/').next().unwrap_or("");
|
|
match cur.chars().last() {
|
|
Some(c) if "kmgujr".contains(c) && am_zug(&k.linie) == k.pos => {
|
|
let mut l = k.linie.clone();
|
|
l.pop();
|
|
Key { linie: l, ..k.clone() }
|
|
}
|
|
_ => k.clone(),
|
|
}
|
|
}
|
|
|
|
/// Rückfallkette (Stufe der Linie, Textur behalten, Typ behalten), erste vorhandene Zeile gilt.
|
|
pub const KETTE: [(u8, bool, bool); 8] = [
|
|
(0, true, true),
|
|
(0, true, false),
|
|
(0, false, true),
|
|
(0, false, false),
|
|
(1, false, true),
|
|
(1, false, false),
|
|
(2, false, true),
|
|
(2, false, false),
|
|
];
|
|
|
|
/// Eine Zeile: Werte und effektive Stichprobe (Kish) sowie die Kettenstufe, auf der sie gefunden wurde.
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub struct Zeile {
|
|
pub n_eff: f64,
|
|
pub werte: Vec<f64>,
|
|
}
|
|
|
|
#[derive(Default, Debug)]
|
|
pub struct Tabelle {
|
|
pub r: std::collections::HashMap<Key, Zeile>,
|
|
pub a: std::collections::HashMap<Key, Zeile>,
|
|
pub l: std::collections::HashMap<(Key, char), Zeile>,
|
|
/// Typ-Grenzen: (Code, vpip von, vpip bis, Aggression von, Aggression bis), Mindesthände.
|
|
pub typen: Vec<(String, f64, f64, f64, f64)>,
|
|
pub min_haende: f64,
|
|
}
|
|
|
|
fn key_lesen(it: &mut std::str::SplitWhitespace) -> Option<Key> {
|
|
let mut c = || it.next().map(|s| s.to_string());
|
|
let strasse = c()?.chars().next()?;
|
|
let topf = c()?.chars().next()?;
|
|
let pfa = c()?.chars().next()?;
|
|
let pos = c()?.chars().next()?;
|
|
let linie = c()?;
|
|
let linie = if linie == "." { String::new() } else { linie };
|
|
Some(Key { strasse, topf, pfa, pos, linie, textur: c()?, typ: c()? })
|
|
}
|
|
|
|
fn zahlen(it: std::str::SplitWhitespace) -> Option<Vec<f64>> {
|
|
it.map(|s| s.parse::<f64>().ok().filter(|v| v.is_finite())).collect()
|
|
}
|
|
|
|
impl Tabelle {
|
|
/// Liest den Text der Tabelle; unlesbare Zeilen werden übersprungen.
|
|
pub fn lesen(text: &str) -> Tabelle {
|
|
let mut t = Tabelle { min_haende: 100.0, ..Default::default() };
|
|
for line in text.lines() {
|
|
let line = line.trim();
|
|
if line.is_empty() || line.starts_with('#') {
|
|
continue;
|
|
}
|
|
let mut it = line.split_whitespace();
|
|
match it.next() {
|
|
Some("R") | Some("A") => {
|
|
let art = line.as_bytes()[0];
|
|
let Some(k) = key_lesen(&mut it) else { continue };
|
|
let Some(v) = zahlen(it) else { continue };
|
|
if v.len() < 2 {
|
|
continue;
|
|
}
|
|
let z = Zeile { n_eff: v[0], werte: v[1..].to_vec() };
|
|
if art == b'R' {
|
|
t.r.insert(k, z);
|
|
} else {
|
|
t.a.insert(k, z);
|
|
}
|
|
}
|
|
Some("L") => {
|
|
let Some(k) = key_lesen(&mut it) else { continue };
|
|
let Some(a) = it.next().and_then(|s| s.chars().next()) else { continue };
|
|
let Some(v) = zahlen(it) else { continue };
|
|
if v.len() < 2 {
|
|
continue;
|
|
}
|
|
t.l.insert((k, a), Zeile { n_eff: v[0], werte: v[1..].to_vec() });
|
|
}
|
|
Some("T") => {
|
|
let code = it.next().unwrap_or("").to_string();
|
|
if let Some(v) = zahlen(it) {
|
|
if v.len() == 4 {
|
|
t.typen.push((code, v[0], v[1], v[2], v[3]));
|
|
}
|
|
}
|
|
}
|
|
Some("M") => {
|
|
if let Some(v) = it.next().and_then(|s| s.parse().ok()) {
|
|
t.min_haende = v;
|
|
}
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
t
|
|
}
|
|
|
|
/// Typ-Code eines Spielers aus vpip, Postflop-Aggression (Einsatz+Raise je Postflop-Aktion ohne Check) und
|
|
/// Handzahl; zu wenig Hände: `*` (Population).
|
|
pub fn typ(&self, vpip: f64, aggression: f64, haende: f64) -> String {
|
|
if haende < self.min_haende {
|
|
return "*".into();
|
|
}
|
|
for (c, v0, v1, a0, a1) in &self.typen {
|
|
if vpip >= *v0 && vpip < *v1 && aggression >= *a0 && aggression < *a1 {
|
|
return c.clone();
|
|
}
|
|
}
|
|
"*".into()
|
|
}
|
|
|
|
fn suche<'a, V>(&'a self, m: &'a std::collections::HashMap<Key, V>, k: &Key) -> Option<(&'a V, usize)> {
|
|
for (i, (stufe, tex, typ)) in KETTE.iter().enumerate() {
|
|
let kk = k.mit(linie_stufe(&k.linie, *stufe), if *tex { &k.textur } else { "*" }, if *typ { &k.typ } else { "*" });
|
|
if let Some(v) = m.get(&kk) {
|
|
return Some((v, i));
|
|
}
|
|
}
|
|
None
|
|
}
|
|
|
|
/// Range am Knoten (Zellen-Verteilung) und die Kettenstufe, auf der sie stand. Steht der Spieler gegen einen
|
|
/// Einsatz am Zug, gilt seine Range vor dem Einsatz (`vor_einsatz`).
|
|
pub fn range(&self, k: &Key) -> Option<(&Zeile, usize)> {
|
|
self.suche(&self.r, &vor_einsatz(k))
|
|
}
|
|
|
|
/// Aktionshäufigkeiten am Knoten (Spalten wie `AKTIONEN`).
|
|
pub fn aktionen(&self, k: &Key) -> Option<(&Zeile, usize)> {
|
|
self.suche(&self.a, k)
|
|
}
|
|
|
|
/// P(Aktion | Zelle) am Knoten.
|
|
pub fn strategie(&self, k: &Key, aktion: char) -> Option<(&Zeile, usize)> {
|
|
for (i, (stufe, tex, typ)) in KETTE.iter().enumerate() {
|
|
let kk = k.mit(linie_stufe(&k.linie, *stufe), if *tex { &k.textur } else { "*" }, if *typ { &k.typ } else { "*" });
|
|
if let Some(v) = self.l.get(&(kk, aktion)) {
|
|
return Some((v, i));
|
|
}
|
|
}
|
|
None
|
|
}
|
|
|
|
/// Standardabweichung eines Zellenanteils `p` bei effektiver Stichprobe `n_eff` (Dirichlet-Näherung).
|
|
pub fn sd(p: f64, n_eff: f64) -> f64 {
|
|
(p * (1.0 - p) / (n_eff + 1.0)).max(0.0).sqrt()
|
|
}
|
|
}
|
|
|
|
/// Die geladene Tabelle aus `RANGES_ECHT` (einmal je Prozess), sonst `None` (aus).
|
|
pub fn tabelle() -> Option<&'static Tabelle> {
|
|
use std::sync::OnceLock;
|
|
static T: OnceLock<Option<Tabelle>> = OnceLock::new();
|
|
T.get_or_init(|| {
|
|
let path = std::env::var("RANGES_ECHT").ok().filter(|p| !p.is_empty())?;
|
|
let text = std::fs::read_to_string(&path).ok()?;
|
|
let t = Tabelle::lesen(&text);
|
|
(!t.r.is_empty()).then_some(t)
|
|
})
|
|
.as_ref()
|
|
}
|
|
|
|
/// Ist `RANGES_ECHT` an (eine lesbare Tabelle)?
|
|
pub fn enabled() -> bool {
|
|
tabelle().is_some()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
fn cards(s: &str) -> Vec<Card> {
|
|
Card::parse_list(s).unwrap()
|
|
}
|
|
|
|
#[test]
|
|
fn nuts_on_river_is_top_bin_and_air_low() {
|
|
assert_eq!(zelle(&cards("Ah Kh"), &cards("Qh Jh Th 2c 3d")), FAECHER - 1);
|
|
assert!(zelle(&cards("7c 2d"), &cards("Ah Kd Qs 9h 4c")) < 3);
|
|
}
|
|
|
|
#[test]
|
|
fn flush_draw_on_flop_counts_as_draw() {
|
|
assert!(draw(&cards("Ah 5h"), &cards("Kh 9h 2c")));
|
|
assert!(!draw(&cards("Ac 5d"), &cards("Kh 9h 2c")));
|
|
let z = zelle(&cards("Ah 5h"), &cards("Kh 9h 2c"));
|
|
assert!(z >= FAECHER);
|
|
// Straight already made: no draw
|
|
assert!(!draw(&cards("Jc Td"), &cards("9h 8s 7c")));
|
|
// OESD
|
|
assert!(draw(&cards("Jc Td"), &cards("9h 8s 2c")));
|
|
}
|
|
|
|
#[test]
|
|
fn texture_codes() {
|
|
assert_eq!(textur(&cards("Kh 9h 2c")), "-");
|
|
assert_eq!(textur(&cards("Kh Kd 2c")), "P");
|
|
assert_eq!(textur(&cards("Kh 9h 2h")), "F");
|
|
assert_eq!(textur(&cards("9h 8s 7c")), "S");
|
|
assert_eq!(textur(&cards("Ah 2s 3c")), "S");
|
|
assert_eq!(textur(&cards("9h 9d 8h 7h")), "PFS");
|
|
}
|
|
|
|
#[test]
|
|
fn line_levels() {
|
|
assert_eq!(linie_stufe("xkc/kc/x", 0), "xkc/kc/x");
|
|
assert_eq!(linie_stufe("xkc/kc/x", 1), "I/O/x");
|
|
assert_eq!(linie_stufe("xkc/kc/x", 2), "*/*/x");
|
|
assert_eq!(linie_stufe("", 1), "");
|
|
assert_eq!(einsatz_token(1.0, 4.0, false), 'k');
|
|
assert_eq!(einsatz_token(3.0, 4.0, false), 'm');
|
|
assert_eq!(einsatz_token(4.0, 4.0, false), 'g');
|
|
assert_eq!(einsatz_token(9.0, 4.0, false), 'u');
|
|
assert_eq!(einsatz_token(1.0, 4.0, true), 'j');
|
|
}
|
|
|
|
#[test]
|
|
fn table_parses_and_backs_off() {
|
|
let text = "# test\nT ra 0.2 0.3 0.3 1.0\nM 100\nR r S 1 i */*/x * * 50 0.5 0.5\nR r S 1 i xkc/kc/x * ra 10 0.9 0.1\nA r S 1 i xkc/kc/x * * 400 0.5 0.2 0.1 0.1 0.05 0.05 0 0 0\nL r S 1 i xkc/kc/x * * k 400 0.1 0.9\n";
|
|
let t = Tabelle::lesen(text);
|
|
let k = Key { strasse: 'r', topf: 'S', pfa: '1', pos: 'i', linie: "xkc/kc/x".into(), textur: "P".into(), typ: "ra".into() };
|
|
let (z, stufe) = t.range(&k).unwrap();
|
|
assert_eq!(stufe, 2);
|
|
assert_eq!(z.werte, vec![0.9, 0.1]);
|
|
let k2 = Key { typ: "fa".into(), linie: "xmc/kc/x".into(), ..k.clone() };
|
|
let (z2, stufe2) = t.range(&k2).unwrap();
|
|
assert_eq!(stufe2, 7);
|
|
assert_eq!(z2.n_eff, 50.0);
|
|
assert_eq!(t.aktionen(&k).unwrap().0.werte.len(), 9);
|
|
assert_eq!(t.strategie(&k, 'k').unwrap().0.werte, vec![0.1, 0.9]);
|
|
assert_eq!(t.typ(0.25, 0.4, 500.0), "ra");
|
|
// facing: the range before the bet
|
|
let kf = Key { linie: "xkc/kc/xk".into(), pos: 'o', ..k.clone() };
|
|
assert_eq!(vor_einsatz(&kf).linie, "xkc/kc/x");
|
|
let kb = Key { linie: "xkc/kc/xk".into(), pos: 'i', ..k.clone() };
|
|
assert_eq!(vor_einsatz(&kb).linie, "xkc/kc/xk");
|
|
let k0 = Key { linie: "k".into(), pos: 'i', ..k.clone() };
|
|
assert_eq!(vor_einsatz(&k0).linie, "");
|
|
assert_eq!(t.typ(0.25, 0.4, 50.0), "*");
|
|
}
|
|
}
|