- river_spieler::solver_lists: Kippung der Gegnerrange nach RangeRead (nur bei confidence > 0) - river_spieler::lock_villain + TrSolver:🔒 geglaubte Gegnerstrategie am River (fold_to_bet, bluff_share), Gewicht = trust - ohne Read / confidence 0 bitgleich (Test) - Turn (Stufe B): Wandzeitbudget 1700 ms je Entscheidung inkl. Rueckfall-Solve, MIN_ITERS 60 - Flop: Rueckfall-Solve nur im Restbudget - Abdeckung Turn: 427/478 HU (89 %), p50 2045 p99 3781 ms (vor dem Budget) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
266 lines
13 KiB
Rust
266 lines
13 KiB
Rust
use super::*;
|
|
use crate::card::Card;
|
|
use crate::game::{ActionKind, ActionRecord, Choice, SeenAction, Spot, Street, Villain};
|
|
use crate::variant::Variant;
|
|
use std::sync::Mutex;
|
|
|
|
/// The switch is process-global: tests that touch it take turns.
|
|
static MODE_LOCK: Mutex<()> = Mutex::new(());
|
|
|
|
fn rec(street: Street, seat: u8, action: SeenAction, amount: Option<f64>) -> ActionRecord {
|
|
ActionRecord { street, seat, action, amount }
|
|
}
|
|
|
|
/// Six-max S1 line to the turn: button (seat 4) opens 2.5, big blind (seat 0) calls; flop check, bet 1.8,
|
|
/// call. Turn pot 9.1. `turn` is the turn line so far; hero is the button.
|
|
fn s1_turn(turn: &[ActionRecord], hero: &str, to_call: f64, pot_extra: f64) -> Spot {
|
|
use SeenAction::*;
|
|
use Street::*;
|
|
let mut h = vec![
|
|
rec(Preflop, 5, PostSmallBlind, Some(0.5)),
|
|
rec(Preflop, 0, PostBigBlind, Some(1.0)),
|
|
rec(Preflop, 1, Fold, None),
|
|
rec(Preflop, 2, Fold, None),
|
|
rec(Preflop, 3, Fold, None),
|
|
rec(Preflop, 4, Raise, Some(2.5)),
|
|
rec(Preflop, 5, Fold, None),
|
|
rec(Preflop, 0, Call, Some(2.5)),
|
|
rec(Flop, 0, Check, None),
|
|
rec(Flop, 4, Bet, Some(1.8)),
|
|
rec(Flop, 0, Call, Some(1.8)),
|
|
];
|
|
h.extend_from_slice(turn);
|
|
let legal = if to_call > 0.0 { vec![ActionKind::Fold, ActionKind::Call, ActionKind::Raise] } else { vec![ActionKind::Check, ActionKind::Raise] };
|
|
Spot {
|
|
variant: Variant::Nlhe,
|
|
big_blind: 1.0,
|
|
seats: 6,
|
|
hero_seat: 4,
|
|
button_seat: Some(4),
|
|
hero_cards: Card::parse_list(hero).unwrap(),
|
|
board: Card::parse_list("Ah 7d 2c Ks").unwrap(),
|
|
street: Street::Turn,
|
|
pot: 9.1 + pot_extra,
|
|
to_call,
|
|
hero_stack: 95.7,
|
|
min_raise_to: Some(if to_call > 0.0 { 2.0 * to_call } else { 1.0 }),
|
|
max_raise_to: Some(95.7),
|
|
legal,
|
|
allowed_sizes: None,
|
|
villains: vec![Villain { seat: 0, stack: 95.7 - to_call, folded: false, profile: None }],
|
|
seed: 7,
|
|
history: h,
|
|
}
|
|
}
|
|
|
|
fn facing_bet(hero: &str) -> Spot {
|
|
s1_turn(&[rec(Street::Turn, 0, SeenAction::Bet, Some(6.0))], hero, 6.0, 6.0)
|
|
}
|
|
|
|
fn hero_first(hero: &str) -> Spot {
|
|
// Hero on the button would be last to act; here villain checks and hero decides.
|
|
s1_turn(&[rec(Street::Turn, 0, SeenAction::Check, None)], hero, 0.0, 0.0)
|
|
}
|
|
|
|
/// A range of `n` combos off the board (and `avoid`), pseudo-random, weights 0.2..1.2; always includes `must`.
|
|
fn range(n: usize, seed: u64, must: &[&str]) -> Vec<(Vec<Card>, f64)> {
|
|
let all = Card::parse_list("2c 3c 4c 5c 6c 7c 8c 9c Tc Jc Qc Kc Ac 2d 3d 4d 5d 6d 7d 8d 9d Td Jd Qd Kd Ad 2h 3h 4h 5h 6h 7h 8h 9h Th Jh Qh Kh Ah 2s 3s 4s 5s 6s 7s 8s 9s Ts Js Qs Ks As").unwrap();
|
|
let board = Card::parse_list("Ah 7d 2c Ks").unwrap();
|
|
let cards: Vec<Card> = all.into_iter().filter(|c| !board.contains(c)).collect();
|
|
let mut x = seed;
|
|
let mut next = || {
|
|
x = x.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407);
|
|
(x >> 33) as usize
|
|
};
|
|
let mut out: Vec<(Vec<Card>, f64)> = must.iter().map(|m| (Card::parse_list(m).unwrap(), 1.0)).collect();
|
|
while out.len() < n {
|
|
let (a, b) = (cards[next() % cards.len()], cards[next() % cards.len()]);
|
|
if a != b && !out.iter().any(|(c, _)| c.contains(&a) && c.contains(&b)) {
|
|
out.push((vec![a, b], 0.2 + (next() % 1000) as f64 / 1000.0));
|
|
}
|
|
}
|
|
out
|
|
}
|
|
|
|
fn cfg(iterations: u32) -> TurnConfig {
|
|
TurnConfig { max_combos: 200, cover: 1.0, river_cards: 12, iterations, measure: true, river_raises: 1, budget_ms: 0.0 }
|
|
}
|
|
|
|
#[test]
|
|
fn the_decision_is_a_legal_choice_at_both_kinds_of_node() {
|
|
let hands = ["Ac Qd", "7h 7c", "3c 4d", "Kd Qs", "9h 8h"];
|
|
for (spot_of, name) in [(facing_bet as fn(&str) -> Spot, "facing a bet"), (hero_first as fn(&str) -> Spot, "first to act")] {
|
|
for hero in hands {
|
|
let spot = spot_of(hero);
|
|
let villain = range(80, 5, &[]);
|
|
let me = range(80, 6, &[hero]);
|
|
let s = solve_turn(&spot, &me, &villain, &cfg(60)).expect("heads-up turn decision is in scope");
|
|
let sum: f64 = s.row.iter().sum();
|
|
assert!((sum - 1.0).abs() < 1e-9, "{name} {hero}: row {:?}", s.row);
|
|
match s.decision.choice {
|
|
Choice::Fold => assert!(spot.to_call > 0.0, "{name}: fold only when facing a bet"),
|
|
Choice::Check => assert_eq!(spot.to_call, 0.0, "{name}"),
|
|
Choice::Call => assert!(spot.to_call > 0.0, "{name}"),
|
|
Choice::Raise { to } => {
|
|
let (lo, hi) = (spot.min_raise_to.unwrap(), spot.max_raise_to.unwrap());
|
|
assert!(to >= lo - 1e-9 && to <= hi + 1e-9, "{name} {hero}: raise to {to} outside [{lo}, {hi}]");
|
|
}
|
|
Choice::Abstain => panic!("{name} {hero}: abstained"),
|
|
}
|
|
assert!(spot.is_legal(match s.decision.choice {
|
|
Choice::Fold => ActionKind::Fold,
|
|
Choice::Check => ActionKind::Check,
|
|
Choice::Call => ActionKind::Call,
|
|
_ => ActionKind::Raise,
|
|
}));
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn out_of_position_hero_acts_first_with_check_or_a_bet() {
|
|
// Hero is the big blind (seat 0) and acts first on the turn; the villain is the button (seat 4).
|
|
let mut spot = s1_turn(&[], "Ac Qd", 0.0, 0.0);
|
|
spot.hero_seat = 0;
|
|
spot.villains = vec![Villain { seat: 4, stack: 95.7, folded: false, profile: None }];
|
|
let (me, villain) = (range(60, 51, &["Ac Qd"]), range(60, 52, &[]));
|
|
let s = solve_turn(&spot, &me, &villain, &cfg(80)).expect("hero first");
|
|
assert_eq!(s.acts[0], Act::Check, "check is the first action at a free node: {:?}", s.acts);
|
|
assert!(s.acts.iter().skip(1).all(|a| matches!(a, Act::Bet(_))));
|
|
assert!(matches!(s.decision.choice, Choice::Check | Choice::Raise { .. }));
|
|
if let Choice::Raise { to } = s.decision.choice {
|
|
assert!(to >= spot.min_raise_to.unwrap() - 1e-9 && to <= spot.max_raise_to.unwrap() + 1e-9);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn strong_hands_do_not_fold_and_air_folds_more() {
|
|
// The villain's range: top pair or better only (aces, kings, pairs of 7 and 2).
|
|
let villain: Vec<(Vec<Card>, f64)> = range(400, 21, &[]).into_iter().filter(|(c, _)| c.iter().any(|x| x.rank as u8 >= 11) || c[0].rank == c[1].rank).collect();
|
|
assert!(villain.len() > 30, "{}", villain.len());
|
|
let me = range(100, 22, &["7s 7c", "3c 4d"]);
|
|
let spot_set = facing_bet("7s 7c");
|
|
let spot_air = facing_bet("3c 4d");
|
|
let set = solve_turn(&spot_set, &me, &villain, &cfg(150)).unwrap();
|
|
let air = solve_turn(&spot_air, &me, &villain, &cfg(150)).unwrap();
|
|
eprintln!("set row {:?}, air row {:?} ({:?})", set.row, air.row, set.acts);
|
|
assert!(set.row[0] < 0.05, "a set never folds to a half-pot bet here: {:?}", set.row);
|
|
assert!(air.row[0] > set.row[0] + 0.3, "air folds clearly more: {:?} vs {:?}", air.row, set.row);
|
|
}
|
|
|
|
#[test]
|
|
fn the_solve_is_deterministic_and_the_game_is_balanced_enough() {
|
|
let spot = facing_bet("Ac Qd");
|
|
let (me, villain) = (range(150, 31, &["Ac Qd"]), range(150, 32, &[]));
|
|
let a = solve_turn(&spot, &me, &villain, &cfg(200)).unwrap();
|
|
let b = solve_turn(&spot, &me, &villain, &cfg(200)).unwrap();
|
|
assert_eq!(a.row, b.row);
|
|
assert_eq!(a.decision.choice, b.decision.choice, "same input, same answer");
|
|
let e = a.solved.exploit.expect("measured");
|
|
eprintln!("150x150 combos, 12 river cards, 200 iterations: {:.0} ms, exploitability {:.3} chips = {:.1} % of the {:.1} pot = {:.0} bb/100 (hero at the real node)", a.solved.millis, e.total, e.total / a.solved.pot * 100.0, a.solved.pot, a.solved.exploit_bb_per_100().unwrap());
|
|
assert!(e.total < 0.05 * a.solved.pot, "exploitability {} on a {} pot", e.total, a.solved.pot);
|
|
}
|
|
|
|
#[test]
|
|
fn a_spot_outside_the_game_is_left_to_the_heuristic() {
|
|
let me = range(50, 1, &["Ac Qd"]);
|
|
let villain = range(50, 2, &[]);
|
|
let mut river = facing_bet("Ac Qd");
|
|
river.street = Street::River;
|
|
river.board = Card::parse_list("Ah 7d 2c Ks 9h").unwrap();
|
|
assert!(solve_turn(&river, &me, &villain, &cfg(10)).is_none());
|
|
let mut multiway = facing_bet("Ac Qd");
|
|
multiway.villains.push(Villain { seat: 1, stack: 90.0, folded: false, profile: None });
|
|
assert!(solve_turn(&multiway, &me, &villain, &cfg(10)).is_none());
|
|
assert!(solve_turn(&facing_bet("Ac Qd"), &me, &[], &cfg(10)).is_none(), "no villain range");
|
|
}
|
|
|
|
#[test]
|
|
fn off_means_none_and_the_engine_is_unchanged() {
|
|
let _g = MODE_LOCK.lock().unwrap_or_else(|e| e.into_inner());
|
|
set_turn_player(TurnPlayer::Off);
|
|
let spots = [facing_bet("Ac Qd"), facing_bet("7s 7c"), hero_first("3c 4d"), hero_first("Kd Qs")];
|
|
let mut flop = hero_first("Ac Qd");
|
|
flop.street = Street::Flop;
|
|
flop.board.truncate(3);
|
|
let mut river = facing_bet("Ac Qd");
|
|
river.street = Street::River;
|
|
river.board = Card::parse_list("Ah 7d 2c Ks 9h").unwrap();
|
|
for spot in spots.iter().chain([&flop, &river]) {
|
|
assert!(turn_game_decision(spot).is_none(), "off: the hook says nothing");
|
|
}
|
|
// Switched on without a blueprint (no GRUNDSTRATEGIE) the hook still says nothing, so the engine's
|
|
// answer is the same either way: compare whole decisions.
|
|
if std::env::var_os("GRUNDSTRATEGIE").is_none() {
|
|
let off: Vec<_> = spots.iter().chain([&flop, &river]).map(crate::engine::decide::decide).collect();
|
|
set_turn_player(TurnPlayer::Grund);
|
|
let on: Vec<_> = spots.iter().chain([&flop, &river]).map(crate::engine::decide::decide).collect();
|
|
set_turn_player(TurnPlayer::Off);
|
|
assert_eq!(off, on, "no blueprint: the heuristic plays unchanged");
|
|
}
|
|
assert_eq!(turn_player(), TurnPlayer::Off);
|
|
}
|
|
|
|
#[test]
|
|
fn the_public_key_ignores_hero_cards_but_not_the_line() {
|
|
let c = TurnConfig::STANDARD;
|
|
assert_eq!(public_key(&facing_bet("Ac Qd"), &c), public_key(&facing_bet("7s 7c"), &c));
|
|
assert_ne!(public_key(&facing_bet("Ac Qd"), &c), public_key(&hero_first("Ac Qd"), &c));
|
|
}
|
|
|
|
#[test]
|
|
#[ignore = "timing probe"]
|
|
fn tree_sizes() {
|
|
use crate::cfr::turn_river_game::{build, TNode, TrBets};
|
|
for (name, forced, first) in [("hero first", vec![], 0usize), ("facing bet", vec![Act::Bet(6.0)], 1), ("check-raise spot", vec![Act::Check, Act::Bet(6.0)], 0)] {
|
|
let g = build(&TrBets::standard(9.1, 95.0, first), &forced);
|
|
let chance = g.nodes.iter().filter(|n| matches!(n, TNode::Chance { .. })).count();
|
|
let river_dec = g.nodes.iter().filter(|n| matches!(n, TNode::Dec { street: 1, .. })).count();
|
|
let turn_dec = g.nodes.iter().filter(|n| matches!(n, TNode::Dec { street: 0, .. })).count();
|
|
eprintln!("{name}: {} nodes, {turn_dec} turn decisions, {chance} river chance nodes, {river_dec} river decisions", g.nodes.len());
|
|
}
|
|
}
|
|
|
|
/// Solve time at table size (run with `--ignored --nocapture`).
|
|
#[test]
|
|
#[ignore = "timing probe"]
|
|
fn solve_time_at_table_size() {
|
|
for (n, cards, iters, rr) in [(160usize, 10usize, 200u32, 0usize), (160, 10, 400, 0), (160, 10, 200, 1), (100, 10, 200, 0)] {
|
|
let spot = facing_bet("Ac Qd");
|
|
let (me, villain) = (range(n, 41, &["Ac Qd"]), range(n, 42, &[]));
|
|
let c = TurnConfig { max_combos: n, cover: 1.0, river_cards: cards, iterations: iters, measure: true, river_raises: rr, budget_ms: 0.0 };
|
|
let s = solve_turn(&spot, &me, &villain, &c).unwrap();
|
|
let e = s.solved.exploit.unwrap();
|
|
eprintln!(
|
|
"{n} combos, {cards} river cards, {iters} iterations, river raises {rr}: {:.0} ms ({} nodes), exploitability {:.2} % of pot = {:.0} bb/100",
|
|
s.solved.millis,
|
|
s.solved.nodes,
|
|
e.total / s.solved.pot * 100.0,
|
|
s.solved.exploit_bb_per_100().unwrap()
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Stage B (SOLVER_TURN): off, nothing; on, a heads-up turn is decided from the range model when no
|
|
/// blueprint is loaded, for any hero hand (in or out of the pruned range), with a legal choice.
|
|
#[test]
|
|
fn stage_b_decides_every_heads_up_turn_from_the_range_model() {
|
|
let _g = MODE_LOCK.lock().unwrap();
|
|
set_turn_ueberall(false);
|
|
assert!(turn_solver_decision(&facing_bet("Qc Jc")).is_none());
|
|
set_turn_ueberall(true);
|
|
for spot in [facing_bet("Qc Jc"), facing_bet("3s 3d"), hero_first("Ac Kc"), facing_bet("9h 4h")] {
|
|
let d = turn_solver_decision(&spot).expect("heads-up turn is in the stage-B slice");
|
|
assert!(d.reason.contains("solver turn"), "{}", d.reason);
|
|
match d.choice {
|
|
Choice::Fold | Choice::Call => assert!(spot.to_call > 0.0),
|
|
Choice::Check => assert_eq!(spot.to_call, 0.0),
|
|
_ => {}
|
|
}
|
|
}
|
|
let mut multi = facing_bet("Qc Jc");
|
|
multi.villains.push(Villain { seat: 1, stack: 90.0, folded: false, profile: None });
|
|
assert!(turn_solver_decision(&multi).is_none());
|
|
set_turn_ueberall(false);
|
|
}
|