Files
pokerfreak/tests/preflop_multi_advice.rs
mmintelandClaude Opus 5.5 242b21c696 Die 10 roten Tests des Live-Stands: 1 echter Fehler, 9 veraltet
- preflop_plo4: echter Fehler. 015baff (PLO5-Kalibrierung) aenderte die geteilten
  Gewichte, die plo4_score mitbenutzt; die PLO4-Perzentiltabelle war veraltet
  (233 statt 231 Stufen). Neu erzeugt (print_table).
- cfr_multi / generated_charts_reproduce "frozen file": Kommentartabellen im
  Repo hatten zusammengezogene Leerzeichen; Renderer-Ausgabe zurueckgeschrieben
  (git diff -w leer, Zahlen unveraendert).
- generated_charts_plausibility x3: verglichen gegen handkopierte Werte bzw.
  Nash ohne Antes, die Tabellen rechnen seit 3d65e85 mit Rake+Antes. Jetzt:
  Ein-Verteidiger-Bezug = BASELINE derselben Umgebung; HU-Nash gegen dasselbe
  Spiel ohne Rake/Antes geloest, ausgelieferte Tabelle mindestens so breit;
  Positions-Teilmengen: eine indifferente Mischung (98s BTN 40bb 30 %) erlaubt.
- preflop_multi_advice x3: die festen Grenzhaende (Q8o, KTo, 77) liegen nach
  dem Neurechnen innen; Grenzhaende werden ueber alle 169 Klassen gesucht.
- pipeline_bluff: "Steal weiter gegen Nits" ist abgeschaltet, nicht kaputt:
  read_gate verwirft Nit-Reads (earned_trust 0, gemessen -50 bb/100). Test
  prueft jetzt: gegen Nits blind, gegen Stations enger.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 23:32:29 +01:00

258 lines
12 KiB
Rust

