//! Nash test: best responses against the FROZEN (permille-rounded) strategies, for every spot //! of both decks (full deck: NLHE / AOF, short deck). //! //! For every decision node and hand class the counterfactual value of each action is computed //! with all other decisions frozen, conditional on reaching the node (normalised by the //! opponents' reach mass). Then: //! //! - an action with zero frozen frequency must not beat the frozen mix by more than `EPSILON`; //! - an action played at 5 % or more must not be worse than the mix by more than //! `EPSILON_SUPPORT`; //! - the players' best-response gains (two players for the small blind as first actor, three //! for EP to BTN against both blinds) sum to the exploitability, below `EXPLOITABILITY_BOUND`, //! and equal the value recorded in the table header. //! //! For three players CFR has no convergence guarantee, so this test is the only evidence that //! the three-player tables are equilibria: every one of the three players is checked. //! //! Only infosets that are actually reached count (own reach x opponent reach >= `REACH_FLOOR`); //! an unreached row is any strategy, because its value is multiplied by zero. //! //! Units: big blinds per deal of the spot (the expected result of the whole spot for the player //! who is first to act, so `0.01` bb is `10` milli-bb per hand dealt). use rechner::cfr::chart_deck::ChartDeck; use rechner::cfr::deviation::{deviations, Deviation}; use rechner::cfr::environment::Environment; use rechner::cfr::exploitability::exploitability; use rechner::cfr::frozen_spot::{OpenSpot, PushFoldSpot}; use rechner::cfr::frozen_strategy::{open3_strategy, open_strategy, push_fold3_strategy, push_fold_strategy}; use rechner::cfr::open3_game::open3_tree; use rechner::cfr::open_game::open_tree; use rechner::cfr::push_fold3_game::push_fold3_tree; use rechner::cfr::push_fold_game::push_fold_tree; use rechner::cfr::seat::Seat; use rechner::cfr::walk3::{deviations3, exploitability3}; use rechner::preflop::generated_charts::{AOF_PUSH_FOLD, OPEN, PUSH_FOLD}; use rechner::preflop::generated_charts_short as short; const EPSILON: f64 = 0.02; const EPSILON_SUPPORT: f64 = 0.05; const REACH_FLOOR: f64 = 1e-3; const EXPLOITABILITY_BOUND: f64 = 0.01; fn check(label: &str, exploit: f64, found: &[Deviation], recorded_micro_bb: u32) { assert!(exploit < EXPLOITABILITY_BOUND, "{label}: exploitability {exploit} bb"); let recorded = recorded_micro_bb as f64 * 1e-6; assert!((exploit - recorded).abs() < 2e-6, "{label}: header says {recorded}, got {exploit}"); let mut worst: f64 = 0.0; for d in found.iter().filter(|d| d.weight >= REACH_FLOOR) { worst = worst.max(d.zero_frequency_gain()); assert!( d.zero_frequency_gain() <= EPSILON, "{label}: {} (player {}) class {} would gain {} bb by deviating (weight {})", d.node, d.player, d.class, d.zero_frequency_gain(), d.weight ); assert!( d.support_shortfall() <= EPSILON_SUPPORT, "{label}: {} (player {}) class {} plays an action that loses {} bb to the mix", d.node, d.player, d.class, d.support_shortfall() ); } assert!(worst.is_finite()); } fn check_push_fold(env: &Environment, deck: &str, spot: &PushFoldSpot) { let label = format!("{deck} push/fold {} {}bb", spot.seat.label(), spot.depth_bb); if spot.seat == Seat::SmallBlind { let tree = push_fold_tree(env, spot.seat, spot.depth_bb as f64).expect("tree"); let strategy = push_fold_strategy(&tree, spot).expect("strategy"); check(&label, exploitability(&tree, &strategy), &deviations(&tree, &strategy), spot.meta.exploitability_micro_bb); } else { let tree = push_fold3_tree(env, spot.seat, spot.depth_bb as f64).expect("tree"); let strategy = push_fold3_strategy(&tree, spot).expect("strategy"); let found = deviations3(&tree, &strategy); assert!((0..3).all(|p| found.iter().any(|d| d.player == p)), "{label}: every player is checked"); check(&label, exploitability3(&tree, &strategy), &found, spot.meta.exploitability_micro_bb); } } fn check_open(env: &Environment, deck: &str, spot: &OpenSpot) { let label = format!("{deck} open {} {}bb", spot.seat.label(), spot.depth_bb); if spot.seat == Seat::SmallBlind { let tree = open_tree(env, spot.seat, spot.depth_bb as f64).expect("tree"); let strategy = open_strategy(&tree, spot).expect("strategy"); check(&label, exploitability(&tree, &strategy), &deviations(&tree, &strategy), spot.meta.exploitability_micro_bb); } else { let tree = open3_tree(env, spot.seat, spot.depth_bb as f64).expect("tree"); let strategy = open3_strategy(&tree, spot).expect("strategy"); let found = deviations3(&tree, &strategy); assert!((0..3).all(|p| found.iter().any(|d| d.player == p)), "{label}: every player is checked"); check(&label, exploitability3(&tree, &strategy), &found, spot.meta.exploitability_micro_bb); } } #[test] fn every_frozen_push_fold_spot_is_a_nash_equilibrium_within_epsilon() { let env = Environment::frozen(); assert_eq!(PUSH_FOLD.len(), 35); PUSH_FOLD.iter().for_each(|spot| check_push_fold(&env, "full", spot)); } #[test] fn every_frozen_open_spot_is_a_nash_equilibrium_within_epsilon() { let env = Environment::frozen(); assert_eq!(OPEN.len(), 15); OPEN.iter().for_each(|spot| check_open(&env, "full", spot)); } #[test] fn every_short_deck_spot_is_a_nash_equilibrium_within_epsilon() { let env = Environment::frozen_for(ChartDeck::Short); assert_eq!((short::PUSH_FOLD.len(), short::OPEN.len()), (35, 15)); short::PUSH_FOLD.iter().for_each(|spot| check_push_fold(&env, "short", spot)); short::OPEN.iter().for_each(|spot| check_open(&env, "short", spot)); } #[test] fn all_in_or_fold_variant_uses_the_same_equilibrium() { assert!(std::ptr::eq(AOF_PUSH_FOLD.as_ptr(), PUSH_FOLD.as_ptr())); } #[test] fn the_test_can_fail_a_wrong_strategy_shows_profitable_deviations() { // Take the frozen 10 bb cutoff spot (three players) and replace the pusher by "push every // hand": folding 72o is then a profitable deviation and the exploitability is far above the // bound. The same for a small blind that calls every jam. let env = Environment::frozen(); let spot = PUSH_FOLD.iter().find(|s| s.depth_bb == 10 && s.seat == Seat::Cutoff).expect("spot"); let tree = push_fold3_tree(&env, spot.seat, 10.0).expect("tree"); let frozen = push_fold3_strategy(&tree, spot).expect("strategy"); for (node, label) in [("push", "reckless pusher"), ("sb_call", "calling station small blind")] { let wrong = frozen.with_pure(&tree, node, |_class| 1); assert!(exploitability3(&tree, &wrong) > 5.0 * EXPLOITABILITY_BOUND, "{label}"); let worst = deviations3(&tree, &wrong) .iter() .filter(|d| d.weight >= REACH_FLOOR) .map(|d| d.zero_frequency_gain()) .fold(0.0_f64, f64::max); assert!(worst > 5.0 * EPSILON, "{label}: worst deviation {worst}"); } }