|
|
|
@@ -0,0 +1,231 @@
|
|
|
|
|
//! One belief about an opponent (`POSTERIOR`, off by default = bit-identical), PLAN §8.11 #4, 5, 8, 15, 16.
|
|
|
|
|
//!
|
|
|
|
|
//! Stage S1 (`POSTERIOR=fold`): P(fold) to a bet is the range mass below the continue boundary, the
|
|
|
|
|
//! same boundary and the same number that the called branch plays against (`exploit::call_floor`).
|
|
|
|
|
//! Today the fold chance is a formula without the board (`exploit::fold_chance`), and the call floor
|
|
|
|
|
//! takes its width from that formula without the street read (`table_fold` uses the street read),
|
|
|
|
|
//! so the fold branch and the called branch disagree.
|
|
|
|
|
//!
|
|
|
|
|
//! The read says HOW MUCH a villain folds (its average over boards), the board says WHERE: in logit,
|
|
|
|
|
//!
|
|
|
|
|
//! `logit P(fold) = logit f_read + w * (logit m_board - logit m_ref)`
|
|
|
|
|
//!
|
|
|
|
|
//! * `f_read`: the fold chance of the read at this pressure (`fold_chance_on` / the c-bet read), the
|
|
|
|
|
//! villain's average;
|
|
|
|
|
//! * `m_board`: the share of his preflop band (`range_model::range_spec` lo..hi, hero's cards and the
|
|
|
|
|
//! board removed) below the calling floor (`call_floor`: rung by street, size notch and style; draws
|
|
|
|
|
//! from a gutshot continue before the river) on THIS board;
|
|
|
|
|
//! * `m_ref`: the same share averaged over `REF_BOARDS` random boards of the same length, so the term
|
|
|
|
|
//! is zero on an average board and the read keeps its level;
|
|
|
|
|
//! * `w`: the board weight (`POSTERIOR=fold:<w>`, default 1).
|
|
|
|
|
//!
|
|
|
|
|
//! Only the preflop band enters `m_board`: the pool folds MORE on later streets (NL2 flop 0,51, turn
|
|
|
|
|
//! 0,55, river 0,63), so the narrowing of a calling range is not the dominant effect, and the read's
|
|
|
|
|
//! street level already carries it. Board texture is what the formula misses.
|
|
|
|
|
|
|
|
|
|
use crate::card::{deck, Card};
|
|
|
|
|
use crate::exploit::call_floor::CALL_RUNG;
|
|
|
|
|
use crate::exploit::fold_chance::{fold_base_cbet, fold_chance_on, sized_fold, MAX_FOLD};
|
|
|
|
|
use crate::exploit::made_ladder::rung;
|
|
|
|
|
use crate::exploit::range_model::range_spec;
|
|
|
|
|
use crate::exploit::size_notch::size_notch;
|
|
|
|
|
use crate::exploit::style_rung::style_rung;
|
|
|
|
|
use crate::game::{line, Spot, Street, Villain};
|
|
|
|
|
use crate::preflop::hand_percentile::percentile;
|
|
|
|
|
use crate::read::draw::Draw;
|
|
|
|
|
use crate::read::glance::glance;
|
|
|
|
|
use crate::read::made::Made;
|
|
|
|
|
use crate::rng::Rng;
|
|
|
|
|
use crate::variant::{Rules, Variant};
|
|
|
|
|
use std::cell::RefCell;
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
|
|
/// Random boards per reference share.
|
|
|
|
|
pub const REF_BOARDS: usize = 48;
|
|
|
|
|
const REF_SEED: u64 = 0x6_1A0B_E000;
|
|
|
|
|
/// Band edges are rounded to this step for the reference cache.
|
|
|
|
|
const BAND_STEP: f64 = 0.05;
|
|
|
|
|
/// Shares are kept inside `[EPS, 1 - EPS]` before the logit.
|
|
|
|
|
const EPS: f64 = 0.01;
|
|
|
|
|
|
|
|
|
|
/// The switch: which stages are on and the board weight of S1.
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
|
|
|
pub struct Mode {
|
|
|
|
|
pub fold: bool,
|
|
|
|
|
pub board_weight: f64,
|
|
|
|
|
/// `nurfold`: the board term only in the fold branch, the called branch keeps `call_floor`.
|
|
|
|
|
pub fold_only_branch: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
thread_local! {
|
|
|
|
|
static OVERRIDE: std::cell::Cell<Option<Option<Mode>>> = const { std::cell::Cell::new(None) };
|
|
|
|
|
static MASS: RefCell<HashMap<String, f64>> = RefCell::new(HashMap::new());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// `POSTERIOR=fold` or `fold:<w>` (comma list of stages; unknown stages are ignored). Else off.
|
|
|
|
|
pub fn mode() -> Option<Mode> {
|
|
|
|
|
use std::sync::OnceLock;
|
|
|
|
|
static M: OnceLock<Option<Mode>> = OnceLock::new();
|
|
|
|
|
OVERRIDE.with(|o| o.get()).unwrap_or_else(|| *M.get_or_init(|| std::env::var("POSTERIOR").ok().and_then(|s| parse(&s))))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse(s: &str) -> Option<Mode> {
|
|
|
|
|
let mut m = Mode { fold: false, board_weight: 1.0, fold_only_branch: false };
|
|
|
|
|
for t in s.split(',').map(str::trim) {
|
|
|
|
|
if t == "fold" {
|
|
|
|
|
m.fold = true;
|
|
|
|
|
} else if t == "nurfold" {
|
|
|
|
|
m.fold_only_branch = true;
|
|
|
|
|
} else if let Some(w) = t.strip_prefix("fold:").and_then(|w| w.parse::<f64>().ok()).filter(|w| w.is_finite()) {
|
|
|
|
|
m.fold = true;
|
|
|
|
|
m.board_weight = w;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
m.fold.then_some(m)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Force the switch for this thread (`Some(None)` = off), `None` = the environment. Tests.
|
|
|
|
|
pub fn set_override(mode: Option<Option<Mode>>) {
|
|
|
|
|
OVERRIDE.with(|o| o.set(mode));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Is S1 on?
|
|
|
|
|
pub fn fold_on() -> bool {
|
|
|
|
|
mode().is_some_and(|m| m.fold)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Does the called branch play against `1 - fold` (S1 without `nurfold`)?
|
|
|
|
|
pub fn called_on() -> bool {
|
|
|
|
|
mode().is_some_and(|m| m.fold && !m.fold_only_branch)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn logit(p: f64) -> f64 {
|
|
|
|
|
let p = p.clamp(EPS, 1.0 - EPS);
|
|
|
|
|
(p / (1.0 - p)).ln()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn sigmoid(x: f64) -> f64 {
|
|
|
|
|
1.0 / (1.0 + (-x).exp())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The calling floor of `villain` against pressure `ratio` on `street` (`call_floor::call_continuation`).
|
|
|
|
|
fn floor(villain: &Villain, street: Street, ratio: f64) -> (Made, bool) {
|
|
|
|
|
let slot = (street as usize).clamp(1, 3) - 1;
|
|
|
|
|
(rung(CALL_RUNG[slot], size_notch(Some(ratio)) + style_rung(villain)), street != Street::River)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Share of the two-card hands with preflop percentile in `[lo, hi]`, outside `dead`, that fall
|
|
|
|
|
/// below the floor on `board`. `None` for other hand sizes or an empty band.
|
|
|
|
|
fn below_share(rules: &Rules, variant: Variant, lo: f64, hi: f64, board: &[Card], dead: &[Card], made: Made, draws: bool) -> Option<f64> {
|
|
|
|
|
if rules.hole_cards != 2 {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
let pool = deck::without_in(rules.deck, dead);
|
|
|
|
|
let (mut all, mut below) = (0usize, 0usize);
|
|
|
|
|
for i in 0..pool.len() {
|
|
|
|
|
for j in i + 1..pool.len() {
|
|
|
|
|
let hand = [pool[i], pool[j]];
|
|
|
|
|
let p = percentile(variant, &hand);
|
|
|
|
|
if p < lo || p > hi {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
let Ok((m, d)) = glance(rules, &hand, board) else { continue };
|
|
|
|
|
all += 1;
|
|
|
|
|
if !(m >= made || (draws && d >= Draw::Gutshot)) {
|
|
|
|
|
below += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
(all > 0).then(|| below as f64 / all as f64)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn cached(key: String, f: impl FnOnce() -> Option<f64>) -> Option<f64> {
|
|
|
|
|
if let Some(x) = MASS.with(|m| m.borrow().get(&key).copied()) {
|
|
|
|
|
return Some(x);
|
|
|
|
|
}
|
|
|
|
|
let x = f()?;
|
|
|
|
|
MASS.with(|m| {
|
|
|
|
|
let mut m = m.borrow_mut();
|
|
|
|
|
if m.len() >= 4096 {
|
|
|
|
|
m.clear();
|
|
|
|
|
}
|
|
|
|
|
m.insert(key, x);
|
|
|
|
|
});
|
|
|
|
|
Some(x)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The board-average share below the floor for this band and street (random boards, fixed seed).
|
|
|
|
|
fn reference(rules: &Rules, variant: Variant, lo: f64, hi: f64, board_len: usize, made: Made, draws: bool) -> Option<f64> {
|
|
|
|
|
let key = format!("ref|{variant:?}|{lo:.2}|{hi:.2}|{board_len}|{made:?}|{draws}");
|
|
|
|
|
cached(key, || {
|
|
|
|
|
let full = deck::without_in(rules.deck, &[]);
|
|
|
|
|
let mut rng = Rng::seeded(REF_SEED ^ board_len as u64);
|
|
|
|
|
let mut sum = 0.0;
|
|
|
|
|
let mut n = 0usize;
|
|
|
|
|
for _ in 0..REF_BOARDS {
|
|
|
|
|
let (board, next) = deck::draw(&full, board_len, rng)?;
|
|
|
|
|
rng = next;
|
|
|
|
|
if let Some(x) = below_share(rules, variant, lo, hi, &board, &board, made, draws) {
|
|
|
|
|
sum += x;
|
|
|
|
|
n += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
(n > 0).then(|| sum / n as f64)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// P(`villain` folds) to pressure `ratio`, S1 (see the module text). Falls back to the read alone
|
|
|
|
|
/// preflop, without a usable band, or for other hand sizes.
|
|
|
|
|
pub fn fold(spot: &Spot, villain: &Villain, ratio: f64, facing_raise: bool, read_fold: f64) -> f64 {
|
|
|
|
|
let Some(m) = mode().filter(|m| m.fold) else { return read_fold };
|
|
|
|
|
if spot.street == Street::Preflop || spot.board.len() < 3 || !(0.0..1.0).contains(&read_fold) || read_fold <= 0.0 {
|
|
|
|
|
return read_fold;
|
|
|
|
|
}
|
|
|
|
|
let rules = spot.rules();
|
|
|
|
|
let spec = range_spec(spot, villain);
|
|
|
|
|
let round = |x: f64| ((x / BAND_STEP).round() * BAND_STEP).clamp(0.0, 1.0);
|
|
|
|
|
let (lo, hi) = (round(spec.lo), round(spec.hi));
|
|
|
|
|
if hi <= lo {
|
|
|
|
|
return read_fold;
|
|
|
|
|
}
|
|
|
|
|
let (made, draws) = floor(villain, spot.street, ratio);
|
|
|
|
|
let dead: Vec<Card> = spot.hero_cards.iter().chain(&spot.board).copied().collect();
|
|
|
|
|
let key = format!("brett|{:?}|{lo:.2}|{hi:.2}|{:?}|{:?}|{made:?}|{draws}", spot.variant, spot.board, spot.hero_cards);
|
|
|
|
|
let board = cached(key, || below_share(&rules, spot.variant, lo, hi, &spot.board, &dead, made, draws));
|
|
|
|
|
let reference = reference(&rules, spot.variant, lo, hi, spot.board.len(), made, draws);
|
|
|
|
|
let _ = facing_raise;
|
|
|
|
|
match (board, reference) {
|
|
|
|
|
(Some(b), Some(r)) => sigmoid(logit(read_fold) + m.board_weight * (logit(b) - logit(r))).clamp(0.0, MAX_FOLD),
|
|
|
|
|
_ => read_fold,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The read's fold chance as `table_fold` prices it (street read, c-bet read on a c-bet), then S1.
|
|
|
|
|
pub fn table_fold_of(spot: &Spot, villain: &Villain, ratio: f64, facing_raise: bool) -> f64 {
|
|
|
|
|
let p = villain.profile.as_ref();
|
|
|
|
|
let read = if line::cbet_spot(&spot.history, spot.hero_seat, spot.street) {
|
|
|
|
|
sized_fold(fold_base_cbet(p), ratio, facing_raise)
|
|
|
|
|
} else {
|
|
|
|
|
fold_chance_on(p, ratio, facing_raise, Some(spot.street))
|
|
|
|
|
};
|
|
|
|
|
fold(spot, villain, ratio, facing_raise, read)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn parses_the_stages() {
|
|
|
|
|
assert_eq!(parse("fold"), Some(Mode { fold: true, board_weight: 1.0, fold_only_branch: false }));
|
|
|
|
|
assert_eq!(parse("fold:0.5,bluff"), Some(Mode { fold: true, board_weight: 0.5, fold_only_branch: false }));
|
|
|
|
|
assert_eq!(parse("fold:0,nurfold"), Some(Mode { fold: true, board_weight: 0.0, fold_only_branch: true }));
|
|
|
|
|
assert_eq!(parse("0"), None);
|
|
|
|
|
assert_eq!(parse(""), None);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn an_average_board_keeps_the_read() {
|
|
|
|
|
assert!((sigmoid(logit(0.45)) - 0.45).abs() < 1e-12);
|
|
|
|
|
}
|
|
|
|
|
}
|