216 lines
6.9 KiB
Rust
216 lines
6.9 KiB
Rust
mod spot_fixture;
|
|
|
|
use pokerfreak::card::deck;
|
|
use pokerfreak::engine::from_plan::from_plan;
|
|
use pokerfreak::engine::postflop::choose_postflop;
|
|
use pokerfreak::equity::estimate::Estimate;
|
|
use pokerfreak::exploit::thresholds::Thresholds;
|
|
use pokerfreak::game::{ActionKind, Choice, Decision, Spot, Street, Villain};
|
|
use pokerfreak::guard::enforce::enforce;
|
|
use pokerfreak::guard::settle::settle;
|
|
use pokerfreak::preflop::plan::{ChartAction, PreflopPlan};
|
|
use pokerfreak::rng::Rng;
|
|
use pokerfreak::variant::{ActionShape, Variant};
|
|
|
|
const CASES: usize = 3000;
|
|
|
|
/// Tiny generator monad over the seeded Rng: each draw returns the value and the next Rng.
|
|
fn unit(rng: Rng) -> (f64, Rng) {
|
|
rng.unit()
|
|
}
|
|
|
|
fn below(rng: Rng, bound: u64) -> (u64, Rng) {
|
|
rng.below(bound)
|
|
}
|
|
|
|
fn chips(rng: Rng, scale: f64) -> (f64, Rng) {
|
|
let (kind, rng) = below(rng, 12);
|
|
let (x, rng) = unit(rng);
|
|
let value = match kind {
|
|
0 => 0.0,
|
|
1 => 0.01 * x,
|
|
_ => (x * scale * 100.0).round() / 100.0,
|
|
};
|
|
(value, rng)
|
|
}
|
|
|
|
fn subset_of_legal(rng: Rng) -> (Vec<ActionKind>, Rng) {
|
|
let (mask, rng) = below(rng, 15);
|
|
let kinds: Vec<ActionKind> = ActionKind::ALL
|
|
.iter()
|
|
.enumerate()
|
|
.filter(|(i, _)| (mask + 1) >> i & 1 == 1)
|
|
.map(|(_, k)| *k)
|
|
.collect();
|
|
(kinds, rng)
|
|
}
|
|
|
|
fn allowed_list(rng: Rng) -> (Option<Vec<f64>>, Rng) {
|
|
let (on, rng) = below(rng, 2);
|
|
if on == 0 {
|
|
return (None, rng);
|
|
}
|
|
let (n, rng) = below(rng, 5);
|
|
let (values, rng) = (0..n).fold((vec![], rng), |(mut acc, rng), _| {
|
|
let (v, rng) = chips(rng, 6.0);
|
|
acc.push(v);
|
|
(acc, rng)
|
|
});
|
|
(Some(values), rng)
|
|
}
|
|
|
|
fn random_spot(rng: Rng) -> (Spot, Rng) {
|
|
let (v, rng) = below(rng, 4);
|
|
let variant = Variant::ALL[v as usize];
|
|
let (s, rng) = below(rng, 4);
|
|
let street = Street::ALL[s as usize];
|
|
let (hole, rng) = (spot_fixture::cards("Ah 5h"), rng);
|
|
let (pot, rng) = chips(rng, 20.0);
|
|
let (to_call, rng) = chips(rng, 5.0);
|
|
let (stack, rng) = chips(rng, 10.0);
|
|
let (min_on, rng) = below(rng, 2);
|
|
let (min, rng) = chips(rng, 2.0);
|
|
let (max_on, rng) = below(rng, 2);
|
|
let (max, rng) = chips(rng, 15.0);
|
|
let (legal, rng) = subset_of_legal(rng);
|
|
let (allowed, rng) = allowed_list(rng);
|
|
let (nv, rng) = below(rng, 4);
|
|
let spot = Spot {
|
|
variant,
|
|
street,
|
|
board: deck::full().into_iter().skip(10).take(street.board_len()).collect(),
|
|
hero_cards: hole,
|
|
pot,
|
|
to_call,
|
|
hero_stack: stack,
|
|
min_raise_to: (min_on == 1).then_some(min),
|
|
max_raise_to: (max_on == 1).then_some(max),
|
|
legal,
|
|
allowed_sizes: allowed,
|
|
villains: (0..nv).map(|i| Villain { seat: i as u8, ..spot_fixture::villain(0) }).collect(),
|
|
..spot_fixture::flop()
|
|
};
|
|
(spot, rng)
|
|
}
|
|
|
|
fn random_estimate(rng: Rng) -> (Estimate, Rng) {
|
|
let (share, rng) = unit(rng);
|
|
let (se, rng) = unit(rng);
|
|
(spot_fixture::estimate(share, se * 0.05), rng)
|
|
}
|
|
|
|
fn random_plan(rng: Rng) -> (PreflopPlan, Rng) {
|
|
let (a, rng) = below(rng, 4);
|
|
let (certainty, rng) = unit(rng);
|
|
let action = [ChartAction::Fold, ChartAction::Call, ChartAction::Raise, ChartAction::Jam][a as usize];
|
|
(spot_fixture::plan(action, certainty), rng)
|
|
}
|
|
|
|
fn random_thresholds(rng: Rng) -> (Thresholds, Rng) {
|
|
let (a, rng) = unit(rng);
|
|
let (b, rng) = unit(rng);
|
|
let (c, rng) = unit(rng);
|
|
let (d, rng) = unit(rng);
|
|
(
|
|
Thresholds {
|
|
call_margin: a * 0.1,
|
|
value_raise: 0.4 + b * 0.5,
|
|
abstain_band: c * 0.05,
|
|
fold_equity_scale: d * 2.0,
|
|
range_width_scale: 1.0,
|
|
},
|
|
rng,
|
|
)
|
|
}
|
|
|
|
fn assert_sound(spot: &Spot, decision: &Decision, label: &str) {
|
|
let ctx = format!("{label}: {spot:?} -> {decision:?}");
|
|
if let Some(kind) = decision.choice.kind() {
|
|
assert!(spot.is_legal(kind), "illegal kind. {ctx}");
|
|
if kind == ActionKind::Fold {
|
|
assert!(!spot.is_legal(ActionKind::Check), "fold with check legal. {ctx}");
|
|
}
|
|
}
|
|
assert!((0.0..=1.0).contains(&decision.confidence.value()), "{ctx}");
|
|
if let Choice::Raise { to } = decision.choice {
|
|
assert!(to.is_finite() && to > 0.0, "{ctx}");
|
|
assert!(to <= spot.hero_stack, "unaffordable. {ctx}");
|
|
if let Some(allowed) = &spot.allowed_sizes {
|
|
assert!(allowed.contains(&to), "not from allowedSizes. {ctx}");
|
|
}
|
|
if let Some(min) = spot.min_raise_to {
|
|
assert!(to >= min || to == spot.hero_stack, "below min and not all-in. {ctx}");
|
|
}
|
|
if let Some(max) = spot.max_raise_to {
|
|
assert!(to <= max, "above max. {ctx}");
|
|
}
|
|
if spot.rules().shape == ActionShape::Binary {
|
|
assert!(to == spot.hero_stack || Some(to) == spot.max_raise_to || spot.allowed_sizes.is_some(), "{ctx}");
|
|
}
|
|
}
|
|
let after = settle(spot, decision.choice);
|
|
assert!(after.stack >= 0.0, "negative stack. {ctx}");
|
|
assert!(
|
|
(after.stack + after.pot - (spot.hero_stack + spot.pot)).abs() < 1e-9,
|
|
"chips not conserved. {ctx}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn postflop_choices_survive_enforcement_on_random_spots() {
|
|
let rng = Rng::seeded(0xD1CE);
|
|
let _ = (0..CASES).fold(rng, |rng, _| {
|
|
let (spot, rng) = random_spot(rng);
|
|
let (est, rng) = random_estimate(rng);
|
|
let (th, rng) = random_thresholds(rng);
|
|
let raw = choose_postflop(&spot, &est, &th);
|
|
assert_sound(&spot, &enforce(&spot, raw), "postflop");
|
|
rng
|
|
});
|
|
}
|
|
|
|
#[test]
|
|
fn preflop_plans_survive_enforcement_on_random_spots() {
|
|
let rng = Rng::seeded(0xBEEF);
|
|
let _ = (0..CASES).fold(rng, |rng, _| {
|
|
let (spot, rng) = random_spot(rng);
|
|
let (plan, rng) = random_plan(rng);
|
|
let raw = from_plan(&spot, &plan);
|
|
assert_sound(&spot, &enforce(&spot, raw), "from_plan");
|
|
rng
|
|
});
|
|
}
|
|
|
|
#[test]
|
|
fn enforce_is_sound_for_arbitrary_decisions() {
|
|
let rng = Rng::seeded(0xFACE);
|
|
let _ = (0..CASES).fold(rng, |rng, _| {
|
|
let (spot, rng) = random_spot(rng);
|
|
let (k, rng) = below(rng, 6);
|
|
let (size, rng) = chips(rng, 30.0);
|
|
let choice = match k {
|
|
0 => Choice::Fold,
|
|
1 => Choice::Check,
|
|
2 => Choice::Call,
|
|
3 => Choice::Raise { to: size },
|
|
4 => Choice::Raise { to: f64::NAN },
|
|
_ => Choice::Abstain,
|
|
};
|
|
assert_sound(&spot, &enforce(&spot, Decision::new(choice, 0.5, "arbitrary")), "enforce");
|
|
rng
|
|
});
|
|
}
|
|
|
|
#[test]
|
|
fn enforce_is_idempotent() {
|
|
let rng = Rng::seeded(0xABCD);
|
|
let _ = (0..CASES).fold(rng, |rng, _| {
|
|
let (spot, rng) = random_spot(rng);
|
|
let (est, rng) = random_estimate(rng);
|
|
let once = enforce(&spot, choose_postflop(&spot, &est, &Thresholds::BASE));
|
|
let twice = enforce(&spot, once.clone());
|
|
assert_eq!(once.choice, twice.choice, "{spot:?}");
|
|
rng
|
|
});
|
|
}
|