Konflikte: range_filter.rs (Bereichs-Cache um die neuen Staerke-Schnitte top_share/RERAISE_ZUG erweitert), engine_gegner.rs (zeile_und_entscheidung neben mit_profil). perf_probe liest LBR_PROFIL wie engine_lbr. Beweis (perf_probe lbr 0 8, Env wie reraise3-lbr-folder-an: MDF_UNREAD=0, TURN_SPIEL=grund, RERAISE_TIEFE=1, RERAISE_BLUFF=erst, LBR_PROFIL reraiseToRaise 0.12): Fingerabdruck 5a7c8900e8126cf6 ueber 16394 Engine-Antworten gleich, Dump byte-gleich. CPU 317.7 s -> 11.5 s (27.6x), 39.7 -> 1.44 CPU-s je Hand. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
234 lines
11 KiB
Rust
234 lines
11 KiB
Rust
//! The live engine as the opponent of LBR on scenario S1 (plan M4): the engine's exploitability
|
|
//! on flop, turn and river with the same yardstick as the blueprint's.
|
|
//!
|
|
//! At an S1 node the engine is asked (`engine::decide`) with a spot built from the tree: six seats as
|
|
//! in the simulator (button 1, small blind 2, big blind 3, the rest folded preflop), the preflop
|
|
//! line, the postflop line up to the node as `history`, the real pot including the folded small
|
|
//! blind. Its choice is mapped back onto the node's children (sizes pseudo-harmonically, as for real
|
|
//! hands in `ranges`). The engine is deterministic per spot seed, so the row is pure.
|
|
//!
|
|
//! Named approximations: the engine sees no read on LBR (profile `None`), so this is the engine's
|
|
//! default play; its own size menu is mapped onto the tree's (an off-tree size is rounded to a tree
|
|
//! size, which LBR then exploits as if the engine had used it).
|
|
|
|
use super::lbr::Gegner;
|
|
use super::mccfr::Knoten;
|
|
use super::ranges::harmonisch;
|
|
use super::s1::{Aktion, OPEN, S1, STACK};
|
|
use crate::card::Card;
|
|
use crate::engine::decide::decide;
|
|
use crate::game::{ActionKind, ActionRecord, Choice, SeenAction, Spot, Street, Villain};
|
|
use crate::hand::fast7;
|
|
use crate::variant::Variant;
|
|
|
|
/// Seats of the S1 players in the built spot: player 0 = big blind, player 1 = button.
|
|
const SITZ: [u8; 2] = [3, 1];
|
|
const SB_TOT: f64 = 0.5;
|
|
|
|
pub struct EngineGegner {
|
|
deck: Vec<Card>,
|
|
/// The read the engine holds on its LBR opponent (plan D1b, Messregel 1a "LBR-Anstieg"): `None`
|
|
/// (default) = no read, the M4 baseline. Set from a wire-form profile with [`EngineGegner::mit_profil`].
|
|
profil: Option<crate::exploit::profile::Profile>,
|
|
}
|
|
|
|
impl EngineGegner {
|
|
/// The engine with a read on its opponent, decoded exactly as a live request's
|
|
/// `villains[].profile` (`wire::decode_villain`: shrink over `hands`, aggression over
|
|
/// `aggressionActions`, per-street reads). `json` is that profile object, e.g.
|
|
/// `{"vpip":0.3,"pfr":0.2,"foldToBet":0.5,"hands":23}`. A broken object is an error, never a silent
|
|
/// unread run.
|
|
pub fn mit_profil(json: &str) -> Result<Self, crate::fault::Fault> {
|
|
let text = format!("{{\"villains\":[{{\"seat\":1,\"stack\":100,\"folded\":false,\"profile\":{json}}}]}}");
|
|
let root = crate::json::parse::parse(&text)?;
|
|
let v = crate::wire::decode_villain::villains(&root)?;
|
|
Ok(EngineGegner { profil: v.into_iter().next().and_then(|v| v.profile), ..EngineGegner::default() })
|
|
}
|
|
|
|
/// The decoded read the engine plays with (`None` = unread).
|
|
pub fn profil(&self) -> Option<&crate::exploit::profile::Profile> {
|
|
self.profil.as_ref()
|
|
}
|
|
}
|
|
|
|
impl Default for EngineGegner {
|
|
fn default() -> Self {
|
|
EngineGegner {
|
|
profil: None,
|
|
deck: Card::parse_list("2c 3c 4c 5c 6c 7c 8c 9c Tc Jc Qc Kc Ac 2d 3d 4d 5d 6d 7d 8d 9d Td Jd Qd Kd Ad 2h 3h 4h 5h 6h 7h 8h 9h Th Jh Qh Kh Ah 2s 3s 4s 5s 6s 7s 8s 9s Ts Js Qs Ks As").unwrap(),
|
|
}
|
|
}
|
|
}
|
|
|
|
const STRASSEN: [Street; 3] = [Street::Flop, Street::Turn, Street::River];
|
|
|
|
impl EngineGegner {
|
|
fn karten(&self, m: u64) -> Vec<Card> {
|
|
self.deck.iter().copied().filter(|c| fast7::bit(*c) & m != 0).collect()
|
|
}
|
|
|
|
/// The spot of S1 node `k` for the acting player holding `hand`; also the contributions at the
|
|
/// start of the node's street.
|
|
pub fn spot(&self, s: &S1, k: usize, hand: u64, board: u64) -> Option<(Spot, f64)> {
|
|
assert_eq!(s.sz.id, "s1", "the engine opponent is built for scenario S1 only");
|
|
let Knoten::Zug { spieler: p, strasse, kinder } = &s.baum.baum.knoten[k] else { return None };
|
|
let (p, strasse) = (*p, *strasse);
|
|
// Path root -> k.
|
|
let mut pfad = vec![k];
|
|
while let Some(&e) = pfad.last().map(|x| &s.baum.eltern[*x]) {
|
|
if e == usize::MAX {
|
|
break;
|
|
}
|
|
pfad.push(e);
|
|
}
|
|
pfad.reverse();
|
|
let rec = |street, seat, action, amount| ActionRecord { street, seat, action, amount };
|
|
let mut history = vec![
|
|
rec(Street::Preflop, 2, SeenAction::PostSmallBlind, Some(SB_TOT)),
|
|
rec(Street::Preflop, 3, SeenAction::PostBigBlind, Some(1.0)),
|
|
rec(Street::Preflop, 4, SeenAction::Fold, None),
|
|
rec(Street::Preflop, 5, SeenAction::Fold, None),
|
|
rec(Street::Preflop, 6, SeenAction::Fold, None),
|
|
rec(Street::Preflop, 1, SeenAction::Raise, Some(OPEN)),
|
|
rec(Street::Preflop, 2, SeenAction::Fold, None),
|
|
rec(Street::Preflop, 3, SeenAction::Call, Some(OPEN)),
|
|
];
|
|
let mut start = OPEN;
|
|
let mut start_strasse = 0;
|
|
for w in pfad.windows(2) {
|
|
let (q, child) = (w[0], w[1]);
|
|
let Knoten::Zug { spieler: a, strasse: st, .. } = &s.baum.baum.knoten[q] else { return None };
|
|
if *st != start_strasse {
|
|
start_strasse = *st;
|
|
start = s.baum.beitrag[q][0];
|
|
}
|
|
let street_total = s.baum.beitrag[child][*a] - start;
|
|
let (action, amount) = match s.baum.aktion[child]? {
|
|
Aktion::Check => (SeenAction::Check, None),
|
|
Aktion::Call => (SeenAction::Call, Some(street_total)),
|
|
Aktion::Bet(_) => (SeenAction::Bet, Some(street_total)),
|
|
Aktion::Raise(_) => (SeenAction::Raise, Some(street_total)),
|
|
Aktion::Fold => (SeenAction::Fold, None),
|
|
};
|
|
history.push(rec(STRASSEN[*st], SITZ[*a], action, amount));
|
|
}
|
|
if strasse != start_strasse {
|
|
start = s.baum.beitrag[k][0];
|
|
}
|
|
let c = s.baum.beitrag[k];
|
|
let o = 1 - p;
|
|
let to_call = c[o] - c[p];
|
|
let raises = kinder.iter().any(|x| matches!(s.baum.aktion[*x], Some(Aktion::Bet(_) | Aktion::Raise(_))));
|
|
let mut legal = if to_call > 0.0 { vec![ActionKind::Fold, ActionKind::Call] } else { vec![ActionKind::Check] };
|
|
if raises {
|
|
legal.push(ActionKind::Raise);
|
|
}
|
|
let (mine_street, theirs_street) = (c[p] - start, c[o] - start);
|
|
let min_raise_to = theirs_street + to_call.max(1.0);
|
|
let max_raise_to = mine_street + (STACK - c[p]);
|
|
let villains = (1..=6u8)
|
|
.filter(|x| *x != SITZ[p])
|
|
.map(|x| Villain { seat: x, stack: if x == SITZ[o] { STACK - c[o] } else { STACK }, folded: x != SITZ[o], profile: if x == SITZ[o] { self.profil } else { None } })
|
|
.collect();
|
|
let spot = Spot {
|
|
variant: Variant::Nlhe,
|
|
big_blind: 1.0,
|
|
seats: 6,
|
|
hero_seat: SITZ[p],
|
|
button_seat: Some(1),
|
|
hero_cards: self.karten(hand),
|
|
board: self.karten(board),
|
|
street: STRASSEN[strasse],
|
|
pot: c[0] + c[1] + SB_TOT,
|
|
to_call,
|
|
hero_stack: STACK - c[p],
|
|
min_raise_to: raises.then_some(min_raise_to.min(max_raise_to)),
|
|
max_raise_to: raises.then_some(max_raise_to),
|
|
legal,
|
|
allowed_sizes: None,
|
|
villains,
|
|
seed: crate::rng::split_mix::Rng::seeded(hand ^ (k as u64) << 52).next_u64().0,
|
|
history,
|
|
};
|
|
Some((spot, start))
|
|
}
|
|
|
|
/// The child of node `k` the engine's choice maps to.
|
|
pub fn kind(&self, s: &S1, k: usize, choice: Choice, start: f64) -> Option<usize> {
|
|
let Knoten::Zug { spieler: p, kinder, .. } = &s.baum.baum.knoten[k] else { return None };
|
|
let finde = |a: Aktion| kinder.iter().copied().find(|c| s.baum.aktion[*c] == Some(a));
|
|
let c = s.baum.beitrag[k];
|
|
let passiv = finde(Aktion::Check).or_else(|| finde(Aktion::Call));
|
|
match choice {
|
|
Choice::Fold => finde(Aktion::Fold).or(passiv),
|
|
Choice::Check | Choice::Call | Choice::Abstain => passiv,
|
|
Choice::Raise { to } => {
|
|
let pot = c[0] + c[1];
|
|
let all_in = start + to >= STACK - 1e-6;
|
|
let kand: Vec<(f64, usize)> = kinder
|
|
.iter()
|
|
.filter_map(|x| match s.baum.aktion[*x] {
|
|
Some(Aktion::Bet(t) | Aktion::Raise(t)) => Some((t, *x)),
|
|
_ => None,
|
|
})
|
|
.collect();
|
|
if all_in {
|
|
if let Some((_, x)) = kand.iter().find(|(t, _)| *t >= STACK - 1e-9) {
|
|
return Some(*x);
|
|
}
|
|
}
|
|
let frac: Vec<(f64, usize)> = kand.iter().map(|(t, x)| ((t - c[*p]) / pot, *x)).collect();
|
|
// The real pot carries the dead small blind; the fraction is taken against it.
|
|
harmonisch((start + to - c[*p]) / (pot + SB_TOT), &frac).or(passiv)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
impl EngineGegner {
|
|
/// The row of `Gegner::zeile` and the engine's full decision behind it (`None` off the tree).
|
|
pub fn zeile_und_entscheidung(&self, s: &S1, k: usize, hand: u64, board: u64) -> (Vec<f64>, Option<crate::game::Decision>) {
|
|
let Knoten::Zug { kinder, .. } = &s.baum.baum.knoten[k] else { return (Vec::new(), None) };
|
|
let mut row = vec![0.0; kinder.len()];
|
|
let mut d = None;
|
|
let pick = self.spot(s, k, hand, board).and_then(|(spot, start)| {
|
|
let e = decide(&spot);
|
|
let c = self.kind(s, k, e.choice, start);
|
|
d = Some(e);
|
|
c
|
|
});
|
|
let i = pick.and_then(|c| kinder.iter().position(|x| *x == c)).unwrap_or(0);
|
|
row[i] = 1.0;
|
|
(row, d)
|
|
}
|
|
}
|
|
|
|
impl Gegner for EngineGegner {
|
|
fn zeile(&self, s: &S1, k: usize, hand: u64, board: u64, _: usize) -> Vec<f64> {
|
|
self.zeile_und_entscheidung(s, k, hand, board).0
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::blueprint::abstraktion::river_cuts;
|
|
use crate::blueprint::lbr::lbr_gegen;
|
|
use crate::blueprint::s1::Abstraktion;
|
|
|
|
#[test]
|
|
fn the_engine_answers_on_s1_nodes_and_lbr_runs_against_it() {
|
|
let s = S1::neu(Abstraktion { flop: None, turn: None, river: river_cuts(50, 1) });
|
|
let g = EngineGegner::default();
|
|
let m = |t: &str| fast7::mask(&Card::parse_list(t).unwrap());
|
|
// Root: big blind first on the flop.
|
|
let (spot, _) = g.spot(&s, s.baum.baum.wurzel, m("As Ad"), m("Ah 7d 2c")).unwrap();
|
|
assert_eq!((spot.hero_seat, spot.pot, spot.to_call), (3, 5.5, 0.0));
|
|
let row = g.zeile(&s, s.baum.baum.wurzel, m("As Ad"), m("Ah 7d 2c"), 0);
|
|
assert_eq!(row.iter().sum::<f64>(), 1.0);
|
|
let t = std::time::Instant::now();
|
|
let e = lbr_gegen(&s, &g, 2, 1, 5);
|
|
eprintln!("LBR vs engine (2 hands, exact): {:+.0} bb/100, {:?} per hand", e.bb100, t.elapsed() / 2);
|
|
}
|
|
}
|