Add odds, sizing, guard and engine glue with tests
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
use super::allowed::valid_allowed;
|
||||
use super::bounds::bounds;
|
||||
use crate::game::Spot;
|
||||
|
||||
/// The jam size: with `allowed_sizes`, the largest usable entry; otherwise
|
||||
/// `min(max_raise_to, hero_stack)` (the stack when no max). `None` when that is not
|
||||
/// a legal raise size (no chips, or below `min_raise_to`). For pot-limit variants
|
||||
/// the "jam" is the pot-sized raise when that is smaller than the stack.
|
||||
pub fn all_in(spot: &Spot) -> Option<f64> {
|
||||
match valid_allowed(spot) {
|
||||
Some(sizes) => sizes.last().copied(),
|
||||
None => bounds(spot).map(|b| b.hi),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
use crate::game::Spot;
|
||||
|
||||
/// The entries of `allowed_sizes` hero may actually use, verbatim and ascending:
|
||||
/// finite, positive, affordable, inside `[min_raise_to, max_raise_to]` when given.
|
||||
/// `None` when the spot has no `allowed_sizes` at all.
|
||||
pub fn valid_allowed(spot: &Spot) -> Option<Vec<f64>> {
|
||||
spot.allowed_sizes.as_ref().map(|sizes| {
|
||||
let mut usable: Vec<f64> = sizes
|
||||
.iter()
|
||||
.copied()
|
||||
.filter(|s| {
|
||||
s.is_finite()
|
||||
&& *s > 0.0
|
||||
&& *s <= spot.hero_stack
|
||||
&& spot.min_raise_to.map_or(true, |min| *s >= min)
|
||||
&& spot.max_raise_to.map_or(true, |max| *s <= max)
|
||||
})
|
||||
.collect();
|
||||
usable.sort_by(f64::total_cmp);
|
||||
usable.dedup();
|
||||
usable
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
use crate::game::Spot;
|
||||
use crate::variant::SizingRule;
|
||||
|
||||
/// Inclusive range a free-form raise-to size may take.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct Bounds {
|
||||
pub lo: f64,
|
||||
pub hi: f64,
|
||||
}
|
||||
|
||||
impl Bounds {
|
||||
pub fn clamp(self, x: f64) -> f64 {
|
||||
x.max(self.lo).min(self.hi)
|
||||
}
|
||||
}
|
||||
|
||||
/// `[min_raise_to, min(max_raise_to, hero_stack)]`; pot-limit rules also cap at a
|
||||
/// pot-sized raise (`pot + 2 * to_call`). `None` when no positive size fits.
|
||||
pub fn bounds(spot: &Spot) -> Option<Bounds> {
|
||||
let lo = spot.min_raise_to.unwrap_or(0.0).max(0.0);
|
||||
let capped = spot.max_raise_to.unwrap_or(f64::INFINITY).min(spot.hero_stack);
|
||||
let hi = match spot.rules().sizing {
|
||||
SizingRule::PotLimit => capped.min(spot.pot + 2.0 * spot.to_call),
|
||||
_ => capped,
|
||||
};
|
||||
(lo.is_finite() && hi > 0.0 && lo <= hi).then_some(Bounds { lo, hi })
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
use super::all_in::all_in;
|
||||
use super::allowed::valid_allowed;
|
||||
use super::bounds::bounds;
|
||||
use super::pot_fractions::{raise_to, FRACTIONS};
|
||||
use crate::game::Spot;
|
||||
use crate::variant::SizingRule;
|
||||
|
||||
/// Raise-to sizes worth evaluating, ascending and distinct. Driven by
|
||||
/// `spot.rules().sizing`; `allowed_sizes`, when present, is the only source.
|
||||
pub fn candidates(spot: &Spot) -> Vec<f64> {
|
||||
if let Some(sizes) = valid_allowed(spot) {
|
||||
return sizes;
|
||||
}
|
||||
match spot.rules().sizing {
|
||||
SizingRule::AllInOnly => all_in(spot).into_iter().collect(),
|
||||
SizingRule::NoLimit | SizingRule::PotLimit | SizingRule::Menu => {
|
||||
let mut sizes: Vec<f64> = bounds(spot)
|
||||
.map(|b| FRACTIONS.iter().map(|f| b.clamp(raise_to(spot, *f))).collect())
|
||||
.unwrap_or_default();
|
||||
sizes.retain(|s| s.is_finite() && *s > 0.0);
|
||||
sizes.sort_by(f64::total_cmp);
|
||||
sizes.dedup_by(|a, b| (*a - *b).abs() < 1e-12);
|
||||
sizes
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
use super::all_in::all_in;
|
||||
use super::allowed::valid_allowed;
|
||||
use super::bounds::bounds;
|
||||
use crate::game::Spot;
|
||||
use crate::variant::{ActionShape, SizingRule};
|
||||
|
||||
/// Turns a wished raise-to size into one hero may legally play, or `None`.
|
||||
/// * `allowed_sizes`: the nearest usable entry, verbatim (the lower one on ties).
|
||||
/// * binary / all-in-only rules: the jam.
|
||||
/// * otherwise: the target clamped into the bounds.
|
||||
pub fn fit(spot: &Spot, target: f64) -> Option<f64> {
|
||||
if !target.is_finite() {
|
||||
return None;
|
||||
}
|
||||
if let Some(sizes) = valid_allowed(spot) {
|
||||
return sizes
|
||||
.into_iter()
|
||||
.min_by(|a, b| (a - target).abs().total_cmp(&(b - target).abs()));
|
||||
}
|
||||
let rules = spot.rules();
|
||||
if rules.shape == ActionShape::Binary || rules.sizing == SizingRule::AllInOnly {
|
||||
return all_in(spot);
|
||||
}
|
||||
bounds(spot).map(|b| b.clamp(target)).filter(|s| *s > 0.0)
|
||||
}
|
||||
@@ -1 +1,9 @@
|
||||
//! Raise-size candidates per sizing rule; `allowedSizes` always wins.
|
||||
|
||||
pub mod all_in;
|
||||
pub mod allowed;
|
||||
pub mod bounds;
|
||||
pub mod candidates;
|
||||
pub mod fit;
|
||||
pub mod pot_fractions;
|
||||
pub mod preflop;
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
use crate::game::Spot;
|
||||
|
||||
/// Pot fractions offered for free-form sizing.
|
||||
pub const FRACTIONS: [f64; 3] = [1.0 / 3.0, 2.0 / 3.0, 1.0];
|
||||
|
||||
/// Raise-to for a bet of `fraction` of the pot after hero calls:
|
||||
/// `to_call + fraction * (pot + to_call)`.
|
||||
pub fn raise_to(spot: &Spot, fraction: f64) -> f64 {
|
||||
spot.to_call + fraction * (spot.pot + spot.to_call)
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
use super::fit::fit;
|
||||
use crate::game::Spot;
|
||||
|
||||
/// Open size in big blinds when nobody has raised yet.
|
||||
pub const OPEN_BB: f64 = 2.5;
|
||||
/// Multiplier on the bet faced for a re-raise.
|
||||
pub const RERAISE_FACTOR: f64 = 3.0;
|
||||
|
||||
/// Wished preflop raise-to: `2.5 bb` when only blinds/limps are faced
|
||||
/// (`to_call <= big_blind`), else `3 x` the amount to call.
|
||||
pub fn raise_target(spot: &Spot) -> f64 {
|
||||
if spot.to_call <= spot.big_blind {
|
||||
OPEN_BB * spot.big_blind
|
||||
} else {
|
||||
RERAISE_FACTOR * spot.to_call
|
||||
}
|
||||
}
|
||||
|
||||
/// The chart `Raise` as a legal size (bounds / allowed sizes applied).
|
||||
pub fn raise_size(spot: &Spot) -> Option<f64> {
|
||||
fit(spot, raise_target(spot))
|
||||
}
|
||||
Reference in New Issue
Block a user