Files
pokerfreak/tests/cfr_games.rs
T
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

148 lines
6.2 KiB
Rust

//! The two preflop game trees: structure, input faults and exact payoff identities.
//!
//! Identities used (all independent of the solver): with a symmetric chance table and an
//! antisymmetric equity matrix, "always push / always call" from the small blind is worth
//! exactly 0 (a fair coin on every deal), "always push / always fold" is +1 (the big blind),
//! "always fold" is -0.5 (the small blind already posted).
mod cfr_support;
use rechner::cfr::environment::Environment;
use rechner::cfr::exploitability::game_value;
use rechner::cfr::open_game::open_tree;
use rechner::cfr::push_fold_game::push_fold_tree;
use rechner::cfr::seat::Seat;
use rechner::cfr::spot_params::{OPEN_SIZE, THREE_BET_SIZE, WAKE_FRACTION};
use rechner::cfr::strategy::Strategy;
use rechner::cfr::tree::Node;
use rechner::cfr::wake_range::top_membership;
use rechner::variant::{rules, Variant};
fn env() -> Environment {
Environment::frozen_free_table()
}
fn names(tree: &rechner::cfr::tree::Tree) -> Vec<&'static str> {
tree.nodes
.iter()
.filter_map(|n| match n {
Node::Decision { name, .. } => Some(*name),
_ => None,
})
.collect()
}
#[test]
fn seats_know_how_many_players_act_behind_them() {
let behind: Vec<usize> = Seat::ALL.iter().map(|s| s.players_behind()).collect();
// EP, MP, CO, BTN, SB: the big blind is the modelled opponent, the others are "behind".
assert_eq!(behind, vec![4, 3, 2, 1, 0]);
assert!(Seat::Button.is_in_position_against_big_blind());
assert!(!Seat::SmallBlind.is_in_position_against_big_blind());
assert_eq!(Seat::Early.label(), "EP");
}
#[test]
fn push_fold_tree_has_two_decisions_over_169_classes() {
let tree = push_fold_tree(&env(), Seat::Button, 10.0).expect("tree");
assert_eq!(tree.classes, 169);
let mut found = names(&tree);
found.sort_unstable();
assert_eq!(found, vec!["call", "push"]);
assert!(tree.find("push").is_some() && tree.find("call").is_some());
assert_eq!(tree.action_count(tree.find("push").unwrap()), Some(2));
}
#[test]
fn open_tree_has_the_four_documented_decisions() {
let tree = open_tree(&env(), Seat::Cutoff, 40.0).expect("tree");
for name in ["open", "defend", "vs_three_bet", "vs_jam"] {
assert!(tree.find(name).is_some(), "{name}");
}
assert_eq!(tree.action_count(tree.find("open").unwrap()), Some(2));
assert_eq!(tree.action_count(tree.find("defend").unwrap()), Some(3));
assert_eq!(tree.action_count(tree.find("vs_three_bet").unwrap()), Some(3));
assert_eq!(tree.action_count(tree.find("vs_jam").unwrap()), Some(2));
}
#[test]
fn bad_stack_depths_are_faults_not_panics() {
let e = env();
for depth in [f64::NAN, f64::INFINITY, 0.0, -5.0, 0.5] {
assert!(push_fold_tree(&e, Seat::Button, depth).is_err(), "push {depth}");
assert!(open_tree(&e, Seat::Button, depth).is_err(), "open {depth}");
}
// The 3-bet size must fit below the stack, otherwise the 3-bet line is not a raise.
assert!(open_tree(&e, Seat::Button, THREE_BET_SIZE).is_err());
assert!(open_tree(&e, Seat::Button, OPEN_SIZE).is_err());
}
#[test]
fn environment_rejects_rules_that_are_not_two_card_hold_em() {
let equity = rechner::cfr::frozen_equity::frozen_equity();
assert!(Environment::new(&rules(Variant::Plo5), equity.clone()).is_err());
assert!(Environment::new(&rules(Variant::Nlhe), equity.clone()).is_ok());
assert!(Environment::new(&rules(Variant::Aof), equity).is_ok());
}
#[test]
fn small_blind_push_fold_identities_are_exact() {
let tree = push_fold_tree(&env(), Seat::SmallBlind, 10.0).expect("tree");
let strat = |push: usize, call: usize| {
Strategy::pure(&tree, |name, _class| if name == "push" { push } else { call })
};
assert!((game_value(&tree, &strat(0, 0)) + 0.5).abs() < 1e-9, "fold: -0.5");
assert!((game_value(&tree, &strat(1, 0)) - 1.0).abs() < 1e-9, "push, they fold: +1");
assert!(game_value(&tree, &strat(1, 1)).abs() < 1e-9, "push, they call: fair coin");
}
#[test]
fn small_blind_open_identities_are_exact() {
let tree = open_tree(&env(), Seat::SmallBlind, 40.0).expect("tree");
// Actions: open [fold, raise]; defend [fold, call, 3-bet]; vs_three_bet [fold, call, jam];
// vs_jam [fold, call].
let strat = |open: usize, defend: usize| {
Strategy::pure(&tree, |name, _| match name {
"open" => open,
"defend" => defend,
_ => 0,
})
};
assert!((game_value(&tree, &strat(0, 0)) + 0.5).abs() < 1e-9, "fold the small blind");
assert!((game_value(&tree, &strat(1, 0)) - 1.0).abs() < 1e-9, "raise, big blind folds");
// Raise, big blind 3-bets everything, the opener always folds: loses the open.
assert!((game_value(&tree, &strat(1, 2)) + OPEN_SIZE_SB).abs() < 1e-9);
}
const OPEN_SIZE_SB: f64 = rechner::cfr::spot_params::SMALL_BLIND_OPEN_SIZE;
#[test]
fn players_behind_cost_the_opener_money_when_they_wake_up() {
// Pushing everything and being folded to is worth the dead money + big blind only if
// nobody behind wakes up; the earlier the seat, the more often somebody does.
let e = env();
let value = |seat: Seat| {
let tree = push_fold_tree(&e, seat, 10.0).expect("tree");
game_value(&tree, &Strategy::pure(&tree, |name, _| if name == "push" { 1 } else { 0 }))
};
// Push-all/fold-all values: BTN > CO > MP > EP (more wake-up chances), all below 1.5.
assert!(value(Seat::Button) > value(Seat::Cutoff));
assert!(value(Seat::Cutoff) > value(Seat::Middle));
assert!(value(Seat::Middle) > value(Seat::Early));
assert!(value(Seat::Button) < 1.5);
}
#[test]
fn wake_range_membership_covers_exactly_the_requested_share_of_combos() {
let e = env();
let membership = top_membership(&e, WAKE_FRACTION);
let combos: f64 = (0..169)
.map(|c| membership[c] * rechner::cfr::hand_class::class_combos(c).len() as f64)
.sum();
assert!((combos / 1326.0 - WAKE_FRACTION).abs() < 1e-9);
assert!(membership.iter().all(|m| (0.0..=1.0).contains(m)));
assert_eq!(membership[cfr_support::id("AA")], 1.0);
assert_eq!(membership[cfr_support::id("72o")], 0.0);
assert_eq!(top_membership(&e, 0.0).iter().sum::<f64>(), 0.0);
assert!((top_membership(&e, 1.0).iter().sum::<f64>() - 169.0).abs() < 1e-9);
}