Files
pokerfreak/src/preflop/open_jam.rs
T

235 lines
11 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.
//! An all-in OPEN (open-jam) is not an open: it is answered by calling against a jam range.
//!
//! **Leak (Micro run 4, 14.11., hand 177615800656, −89.5 BB).** Five-handed NL 0.01/0.02, the
//! cutoff open-jammed 1.79 (≈ 90 bb), hero A7o on the button with 1.98 behind. Hero covered the
//! jammer, so the chip heuristic (`classify`) did not see an all-in, the history showed one raise,
//! and `situation_of` read "vs open": the CFR `BTN vs open` table (built for a 2.5 bb open) called
//! 99 %. Against a 90 bb jam that table means nothing — there is no postflop, no fold equity, only
//! the price and the equity against the jamming range.
//!
//! **Model.** An open-jam is the only preflop raise, made all-in (`ALL_IN` in the history, or the
//! raiser's stack is 0 afterwards), or committing: the raise is at least the stack the raiser keeps
//! behind (half its stack or more), which it cannot fold any more. Then
//!
//! * the situation is `FacingAllIn` (`situation_of`), and the depth is the jam, not the deepest
//! stack at the table (`depth_bb`): `min(jammer's whole stack, hero's whole stack)`;
//! * where the frozen Nash tables cover it (small or big blind, jam ≤ 25 bb: `call_vs_push`), the
//! equilibrium answers, as for any jam;
//! * everywhere else `call_vs_jam`: hero calls iff its equity against the jammer's range reaches
//! the price `call / final pot`. The range is the jammer seat's Nash PUSH range from the frozen
//! push/fold tables at the jam depth, clamped to the deepest table (25 bb). The Nash push range
//! narrows as stacks deepen, so for a deeper jam the 25 bb range is the WIDEST range a jam of
//! that size can rationally hold: calling only what beats it never calls on the hope of a bluff
//! a rational jammer does not have. Equity is exact per class with card removal
//! (`cfr::wake_range::equity_against`, the frozen class-vs-class matrix).
//!
//! Not modelled (stated, not hidden): players still to act behind hero (they can overcall). It only
//! makes a call worse, so the rule errs towards calling. The rake is priced under `RAKE_LIVE`
//! (`rake::live`: the price is `call / net(final pot)`); off, the raw price as before.
//!
//! Switch `OPEN_JAM=1` (read once; `set_open_jam` for tests and ladder arms). Default off: the old
//! behaviour, until the owner's switch point (`betrieb/kern_schalter.txt`).
use super::first_raiser::{preflop_callers, sole_raiser};
use super::generated_lookup::{has_charts, push_frequency, seat_of};
use super::hand_percentile::percentile;
use super::plan::{ChartAction, PreflopPlan};
use super::seat_position::seat_position;
use crate::cfr::chart_deck::ChartDeck;
use crate::cfr::environment::Environment;
use crate::cfr::hand_class::{class_combos, class_of, CLASS_COUNT};
use crate::cfr::wake_range::equity_against;
use crate::game::{SeenAction, Spot, Street};
use crate::variant::ActionShape;
use std::sync::atomic::{AtomicU8, AtomicUsize, Ordering};
use std::sync::OnceLock;
/// Switch state: 0 = not set (read `OPEN_JAM` from the environment), 1 = off, 2 = on.
static SWITCH: AtomicU8 = AtomicU8::new(0);
/// Force the open-jam answer on or off (tests, ladder arms). Overrides `OPEN_JAM`.
pub fn set_open_jam(on: bool) {
SWITCH.store(if on { 2 } else { 1 }, Ordering::Relaxed);
}
/// `OPEN_JAM=1` (or `set_open_jam(true)`). Default off.
pub fn open_jam_on() -> bool {
match SWITCH.load(Ordering::Relaxed) {
1 => false,
2 => true,
_ => {
static ENV: OnceLock<bool> = OnceLock::new();
*ENV.get_or_init(|| std::env::var("OPEN_JAM").as_deref() == Ok("1"))
}
}
}
/// Preflop decisions the open-jam rule took over (switch on): the ladder reports it, so an arm
/// that never met an open-jam is known to be identical to the base arm deal by deal.
static TAKEN: AtomicUsize = AtomicUsize::new(0);
/// How often `situation_of` read an open-jam since the start of the process (switch on).
pub fn taken() -> usize {
TAKEN.load(Ordering::Relaxed)
}
/// Count one decision taken over (`situation_of`).
pub fn note_taken() {
TAKEN.fetch_add(1, Ordering::Relaxed);
}
/// Smaller than any chip: a stack at or below this is gone.
const EMPTY: f64 = 1e-9;
/// The jam hero faces.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct OpenJam {
/// Seat of the jammer.
pub seat: u8,
/// What the jammer has in now (raise-to).
pub raised_to: f64,
/// The jammer's whole stack: what it has in plus what it keeps behind.
pub total: f64,
}
/// The open-jam hero faces preflop, `None` for anything else (no raise, several raises, a normal
/// open, a raise of a blind or less, unknown amounts).
pub fn open_jam(spot: &Spot) -> Option<OpenJam> {
if spot.street != Street::Preflop {
return None;
}
let seat = sole_raiser(spot)?;
if seat == spot.hero_seat {
return None;
}
let record = spot
.history
.iter()
.rev()
.find(|r| r.street == Street::Preflop && r.seat == seat && matches!(r.action, SeenAction::Raise | SeenAction::AllIn))?;
let raised_to = record.amount.filter(|a| a.is_finite())?;
if !(spot.big_blind.is_finite() && spot.big_blind > 0.0 && raised_to > spot.big_blind) {
return None;
}
let behind = spot.villains.iter().find(|v| v.seat == seat).map(|v| v.stack).filter(|s| s.is_finite() && *s >= 0.0);
let all_in = record.action == SeenAction::AllIn || behind.is_some_and(|s| s <= EMPTY);
let committed = behind.is_some_and(|s| raised_to >= s);
if !(all_in || committed) {
return None;
}
let total = raised_to + if all_in { 0.0 } else { behind.unwrap_or(0.0) };
Some(OpenJam { seat, raised_to, total })
}
/// Hero's chips in this street already (its blind).
fn hero_in(spot: &Spot) -> f64 {
spot.history
.iter()
.filter(|r| r.street == Street::Preflop && r.seat == spot.hero_seat)
.filter_map(|r| r.amount)
.filter(|a| a.is_finite() && *a > 0.0)
.fold(0.0, f64::max)
}
/// Hero's whole stack this hand: what it has in plus what it has behind.
fn hero_total(spot: &Spot) -> f64 {
hero_in(spot) + spot.hero_stack.max(0.0)
}
/// The stakes of the jam: what both can lose, `min(jammer total, hero total)`.
fn stake(spot: &Spot, jam: &OpenJam) -> f64 {
jam.total.min(hero_total(spot))
}
/// Depth of the jam in big blinds (the effective stack between hero and the jammer).
pub fn depth_bb(spot: &Spot, jam: &OpenJam) -> f64 {
stake(spot, jam) / spot.big_blind
}
/// `(hero's call, the pot after it)`: hero pays up to the stake; chips of the jam above hero's
/// stack go back, chips a committed jammer still has behind go in.
pub fn price(spot: &Spot, jam: &OpenJam) -> (f64, f64) {
let stake = stake(spot, jam);
let call = (stake - hero_in(spot)).max(0.0);
// VORFLOP_FIX=openjam: der Topf, wie der Client ihn schickt. Mit UEBERSCHUSS=1 ist der ungedeckte Teil des Jams
// schon aus `spot.pot` genommen; `stake - raised_to` zoege ihn ein zweites Mal ab (AA foldete 111 bb, Preis 0,97).
let pot = if super::vorflop_fix::openjam_on() { crate::engine::ueberschuss::ungekuerzt(spot) } else { spot.pot };
(call, pot + (stake - jam.raised_to) + call)
}
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)),
}
}
/// The jammer's Nash push range at `depth` bb (membership per class), and its combo share.
pub fn jam_range(spot: &Spot, jam: &OpenJam, depth: f64) -> Option<(Vec<f64>, f64)> {
let deck = ChartDeck::of(spot.variant)?;
let seat = seat_of(seat_position(spot, jam.seat))?;
let env = environment(deck);
let membership: Vec<f64> =
(0..CLASS_COUNT).map(|c| push_frequency(spot.variant, seat, depth, c).map_or(0.0, |f| f.raise)).collect();
let (kept, all) = (0..CLASS_COUNT).fold((0.0, 0.0), |(k, a), c| {
let n = env.chance.combos(c) as f64;
(k + n * membership[c], a + n)
});
(kept > 0.0).then(|| (membership, kept / all))
}
/// Hero's equity against the membership (exact per class, card removal included).
pub fn equity_vs(spot: &Spot, membership: &[f64]) -> Option<f64> {
let deck = ChartDeck::of(spot.variant)?;
let class = match spot.hero_cards.as_slice() {
[a, b] => class_of(*a, *b)?,
_ => return None,
};
equity_against(environment(deck), membership).get(class).copied()
}
/// Certainty from the equity margin: 10 points of equity away from the price is certain.
const SURE_MARGIN: f64 = 0.10;
/// Call (jam in jam-or-fold games) or fold against an open-jam, by equity against the jammer's Nash
/// push range; `margin` is equity the call needs on top of the price (tournament premium; 0 in
/// cash). `None` where it does not apply: no open-jam, callers in between, no tables for the
/// variant or the jammer's seat, not a two-card hand, nothing to call.
pub fn call_vs_jam(spot: &Spot, margin: f64) -> Option<PreflopPlan> {
let jam = open_jam(spot)?;
if !has_charts(spot.variant) || !preflop_callers(spot).is_empty() {
return None;
}
let depth = depth_bb(spot, &jam);
let (membership, width) = jam_range(spot, &jam, depth)?;
let equity = equity_vs(spot, &membership)?;
let (call, pot) = price(spot, &jam);
if !(call > 0.0 && pot > 0.0) {
return None;
}
// The pot hero takes home: net of the room's rake under RAKE_LIVE (`rake::live`), raw otherwise.
let need = call / crate::rake::live::net(pot) + margin.max(0.0);
let continue_ = if spot.rules().shape == ActionShape::Binary { ChartAction::Jam } else { ChartAction::Call };
let action = if equity >= need { continue_ } else { ChartAction::Fold };
let verb = if action == ChartAction::Fold { "fold" } else { "call" };
let pos = seat_position(spot, spot.hero_seat).label();
let from = seat_position(spot, jam.seat).label();
let table = depth.min(super::generated_lookup::push_depth_range().1);
Some(PreflopPlan {
action,
percentile: percentile(spot.variant, &spot.hero_cards),
certainty: ((equity - need).abs() / SURE_MARGIN).clamp(0.0, 1.0),
reason: format!(
"{pos} vs open-jam {depth:.0}bb from {from}: equity {equity:.3} vs Nash push {table:.0}bb (top {:.0}%), price {need:.3}: {verb}",
100.0 * width
),
})
}
/// One combo of each class that misses `dead` (for measurements over all hero hands).
pub fn class_representative(class: usize, dead: &[crate::card::Card]) -> Option<[crate::card::Card; 2]> {
class_combos(class).into_iter().find(|[a, b]| !dead.contains(a) && !dead.contains(b))
}