//! The advice reads the multi-defender tables (`preflop::cfr_advice`, `multi_lookup`): the
//! table is chosen by how many opponents behind the attacker are still in the hand, and the
//! recommendation changes with that number. Where the tables do not reach, the old answer stays.
//!
//! Six-max, button on seat 3: EP 0, MP 1, CO 2, BTN 3, SB 4, BB 5. Big blind = 1.0. A seat left
//! out of `present` has nobody sitting there.
use rechner::card::Card;
use rechner::engine::decide::decide;
use rechner::exploit::thresholds::Thresholds;
use rechner::game::{ActionKind, ActionRecord, SeenAction, Spot, Street, Villain};
use rechner::preflop::advise::advise;
use rechner::preflop::plan::{ChartAction, PreflopPlan};
use rechner::variant::Variant;
const EARLY: u8 = 0;
const BUTTON: u8 = 3;
fn record(seat: u8, action: SeenAction, amount: Option<f64>) -> ActionRecord {
ActionRecord { street: Street::Preflop, seat, action, amount }
}
/// Blinds of the seats that are present, then folds of `folds`.
fn history(present: &[u8], folds: &[u8]) -> Vec<ActionRecord> {
let mut h = Vec::new();
if present.contains(&4) {
h.push(record(4, SeenAction::PostSmallBlind, Some(0.5)));
}
h.push(record(5, SeenAction::PostBigBlind, Some(1.0)));
h.extend(folds.iter().map(|s| record(*s, SeenAction::Fold, None)));
h
}
fn spot(hole: &str, hero_seat: u8, present: &[u8], history: Vec<ActionRecord>, to_call: f64, depth: f64) -> Spot {
let pot = history.iter().filter_map(|r| r.amount).sum();
Spot {
variant: Variant::Nlhe,
big_blind: 1.0,
seats: 6,
hero_seat,
button_seat: Some(3),
hero_cards: Card::parse_list(hole).expect("cards"),
board: vec![],
street: Street::Preflop,
pot,
to_call,
hero_stack: depth,
min_raise_to: Some(2.0 * to_call.max(1.0) + 1.0),
max_raise_to: Some(depth),
legal: vec![ActionKind::Fold, ActionKind::Call, ActionKind::Raise],
allowed_sizes: None,
villains: present
.iter()
.filter(|s| **s != hero_seat)
.map(|seat| Villain { seat: *seat, stack: depth, folded: false, profile: None })
.collect(),
seed: 1,
history,
}
}
fn plan(s: &Spot) -> PreflopPlan {
advise(s, &Thresholds::BASE)
}
/// The button first in, with only the big blind behind (the small blind seat is empty) or with
/// both blinds behind.
fn button_first_in(hole: &str, with_small_blind: bool) -> Spot {
let present: Vec<u8> = if with_small_blind { vec![0, 1, 2, 3, 4, 5] } else { vec![0, 1, 2, 3, 5] };
let h = history(&present, &[0, 1, 2]);
spot(hole, BUTTON, &present, h, 1.0, 40.0)
}
/// One hand of each of the 169 classes: pairs, suited and offsuit.
fn every_class() -> Vec<String> {
let ranks = ['A', 'K', 'Q', 'J', 'T', '9', '8', '7', '6', '5', '4', '3', '2'];
let mut hands = Vec::new();
for (i, hi) in ranks.iter().enumerate() {
hands.push(format!("{hi}h {hi}d"));
for lo in &ranks[i + 1..] {
hands.push(format!("{hi}h {lo}h"));
hands.push(format!("{hi}h {lo}d"));
}
}
hands
}
// The three tests below used to pin one hand each (Q8o, KTo, 77) at the border of the ranges.
// The tables were re-solved with the measured rake and antes (3d65e85) and every range moved
// wider, so those hands now raise on both sides. The claim of each test is structural (a second
// defender, a full table, a non-blind defender change the answer and read their table); the
// border hand is now searched over all 169 classes instead of hard-coded.
#[test]
fn a_second_defender_changes_the_button_open() {
// Some hand opens against the big blind alone and folds once the small blind sits behind too.
let moved: Vec<String> = every_class()
.into_iter()
.filter(|hole| {
plan(&button_first_in(hole, false)).action == ChartAction::Raise
&& plan(&button_first_in(hole, true)).action == ChartAction::Fold
})
.collect();
assert!(!moved.is_empty(), "no hand opens vs 1 defender and folds vs 2");
let (one, two) = (plan(&button_first_in(&moved[0], false)), plan(&button_first_in(&moved[0], true)));
assert!(one.reason.contains("CFR open 40bb vs 1 defender"), "{}", one.reason);
assert!(two.reason.contains("CFR open 40bb vs 2 defenders"), "{}", two.reason);
// No hand goes the other way by more than the mixing at the border.
let backwards = every_class()
.into_iter()
.filter(|hole| {
plan(&button_first_in(hole, false)).action == ChartAction::Fold
&& plan(&button_first_in(hole, true)).action == ChartAction::Raise
})
.count();
assert!(backwards <= moved.len() / 4, "{backwards} hands open only against two defenders, {} the other way", moved.len());
// Hands at both ends do not move: aces open, seven-deuce folds.
for with_small_blind in [false, true] {
assert_eq!(plan(&button_first_in("Ah Ad", with_small_blind)).action, ChartAction::Raise);
assert_eq!(plan(&button_first_in("7h 2d", with_small_blind)).action, ChartAction::Fold);
}
}
#[test]
fn a_full_table_opens_tighter_from_early_position() {
// EP with five players behind reads the four-defender table (MP is the wake field); with
// only the blinds behind, the two-defender one.
let full = |hole: &str| plan(&spot(hole, EARLY, &[0, 1, 2, 3, 4, 5], history(&[0, 1, 2, 3, 4, 5], &[]), 1.0, 40.0));
let blinds = |hole: &str| plan(&spot(hole, EARLY, &[0, 4, 5], history(&[0, 4, 5], &[]), 1.0, 40.0));
let (mut tighter, mut wider) = (0, 0);
for hole in every_class() {
let (f, b) = (full(&hole).action, blinds(&hole).action);
tighter += (b == ChartAction::Raise && f == ChartAction::Fold) as usize;
wider += (b == ChartAction::Fold && f == ChartAction::Raise) as usize;
}
assert!(tighter > 0 && tighter > 4 * wider, "full table folds {tighter} blind-only opens, opens {wider} blind-only folds");
let (f, b) = (full("Kh Td"), blinds("Kh Td"));
assert!(f.reason.contains("vs 4 defenders"), "{}", f.reason);
assert!(b.reason.contains("vs 2 defenders"), "{}", b.reason);
}
#[test]
fn a_defender_that_is_not_a_blind_now_reads_the_table() {
// EP opens, MP and CO fold: the button defends with the blinds still behind it.
let button = |hole: &str| {
let mut h = history(&[0, 1, 2, 3, 4, 5], &[]);
h.extend([record(0, SeenAction::Raise, Some(2.5)), record(1, SeenAction::Fold, None), record(2, SeenAction::Fold, None)]);
plan(&spot(hole, BUTTON, &[0, 1, 2, 3, 4, 5], h, 2.5, 40.0))
};
let p = button("7h 7d");
assert!(p.reason.contains("CFR BTN vs open 40bb vs 4 defenders"), "{}", p.reason);
assert_ne!(p.action, ChartAction::Fold, "{}", p.reason);
// The table has all three answers: some hands call, some 3-bet, junk folds.
let actions: Vec<ChartAction> = every_class().iter().map(|h| button(h).action).collect();
for want in [ChartAction::Call, ChartAction::Raise, ChartAction::Fold] {
assert!(actions.contains(&want), "BTN vs EP open never {want:?}");
}
assert_eq!(button("7h 2d").action, ChartAction::Fold);
}
#[test]
fn where_the_multi_tables_do_not_reach_the_old_answer_stays() {
let full = [0, 1, 2, 3, 4, 5];
let unopened = |depth: f64| spot("Kh Td", EARLY, &full, history(&full, &[]), 1.0, depth);
// Deeper than the grid (100 bb): the old tables, which clamp to 100 bb.
let deep = plan(&unopened(150.0));
assert!(deep.reason.contains("CFR open 100bb") || deep.reason.contains("CFR open 150bb"), "{}", deep.reason);
assert!(!deep.reason.contains("defender"), "{}", deep.reason);
// Push / fold depth: the push / fold table.
let short = plan(&unopened(12.0));
assert!(short.reason.contains("CFR push/fold"), "{}", short.reason);
assert!(!short.reason.contains("defender"), "{}", short.reason);
// Between push / fold and the grid (15 to 20 bb): the old open table.
assert!(!plan(&unopened(18.0)).reason.contains("defender"));
// Other variants: the short deck keeps its own one table.
let mut s = unopened(40.0);
s.variant = Variant::ShortDeck;
s.hero_cards = Card::parse_list("Ah Ad").expect("cards");
let p = plan(&s);
assert!(p.reason.contains("CFR open") && !p.reason.contains("defender"), "{}", p.reason);
// A spot without villains carries no count: the old two-blind table.
let mut s = unopened(40.0);
s.villains.clear();
let p = plan(&s);
assert!(p.reason.contains("CFR open 40bb") && !p.reason.contains("defender"), "{}", p.reason);
// The small blind first in: its own table, already the one-defender game.
let s = spot("Kh Td", 4, &full, history(&full, &[0, 1, 2, 3]), 0.5, 40.0);
assert!(!plan(&s).reason.contains("defender"), "{}", plan(&s).reason);
// A gap among the last places: EP with four players behind but the small blind seat empty
// is not a modelled lineup (the last four places are CO, BTN, SB, BB).
let gap = [0, 1, 2, 3, 5];
let s = spot("Kh Td", EARLY, &gap, history(&gap, &[]), 1.0, 40.0);
assert!(!plan(&s).reason.contains("defender"), "{}", plan(&s).reason);
}
#[test]
fn postflop_never_reads_the_preflop_tables() {
let full = [0, 1, 2, 3, 4, 5];
let mut s = spot("Kh Td", EARLY, &full, history(&full, &[]), 0.0, 40.0);
s.street = Street::Flop;
s.board = Card::parse_list("2c 7d 9s").expect("board");
s.legal = vec![ActionKind::Check, ActionKind::Raise];
s.pot = 6.0;
s.min_raise_to = Some(1.0);
let d = decide(&s);
assert!(!format!("{d:?}").contains("CFR"), "{d:?}");
}
/// The same lineup at a four-handed table: CO 1, BTN 2, SB 3, BB 4 (button on seat 2).
fn four_handed(hole: &str, hero_seat: u8) -> Spot {
let mut s = spot(hole, hero_seat, &[1, 2, 3, 4], Vec::new(), 1.0, 40.0);
s.seats = 4;
s.button_seat = Some(2);
s.history = vec![record(3, SeenAction::PostSmallBlind, Some(0.5)), record(4, SeenAction::PostBigBlind, Some(1.0))];
s.pot = 1.5;
s
}
#[test]
fn a_four_handed_cutoff_reads_the_six_max_cutoff_lineup() {
// Four players: the first to act is the cutoff with three behind - the six-max cutoff after
// EP and MP folded. Same table, same answer; before, short tables never reached the tables.
let six = spot("Kd 9c", 2, &[0, 1, 2, 3, 4, 5], history(&[0, 1, 2, 3, 4, 5], &[0, 1]), 1.0, 40.0);
for hole in ["Kd 9c", "Qh 8d", "7s 6s", "Ac 2d", "Jh 5h"] {
let mut six = six.clone();
six.hero_cards = rechner::card::Card::parse_list(hole).expect("cards");
let (a, b) = (plan(&six), plan(&four_handed(hole, 1)));
assert_eq!(a.action, b.action, "{hole}: {} / {}", a.reason, b.reason);
assert!(b.reason.starts_with("CO open, CFR open 40bb vs 3 defenders"), "{}", b.reason);
}
}
#[test]
fn a_short_table_keeps_lineups_and_the_blind_battle_only() {
// Small blind first in at four-handed: its own one-defender table, as at six-max.
let mut sb = four_handed("Kd 9c", 3);
sb.history.push(record(1, SeenAction::Fold, None));
sb.history.push(record(2, SeenAction::Fold, None));
sb.to_call = 0.5;
sb.villains.iter_mut().filter(|v| v.seat <= 2).for_each(|v| v.folded = true);
let p = plan(&sb);
assert!(p.reason.contains("CFR"), "{}", p.reason);
// Button facing the cutoff's open: a lineup (button and both blinds defend).
let mut btn = four_handed("Kd 9c", 2);
btn.history.push(record(1, SeenAction::Raise, Some(2.5)));
btn.to_call = 2.5;
let p = plan(&btn);
assert!(p.reason.contains("CFR BTN vs open 40bb vs 3 defenders"), "{}", p.reason);
// Small blind facing that open with the button calling: no lineup, and the six-max
// one-defender tables do not apply at four-handed - no CFR answer.
let mut sb = four_handed("Kd 9c", 3);
sb.history.push(record(1, SeenAction::Raise, Some(2.5)));
sb.history.push(record(2, SeenAction::Call, Some(2.5)));
sb.to_call = 2.0;
let p = plan(&sb);
assert!(!p.reason.contains("CFR"), "{}", p.reason);
}