Files
pokerfreak/tests/pipeline_tournament.rs
mmintelandClaude Opus 5.5 91fe71d37d Turnier: chip-lineares Geld spielt exakt den Cash-Pfad (WTA-Test wieder gruen)
tests/pipeline_tournament.rs winner_takes_all_is_cash_on_the_wire war rot (68 vs 88 Calls): der
Push/Fold-Solver lieferte beim WTA Chip-NASH statt des Cash-Pfads. Allgemein ist Geld affin in Chips,
wenn Platz 2..n gleich zahlen (unbezahlt = 0): Turnierlage::is_chip_linear. decide_with laesst eine
solche Lage fallen (der Solver prueft es zusaetzlich); unbepreisbar bleibt Aussetzen.
Zweiter Fall gefunden: alle Verbliebenen gleich bezahlt -> der alte Risiko-Pfad callte 0 von 169
(Praemie explodiert ohne Chip-Abhaengigkeit), jetzt 88 = Cash. Neuer Test chip_linear_money_is_cash_on_the_wire
(gleich bezahlt, 70/10/10/10, WTA-Liste laenger als das Feld, HU 65/35) + Einheitstests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 00:29:34 +01:00

198 lines
8.5 KiB
Rust

//! Optional request field `tournament` (not in VERTRAG.md, proposed in `icm`):
//! `{"payouts": [..], "stacks": [..], "heroIndex": n, "ante": a, "level": l}`.
//! With it the answer is priced in money (ICM-lite), without it cash is unchanged.
mod support;
use rechner::icm::harville::{equities, MAX_PLAYERS};
use rechner::wire::respond::respond;
use support::field;
const RANKS: &str = "AKQJT98765432";
fn card(rank: char, suit: char) -> String {
let rank = match rank {
'A' => "ACE", 'K' => "KING", 'Q' => "QUEEN", 'J' => "JACK", 'T' => "TEN", '9' => "NINE",
'8' => "EIGHT", '7' => "SEVEN", '6' => "SIX", '5' => "FIVE", '4' => "FOUR", '3' => "THREE",
_ => "TWO",
};
let suit = if suit == 's' { "SPADES" } else { "HEARTS" };
format!(r#"{{"rank":"{rank}","suit":"{suit}"}}"#)
}
/// The 169 starting hands, one combination each (spades first card, suited or hearts second).
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!("[{},{}]", card(*a, 's'), card(*b, 'h')));
} else {
out.push(format!("[{},{}]", card(*a, 's'), card(*b, 's')));
out.push(format!("[{},{}]", card(*a, 's'), card(*b, 'h')));
}
}
}
out
}
/// Hero in the big blind (seat 1) faces the small blind's (seat 2) jam of `depth` bb; `extra`
/// is appended to the request object as is.
fn facing_jam(hole: &str, depth: f64, extra: &str) -> String {
format!(
r#"{{"variant":"NLHE","bigBlind":1.0,"seats":2,"heroSeat":1,"buttonSeat":2,"heroCards":{hole},"board":{{"FLOP":null,"TURN":null,"RIVER":null}},"street":"PREFLOP","pot":{pot},"toCall":{call},"heroStack":{call},"legalActions":["FOLD","CALL"],"villains":[{{"seat":2,"stack":{depth},"folded":false}}],"seed":7,"history":[{{"street":"PREFLOP","seat":2,"action":"POST_SB","amount":0.5}},{{"street":"PREFLOP","seat":1,"action":"POST_BB","amount":1.0}},{{"street":"PREFLOP","seat":2,"action":"ALL_IN","amount":{depth}}}]{extra}}}"#,
pot = depth + 1.0,
call = depth - 1.0,
)
}
fn action(response: &str) -> &str {
field(response, "action").expect("action").trim_matches('"')
}
/// How many of the 169 hands call the jam, with `extra` added to each request.
fn calls(depth: f64, extra: &str) -> usize {
holes().iter().filter(|h| action(&respond(&facing_jam(h, depth, extra))) == "CALL").count()
}
fn tournament(payouts: &[f64], stacks: &[f64]) -> String {
let list = |xs: &[f64]| xs.iter().map(|x| format!("{x:?}")).collect::<Vec<_>>().join(",");
format!(
r#","tournament":{{"payouts":[{}],"stacks":[{}],"heroIndex":0,"ante":0.0,"level":3}}"#,
list(payouts),
list(stacks)
)
}
#[test]
fn a_bubble_on_the_wire_calls_fewer_jams_than_cash() {
let depth = 10.0;
let bubble = tournament(&[50.0, 30.0, 20.0], &[depth, depth, 2.0 * depth, 2.0 * depth]);
let (cash, money) = (calls(depth, ""), calls(depth, &bubble));
println!("{depth} bb ueber respond: Cash {cash} von 169, Bubble {money}");
assert!(money < cash, "bubble {money} >= cash {cash}");
}
#[test]
fn a_null_tournament_is_cash() {
for hole in holes().iter().take(12) {
assert_eq!(respond(&facing_jam(hole, 12.0, r#","tournament":null"#)), respond(&facing_jam(hole, 12.0, "")));
}
}
#[test]
fn winner_takes_all_is_cash_on_the_wire() {
// Money is linear in chips when one place is paid: nothing may tighten.
let depth = 10.0;
let wta = tournament(&[100.0], &[depth, depth, 2.0 * depth, 2.0 * depth]);
assert_eq!(calls(depth, &wta), calls(depth, ""));
}
#[test]
fn chip_linear_money_is_cash_on_the_wire() {
// Every place after the first pays alike (incl. 0 when unpaid): equity = c + (p1 - c) * share,
// money is affine in chips. All left paid alike, a top prize over equal rest, and a WTA whose
// payout list is longer than the field.
let depth = 10.0;
let field = [depth, depth, 2.0 * depth, 2.0 * depth];
let cash = calls(depth, "");
for payouts in [&[25.0, 25.0, 25.0, 25.0][..], &[70.0, 10.0, 10.0, 10.0], &[100.0, 0.0, 0.0, 0.0, 0.0]] {
assert_eq!(calls(depth, &tournament(payouts, &field)), cash, "{payouts:?}");
}
// Heads-up any prize split is linear too (only place 2 after place 1).
assert_eq!(calls(depth, &tournament(&[65.0, 35.0], &[depth, depth])), cash, "heads-up 65/35");
}
#[test]
fn a_malformed_tournament_is_an_invalid_request() {
let hole = &holes()[0];
for bad in [
r#","tournament":{"payouts":[50,30,20],"stacks":[10,10,20],"heroIndex":"x","ante":0,"level":1}"#,
r#","tournament":{"stacks":[10,10,20],"heroIndex":0,"ante":0,"level":1}"#,
r#","tournament":{"payouts":[50,30,20],"stacks":"many","heroIndex":0,"ante":0,"level":1}"#,
r#","tournament":[1,2,3]"#,
] {
let answer = respond(&facing_jam(hole, 10.0, bad));
assert_eq!(action(&answer), "ABSTAIN", "{bad}: {answer}");
assert!(answer.contains("invalid request"), "{bad}: {answer}");
}
}
#[test]
fn a_tournament_that_cannot_be_priced_abstains_instead_of_guessing() {
let hole = &holes()[0];
for bad in [
r#","tournament":{"payouts":[],"stacks":[10,10,20],"heroIndex":0,"ante":0,"level":1}"#,
r#","tournament":{"payouts":[50,30,20],"stacks":[10,10,20],"heroIndex":7,"ante":0,"level":1}"#,
r#","tournament":{"payouts":[50,30,20],"stacks":[10,0,20],"heroIndex":0,"ante":0,"level":1}"#,
r#","tournament":{"payouts":[50,30,20],"stacks":[10,10,20],"heroIndex":3,"ante":0,"level":1}"#,
r#","tournament":{"payouts":[-5,30,20],"stacks":[10,10,20],"heroIndex":0,"ante":0,"level":1}"#,
r#","tournament":{"payouts":[0,0,0],"stacks":[10,10,20],"heroIndex":0,"ante":0,"level":1}"#,
] {
assert_eq!(action(&respond(&facing_jam(hole, 10.0, bad))), "ABSTAIN", "{bad}");
}
}
/// The real field of the stock tournament on its bubble: 18 left, 14 paid (shape of
/// `Comm_Tournament_117632`, payouts in percent of the pool).
const PAID_14: [f64; 14] =
[28.38, 18.68, 12.49, 8.56, 6.09, 4.55, 3.57, 2.95, 2.95, 2.53, 2.53, 2.24, 2.24, 2.24];
#[test]
fn a_field_larger_than_the_exact_expansion_is_still_priced() {
let depth = 8.0;
let mut stacks = vec![depth, depth];
stacks.extend([20.0, 25.0, 30.0, 12.0, 40.0, 18.0, 22.0, 35.0, 15.0, 28.0, 33.0, 26.0, 19.0, 24.0, 31.0, 21.0]);
assert!(stacks.len() > MAX_PLAYERS);
let line = tournament(&PAID_14, &stacks);
let (cash, money) = (calls(depth, ""), calls(depth, &line));
println!("18 Spieler, 14 bezahlt, {depth} bb: Cash {cash} von 169, Bubble {money}");
assert!(money < cash, "bubble {money} >= cash {cash}");
assert_eq!(calls(depth, &line), money, "same request, same answer");
}
#[test]
fn sampled_equities_pay_out_the_pool_and_follow_the_stacks() {
let stacks: Vec<f64> = (1..=16).map(|i| i as f64 * 5.0).collect();
let e = equities(&stacks, &PAID_14);
let pool: f64 = PAID_14.iter().sum();
assert!((e.iter().sum::<f64>() - pool).abs() < 1e-6, "{e:?}");
assert!(e.first() < e.last(), "{e:?}");
assert_eq!(e, equities(&stacks, &PAID_14), "deterministic");
}
#[test]
fn sampled_equities_agree_with_the_exact_expansion() {
// Where both can run (10 players): the sample stays within 0.6 % of the pool of the exact value.
let stacks = [3.0, 5.0, 8.0, 9.0, 11.0, 12.0, 14.0, 17.0, 20.0, 25.0];
let payouts = [40.0, 25.0, 15.0, 10.0, 6.0, 4.0];
let exact = equities(&stacks, &payouts);
let sampled = rechner::icm::harville::sampled_equities(&stacks, &payouts);
for (x, s) in exact.iter().zip(&sampled) {
assert!((x - s).abs() < 0.6, "exact {exact:?} sampled {sampled:?}");
}
}
#[test]
fn a_field_beyond_the_largest_priced_one_abstains() {
let stacks = vec![10.0; rechner::icm::harville::MAX_FIELD + 1];
let answer = respond(&facing_jam(&holes()[0], 10.0, &tournament(&PAID_14, &stacks)));
assert_eq!(action(&answer), "ABSTAIN", "{answer}");
}
#[test]
fn up_to_the_exact_limit_the_expansion_is_exact() {
// Equal stacks share the pool exactly; a sample would miss by its noise.
let e = equities(&[7.0; MAX_PLAYERS], &[50.0, 30.0, 20.0]);
assert!(e.iter().all(|x| (x - 10.0).abs() < 1e-9), "{e:?}");
}
#[test]
fn a_sampled_field_may_pay_every_place() {
let stacks: Vec<f64> = (1..=12).map(f64::from).collect();
let payouts: Vec<f64> = (1..=12).rev().map(f64::from).collect();
let e = equities(&stacks, &payouts);
assert!((e.iter().sum::<f64>() - payouts.iter().sum::<f64>()).abs() < 1e-6, "{e:?}");
}