Files
pokerfreak/src/cfr/render_charts.rs
T
mmintelandClaude Opus 5.5 7f40fa49a0 CFR charts with both blinds as defenders, short deck tables
EP, MP, CO and BTN now play three-player games against small and big
blind (push/fold and open/call/3-bet trees, CFR+ for three players,
exploitability summed over all three best responses). Deal weights and
three-way shares are documented approximations. Short deck gets its own
equity table and charts from the same generator (--deck short).

Open width shrinks in EP/MP (e.g. EP 100bb 0.291 -> 0.249, MP 40bb
0.357 -> 0.293); frozen exploitability stays at 0-4 micro-bb (full) and
0-9 micro-bb (short). Lookup and advice cover SB vs open/jam, BB after
an SB call and the opener vs an SB 3-bet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 15:02:45 +01:00

192 lines
8.6 KiB
Rust

//! Rust source text of `preflop/generated_charts.rs` / `generated_charts_short.rs`: header,
//! convergence table, data.
use super::chart_deck::ChartDeck;
use super::frozen_spot::{OpenSpot, PushFoldSpot, SpotMeta};
use super::render_equity::wrapped;
use super::run_plan::{CFR_SEED, OPEN_DEPTHS, PUSH_FOLD_DEPTHS};
use super::spot_params::{
OPEN_SIZE, REALIZATION_OOP_SINGLE_RAISED, REALIZATION_OOP_THREE_BET, REALIZATION_SPREAD,
SMALL_BLIND_OPEN_SIZE, SMALL_BLIND_THREE_BET_SIZE, THREE_BET_SIZE, WAKE_FRACTION,
};
use super::{generated_equity, generated_equity_short};
fn deck_lines(deck: ChartDeck) -> &'static str {
match deck {
ChartDeck::Full => "//! - 169 hold'em classes of the 52-card deck.\n",
ChartDeck::Short => {
"//! - Short deck: 81 classes of the 36-card deck (six to ace), short deck hand order. The other\n\
//! cells of the 13 x 13 grid are not in the deck and stay 0.\n"
}
}
}
fn header(deck: ChartDeck) -> String {
format!(
"\
//! GENERATED by `cargo run --release --bin chartgen -- charts{arg} > {charts}`.
//! Do not edit. Frozen output of `crate::cfr`; the equity table is `{equity}`
//! (`chartgen equity{arg} > {equity}`).
//!
//! Frequencies are permille (0..=1000) per hand class, indexed by `ClassId` of
//! `cfr::hand_class` (13 x 13 grid; pairs on the diagonal, suited above, offsuit below).
//! Layout and meaning of every field: `cfr::frozen_spot`.
//!
//! Abstraction (details in the `cfr` module docs; all numbers in `cfr::spot_params`):
{deck}\
//! - Exact combo weights with card removal between two players; for three players the deal
//! weight is the product of the three pairwise compatibilities (`cfr::tree3`).
//! - Class-vs-class all-in equity: seeded Monte Carlo, `hand::showdown::judge`. Three-way pots
//! split by the weaker pairwise equity of each hand, normalised (`cfr::three_way`).
//! - Blinds 0.5 / 1, 6-max. EP, MP, CO and BTN face BOTH blinds as players (three-player games);
//! the small blind as first actor faces the big blind. Players between the first actor and the
//! small blind are a multiway tax: each holds a continuing hand (top {wake:.0} % of combos) with
//! probability {wake_p:.2}; if one does, the first actor is simply called by that range.
//! - Push/fold: jam the effective stack or fold; the small blind calls or folds, then the big
//! blind calls or folds (separately after a small-blind fold and call). AOF is the same game
//! (`AOF_PUSH_FOLD` aliases `PUSH_FOLD`).
//! - Open/call/3-bet: open to {open} bb ({sb_open} bb from the small blind). The small blind folds,
//! calls or 3-bets to {sb_three_bet} bb; after its fold the big blind folds, calls or 3-bets to
//! {three_bet} bb, the opener folds, calls or jams, the big blind calls or folds the jam. After
//! the small blind's call the big blind folds or calls (no squeeze); after its 3-bet the big
//! blind folds and the opener folds, calls or jams. Lines that leave chips behind pay pot x
//! realised share: equity stretched by {spread} around the even share, then split by position
//! (out of position realises {oop} of its equity, {oop3} in 3-bet pots); all-in lines pay raw
//! equity.
//! - Solver: full-width CFR+ (regret floor 0, alternating players, average strategy weighted by
//! iteration). Exploitability is that of the permille strategy printed here, in micro-bb
//! (1e-6 bb): the sum of every player's best-response gain, as a share of every deal.
//! - The field behind only calls, so EP / MP still open wider than 6-max solver charts: read the
//! tables as abstraction-internal equilibria, not as solver output.
//!
//! Convergence per spot (`iterations`, `stable_since` = argmax chart final from that iteration,
//! exploitability in micro-bb at N/100, N/20, N/5, N/2, N):
//!
",
arg = deck.deck_argument(),
charts = deck.charts_file(),
equity = deck.equity_file(),
deck = deck_lines(deck),
wake = WAKE_FRACTION * 100.0,
wake_p = WAKE_FRACTION,
open = OPEN_SIZE,
sb_open = SMALL_BLIND_OPEN_SIZE,
sb_three_bet = SMALL_BLIND_THREE_BET_SIZE,
three_bet = THREE_BET_SIZE,
spread = REALIZATION_SPREAD,
oop = REALIZATION_OOP_SINGLE_RAISED,
oop3 = REALIZATION_OOP_THREE_BET,
)
}
fn meta_line(label: &str, meta: &SpotMeta) -> String {
let curve: Vec<String> = meta.checkpoints.iter().map(|(_, e)| e.to_string()).collect();
format!(
"//! {label:<10} iterations {:>6} stable_since {:>6} exploitability {}\n",
meta.iterations,
meta.stable_since,
curve.join(" / ")
)
}
fn meta_source(meta: &SpotMeta) -> String {
let points: Vec<String> = meta.checkpoints.iter().map(|(t, e)| format!("({t}, {e})")).collect();
format!(
"SpotMeta {{ iterations: {}, stable_since: {}, checkpoints: [{}], exploitability_micro_bb: {} }}",
meta.iterations,
meta.stable_since,
points.join(", "),
meta.exploitability_micro_bb
)
}
fn row(name: &str, values: &[u16]) -> String {
format!(" {name}: [\n{} ],\n", wrapped(values, 24).lines().map(|l| format!(" {l}\n")).collect::<String>())
}
fn pair_row(name: &str, values: &[[u16; 2]]) -> String {
let flat: Vec<u16> = values.iter().flat_map(|p| p.iter().copied()).collect();
let lines: String = flat
.chunks(24)
.map(|c| {
let items: Vec<String> = c.chunks(2).map(|p| format!("[{}, {}]", p[0], p[1])).collect();
format!(" {},\n", items.join(", "))
})
.collect();
format!(" {name}: [\n{lines} ],\n")
}
fn push_spot_source(s: &PushFoldSpot) -> String {
format!(
" PushFoldSpot {{\n seat: Seat::{:?},\n depth_bb: {},\n{}{}{}{} meta: {},\n }},\n",
s.seat,
s.depth_bb,
row("push", &s.push),
row("call", &s.call),
row("sb_call", &s.sb_call),
row("bb_overcall", &s.bb_overcall),
meta_source(&s.meta)
)
}
fn open_spot_source(s: &OpenSpot) -> String {
format!(
" OpenSpot {{\n seat: Seat::{:?},\n depth_bb: {},\n{}{}{}{}{}{}{}{} meta: {},\n }},\n",
s.seat,
s.depth_bb,
row("open", &s.open),
pair_row("defend", &s.defend),
pair_row("vs_three_bet", &s.vs_three_bet),
row("vs_jam", &s.vs_jam),
pair_row("sb_defend", &s.sb_defend),
row("bb_overcall", &s.bb_overcall),
pair_row("vs_sb_three_bet", &s.vs_sb_three_bet),
row("sb_vs_jam", &s.sb_vs_jam),
meta_source(&s.meta)
)
}
fn equity_source(deck: ChartDeck) -> (u64, u32) {
match deck {
ChartDeck::Full => (generated_equity::SEED, generated_equity::SAMPLES),
ChartDeck::Short => (generated_equity_short::SEED, generated_equity_short::SAMPLES),
}
}
pub fn render_charts(deck: ChartDeck, push: &[PushFoldSpot], open: &[OpenSpot]) -> String {
let convergence: String = push
.iter()
.map(|s| meta_line(&format!("push {} {}", s.seat.label(), s.depth_bb), &s.meta))
.chain(open.iter().map(|s| meta_line(&format!("open {} {}", s.seat.label(), s.depth_bb), &s.meta)))
.collect();
let push_body: String = push.iter().map(push_spot_source).collect();
let open_body: String = open.iter().map(open_spot_source).collect();
let aof = match deck {
ChartDeck::Full => "\n/// All-in-or-fold has no other choice than push or fold: the same game, the same tables.\n\
pub static AOF_PUSH_FOLD: &[PushFoldSpot] = &PUSH_FOLD;\n",
ChartDeck::Short => "",
};
let (eq_seed, eq_samples) = equity_source(deck);
format!(
"{header}{convergence}//!\n\
//! Equity table: seed {eq_seed}, {eq_samples} samples per class pair. CFR seed {cfr_seed}.\n\n\
use crate::cfr::frozen_spot::{{OpenSpot, PushFoldSpot, SpotMeta}};\n\
use crate::cfr::seat::Seat;\n\n\
pub const EQUITY_SEED: u64 = {eq_seed};\n\
pub const EQUITY_SAMPLES: u32 = {eq_samples};\n\
pub const CFR_SEED: u64 = {cfr_seed};\n\
pub const PUSH_FOLD_DEPTHS: [u8; {nd}] = {push_depths:?};\n\
pub const OPEN_DEPTHS: [u8; {nod}] = {open_depths:?};\n\n\
pub static PUSH_FOLD: [PushFoldSpot; {np}] = [\n{push_body}];\n\n\
pub static OPEN: [OpenSpot; {no}] = [\n{open_body}];\n{aof}",
header = header(deck),
nd = PUSH_FOLD_DEPTHS.len(),
nod = OPEN_DEPTHS.len(),
push_depths = PUSH_FOLD_DEPTHS,
open_depths = OPEN_DEPTHS,
cfr_seed = CFR_SEED,
np = push.len(),
no = open.len(),
)
}