Files
pokerfreak/src/preflop/generated_lookup.rs
T
mmintelandClaude Opus 5.5 a7b32cf425 Add CFR preflop chart generator, frozen charts and lookup
Pure CFR+ core over a 169-class abstraction, push/fold and open/call/3-bet games,
Nash/Kuhn/plausibility/lookup tests, chartgen binary, frozen equity and charts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 13:05:07 +01:00

181 lines
6.6 KiB
Rust

//! Pure lookup over the frozen CFR charts (`generated_charts`). No allocation, no I/O.
//!
//! Depth rule: the frequencies of the two grid depths around `depth_bb` are interpolated
//! linearly in the stack size; below the shallowest or above the deepest grid depth the nearest
//! table is used unchanged (clamping -- see `push_depth_range` / `open_depth_range` for the
//! covered range). Non-finite or non-positive depths, unknown classes and variants without a
//! chart give `None`.
//!
//! Every function answers for the *seat that acts* in that situation:
//! - `push_frequency`: the first actor jams (raise = jam) or folds.
//! - `call_vs_push`: the big blind facing that jam (`seat` = the pusher).
//! - `open_frequency`: the first actor raises or folds.
//! - `vs_open`: the big blind facing the open of `seat`: fold / call / 3-bet.
//! - `vs_three_bet`: the opener (`seat`) facing the big blind's 3-bet: fold / call / jam.
//! - `vs_jam`: the big blind facing the opener's jam: fold / call.
//!
//! Class ids are those of `crate::cfr::hand_class` (`class_of(card, card)`).
use super::generated_charts::{AOF_PUSH_FOLD, OPEN, OPEN_DEPTHS, PUSH_FOLD, PUSH_FOLD_DEPTHS};
use super::position::Position;
use crate::cfr::frozen_spot::{OpenSpot, PushFoldSpot};
use crate::cfr::hand_class::{ClassId, CLASS_COUNT};
use crate::cfr::seat::Seat;
use crate::variant::Variant;
/// Action probabilities; `raise` is the aggressive action of the node (raise / 3-bet / jam).
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Frequencies {
pub fold: f64,
pub call: f64,
pub raise: f64,
}
impl Frequencies {
fn aggressive(permille: u16) -> Frequencies {
let p = permille as f64 / 1000.0;
Frequencies { fold: 1.0 - p, call: 0.0, raise: p }
}
fn passive(permille: u16) -> Frequencies {
let p = permille as f64 / 1000.0;
Frequencies { fold: 1.0 - p, call: p, raise: 0.0 }
}
fn split(call_raise: [u16; 2]) -> Frequencies {
let (call, raise) = (call_raise[0] as f64 / 1000.0, call_raise[1] as f64 / 1000.0);
Frequencies { fold: (1.0 - call - raise).max(0.0), call, raise }
}
fn blend(self, other: Frequencies, t: f64) -> Frequencies {
let mix = |a: f64, b: f64| a * (1.0 - t) + b * t;
Frequencies {
fold: mix(self.fold, other.fold),
call: mix(self.call, other.call),
raise: mix(self.raise, other.raise),
}
}
}
/// Chart seat for a table position; `None` for the big blind (it never acts first in these
/// charts). An unknown position uses the tightest (early) row, like the heuristic charts do.
pub fn seat_of(position: Position) -> Option<Seat> {
match position {
Position::Middle => Some(Seat::Middle),
Position::Cutoff => Some(Seat::Cutoff),
Position::Button => Some(Seat::Button),
Position::SmallBlind | Position::HeadsUpButton => Some(Seat::SmallBlind),
Position::BigBlind => None,
_ => Some(Seat::Early),
}
}
/// Covered stack range in bb for the push / fold charts.
pub fn push_depth_range() -> (f64, f64) {
range(&PUSH_FOLD_DEPTHS)
}
/// Covered stack range in bb for the open / call / 3-bet charts.
pub fn open_depth_range() -> (f64, f64) {
range(&OPEN_DEPTHS)
}
fn range(depths: &[u8]) -> (f64, f64) {
let lo = depths.iter().copied().min().unwrap_or(0) as f64;
let hi = depths.iter().copied().max().unwrap_or(0) as f64;
(lo, hi)
}
trait Placed {
fn seat(&self) -> Seat;
fn depth(&self) -> u8;
}
impl Placed for PushFoldSpot {
fn seat(&self) -> Seat {
self.seat
}
fn depth(&self) -> u8 {
self.depth_bb
}
}
impl Placed for OpenSpot {
fn seat(&self) -> Seat {
self.seat
}
fn depth(&self) -> u8 {
self.depth_bb
}
}
/// The two grid spots around `depth` (both the same one when clamped or on the grid) and the
/// weight of the deeper one.
fn bracket<T: Placed>(spots: &[T], seat: Seat, depth: f64) -> Option<(&T, &T, f64)> {
let mine = || spots.iter().filter(|s| s.seat() == seat);
let below = mine().filter(|s| s.depth() as f64 <= depth).max_by_key(|s| s.depth());
let above = mine().filter(|s| s.depth() as f64 >= depth).min_by_key(|s| s.depth());
match (below, above) {
(Some(lo), Some(hi)) if lo.depth() != hi.depth() => {
let t = (depth - lo.depth() as f64) / (hi.depth() as f64 - lo.depth() as f64);
Some((lo, hi, t))
}
(Some(lo), Some(_)) => Some((lo, lo, 0.0)),
(Some(lo), None) => Some((lo, lo, 0.0)),
(None, Some(hi)) => Some((hi, hi, 0.0)),
(None, None) => None,
}
}
fn lookup<T: Placed>(
spots: &[T],
seat: Seat,
depth_bb: f64,
class: ClassId,
read: impl Fn(&T, ClassId) -> Frequencies,
) -> Option<Frequencies> {
if !depth_bb.is_finite() || depth_bb <= 0.0 || class >= CLASS_COUNT {
return None;
}
let (lo, hi, t) = bracket(spots, seat, depth_bb)?;
Some(read(lo, class).blend(read(hi, class), t))
}
fn push_chart(variant: Variant) -> Option<&'static [PushFoldSpot]> {
match variant {
Variant::Nlhe => Some(&PUSH_FOLD),
Variant::Aof => Some(AOF_PUSH_FOLD),
_ => None,
}
}
/// The first actor's jam frequency (`raise`) or fold.
pub fn push_frequency(variant: Variant, seat: Seat, depth_bb: f64, class: ClassId) -> Option<Frequencies> {
lookup(push_chart(variant)?, seat, depth_bb, class, |s, c| Frequencies::aggressive(s.push[c]))
}
/// The big blind's response to a jam from `pusher`.
pub fn call_vs_push(variant: Variant, pusher: Seat, depth_bb: f64, class: ClassId) -> Option<Frequencies> {
lookup(push_chart(variant)?, pusher, depth_bb, class, |s, c| Frequencies::passive(s.call[c]))
}
/// The first actor's raise frequency or fold (NLHE only).
pub fn open_frequency(seat: Seat, depth_bb: f64, class: ClassId) -> Option<Frequencies> {
lookup(&OPEN, seat, depth_bb, class, |s, c| Frequencies::aggressive(s.open[c]))
}
/// The big blind facing the open of `opener`: fold / call / 3-bet.
pub fn vs_open(opener: Seat, depth_bb: f64, class: ClassId) -> Option<Frequencies> {
lookup(&OPEN, opener, depth_bb, class, |s, c| Frequencies::split(s.defend[c]))
}
/// The opener facing the big blind's 3-bet: fold / call / jam.
pub fn vs_three_bet(opener: Seat, depth_bb: f64, class: ClassId) -> Option<Frequencies> {
lookup(&OPEN, opener, depth_bb, class, |s, c| Frequencies::split(s.vs_three_bet[c]))
}
/// The big blind facing the opener's jam over the 3-bet: fold / call.
pub fn vs_jam(opener: Seat, depth_bb: f64, class: ClassId) -> Option<Frequencies> {
lookup(&OPEN, opener, depth_bb, class, |s, c| Frequencies::passive(s.vs_jam[c]))
}