Files
pokerfreak/src/bin/engine_lbr.rs
T
mmintelandClaude Opus 5.5 c3e2726cd0 Solver-Kern Stufe 4b: Auflagen Multiway (frische Scheibe +0,76 bb/100, Top-Paar-Flop-Fold-Sperre, Drilling/Monster Flop/Turn ausgeschlossen)
SOLVER3_FOHNE, SOLVER3_TOHNE, SOLVER3_FSPERRE (aus, live bitgleich); drei.py DREI_FENSTER; drei_kipp.py Mischarme.
Pflicht 39/44, Latenz max 1215 ms, Faustregel 97->78. Bericht messung/solver_kern/STUFE4B.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 17:11:34 +01:00

116 lines
6.3 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! LBR against the live engine on scenario S1 (plan M4, Messregel 1a): how much the best local counter
//! wins against the engine's default play, flop to river, button vs big blind in a single-raised pot.
//!
//! Exact range tracking (subsampling was measured biased: k=48 −112, k=96 −35 bb/100 against the exact
//! value on the real blueprint, `lbr::tests::bias_on_the_real_blueprint`). One hand costs ~27 s, so the
//! run is resumable: hand `i` is deterministic (`lbr::lbr_hand`), every result is appended to the out
//! file as `i value_bb` at once, and a restart computes only the missing hands.
//!
//! Run: `engine_lbr <out-file> <hands> [seed]`, threads from `POKERBENCH_THREADS` (default 1).
//! The engine runs with its default switches; the one env arm is `TURN_SPIEL=grund` (turn game, E3).
use rechner::blueprint::abstraktion::river_cuts;
use rechner::blueprint::engine_gegner::EngineGegner;
use rechner::blueprint::lbr::lbr_hand;
use rechner::blueprint::s1::{Abstraktion, S1};
use std::io::Write;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Mutex;
fn main() {
let args: Vec<String> = std::env::args().collect();
if args.len() < 3 {
eprintln!("engine_lbr <out-file> <hands> [seed]");
std::process::exit(2);
}
let out = std::path::PathBuf::from(&args[1]);
let haende: u64 = args[2].parse().expect("hands");
let seed: u64 = args.get(3).map(|x| x.parse().expect("seed")).unwrap_or(7);
let threads: usize = std::env::var("POKERBENCH_THREADS").ok().and_then(|x| x.parse().ok()).unwrap_or(1).max(1);
// TURN_SPIEL=grund (plan E3): the turn+river game with blueprint ranges plays the engine's turn
// where the blueprint recognises the spot (needs GRUNDSTRATEGIE: tables and checkpoint).
if std::env::var("TURN_SPIEL").as_deref() == Ok("grund") {
rechner::engine::turn_game::set_turn_player(rechner::engine::turn_game::TurnPlayer::Grund);
assert!(rechner::blueprint::ranges::grundstrategie().is_some(), "TURN_SPIEL=grund needs GRUNDSTRATEGIE with tables and checkpoint");
}
// SOLVER_RIVER=1|modell (stage A): the river solver plays the engine's heads-up river.
rechner::engine::river_spieler::set_river_spieler(rechner::engine::river_spieler::from_env());
rechner::engine::turn_game::set_turn_ueberall(rechner::engine::turn_game::turn_ueberall_from_env());
rechner::engine::flop_spieler::set_flop_spieler(rechner::engine::flop_spieler::from_env());
rechner::engine::river3_spieler::set_river3(rechner::engine::river3_spieler::from_env());
let (turn3, flop3) = rechner::engine::multi3_spieler::from_env();
rechner::engine::multi3_spieler::set_turn3(turn3);
rechner::engine::multi3_spieler::set_flop3(flop3);
let (nur_offen, ohne) = rechner::engine::multi3_spieler::lage_from_env();
rechner::engine::multi3_spieler::set_lage(nur_offen, ohne);
rechner::engine::multi3_spieler::ohne_from_env();
if rechner::engine::river_spieler::river_spieler() == rechner::engine::river_spieler::RiverSpieler::An {
assert!(rechner::blueprint::ranges::grundstrategie().is_some(), "SOLVER_RIVER=1 on LBR needs GRUNDSTRATEGIE (else it measures the model path)");
}
let mut fertig = vec![None; haende as usize];
if let Ok(t) = std::fs::read_to_string(&out) {
for z in t.lines() {
let mut f = z.split_whitespace();
if let (Some(i), Some(v)) = (f.next().and_then(|x| x.parse::<usize>().ok()), f.next().and_then(|x| x.parse::<f64>().ok())) {
if i < fertig.len() {
fertig[i] = Some(v);
}
}
}
}
let offen: Vec<u64> = (0..haende).filter(|i| fertig[*i as usize].is_none()).collect();
eprintln!("{} of {haende} hands done, {} to go, {threads} threads", haende as usize - offen.len(), offen.len());
// The engine never sees the blueprint's classes, so the tables are not needed (flop/turn unclassed).
let s = S1::neu(Abstraktion { flop: None, turn: None, river: river_cuts(50, 1) });
// LBR_PROFIL (plan D1b): the engine holds this read on its LBR opponent, as a live request would
// carry it (`villains[].profile`, wire form). Unset = no read (the M4 baseline).
let g = match std::env::var("LBR_PROFIL") {
Ok(p) if !p.trim().is_empty() => {
let g = EngineGegner::mit_profil(&p).expect("LBR_PROFIL: wire profile");
eprintln!("engine read on LBR: {:?}", g.profil());
g
}
_ => EngineGegner::default(),
};
let next = AtomicUsize::new(0);
let datei = Mutex::new(std::fs::OpenOptions::new().create(true).append(true).open(&out).expect("out file"));
let werte = Mutex::new(fertig);
let t0 = std::time::Instant::now();
std::thread::scope(|sc| {
for _ in 0..threads {
sc.spawn(|| loop {
let j = next.fetch_add(1, Ordering::Relaxed);
let Some(&i) = offen.get(j) else { break };
let v = lbr_hand(&s, &g, i, seed, None);
writeln!(datei.lock().unwrap(), "{i} {v}").expect("write");
let mut w = werte.lock().unwrap();
w[i as usize] = Some(v);
let n = j + 1;
if n % 20 == 0 || n == offen.len() {
let (m, ci, k) = bericht(&w);
eprintln!("{n}/{} new, {k} total: LBR {m:+.1} ±{ci:.1} bb/100, {:.1} s/hand", offen.len(), t0.elapsed().as_secs_f64() * threads as f64 / n as f64);
}
});
}
});
let (m, ci, k) = bericht(&werte.lock().unwrap());
println!("LBR gegen Engine (S1, exakt): {m:+.1} ±{ci:.1} bb/100 ueber {k} Haende");
}
/// Mean over both seats and its 95 % half-width, in bb/100, over the hands done.
fn bericht(w: &[Option<f64>]) -> (f64, f64, usize) {
let mut t = [(0.0, 0.0, 0usize); 2];
for (i, v) in w.iter().enumerate() {
if let Some(v) = v {
t[i % 2].0 += v;
t[i % 2].1 += v * v;
t[i % 2].2 += 1;
}
}
let mean = |x: (f64, f64, usize)| x.0 / x.2.max(1) as f64;
let var = |x: (f64, f64, usize)| (x.1 / x.2.max(1) as f64 - mean(x).powi(2)) / x.2.max(1) as f64;
(50.0 * (mean(t[0]) + mean(t[1])), 98.0 * (var(t[0]) + var(t[1])).sqrt(), t[0].2 + t[1].2)
}