ANTWORT_EICHUNG: logistisches Modell aus dem ganzen Profil (Zeilen M/P) als zweite Quelle der Naht
Die Suche bekommt weiter nur eine Foldrate-Funktion; die Typ-Tafel kann so ohne Umbau durch ein Modell aus allen Profilwerten ersetzt werden. Merkmale sind auf den Poolwert geschrumpfte Raten (0 = Durchschnitt, 0 bei fehlenden Feldern); unbekannte Merkmale verwerfen die Zeile. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -53,18 +53,87 @@ pub fn typ(profile: &Value) -> u8 {
|
||||
}
|
||||
}
|
||||
|
||||
/// Foldrate je (Typ, Strasse 2 = Turn / 3 = River, Groessenband bis `bis` = Einsatz / Topf davor), gueltig bis `bis_bb` (in $).
|
||||
/// Merkmale des Modells (Reihenfolge fest): jede Rate mit Zaehler, auf den Poolwert geschrumpft und um ihn vermindert (0 = Durchschnitt).
|
||||
pub const MERKMALE: [&str; 9] = ["vpip", "pfr", "wtsd", "aggression", "foldToBet", "flopFoldToBet", "turnFoldToBet", "riverFoldToBet", "bekannt"];
|
||||
/// Haende, ab denen `bekannt` seinen Hoechstwert 1 hat.
|
||||
const BEKANNT_BIS: f64 = 200.0;
|
||||
/// Messpunkte der Groessenbaender (Einsatz / Topf), an denen der Merk-Schluessel eines Modells die Foldraten abtastet.
|
||||
const BAENDER: [f32; 4] = [0.45, 0.85, 1.3, 99.0];
|
||||
|
||||
/// Logistisches Modell fuer ein Band: `fold = sigmoid(c0 + Summe koeffizient * merkmal)`.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Modell {
|
||||
pub strasse: u8,
|
||||
pub bis: f32,
|
||||
pub c0: f32,
|
||||
pub koeff: Vec<(&'static str, f32)>,
|
||||
}
|
||||
|
||||
/// Poolwert und Gewicht (Zahl Beobachtungen) eines Merkmals fuer die Schrumpfung.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Pool {
|
||||
pub name: &'static str,
|
||||
pub wert: f32,
|
||||
pub prior_n: f32,
|
||||
}
|
||||
|
||||
/// Foldrate je (Typ, Strasse 2 = Turn / 3 = River, Groessenband bis `bis` = Einsatz / Topf davor), gueltig bis `bis_bb` (in $);
|
||||
/// dazu optional Modelle aus dem ganzen Profil (Zeilen `M`, haben je Strasse und Band Vorrang) mit ihren Poolwerten (Zeilen `P`).
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Tafel {
|
||||
pub bis_bb: f64,
|
||||
pub zeilen: Vec<(u8, u8, f32, f32)>,
|
||||
pub modelle: Vec<Modell>,
|
||||
pub pool: Vec<Pool>,
|
||||
}
|
||||
|
||||
fn merkmal_name(n: &str) -> Option<&'static str> {
|
||||
MERKMALE.iter().copied().find(|m| *m == n)
|
||||
}
|
||||
|
||||
/// Auf den Poolwert geschrumpfte Rate minus Poolwert: `(rate*n + pool*prior) / (n + prior) - pool`. Ohne Rate, Zaehler oder Poolzeile 0.
|
||||
fn geschrumpft(profile: &Value, rate: &str, n: f64, pool: Option<&Pool>) -> f32 {
|
||||
let (Some(p), Some(r)) = (pool, profile[rate].as_f64()) else { return 0.0 };
|
||||
let (pw, prior) = (p.wert as f64, p.prior_n as f64);
|
||||
let n = n.max(0.0);
|
||||
if n + prior <= 0.0 {
|
||||
return 0.0;
|
||||
}
|
||||
((r * n + pw * prior) / (n + prior) - pw) as f32
|
||||
}
|
||||
|
||||
/// Die Merkmale eines Profils in der Reihenfolge von `MERKMALE`. Fehlende Felder geben 0 (= Durchschnitt).
|
||||
pub fn merkmale(profile: &Value, pool: &[Pool]) -> Vec<(&'static str, f32)> {
|
||||
let zahl = |k: &str| profile[k].as_f64().unwrap_or(0.0);
|
||||
let hands = zahl("hands");
|
||||
let gegen = zahl("flopFacingBet") + zahl("turnFacingBet") + zahl("riverFacingBet");
|
||||
MERKMALE
|
||||
.iter()
|
||||
.map(|&m| {
|
||||
let n = match m {
|
||||
"vpip" | "pfr" | "wtsd" => hands,
|
||||
"aggression" => zahl("aggressionActions"),
|
||||
"foldToBet" => gegen,
|
||||
"flopFoldToBet" => zahl("flopFacingBet"),
|
||||
"turnFoldToBet" => zahl("turnFacingBet"),
|
||||
"riverFoldToBet" => zahl("riverFacingBet"),
|
||||
_ => 0.0,
|
||||
};
|
||||
let w = if m == "bekannt" { (hands.min(BEKANNT_BIS) / BEKANNT_BIS) as f32 } else { geschrumpft(profile, m, n, pool.iter().find(|p| p.name == m)) };
|
||||
(m, w)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
impl Tafel {
|
||||
/// Zeilen: `B <groesster Big Blind in $>`, `T <typ 0..3> <strasse 2|3> <bis Einsatz/Topf> <fold>`; '#' Kommentar.
|
||||
/// Zeilen: `B <groesster Big Blind in $>`, `T <typ 0..3> <strasse 2|3> <bis Einsatz/Topf> <fold>`,
|
||||
/// `M <strasse 2|3> <bis Einsatz/Topf> <c0> <merkmal>=<koeffizient> ...`, `P <merkmal> <poolwert> <prior_n>`; '#' Kommentar.
|
||||
/// Eine M- oder P-Zeile mit unbekanntem Merkmal wird verworfen (kein stilles Halb-Modell); eine unlesbare Zahl macht die Tafel ungueltig.
|
||||
pub fn lies(text: &str) -> Option<Tafel> {
|
||||
let mut bis_bb = 0.0f64;
|
||||
let mut zeilen: Vec<(u8, u8, f32, f32)> = Vec::new();
|
||||
let mut modelle: Vec<Modell> = Vec::new();
|
||||
let mut pool: Vec<Pool> = Vec::new();
|
||||
for z in text.lines() {
|
||||
let w: Vec<&str> = z.split('#').next().unwrap_or("").split_whitespace().collect();
|
||||
match w.as_slice() {
|
||||
@@ -76,11 +145,44 @@ impl Tafel {
|
||||
}
|
||||
zeilen.push((t, s, bis, f));
|
||||
}
|
||||
["P", name, wert, prior] => {
|
||||
let (wert, prior_n): (f32, f32) = (wert.parse().ok()?, prior.parse().ok()?);
|
||||
if let Some(name) = merkmal_name(name) {
|
||||
pool.retain(|p| p.name != name);
|
||||
pool.push(Pool { name, wert, prior_n });
|
||||
}
|
||||
}
|
||||
["M", s, bis, c0, rest @ ..] => {
|
||||
let (strasse, bis, c0): (u8, f32, f32) = (s.parse().ok()?, bis.parse().ok()?, c0.parse().ok()?);
|
||||
if !(2..=3).contains(&strasse) {
|
||||
return None;
|
||||
}
|
||||
let mut koeff = Vec::new();
|
||||
let mut bekannt = true;
|
||||
for kv in rest {
|
||||
let (k, v) = kv.split_once('=')?;
|
||||
let v: f32 = v.parse().ok()?;
|
||||
match merkmal_name(k) {
|
||||
Some(name) => koeff.push((name, v)),
|
||||
None => bekannt = false,
|
||||
}
|
||||
}
|
||||
if bekannt {
|
||||
modelle.push(Modell { strasse, bis, c0, koeff });
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
zeilen.sort_by(|x, y| (x.0, x.1, x.2).partial_cmp(&(y.0, y.1, y.2)).unwrap_or(std::cmp::Ordering::Equal));
|
||||
(bis_bb > 0.0 && !zeilen.is_empty()).then_some(Tafel { bis_bb, zeilen })
|
||||
modelle.sort_by(|x, y| (x.strasse, x.bis).partial_cmp(&(y.strasse, y.bis)).unwrap_or(std::cmp::Ordering::Equal));
|
||||
(bis_bb > 0.0 && (!zeilen.is_empty() || !modelle.is_empty())).then_some(Tafel { bis_bb, zeilen, modelle, pool })
|
||||
}
|
||||
|
||||
/// Modell fuer Strasse `st` und Einsatz/Topf `g` (Band wie bei `fold`); None: keines fuer diese Strasse.
|
||||
fn modell(&self, st: u8, g: f32) -> Option<&Modell> {
|
||||
let gruppe = || self.modelle.iter().filter(|m| m.strasse == st);
|
||||
gruppe().find(|m| g <= m.bis).or_else(|| gruppe().last())
|
||||
}
|
||||
|
||||
/// Foldrate fuer Typ `typ` auf Strasse `st` bei Einsatz/Topf `g`; None: keine Zeile (Gleichgewicht bleibt).
|
||||
@@ -124,7 +226,31 @@ pub fn fuer_tafel(t: Tafel, profile: &Value, bb_dollar: f64) -> Option<Antwort>
|
||||
return None;
|
||||
}
|
||||
let typ = typ(profile);
|
||||
Some(Antwort { schluessel: 0xA17F_0000 + typ as u64, kurz: format!("T{typ}"), fold: Box::new(move |st, g| t.fold(typ, st, g)) })
|
||||
if t.modelle.is_empty() {
|
||||
return Some(Antwort { schluessel: 0xA17F_0000 + typ as u64, kurz: format!("T{typ}"), fold: Box::new(move |st, g| t.fold(typ, st, g)) });
|
||||
}
|
||||
// Modell hat je Strasse und Band Vorrang vor der Typ-Zeile
|
||||
let merk: Vec<(&'static str, f32)> = merkmale(profile, &t.pool);
|
||||
let fold = move |st: u8, g: f32| -> Option<f32> {
|
||||
match t.modell(st, g) {
|
||||
Some(m) => {
|
||||
let z: f64 = m.c0 as f64 + m.koeff.iter().map(|(k, c)| *c as f64 * merk.iter().find(|x| x.0 == *k).map_or(0.0, |x| x.1 as f64)).sum::<f64>();
|
||||
Some(sigmoid(z) as f32)
|
||||
}
|
||||
None => t.fold(typ, st, g),
|
||||
}
|
||||
};
|
||||
// Gleiche Gegner (gleiche auf 2 Stellen gerundete Foldraten je Strasse und Band) teilen sich das gemerkte Spiel
|
||||
let mut h: u64 = 0xcbf2_9ce4_8422_2325;
|
||||
for st in [2u8, 3] {
|
||||
for g in BAENDER {
|
||||
let x = fold(st, g).map_or(-1, |f| (f * 100.0).round() as i64);
|
||||
for b in x.to_le_bytes() {
|
||||
h = (h ^ b as u64).wrapping_mul(0x0100_0000_01b3);
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(Antwort { schluessel: h, kurz: "M".into(), fold: Box::new(fold) })
|
||||
}
|
||||
|
||||
/// Zielfoldrate: vom Gleichgewicht um `dosis` des Weges zur Messung.
|
||||
@@ -252,6 +378,97 @@ mod spec {
|
||||
assert_eq!((neu.kurz.as_str(), (neu.fold)(3, 0.5)), ("T0", Some(0.32)));
|
||||
}
|
||||
|
||||
// ---- Modell aus dem ganzen Profil (Zeilen M und P) ----
|
||||
|
||||
fn nahe(a: f32, b: f32) -> bool {
|
||||
(a - b).abs() < 1e-5
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn modell_ohne_koeffizienten_ist_sigmoid_c0() {
|
||||
let t = Tafel::lies("B 0.05\nM 3 99 0.5\nM 3 99 0.5 vpip=0 pfr=0 foldToBet=0\n").unwrap();
|
||||
for p in [json!({}), json!({"hands": 500, "vpip": 0.9, "pfr": 0.5})] {
|
||||
let a = fuer_tafel(t.clone(), &p, 0.02).unwrap();
|
||||
assert!(nahe((a.fold)(3, 0.7).unwrap(), sigmoid(0.5) as f32), "{p}");
|
||||
assert_eq!(a.kurz, "M");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fehlende_profilfelder_aendern_nichts() {
|
||||
let t = Tafel::lies("B 0.05\nP vpip 0.3 40\nP foldToBet 0.4 20\nM 3 99 -0.2 vpip=2 foldToBet=-3\n").unwrap();
|
||||
let leer = fuer_tafel(t.clone(), &json!({}), 0.02).unwrap();
|
||||
assert!(nahe((leer.fold)(3, 1.0).unwrap(), sigmoid(-0.2) as f32));
|
||||
// Rate ohne Zaehler oder Zaehler ohne Rate: Merkmal 0
|
||||
let halb = json!({"vpip": 0.9, "foldToBet": 0.9});
|
||||
assert!(nahe((fuer_tafel(t, &halb, 0.02).unwrap().fold)(3, 1.0).unwrap(), sigmoid(-0.2) as f32), "Rate ohne Zaehler (n = 0) zaehlt nicht");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn schrumpfung_zum_poolwert() {
|
||||
let pool = [Pool { name: "vpip", wert: 0.30, prior_n: 50.0 }];
|
||||
let m = |h: u64, v: f64| merkmale(&json!({"hands": h, "vpip": v}), &pool).into_iter().find(|x| x.0 == "vpip").unwrap().1;
|
||||
assert_eq!(m(0, 0.9), 0.0, "n = 0: Poolwert, also Merkmal 0");
|
||||
assert!(nahe(m(50, 0.6), 0.15), "n = prior: halbe Strecke (0.6-0.3)/2");
|
||||
assert!((m(1_000_000, 0.6) - 0.30).abs() < 1e-3, "grosses n: rohe Rate minus Pool");
|
||||
assert_eq!(merkmale(&json!({"hands": 80, "vpip": 0.6}), &[])[0], ("vpip", 0.0), "ohne P-Zeile Merkmal 0");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn merkmale_kennen_ihre_zaehler() {
|
||||
let pool: Vec<Pool> = MERKMALE.iter().filter(|m| **m != "bekannt").map(|m| Pool { name: m, wert: 0.5, prior_n: 10.0 }).collect();
|
||||
let p = json!({"hands": 10, "vpip": 1.0, "pfr": 1.0, "wtsd": 1.0, "aggression": 1.0, "aggressionActions": 30,
|
||||
"foldToBet": 1.0, "flopFacingBet": 5, "turnFacingBet": 3, "riverFacingBet": 2,
|
||||
"flopFoldToBet": 1.0, "turnFoldToBet": 1.0, "riverFoldToBet": 1.0});
|
||||
let m = merkmale(&p, &pool);
|
||||
assert_eq!(m.iter().map(|x| x.0).collect::<Vec<_>>(), MERKMALE.to_vec());
|
||||
let wert = |n: &str| m.iter().find(|x| x.0 == n).unwrap().1;
|
||||
assert!(nahe(wert("vpip"), 0.25), "n = hands = 10 von prior 10");
|
||||
assert!(nahe(wert("aggression"), 0.5 * 30.0 / 40.0));
|
||||
assert!(nahe(wert("foldToBet"), 0.5 * 10.0 / 20.0), "Zaehler = Summe der drei FacingBet");
|
||||
assert!(nahe(wert("flopFoldToBet"), 0.5 * 5.0 / 15.0));
|
||||
assert!(nahe(wert("riverFoldToBet"), 0.5 * 2.0 / 12.0));
|
||||
assert!(nahe(wert("bekannt"), 0.05), "min(hands, 200) / 200");
|
||||
assert!(nahe(merkmale(&json!({"hands": 900}), &[]).last().unwrap().1, 1.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn modell_hat_vorrang_vor_der_typ_zeile_je_strasse() {
|
||||
let t = Tafel::lies("B 0.05\nT 3 2 99 0.25\nT 3 3 99 0.37\nM 3 99 0.0\n").unwrap();
|
||||
let a = fuer_tafel(t.clone(), &json!({"hands": 200, "vpip": 0.6}), 0.02).unwrap();
|
||||
assert!(nahe((a.fold)(3, 0.5).unwrap(), 0.5), "River: Modell");
|
||||
assert_eq!((a.fold)(2, 0.5), Some(0.25), "Turn: keine M-Zeile, die Typ-Zeile gilt");
|
||||
assert_eq!(a.kurz, "M");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn modell_band_waehlt_wie_die_tafel() {
|
||||
let t = Tafel::lies("B 0.05\nM 3 0.5 -1.0\nM 3 99 1.0\n").unwrap();
|
||||
let a = fuer_tafel(t, &json!({}), 0.02).unwrap();
|
||||
assert!(nahe((a.fold)(3, 0.5).unwrap(), sigmoid(-1.0) as f32));
|
||||
assert!(nahe((a.fold)(3, 0.51).unwrap(), sigmoid(1.0) as f32));
|
||||
assert!(nahe((a.fold)(3, 500.0).unwrap(), sigmoid(1.0) as f32));
|
||||
assert_eq!((a.fold)(2, 0.5), None, "Turn ohne Zeile");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unbekanntes_merkmal_verwirft_die_zeile() {
|
||||
let t = Tafel::lies("B 0.05\nT 3 3 99 0.37\nM 3 99 0.0 vpip=1 gibtesnicht=2\n").unwrap();
|
||||
assert!(t.modelle.is_empty(), "kein stilles Halb-Modell");
|
||||
let a = fuer_tafel(t, &json!({"hands": 200, "vpip": 0.6}), 0.02).unwrap();
|
||||
assert_eq!((a.kurz.as_str(), (a.fold)(3, 0.5)), ("T3", Some(0.37)));
|
||||
assert_eq!(Tafel::lies("B 0.05\nM 3 99 0.0 gibtesnicht=2\n"), None, "nur diese Zeile: nichts uebrig");
|
||||
assert_eq!(Tafel::lies("B 0.05\nM 3 99 0.0 vpip=x\n"), None, "unlesbare Zahl macht die Tafel ungueltig");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn modell_schluessel_gleich_bei_gleichen_foldraten() {
|
||||
let t = Tafel::lies("B 0.05\nP vpip 0.3 50\nM 2 99 0.0 vpip=4\nM 3 99 0.0 vpip=4\n").unwrap();
|
||||
let schl = |h: u64, v: f64| fuer_tafel(t.clone(), &json!({"hands": h, "vpip": v}), 0.02).unwrap().schluessel;
|
||||
assert_eq!(schl(100, 0.30), schl(300, 0.30), "gleiche Rate, anderes n: gleiche Foldraten, gleicher Schluessel");
|
||||
assert_ne!(schl(100, 0.30), schl(100, 0.60));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ziel_geht_dosiert_zur_messung() {
|
||||
assert_eq!(ziel(0.7, 0.3, 0.0), 0.7);
|
||||
|
||||
Reference in New Issue
Block a user