//! The CFR core against a game with a known analytic equilibrium: Kuhn poker. //! //! Kuhn (1950): three cards J < Q < K, one dealt to each player, ante 1, one bet of 1. //! Known solution (Kuhn 1950; Wikipedia "Kuhn poker"): game value for the first player = -1/18; //! the first player bets J with probability a in [0, 1/3], K with 3a, checks Q always, and after //! check-bet calls with Q at 1/3 + a, K always, J never. The second player facing a bet calls //! with K always, Q at 1/3, J never; after a check bets J at 1/3, K always, Q never. use rechner::cfr::exploitability::{best_response_gain, exploitability, game_value}; use rechner::cfr::iterate::iterate; use rechner::cfr::solve::solve; use rechner::cfr::solver_state::SolverState; use rechner::cfr::strategy::{average_strategy, Strategy}; use rechner::cfr::tree::{Node, Payoff, Tree}; use rechner::rng::Rng; const J: usize = 0; const Q: usize = 1; const K: usize = 2; fn showdown(stake: f64) -> Payoff { let cell = |i: usize, j: usize| if i > j { stake } else { -stake }; Payoff::Pair((0..9).map(|k| if k / 3 == k % 3 { 0.0 } else { cell(k / 3, k % 3) }).collect()) } /// Node ids: 0 first player, 1 second player after check, 2 first player vs bet, 3 second vs bet. fn kuhn() -> Tree { let chance = (0..9).map(|k| if k / 3 == k % 3 { 0.0 } else { 1.0 / 6.0 }).collect(); let nodes = vec![ Node::Decision { player: 0, name: "p0_open", children: vec![1, 3] }, Node::Decision { player: 1, name: "p1_after_check", children: vec![4, 2] }, Node::Decision { player: 0, name: "p0_vs_bet", children: vec![5, 6] }, Node::Decision { player: 1, name: "p1_vs_bet", children: vec![7, 8] }, Node::Terminal(showdown(1.0)), // 4: check-check Node::Terminal(Payoff::Flat(-1.0)), // 5: check-bet-fold Node::Terminal(showdown(2.0)), // 6: check-bet-call Node::Terminal(Payoff::Flat(1.0)), // 7: bet-fold Node::Terminal(showdown(2.0)), // 8: bet-call ]; Tree { classes: 3, chance, nodes, root: 0 } } fn solved(iterations: u32) -> (Tree, Strategy) { let tree = kuhn(); let (state, _) = (0..iterations) .fold((SolverState::fresh(&tree), Rng::seeded(1)), |(s, r), _| iterate(&tree, s, r)); let strategy = average_strategy(&tree, &state); (tree, strategy) } /// Probability of the last action (bet / call) of `class` at the named node. fn last_action(tree: &Tree, strategy: &Strategy, name: &str, class: usize) -> f64 { let node = tree.find(name).expect("node"); let actions = tree.action_count(node).expect("decision"); strategy.probability(node, class, actions - 1) } #[test] fn kuhn_value_and_exploitability_converge() { let (tree, strategy) = solved(20_000); assert!((game_value(&tree, &strategy) + 1.0 / 18.0).abs() < 0.002, "value"); assert!(exploitability(&tree, &strategy) < 0.002, "exploitability {}", exploitability(&tree, &strategy)); assert!(best_response_gain(&tree, &strategy, 0) >= -1e-9); assert!(best_response_gain(&tree, &strategy, 1) >= -1e-9); } #[test] fn kuhn_strategy_has_the_analytic_shape() { let (tree, s) = solved(30_000); let a = last_action(&tree, &s, "p0_open", J); assert!(a < 0.34, "J bluff frequency in [0, 1/3]: {a}"); assert!((last_action(&tree, &s, "p0_open", K) - 3.0 * a).abs() < 0.04, "K bets three times J"); assert!(last_action(&tree, &s, "p0_open", Q) < 0.03, "Q checks"); assert!((last_action(&tree, &s, "p0_vs_bet", Q) - (1.0 / 3.0 + a)).abs() < 0.04); assert!(last_action(&tree, &s, "p0_vs_bet", K) > 0.97); assert!(last_action(&tree, &s, "p0_vs_bet", J) < 0.03); assert!(last_action(&tree, &s, "p1_vs_bet", K) > 0.97); assert!((last_action(&tree, &s, "p1_vs_bet", Q) - 1.0 / 3.0).abs() < 0.03); assert!(last_action(&tree, &s, "p1_vs_bet", J) < 0.03); assert!((last_action(&tree, &s, "p1_after_check", J) - 1.0 / 3.0).abs() < 0.03); assert!(last_action(&tree, &s, "p1_after_check", K) > 0.97); assert!(last_action(&tree, &s, "p1_after_check", Q) < 0.03); } #[test] fn the_uniform_strategy_is_clearly_exploitable() { let tree = kuhn(); // Uniform strategy: never folds, bets half the time; strictly exploitable. let uniform = average_strategy(&tree, &SolverState::fresh(&tree)); assert!(exploitability(&tree, &uniform) > 0.05); // "Always call, always bet": the best response folds / checks accordingly; exploitability > 0. let value = game_value(&tree, &uniform); assert!(value.abs() < 0.5); } #[test] fn iteration_is_a_pure_fold_and_deterministic() { let tree = kuhn(); let start = SolverState::fresh(&tree); let (a, ra) = iterate(&tree, start.clone(), Rng::seeded(5)); let (b, rb) = iterate(&tree, start.clone(), Rng::seeded(5)); assert_eq!(a, b); assert_eq!(ra, rb); // The input value is untouched by the step (it was cloned, but also compare to a fresh one). assert_eq!(start, SolverState::fresh(&tree)); assert_eq!(a.iteration(), 1); // Different seeds give the identical result: full-width CFR draws no random numbers. let (c, _) = iterate(&tree, start, Rng::seeded(99)); assert_eq!(a, c); } #[test] fn solve_reports_checkpoints_and_stability() { let tree = kuhn(); let solved = solve(&tree, 4_000, Rng::seeded(1)); assert_eq!(solved.iterations, 4_000); assert_eq!(solved.checkpoints.last().map(|c| c.0), Some(4_000)); let curve: Vec = solved.checkpoints.iter().map(|c| c.1).collect(); assert!(curve.first() > curve.last(), "exploitability falls: {curve:?}"); assert!(solved.stable_since <= 4_000); assert!(solved.final_exploitability < 0.01); // solve is the same fold as iterating by hand. let (_, strategy) = solved_by_hand(4_000); assert_eq!(strategy, solved.strategy); } fn solved_by_hand(iterations: u32) -> (Tree, Strategy) { solved(iterations) } #[test] fn degenerate_inputs_do_not_panic() { let empty = Tree { classes: 0, chance: vec![], nodes: vec![], root: 0 }; let state = SolverState::fresh(&empty); let (state, _) = iterate(&empty, state, Rng::seeded(1)); let strategy = average_strategy(&empty, &state); assert_eq!(exploitability(&empty, &strategy), 0.0); let lonely = Tree { classes: 1, chance: vec![1.0], nodes: vec![Node::Terminal(Payoff::Flat(0.25))], root: 0, }; let s = average_strategy(&lonely, &SolverState::fresh(&lonely)); assert!((game_value(&lonely, &s) - 0.25).abs() < 1e-12); // A root index outside the tree is a no-op, not a panic. let broken = Tree { classes: 1, chance: vec![1.0], nodes: vec![], root: 7 }; let s = average_strategy(&broken, &SolverState::fresh(&broken)); assert_eq!(game_value(&broken, &s), 0.0); }