Files
pokerfreak/tests/cfr_equity.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

110 lines
4.6 KiB
Rust

//! Class-vs-class all-in equity: seeded Monte Carlo with `hand::showdown`, frozen as integers.
//!
//! References (same as tests/equity_reference.rs): AA vs KK ~ 81.9 %, AKo vs QQ ~ 43 %,
//! AKs vs 22 ~ 50 % (PokerStove / ProPokerTools); AA vs a random hand 85.2 % and 32o 32.3 %
//! (the measurement quoted in `preflop/holdem_ranking.rs`).
mod cfr_support;
use cfr_support::id;
use pokerfreak::cfr::chance_table::chance_table;
use pokerfreak::cfr::equity_matrix::{triangle_pairs, EquityMatrix, TRIANGLE_LEN};
use pokerfreak::cfr::equity_pair::pair_equity;
use pokerfreak::cfr::frozen_equity::frozen_equity;
use pokerfreak::cfr::generated_equity as frozen;
use pokerfreak::cfr::hand_class::CLASS_COUNT;
use pokerfreak::variant::{rules, DeckRule, Rules, Variant};
fn nlhe() -> Rules {
rules(Variant::Nlhe)
}
#[test]
fn pair_equity_matches_published_numbers() {
let aa_kk = pair_equity(&nlhe(), 7, 40_000, id("AA"), id("KK")).expect("equity");
assert!((aa_kk as f64 / 1e4 - 0.82).abs() < 0.012, "AA vs KK {aa_kk}");
let ako_qq = pair_equity(&nlhe(), 7, 40_000, id("AKo"), id("QQ")).expect("equity");
assert!((ako_qq as f64 / 1e4 - 0.43).abs() < 0.015, "AKo vs QQ {ako_qq}");
let aks_22 = pair_equity(&nlhe(), 7, 40_000, id("AKs"), id("22")).expect("equity");
assert!((aks_22 as f64 / 1e4 - 0.50).abs() < 0.015, "AKs vs 22 {aks_22}");
}
#[test]
fn pair_equity_is_deterministic_per_seed_and_close_across_seeds() {
let a = pair_equity(&nlhe(), 11, 5_000, id("JTs"), id("A5o")).expect("equity");
let b = pair_equity(&nlhe(), 11, 5_000, id("JTs"), id("A5o")).expect("equity");
let c = pair_equity(&nlhe(), 12, 5_000, id("JTs"), id("A5o")).expect("equity");
assert_eq!(a, b);
assert!(a.abs_diff(c) < 400, "different seeds stay statistically close");
}
#[test]
fn pair_equity_rejects_nonsense_without_panicking() {
assert!(pair_equity(&nlhe(), 1, 0, id("AA"), id("KK")).is_err(), "zero samples");
assert!(pair_equity(&nlhe(), 1, 100, CLASS_COUNT, id("KK")).is_err(), "class out of range");
let omaha = rules(Variant::Plo5);
assert!(pair_equity(&omaha, 1, 100, id("AA"), id("KK")).is_err(), "only two hole cards");
let short = rules(Variant::ShortDeck);
assert!(pair_equity(&short, 1, 100, id("52o"), id("KK")).is_err(), "class not in the deck");
}
#[test]
fn triangle_layout_is_dense_and_covers_every_pair_once() {
let pairs = triangle_pairs();
assert_eq!(pairs.len(), TRIANGLE_LEN);
assert_eq!(TRIANGLE_LEN, 169 * 170 / 2);
assert!(pairs.iter().all(|(i, j)| i <= j && *j < CLASS_COUNT));
let distinct: std::collections::HashSet<_> = pairs.iter().collect();
assert_eq!(distinct.len(), TRIANGLE_LEN);
}
#[test]
fn matrix_from_triangle_is_antisymmetric_and_rejects_bad_input() {
let matrix = frozen_equity();
for i in (0..CLASS_COUNT).step_by(7) {
assert!((matrix.get(i, i) - 0.5).abs() < 1e-9, "class vs itself is a coin flip");
for j in (0..CLASS_COUNT).step_by(5) {
assert!((matrix.get(i, j) + matrix.get(j, i) - 1.0).abs() < 1e-9);
}
}
assert!(EquityMatrix::from_triangle(&[0u16; 10]).is_err(), "wrong length");
assert!(EquityMatrix::from_triangle(&vec![10_001u16; TRIANGLE_LEN]).is_err(), "above 100 %");
}
#[test]
fn frozen_table_has_the_published_values() {
let m = frozen_equity();
assert!((m.get(id("AA"), id("KK")) - 0.819).abs() < 0.01);
assert!((m.get(id("AKo"), id("QQ")) - 0.43).abs() < 0.012);
assert!((m.get(id("AKs"), id("22")) - 0.50).abs() < 0.012);
assert!((m.get(id("AA"), id("72o")) - 0.88).abs() < 0.02);
}
#[test]
fn frozen_table_reproduces_equity_against_a_random_hand() {
let m = frozen_equity();
let chance = chance_table(DeckRule::FULL);
let vs_random = |c: usize| -> f64 {
let (num, den) = (0..CLASS_COUNT).fold((0.0, 0.0), |(n, d), j| {
let p = chance.probability(c, j);
(n + p * m.get(c, j), d + p)
});
num / den
};
assert!((vs_random(id("AA")) - 0.852).abs() < 0.006, "AA {}", vs_random(id("AA")));
assert!((vs_random(id("32o")) - 0.323).abs() < 0.006, "32o {}", vs_random(id("32o")));
}
#[test]
fn sampled_frozen_entries_regenerate_bit_for_bit_from_the_recorded_seed() {
let pairs = triangle_pairs();
// A fixed stride so the whole table is represented, not just the corners.
for k in (0..TRIANGLE_LEN).step_by(2_411) {
let (i, j) = pairs[k];
if i == j {
continue;
}
let again = pair_equity(&nlhe(), frozen::SEED, frozen::SAMPLES, i, j).expect("equity");
assert_eq!(again, frozen::TRIANGLE[k], "entry {k} ({i},{j})");
}
}