Files
pokerfreak/tests/icm_bubble.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

165 lines
5.4 KiB
Rust

//! ICM-lite: harville equities and the bubble. Same spots, cash against bubble.
use rechner::card::Card;
use rechner::engine::decide::{decide, decide_with};
use rechner::game::{ActionKind, ActionRecord, Choice, SeenAction, Spot, Street, Villain};
use rechner::icm::harville::equities;
use rechner::icm::lage::Turnierlage;
use rechner::icm::risk::{call_premium, required_equity};
use rechner::variant::Variant;
const RANKS: &str = "AKQJT98765432";
fn close(a: f64, b: f64) -> bool {
(a - b).abs() < 1e-9
}
#[test]
fn equities_pay_out_exactly_the_prize_pool() {
let e = equities(&[10.0, 20.0, 30.0, 40.0], &[50.0, 30.0, 20.0]);
assert!(close(e.iter().sum::<f64>(), 100.0), "{e:?}");
assert!(e.windows(2).all(|w| w[0] < w[1]), "bigger stack, bigger equity: {e:?}");
}
#[test]
fn equal_stacks_are_worth_the_same_and_one_winner_takes_first() {
let e = equities(&[5.0; 4], &[50.0, 30.0, 20.0]);
assert!(e.iter().all(|x| close(*x, 25.0)), "{e:?}");
let winner_takes_all = equities(&[1.0, 3.0], &[100.0]);
assert!(close(winner_takes_all[1], 75.0) && close(winner_takes_all[0], 25.0));
}
#[test]
fn a_chip_is_worth_less_than_it_costs_to_lose_one() {
// The core of ICM: the equity gained by doubling up is less than the equity lost by busting.
let (fold, win, lose) = (
equities(&[10.0, 10.0, 20.0, 20.0], &[50.0, 30.0, 20.0])[0],
equities(&[20.0, 10.0, 20.0, 20.0], &[50.0, 30.0, 20.0])[0],
equities(&[1e-9, 10.0, 20.0, 20.0], &[50.0, 30.0, 20.0])[0],
);
assert!(win - fold < fold - lose);
}
#[test]
fn the_required_equity_exceeds_the_chip_break_even_on_the_bubble() {
let needed = required_equity(&[20.0, 20.0], &[50.0, 30.0, 20.0], 10.0, 1e-9, 10.0, 11.5).expect("priced");
assert!(needed > 10.0 / 21.5, "{needed}");
}
#[test]
fn winner_takes_all_has_no_premium() {
// Winner-takes-all: money is linear in chips, so money EV equals chip EV.
let needed = required_equity(&[20.0, 20.0], &[100.0], 10.0, 1e-9, 10.0, 11.5).expect("priced");
assert!((needed - 10.0 / 21.5).abs() < 1e-6, "{needed}");
}
fn record(seat: u8, action: SeenAction, amount: f64) -> ActionRecord {
ActionRecord { street: Street::Preflop, seat, action, amount: Some(amount) }
}
/// Heads-up pot, hero in the big blind (seat 1) facing the small blind's (seat 2) jam of `depth` bb.
fn facing_jam(hole: &str, depth: f64) -> Spot {
Spot {
variant: Variant::Nlhe,
big_blind: 1.0,
seats: 2,
hero_seat: 1,
button_seat: Some(2),
hero_cards: Card::parse_list(hole).expect("cards"),
board: vec![],
street: Street::Preflop,
pot: depth + 1.0,
to_call: depth - 1.0,
hero_stack: depth - 1.0,
min_raise_to: None,
max_raise_to: None,
legal: vec![ActionKind::Fold, ActionKind::Call],
allowed_sizes: None,
villains: vec![Villain { seat: 2, stack: depth, folded: false, profile: None }],
seed: 7,
history: vec![
record(2, SeenAction::PostSmallBlind, 0.5),
record(1, SeenAction::PostBigBlind, 1.0),
record(2, SeenAction::AllIn, depth),
],
}
}
fn bubble(depth: f64) -> Turnierlage {
Turnierlage {
payouts: vec![50.0, 30.0, 20.0],
stacks: vec![depth, depth, 2.0 * depth, 2.0 * depth],
hero: 0,
ante: 0.0,
level: 1,
}
}
fn holes() -> Vec<String> {
let r: Vec<char> = RANKS.chars().collect();
let mut out = Vec::new();
for (i, a) in r.iter().enumerate() {
for b in &r[i..] {
if a == b {
out.push(format!("{a}s {b}h"));
} else {
out.push(format!("{a}s {b}s"));
out.push(format!("{a}s {b}h"));
}
}
}
out
}
fn calls(choice: Choice) -> bool {
matches!(choice, Choice::Call)
}
#[test]
fn bubble_spots_call_narrower_than_cash_at_every_depth() {
let mut printed = String::new();
for depth in [5.0, 10.0, 15.0, 25.0] {
let (mut cash_calls, mut bubble_calls, mut wider) = (0, 0, 0);
let lage = bubble(depth);
for hole in holes() {
let spot = facing_jam(&hole, depth);
let cash = calls(decide(&spot).choice);
let money = calls(decide_with(&spot, Some(&lage)).choice);
cash_calls += usize::from(cash);
bubble_calls += usize::from(money);
wider += usize::from(money && !cash);
}
printed += &format!("{depth} bb: Cash {cash_calls} von 169 Haenden, Bubble {bubble_calls}\n");
assert_eq!(wider, 0, "{depth} bb: the bubble never calls a hand cash folds");
assert!(bubble_calls < cash_calls, "{depth} bb: {bubble_calls} >= {cash_calls}");
}
println!("{printed}");
}
#[test]
fn without_a_situation_nothing_changes() {
for hole in ["As Ah", "7s 2h", "Ks Qs"] {
let spot = facing_jam(hole, 12.0);
assert_eq!(decide(&spot), decide_with(&spot, None));
}
}
#[test]
fn an_unusable_situation_abstains() {
let spot = facing_jam("As Ah", 12.0);
for lage in [
Turnierlage { payouts: vec![], ..bubble(12.0) },
Turnierlage { hero: 9, ..bubble(12.0) },
Turnierlage { stacks: vec![12.0, f64::NAN], ..bubble(12.0) },
] {
assert_eq!(decide_with(&spot, Some(&lage)).choice, Choice::Abstain);
}
}
#[test]
fn no_bet_to_face_costs_no_premium() {
let mut spot = facing_jam("As Ah", 12.0);
spot.to_call = 0.0;
assert_eq!(call_premium(&spot, &bubble(12.0)), Some(0.0));
}