Die Welt nannte 'pokerfreak' nur noch in zwei Harness-Zeilen - der Rest waren veraltete Rust-Pruefungen im Pruefordner, die der Aufbau nie auffrischte.
129 lines
5.7 KiB
Rust
129 lines
5.7 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 rechner::cfr::chance_table::chance_table;
|
|
use rechner::cfr::equity_matrix::{triangle_pairs, EquityMatrix, TRIANGLE_LEN};
|
|
use rechner::cfr::equity_pair::pair_equity;
|
|
use rechner::cfr::frozen_equity::frozen_equity;
|
|
use rechner::cfr::generated_equity as frozen;
|
|
use rechner::cfr::hand_class::CLASS_COUNT;
|
|
use rechner::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})");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn frozen_short_deck_table_has_the_reference_values_and_only_deck_classes() {
|
|
// Exact-enumeration references for short deck (36 cards, flush beats full house):
|
|
// AA vs KK 74.26 %, AKs vs QQ 54.28 %, AKo vs JTs 49.35 %. The frozen table is Monte Carlo
|
|
// (60 000 samples per pair, standard error about 0.2 %) and lands within about one point.
|
|
use rechner::cfr::chart_deck::ChartDeck;
|
|
use rechner::cfr::frozen_equity::frozen_equity_for;
|
|
use rechner::cfr::hand_class::class_exists;
|
|
let m = frozen_equity_for(ChartDeck::Short);
|
|
assert!((m.get(id("AA"), id("KK")) - 0.7426).abs() < 0.012, "{}", m.get(id("AA"), id("KK")));
|
|
assert!((m.get(id("AKs"), id("QQ")) - 0.5428).abs() < 0.012, "{}", m.get(id("AKs"), id("QQ")));
|
|
assert!((m.get(id("AKo"), id("JTs")) - 0.4935).abs() < 0.012, "{}", m.get(id("AKo"), id("JTs")));
|
|
// Short deck differs from hold'em: AA vs KK is far closer than 82 %.
|
|
assert!(m.get(id("AA"), id("KK")) < frozen_equity().get(id("AA"), id("KK")) - 0.05);
|
|
let deck = rules(Variant::ShortDeck).deck;
|
|
assert_eq!((0..CLASS_COUNT).filter(|c| class_exists(deck, *c)).count(), 81);
|
|
assert!(!class_exists(deck, id("52o")) && class_exists(deck, id("A6s")));
|
|
}
|