Files
pokerfreak/tests/generated_charts_lookup.rs
mmintel 04d5251acf Welt ohne fremden Namen: Pruefordner vollstaendig mitkopieren, Engine nur im eigenen Baum
Die Welt nannte 'pokerfreak' nur noch in zwei Harness-Zeilen - der Rest waren veraltete
Rust-Pruefungen im Pruefordner, die der Aufbau nie auffrischte.
2026-11-01 23:37:11 +01:00

207 lines
10 KiB
Rust

//! Pure lookup over the frozen charts: grid points, interpolation, clamping, refusals.
//!
//! Depth rule (documented in `generated_lookup.rs`): linear interpolation of the frequencies
//! between the two neighbouring grid depths; outside the grid the nearest table is used
//! (clamping). Non-finite or non-positive depths are refused (`None`).
mod cfr_support;
use cfr_support::id;
use rechner::card::Card;
use rechner::cfr::hand_class::{class_of, CLASS_COUNT};
use rechner::cfr::seat::Seat;
use rechner::preflop::generated_charts::{OPEN, PUSH_FOLD};
use rechner::preflop::generated_charts_short as short;
use rechner::preflop::generated_lookup::{
bb_overcall_vs_push, bb_vs_open_and_call, call_vs_push, has_charts, open_depth_range, open_frequency,
push_depth_range, push_frequency, sb_call_vs_push, sb_vs_open, seat_of, vs_jam, vs_open,
vs_sb_three_bet, vs_three_bet, Frequencies,
};
use rechner::preflop::position::Position;
use rechner::variant::Variant;
fn close(a: f64, b: f64) -> bool {
(a - b).abs() < 1e-9
}
fn sums_to_one(f: Frequencies) -> bool {
close(f.fold + f.call + f.raise, 1.0)
&& [f.fold, f.call, f.raise].iter().all(|x| (0.0..=1.0).contains(x))
}
#[test]
fn grid_depths_return_the_table_entry_exactly() {
let spot = PUSH_FOLD.iter().find(|s| s.seat == Seat::Button && s.depth_bb == 10).expect("spot");
for c in [id("AA"), id("A5s"), id("72o"), id("98s"), id("QJo")] {
let f = push_frequency(Variant::Nlhe, Seat::Button, 10.0, c).expect("lookup");
assert!(close(f.raise, spot.push[c] as f64 / 1000.0));
assert!(close(f.fold, 1.0 - spot.push[c] as f64 / 1000.0));
assert_eq!(f.call, 0.0);
let g = call_vs_push(Variant::Nlhe, Seat::Button, 10.0, c).expect("lookup");
assert!(close(g.call, spot.call[c] as f64 / 1000.0));
assert!(sums_to_one(f) && sums_to_one(g));
}
let open = OPEN.iter().find(|s| s.seat == Seat::Cutoff && s.depth_bb == 40).expect("spot");
let c = id("KJs");
let f = open_frequency(Variant::Nlhe, Seat::Cutoff, 40.0, c).expect("lookup");
assert!(close(f.raise, open.open[c] as f64 / 1000.0));
let d = vs_open(Variant::Nlhe, Seat::Cutoff, 40.0, c).expect("lookup");
assert!(close(d.call, open.defend[c][0] as f64 / 1000.0));
assert!(close(d.raise, open.defend[c][1] as f64 / 1000.0));
let t = vs_three_bet(Variant::Nlhe, Seat::Cutoff, 40.0, c).expect("lookup");
assert!(close(t.call, open.vs_three_bet[c][0] as f64 / 1000.0));
assert!(close(t.raise, open.vs_three_bet[c][1] as f64 / 1000.0));
let j = vs_jam(Variant::Nlhe, Seat::Cutoff, 40.0, c).expect("lookup");
assert!(close(j.call, open.vs_jam[c] as f64 / 1000.0));
assert!([d, t, j].into_iter().all(sums_to_one));
}
#[test]
fn between_grid_depths_the_frequencies_are_linearly_interpolated() {
for c in 0..CLASS_COUNT {
let lo = push_frequency(Variant::Nlhe, Seat::Cutoff, 8.0, c).expect("lookup").raise;
let hi = push_frequency(Variant::Nlhe, Seat::Cutoff, 10.0, c).expect("lookup").raise;
let mid = push_frequency(Variant::Nlhe, Seat::Cutoff, 9.0, c).expect("lookup");
assert!(close(mid.raise, (lo + hi) / 2.0), "class {c}");
assert!(sums_to_one(mid));
let quarter = push_frequency(Variant::Nlhe, Seat::Cutoff, 8.5, c).expect("lookup").raise;
assert!(close(quarter, lo * 0.75 + hi * 0.25));
}
let a = open_frequency(Variant::Nlhe, Seat::Button, 20.0, id("K9o")).expect("lookup").raise;
let b = open_frequency(Variant::Nlhe, Seat::Button, 40.0, id("K9o")).expect("lookup").raise;
let m = open_frequency(Variant::Nlhe, Seat::Button, 30.0, id("K9o")).expect("lookup").raise;
assert!(close(m, (a + b) / 2.0));
}
#[test]
fn outside_the_grid_the_nearest_table_is_used() {
let (lo, hi) = push_depth_range();
assert_eq!((lo, hi), (5.0, 25.0));
for c in [id("A2o"), id("T8s"), id("54s")] {
let at_lo = push_frequency(Variant::Nlhe, Seat::Early, lo, c).expect("lookup");
let below = push_frequency(Variant::Nlhe, Seat::Early, 1.0, c).expect("lookup");
let at_hi = push_frequency(Variant::Nlhe, Seat::Early, hi, c).expect("lookup");
let above = push_frequency(Variant::Nlhe, Seat::Early, 80.0, c).expect("lookup");
assert_eq!(at_lo, below);
assert_eq!(at_hi, above);
}
assert_eq!(open_depth_range(), (20.0, 100.0));
assert_eq!(
open_frequency(Variant::Nlhe, Seat::Early, 500.0, id("JTs")),
open_frequency(Variant::Nlhe, Seat::Early, 100.0, id("JTs"))
);
}
#[test]
fn nonsense_inputs_are_refused_not_panicked_on() {
for depth in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY, 0.0, -3.0] {
assert!(push_frequency(Variant::Nlhe, Seat::Button, depth, 0).is_none(), "{depth}");
assert!(call_vs_push(Variant::Nlhe, Seat::Button, depth, 0).is_none());
assert!(open_frequency(Variant::Nlhe, Seat::Button, depth, 0).is_none());
assert!(vs_open(Variant::Nlhe, Seat::Button, depth, 0).is_none());
assert!(vs_three_bet(Variant::Nlhe, Seat::Button, depth, 0).is_none());
assert!(vs_jam(Variant::Nlhe, Seat::Button, depth, 0).is_none());
}
assert!(push_frequency(Variant::Nlhe, Seat::Button, 10.0, CLASS_COUNT).is_none());
assert!(open_frequency(Variant::Nlhe, Seat::Button, 40.0, usize::MAX).is_none());
}
#[test]
fn unsupported_variants_have_no_chart() {
for v in [Variant::Plo5, Variant::KPoker] {
assert!(!has_charts(v));
assert!(push_frequency(v, Seat::Button, 10.0, 0).is_none(), "{v:?}");
assert!(call_vs_push(v, Seat::Button, 10.0, 0).is_none());
assert!(open_frequency(v, Seat::Button, 40.0, 0).is_none());
}
// All-in-or-fold has no open tables.
assert!(open_frequency(Variant::Aof, Seat::Button, 40.0, 0).is_none());
assert!([Variant::Nlhe, Variant::Aof, Variant::ShortDeck].into_iter().all(has_charts));
}
#[test]
fn the_tables_for_both_blinds_are_read_from_their_own_rows() {
let push = PUSH_FOLD.iter().find(|s| s.seat == Seat::Cutoff && s.depth_bb == 12).expect("spot");
let open = OPEN.iter().find(|s| s.seat == Seat::Middle && s.depth_bb == 40).expect("spot");
for c in [id("AA"), id("AJo"), id("77"), id("KTs"), id("72o")] {
let sb = sb_call_vs_push(Variant::Nlhe, Seat::Cutoff, 12.0, c).expect("lookup");
assert!(close(sb.call, push.sb_call[c] as f64 / 1000.0) && sb.raise == 0.0);
let over = bb_overcall_vs_push(Variant::Nlhe, Seat::Cutoff, 12.0, c).expect("lookup");
assert!(close(over.call, push.bb_overcall[c] as f64 / 1000.0));
let d = sb_vs_open(Variant::Nlhe, Seat::Middle, 40.0, c).expect("lookup");
assert!(close(d.call, open.sb_defend[c][0] as f64 / 1000.0));
assert!(close(d.raise, open.sb_defend[c][1] as f64 / 1000.0));
let o = bb_vs_open_and_call(Variant::Nlhe, Seat::Middle, 40.0, c).expect("lookup");
assert!(close(o.call, open.bb_overcall[c] as f64 / 1000.0) && o.raise == 0.0);
let t = vs_sb_three_bet(Variant::Nlhe, Seat::Middle, 40.0, c).expect("lookup");
assert!(close(t.call, open.vs_sb_three_bet[c][0] as f64 / 1000.0));
assert!(close(t.raise, open.vs_sb_three_bet[c][1] as f64 / 1000.0));
assert!([sb, over, d, o, t].into_iter().all(sums_to_one));
}
}
#[test]
fn the_small_blind_as_first_actor_has_no_small_blind_or_overcall_rows() {
for v in [Variant::Nlhe, Variant::ShortDeck] {
assert!(sb_call_vs_push(v, Seat::SmallBlind, 10.0, id("AA")).is_none());
assert!(bb_overcall_vs_push(v, Seat::SmallBlind, 10.0, id("AA")).is_none());
assert!(sb_vs_open(v, Seat::SmallBlind, 40.0, id("AA")).is_none());
assert!(bb_vs_open_and_call(v, Seat::SmallBlind, 40.0, id("AA")).is_none());
assert!(vs_sb_three_bet(v, Seat::SmallBlind, 40.0, id("AA")).is_none());
// The big blind's tables against the small blind exist.
assert!(call_vs_push(v, Seat::SmallBlind, 10.0, id("AA")).is_some());
assert!(vs_open(v, Seat::SmallBlind, 40.0, id("AA")).is_some());
}
}
#[test]
fn short_deck_reads_its_own_tables() {
let spot = short::PUSH_FOLD.iter().find(|s| s.seat == Seat::Button && s.depth_bb == 10).expect("spot");
let open = short::OPEN.iter().find(|s| s.seat == Seat::Early && s.depth_bb == 100).expect("spot");
for c in [id("AA"), id("A6s"), id("T9o"), id("66")] {
let f = push_frequency(Variant::ShortDeck, Seat::Button, 10.0, c).expect("lookup");
assert!(close(f.raise, spot.push[c] as f64 / 1000.0));
let o = open_frequency(Variant::ShortDeck, Seat::Early, 100.0, c).expect("lookup");
assert!(close(o.raise, open.open[c] as f64 / 1000.0));
}
// A class that is not in the short deck reads as a fold.
let deuces = push_frequency(Variant::ShortDeck, Seat::Button, 10.0, id("22")).expect("lookup");
assert_eq!(deuces.raise, 0.0);
}
#[test]
fn all_in_or_fold_answers_exactly_like_push_fold() {
for c in 0..CLASS_COUNT {
for depth in [5.0, 9.3, 17.0, 25.0] {
assert_eq!(
push_frequency(Variant::Aof, Seat::SmallBlind, depth, c),
push_frequency(Variant::Nlhe, Seat::SmallBlind, depth, c)
);
assert_eq!(
call_vs_push(Variant::Aof, Seat::Early, depth, c),
call_vs_push(Variant::Nlhe, Seat::Early, depth, c)
);
}
}
}
#[test]
fn positions_map_to_chart_seats() {
assert_eq!(seat_of(Position::Early), Some(Seat::Early));
assert_eq!(seat_of(Position::Middle), Some(Seat::Middle));
assert_eq!(seat_of(Position::Cutoff), Some(Seat::Cutoff));
assert_eq!(seat_of(Position::Button), Some(Seat::Button));
assert_eq!(seat_of(Position::SmallBlind), Some(Seat::SmallBlind));
assert_eq!(seat_of(Position::HeadsUpButton), Some(Seat::SmallBlind), "heads-up button is the small blind");
assert_eq!(seat_of(Position::Unknown), Some(Seat::Early), "unknown position uses the tightest row");
assert_eq!(seat_of(Position::BigBlind), None, "the big blind never opens or jams first");
}
#[test]
fn classes_come_from_cards() {
let ace = Card::parse_short("Ah").expect("card");
let king = Card::parse_short("Kh").expect("card");
let c = class_of(ace, king).expect("class");
let f = push_frequency(Variant::Nlhe, Seat::Button, 8.0, c).expect("lookup");
assert!(f.raise > 0.9, "AKs jams the button at 8 bb");
}