Mikro-Tisch NL 0.01/0.02 aus 857 Haenden gemessen: 5 % auf den Cent (half-even), Deckel 10 bb,
kein no-flop-no-drop (RakeRule::MICRO_BB). rake::live: thread-lokaler Scope, nur Cash-Spots unter
RAKE_LIVE=1; ohne Scope ist net() die Identitaet -> aus = bit-gleich zu 62e51bb (1767 Postflop-
und 2776 Preflop-Spots byte-gleich).
Gerakt: odds::ev, break_even/MDF, Bluff-Zweige und Bluff-Schranke, River-Baum v1, river_game,
turn_river_game, open_jam, Audit (Jam/All-in). Simulator: SIM_RAKE=mikro (TableCost::MICRO).
chartgen charts --rake mikro (ChartRake::Micro) fuer Stufe 2 (Preflop-Tabellen, noch nicht verdrahtet).
Nachspiel: 20/1767 Aktionen kippen, Audit +21.94 bb fuer das Rake-Spiel (ein Turn-Spot +21.72),
River -1.73. Bericht und Schaltvorschlag: messung_rake/BERICHT.md. Specs rake_rule 22, rake_live 7 gruen.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
401 lines
17 KiB
Rust
401 lines
17 KiB
Rust
//! Decision audit (plan D4) on preflop and flop: only where a price decides alone.
|
|
//!
|
|
//! There is no preflop or flop solver over real lines yet (plan E). What can be priced honestly:
|
|
//!
|
|
//! - **Preflop, facing an all-in** (open-jam, or a jam over hero's raise, heads-up with the jammer):
|
|
//! `EV(call) = equity x final pot - call`, `EV(fold) = 0`. The equity is against the jammer seat's
|
|
//! Nash PUSH range at the jam depth, clamped to the deepest frozen table (25 bb), exact per class
|
|
//! with card removal (`preflop::open_jam`): the widest range a jam of that size can rationally hold.
|
|
//! A raise against an all-in is priced as the call. Method `jam_preis`.
|
|
//! - **Preflop, first in at a short stack** (no raise and no limp before hero, effective stack within
|
|
//! the push/fold charts, at most 25 bb): push or fold in the Nash push/fold game the frozen charts
|
|
//! are solved from (`cfr::push_fold_game`, solved here), EV of both against the solved callers.
|
|
//! A first-in raise of at least half the stack counts as the push; a smaller open or a limp is not
|
|
//! a push/fold decision and is skipped. Method `push_fold`.
|
|
//! - **Flop, facing an all-in** heads-up (the villain has nothing behind, or the call puts hero all-in):
|
|
//! `EV(call) = equity x final pot - call` with the equity exactly enumerated over all turn and river
|
|
//! cards against the villain's range (blueprint where it knows the spot, else the range model; the
|
|
//! same lists as the river audit). Method `allin_equity`.
|
|
//!
|
|
//! Everything else on these streets is skipped (`Skip::NotCovered`) and counted by the report.
|
|
//! EVs are chips of the spot; only differences between actions mean anything.
|
|
|
|
use crate::card::Card;
|
|
use crate::cfr::chart_deck::ChartDeck;
|
|
use crate::cfr::environment::Environment;
|
|
use crate::cfr::hand_class::class_of;
|
|
use crate::cfr::push_fold_game::push_fold_tree;
|
|
use crate::cfr::river_game::Act;
|
|
use crate::cfr::solve::solve;
|
|
use crate::cfr::tree::Node;
|
|
use crate::engine::audit::{ranges, value, ActEv, Audit, RangeSource, Skip, Taken};
|
|
use crate::game::{SeenAction, Spot, Street};
|
|
use crate::hand::fast7;
|
|
use crate::preflop::first_raiser::{preflop_callers, raise_count};
|
|
use crate::preflop::generated_lookup::{has_charts, push_depth_range, seat_of};
|
|
use crate::preflop::open_jam::{depth_bb, equity_vs, jam_range, open_jam, price, OpenJam};
|
|
use crate::preflop::seat_position::seat_position;
|
|
use crate::rng::Rng;
|
|
use std::sync::OnceLock;
|
|
|
|
/// A stack at or below this is gone.
|
|
const EMPTY: f64 = 1e-9;
|
|
/// CFR iterations of the push/fold game (169 classes, a handful of nodes: milliseconds).
|
|
const PUSH_FOLD_ITERS: u32 = 4_000;
|
|
|
|
fn environment(deck: ChartDeck) -> &'static Environment {
|
|
static FULL: OnceLock<Environment> = OnceLock::new();
|
|
static SHORT: OnceLock<Environment> = OnceLock::new();
|
|
match deck {
|
|
ChartDeck::Full => FULL.get_or_init(|| Environment::frozen_for(ChartDeck::Full)),
|
|
ChartDeck::Short => SHORT.get_or_init(|| Environment::frozen_for(ChartDeck::Short)),
|
|
}
|
|
}
|
|
|
|
/// Chips `seat` has in on `street` (its largest street total in the history).
|
|
fn put_in(spot: &Spot, seat: u8, street: Street) -> f64 {
|
|
spot.history
|
|
.iter()
|
|
.filter(|r| r.street == street && r.seat == seat)
|
|
.filter_map(|r| r.amount)
|
|
.filter(|a| a.is_finite() && *a > 0.0)
|
|
.fold(0.0, f64::max)
|
|
}
|
|
|
|
/// A two-action audit (fold / continue) from the two EVs.
|
|
#[allow(clippy::too_many_arguments)]
|
|
fn two_way(fold_ev: f64, go: Act, go_ev: f64, taken_go: bool, source: RangeSource, method: &'static str, note: String, first: usize) -> Audit {
|
|
let best_go = go_ev >= fold_ev;
|
|
let acts = vec![
|
|
ActEv { act: Act::Fold, prob: if best_go { 0.0 } else { 1.0 }, ev: fold_ev },
|
|
ActEv { act: go, prob: if best_go { 1.0 } else { 0.0 }, ev: go_ev },
|
|
];
|
|
let taken = usize::from(taken_go);
|
|
let ev_best = fold_ev.max(go_ev);
|
|
let ev_taken = acts[taken].ev;
|
|
Audit {
|
|
source,
|
|
class: 0,
|
|
classes: 0,
|
|
nodes: 0,
|
|
first,
|
|
acts,
|
|
taken,
|
|
size_gap: 0.0,
|
|
ev_best,
|
|
ev_mix: ev_best,
|
|
ev_taken,
|
|
loss: (ev_best - ev_taken).max(0.0),
|
|
off_line: false,
|
|
hero_in_range: true,
|
|
exploitability: 0.0,
|
|
method,
|
|
note,
|
|
}
|
|
}
|
|
|
|
/// Hero continues against an all-in (call, or a raise priced as the call), or folds.
|
|
fn continues(taken: Taken, to_call: f64) -> Result<bool, Skip> {
|
|
match taken {
|
|
Taken::Fold => Ok(false),
|
|
Taken::Call | Taken::Aggress(_) => Ok(true),
|
|
Taken::Check if to_call > 0.0 => Err(Skip::Illegal("check facing a bet")),
|
|
Taken::Check => Err(Skip::Illegal("check facing an all-in")),
|
|
}
|
|
}
|
|
|
|
/// The last preflop raise or jam and its seat, when it left the raiser with nothing behind (or
|
|
/// committed: the raise is at least the stack kept).
|
|
fn facing_jam(spot: &Spot) -> Option<OpenJam> {
|
|
if let Some(j) = open_jam(spot) {
|
|
return preflop_callers(spot).is_empty().then_some(j);
|
|
}
|
|
let r = spot
|
|
.history
|
|
.iter()
|
|
.rev()
|
|
.find(|r| r.street == Street::Preflop && matches!(r.action, SeenAction::Raise | SeenAction::AllIn))?;
|
|
if r.seat == spot.hero_seat || spot.live_villains().count() != 1 {
|
|
return None;
|
|
}
|
|
let to = r.amount.filter(|a| a.is_finite() && *a > spot.big_blind)?;
|
|
let v = spot.villains.iter().find(|v| v.seat == r.seat)?;
|
|
let all_in = r.action == SeenAction::AllIn || v.stack <= EMPTY;
|
|
if !(all_in || to >= v.stack) || v.folded {
|
|
return None;
|
|
}
|
|
Some(OpenJam { seat: r.seat, raised_to: to, total: to + if all_in { 0.0 } else { v.stack } })
|
|
}
|
|
|
|
/// Audit a preflop decision: facing an all-in, or a first-in push/fold at a short stack.
|
|
pub fn audit_preflop(spot: &Spot, taken: Taken) -> Result<Audit, Skip> {
|
|
if spot.street != Street::Preflop {
|
|
return Err(Skip::Street(spot.street));
|
|
}
|
|
if !has_charts(spot.variant) || spot.hero_cards.len() != 2 {
|
|
return Err(Skip::NotCovered("preflop: no charts for this variant"));
|
|
}
|
|
if spot.to_call > 0.0 {
|
|
if let Some(jam) = facing_jam(spot) {
|
|
return price_jam(spot, taken, &jam);
|
|
}
|
|
}
|
|
if raise_count(spot) == 0 && preflop_callers(spot).is_empty() {
|
|
return push_or_fold(spot, taken);
|
|
}
|
|
Err(Skip::NotCovered("preflop: no all-in and no short-stack push/fold"))
|
|
}
|
|
|
|
fn price_jam(spot: &Spot, taken: Taken, jam: &OpenJam) -> Result<Audit, Skip> {
|
|
let go = continues(taken, spot.to_call)?;
|
|
let depth = depth_bb(spot, jam);
|
|
let (membership, width) = jam_range(spot, jam, depth).ok_or(Skip::NoRange("jammer seat has no push chart"))?;
|
|
let equity = equity_vs(spot, &membership).ok_or(Skip::NoRange("hero class"))?;
|
|
let (call, pot) = price(spot, jam);
|
|
if !(call > 0.0 && pot > 0.0) {
|
|
return Err(Skip::NotCovered("preflop: nothing to call"));
|
|
}
|
|
let table = depth.min(push_depth_range().1);
|
|
let behind = spot.live_villains().count() > 1;
|
|
let note = format!(
|
|
"equity {equity:.3} vs Nash push {} {table:.0}bb (top {:.0}%), price {:.3}{}",
|
|
seat_position(spot, jam.seat).label(),
|
|
100.0 * width,
|
|
call / pot,
|
|
if behind { "; players behind not modelled" } else { "" }
|
|
);
|
|
Ok(two_way(0.0, Act::Call, equity * crate::rake::live::net(pot) - call, go, RangeSource::Model, "jam_preis", note, 1))
|
|
}
|
|
|
|
fn push_or_fold(spot: &Spot, taken: Taken) -> Result<Audit, Skip> {
|
|
let seat = seat_of(seat_position(spot, spot.hero_seat)).ok_or(Skip::NotCovered("preflop: big blind is never first in"))?;
|
|
let hero_total = put_in(spot, spot.hero_seat, Street::Preflop) + spot.hero_stack.max(0.0);
|
|
let deepest = spot
|
|
.live_villains()
|
|
.map(|v| v.stack.max(0.0) + put_in(spot, v.seat, Street::Preflop))
|
|
.fold(0.0, f64::max);
|
|
let depth = hero_total.min(deepest) / spot.big_blind;
|
|
let (lo, hi) = push_depth_range();
|
|
if !(depth.is_finite() && depth <= hi) {
|
|
return Err(Skip::NotCovered("preflop: stack deeper than the push/fold charts"));
|
|
}
|
|
let push = match taken {
|
|
Taken::Fold => false,
|
|
Taken::Aggress(to) if to >= 0.5 * hero_total => true,
|
|
Taken::Check => return Err(Skip::Illegal("check first in")),
|
|
_ => return Err(Skip::NotCovered("preflop: short-stack open that is no jam")),
|
|
};
|
|
let deck = ChartDeck::of(spot.variant).ok_or(Skip::NotCovered("preflop: no charts for this variant"))?;
|
|
let env = environment(deck);
|
|
let class = class_of(spot.hero_cards[0], spot.hero_cards[1]).ok_or(Skip::NoRange("hero class"))?;
|
|
let tree = push_fold_tree(env, seat, depth.max(lo).max(1.5)).map_err(|_| Skip::NoRange("push/fold tree"))?;
|
|
let solved = solve(&tree, PUSH_FOLD_ITERS, Rng::seeded(spot.seed));
|
|
let n = tree.classes;
|
|
let w: Vec<f64> = (0..n).map(|j| tree.chance[class * n + j]).collect();
|
|
let mass: f64 = w.iter().sum();
|
|
if mass <= 0.0 {
|
|
return Err(Skip::NoRange("hero class has no deals"));
|
|
}
|
|
let kids = match &tree.nodes[tree.root] {
|
|
Node::Decision { children, .. } if children.len() == 2 => children.clone(),
|
|
_ => return Err(Skip::LineLeftTree),
|
|
};
|
|
let bb = spot.big_blind;
|
|
let fold_ev = value(&tree, &solved.strategy, kids[0], &w, class) / mass * bb;
|
|
let push_ev = value(&tree, &solved.strategy, kids[1], &w, class) / mass * bb;
|
|
let note = format!("Nash push/fold {} {depth:.1}bb (6-max model)", seat.label());
|
|
let mut a = two_way(fold_ev, Act::Raise(hero_total), push_ev, push, RangeSource::Model, "push_fold", note, 0);
|
|
a.exploitability = solved.final_exploitability * bb;
|
|
a.class = class;
|
|
a.classes = n;
|
|
a.nodes = tree.nodes.len();
|
|
Ok(a)
|
|
}
|
|
|
|
/// Hero's equity against a weighted list on `board`, every remaining card enumerated, card removal exact.
|
|
pub fn equity_vs_list(hero: &[Card], board: &[Card], list: &[(Vec<Card>, f64)]) -> Option<f64> {
|
|
let h = fast7::mask(hero);
|
|
let b = fast7::mask(board);
|
|
if hero.len() != 2 || h & b != 0 || board.len() > 5 || board.len() < 3 {
|
|
return None;
|
|
}
|
|
let missing = 5 - board.len();
|
|
let deck: Vec<u64> = (0..4u32).flat_map(|s| (0..13u32).map(move |r| 1u64 << (16 * s + r))).filter(|c| c & (h | b) == 0).collect();
|
|
let mut runs: Vec<u64> = Vec::new();
|
|
match missing {
|
|
0 => runs.push(0),
|
|
1 => runs.extend(deck.iter().copied()),
|
|
_ => {
|
|
for i in 0..deck.len() {
|
|
for j in i + 1..deck.len() {
|
|
runs.push(deck[i] | deck[j]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
let (mut won, mut total) = (0.0f64, 0.0f64);
|
|
for (c, w) in list {
|
|
if c.len() != 2 || !(*w > 0.0) {
|
|
continue;
|
|
}
|
|
let v = fast7::mask(c);
|
|
if v & (h | b) != 0 || v.count_ones() != 2 {
|
|
continue;
|
|
}
|
|
for r in &runs {
|
|
if r & v != 0 {
|
|
continue;
|
|
}
|
|
let (sh, sv) = (fast7::score(h | b | r), fast7::score(v | b | r));
|
|
won += w * if sh > sv { 1.0 } else if sh == sv { 0.5 } else { 0.0 };
|
|
total += w;
|
|
}
|
|
}
|
|
(total > 0.0).then(|| won / total)
|
|
}
|
|
|
|
/// Audit a flop decision: only heads-up facing an all-in.
|
|
pub fn audit_flop(spot: &Spot, taken: Taken) -> Result<Audit, Skip> {
|
|
if spot.street != Street::Flop {
|
|
return Err(Skip::Street(spot.street));
|
|
}
|
|
if spot.live_villains().count() != 1 {
|
|
return Err(Skip::Multiway);
|
|
}
|
|
let villain = spot.live_villains().next().ok_or(Skip::Multiway)?;
|
|
if !(spot.to_call > 0.0 && (villain.stack <= EMPTY || spot.to_call >= spot.hero_stack - EMPTY)) {
|
|
return Err(Skip::NotCovered("flop without all-in: no flop solver yet (plan E)"));
|
|
}
|
|
let go = continues(taken, spot.to_call)?;
|
|
let gated = crate::engine::read_gate::gate_reads(spot);
|
|
let (_, list, source) = ranges(&gated, villain).ok_or(Skip::NoRange("range lists"))?;
|
|
let equity = equity_vs_list(&spot.hero_cards, &spot.board, &list).ok_or(Skip::NoRange("equity"))?;
|
|
let call = spot.to_call.min(spot.hero_stack);
|
|
let refund = (spot.to_call - spot.hero_stack).max(0.0);
|
|
let pot = spot.pot - refund + call;
|
|
let note = format!("equity {equity:.3} vs {} villain range ({} combos), price {:.3}", source.name(), list.len(), call / pot);
|
|
Ok(two_way(0.0, Act::Call, equity * crate::rake::live::net(pot) - call, go, source, "allin_equity", note, 1))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::game::{ActionKind, ActionRecord, Villain};
|
|
use crate::variant::Variant;
|
|
|
|
fn rec(street: Street, seat: u8, action: SeenAction, amount: Option<f64>) -> ActionRecord {
|
|
ActionRecord { street, seat, action, amount }
|
|
}
|
|
|
|
/// Six-max, hero `hero` in seat 3 (UTG side), seat 1 open-jams `jam`, everybody else folded.
|
|
fn facing_open_jam(hero: &str, jam: f64, stack: f64) -> Spot {
|
|
Spot {
|
|
variant: Variant::Nlhe,
|
|
big_blind: 1.0,
|
|
seats: 6,
|
|
hero_seat: 2,
|
|
button_seat: Some(6),
|
|
hero_cards: Card::parse_list(hero).unwrap(),
|
|
board: Vec::new(),
|
|
street: Street::Preflop,
|
|
pot: jam + 1.5,
|
|
to_call: (jam - 1.0).min(stack),
|
|
hero_stack: stack,
|
|
min_raise_to: None,
|
|
max_raise_to: None,
|
|
legal: vec![ActionKind::Fold, ActionKind::Call],
|
|
allowed_sizes: None,
|
|
villains: vec![
|
|
Villain { seat: 1, stack: 0.5, folded: true, profile: None },
|
|
Villain { seat: 3, stack: 0.0, folded: false, profile: None },
|
|
Villain { seat: 4, stack: 100.0, folded: true, profile: None },
|
|
Villain { seat: 5, stack: 100.0, folded: true, profile: None },
|
|
Villain { seat: 6, stack: 100.0, folded: true, profile: None },
|
|
],
|
|
seed: 11,
|
|
history: vec![
|
|
rec(Street::Preflop, 1, SeenAction::PostSmallBlind, Some(0.5)),
|
|
rec(Street::Preflop, 2, SeenAction::PostBigBlind, Some(1.0)),
|
|
rec(Street::Preflop, 3, SeenAction::AllIn, Some(jam)),
|
|
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::Fold, None),
|
|
],
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn aces_folding_to_a_jam_lose_and_calling_loses_nothing() {
|
|
let spot = facing_open_jam("Ac Ad", 20.0, 99.0);
|
|
let fold = audit_preflop(&spot, Taken::Fold).expect("priced");
|
|
let call = audit_preflop(&spot, Taken::Call).expect("priced");
|
|
assert_eq!(fold.method, "jam_preis");
|
|
assert!(fold.loss > 5.0, "folding aces to a 20 bb jam is expensive, got {}", fold.loss);
|
|
assert!(call.loss < 1e-9);
|
|
}
|
|
|
|
#[test]
|
|
fn calling_a_jam_with_trash_loses_folding_does_not() {
|
|
let spot = facing_open_jam("7c 2d", 20.0, 99.0);
|
|
let call = audit_preflop(&spot, Taken::Call).expect("priced");
|
|
assert!(call.loss > 3.0, "72o calling a 20 bb jam loses, got {}", call.loss);
|
|
assert!(audit_preflop(&spot, Taken::Fold).unwrap().loss < 1e-9);
|
|
}
|
|
|
|
fn first_in_sb(hero: &str, stack: f64) -> Spot {
|
|
Spot {
|
|
variant: Variant::Nlhe,
|
|
big_blind: 1.0,
|
|
seats: 2,
|
|
hero_seat: 1,
|
|
button_seat: Some(1),
|
|
hero_cards: Card::parse_list(hero).unwrap(),
|
|
board: Vec::new(),
|
|
street: Street::Preflop,
|
|
pot: 1.5,
|
|
to_call: 0.5,
|
|
hero_stack: stack - 0.5,
|
|
min_raise_to: Some(2.0),
|
|
max_raise_to: Some(stack),
|
|
legal: vec![ActionKind::Fold, ActionKind::Call, ActionKind::Raise],
|
|
allowed_sizes: None,
|
|
villains: vec![Villain { seat: 2, stack: stack - 1.0, folded: false, profile: None }],
|
|
seed: 3,
|
|
history: vec![
|
|
rec(Street::Preflop, 1, SeenAction::PostSmallBlind, Some(0.5)),
|
|
rec(Street::Preflop, 2, SeenAction::PostBigBlind, Some(1.0)),
|
|
],
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn short_stack_push_fold_prices_the_chart() {
|
|
let aces = first_in_sb("Ah As", 10.0);
|
|
assert!(audit_preflop(&aces, Taken::Fold).unwrap().loss > 1.0, "folding aces first in at 10 bb");
|
|
assert!(audit_preflop(&aces, Taken::Aggress(10.0)).unwrap().loss < 1e-9);
|
|
let open = audit_preflop(&aces, Taken::Aggress(2.0));
|
|
assert!(matches!(open, Err(Skip::NotCovered(_))), "a min-raise is no push/fold decision");
|
|
let deep = first_in_sb("Ah As", 100.0);
|
|
assert!(matches!(audit_preflop(&deep, Taken::Fold), Err(Skip::NotCovered(_))));
|
|
}
|
|
|
|
#[test]
|
|
fn exact_equity_of_a_made_hand_on_the_flop() {
|
|
let hero = Card::parse_list("Ac Ad").unwrap();
|
|
let board = Card::parse_list("Ah 7c 2d").unwrap();
|
|
let worse = vec![(Card::parse_list("Kc Kd").unwrap(), 1.0)];
|
|
let e = equity_vs_list(&hero, &board, &worse).unwrap();
|
|
assert!(e > 0.98 && e < 1.0, "set over an underpair, got {e}");
|
|
}
|
|
|
|
#[test]
|
|
fn the_flop_without_an_all_in_is_skipped() {
|
|
let mut s = first_in_sb("Ah As", 100.0);
|
|
s.street = Street::Flop;
|
|
s.board = Card::parse_list("Kh 7c 2d").unwrap();
|
|
s.to_call = 3.0;
|
|
assert!(matches!(audit_flop(&s, Taken::Call), Err(Skip::NotCovered(_))));
|
|
}
|
|
}
|