Bluffing and bluff catching driven by opponent frequencies
- exploit::fold_chance: documented foldToBet -> P(fold) map by bet size, per villain - engine: two-branch EV for every raise (fold branch, called branch vs calling range) - semibluffs first, pure bluffs heads-up above a fold and EV threshold - betting ranges keep a bluff share (size, street, style or new profile.aggression) - guards: no bluffs vs never-folders, no bluff catch vs ranges without bluffs - steal width in unopened pots from how often the blinds fold - combo-list sampling for narrow two-card ranges Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
//! Bluffing and bluff catching: when a raise without a value hand pays, and when a call
|
||||
//! with a hand that only beats bluffs is allowed.
|
||||
//!
|
||||
//! | rule | value |
|
||||
//! |---|---|
|
||||
//! | two branches | `EV = P(fold) * pot + (1 - P(fold)) * (called share * final pot - added)` |
|
||||
//! | semibluff | real draw (`semibluff::has_real_draw`), at most two villains, fold branch > 0, EV > 0 and above the best passive line |
|
||||
//! | pure bluff | weak hand without a real draw, heads-up, P(fold) >= 0.5, EV > 0 and at least 0.05 pot above the best passive line |
|
||||
//! | never bluff | no semibluff or bluff when any live villain's foldToBet is at most 0.12 |
|
||||
//! | never bluff-catch | facing a bet without a real draw, a hand below the bettor's value floor may not call when their bluff share is under 0.05 |
|
||||
|
||||
use super::semibluff::has_real_draw;
|
||||
use crate::exploit::range_spec::Continuation;
|
||||
use crate::exploit::street_continuation::continuations;
|
||||
use crate::game::{Spot, Villain};
|
||||
use crate::odds::ev::final_pot;
|
||||
use crate::read::hand_read::HandRead;
|
||||
use crate::read::tier::{tier, Tier};
|
||||
|
||||
/// A villain with foldToBet at or below this never folds: no bluffs against them.
|
||||
pub const NEVER_FOLDS: f64 = 0.12;
|
||||
/// A pure bluff needs at least this chance that everyone folds.
|
||||
pub const PURE_BLUFF_MIN_FOLD: f64 = 0.5;
|
||||
/// A pure bluff must beat the passive line by this share of the pot.
|
||||
pub const PURE_BLUFF_EDGE: f64 = 0.05;
|
||||
/// Below this bluff share a betting range counts as pure value.
|
||||
pub const MIN_BLUFF_SHARE: f64 = 0.05;
|
||||
|
||||
/// Why a raise is made.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum RaiseKind {
|
||||
Value,
|
||||
Semibluff,
|
||||
Bluff,
|
||||
}
|
||||
|
||||
/// The two branches of a raise to `to` and their sum, in chips relative to folding now.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct Branches {
|
||||
/// `P(fold) * pot`: everyone folds, hero takes the pot.
|
||||
pub fold: f64,
|
||||
/// `(1 - P(fold)) * (called share * final pot - added)`: hero is called.
|
||||
pub called: f64,
|
||||
pub total: f64,
|
||||
}
|
||||
|
||||
/// `fold` = P(every villain folds), `called_share` = hero's (realised) share against the
|
||||
/// calling range. Hero adds `to` (nothing committed on this street, see `odds::ev`).
|
||||
pub fn branches(spot: &Spot, to: f64, fold: f64, called_share: f64) -> Branches {
|
||||
let fold_branch = fold * spot.pot;
|
||||
let called = (1.0 - fold) * (called_share * final_pot(spot.pot, spot.to_call, to) - to);
|
||||
Branches { fold: fold_branch, called, total: fold_branch + called }
|
||||
}
|
||||
|
||||
/// Some live villain never folds (foldToBet at or below `NEVER_FOLDS`).
|
||||
pub fn never_folds(spot: &Spot) -> bool {
|
||||
spot.live_villains()
|
||||
.any(|v| v.profile.and_then(|p| p.fold_to_bet).is_some_and(|f| f <= NEVER_FOLDS))
|
||||
}
|
||||
|
||||
/// A hand to bluff with: weak and without a real draw.
|
||||
pub fn is_bluff_hand(read: &HandRead, spot: &Spot) -> bool {
|
||||
tier(read.made) == Tier::Weak && !has_real_draw(read, spot.street)
|
||||
}
|
||||
|
||||
/// The live villain who made the last bet or raise on this street, with their
|
||||
/// continuation there.
|
||||
fn bettor(spot: &Spot) -> Option<(&Villain, Continuation)> {
|
||||
let seat = spot
|
||||
.history
|
||||
.iter()
|
||||
.rev()
|
||||
.find(|r| r.street == spot.street && r.action.is_aggressive())
|
||||
.map(|r| r.seat)?;
|
||||
let villain = spot.live_villains().find(|v| v.seat == seat)?;
|
||||
let here = continuations(spot, villain)
|
||||
.into_iter()
|
||||
.find(|c| c.board_len == spot.street.board_len())?;
|
||||
Some((villain, here))
|
||||
}
|
||||
|
||||
/// Calling would be a bluff catch against a range without bluffs: hero faces a bet,
|
||||
/// holds no real draw, sits below the bettor's value floor, and the bettor's bluff share
|
||||
/// is under `MIN_BLUFF_SHARE`. Unknown bettor (no history): never blocked.
|
||||
pub fn bluff_catch_blocked(spot: &Spot, read: &HandRead) -> bool {
|
||||
spot.to_call > 0.0
|
||||
&& !has_real_draw(read, spot.street)
|
||||
&& bettor(spot).is_some_and(|(_, c)| read.made < c.min_made && c.bluff_share < MIN_BLUFF_SHARE)
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/// Hero's equity share in the called branch of a raise: against the hands that call
|
||||
/// it, not against the whole range (a bet folds out the weak part, the rest is stronger).
|
||||
/// Raw shares, before the multiway realisation discount.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Called {
|
||||
by_size: Vec<(f64, f64)>,
|
||||
flat: Option<f64>,
|
||||
}
|
||||
|
||||
impl Called {
|
||||
/// The same share for every size (no calling-range model: the whole range calls).
|
||||
pub fn flat(share: f64) -> Called {
|
||||
Called { by_size: Vec::new(), flat: Some(share) }
|
||||
}
|
||||
|
||||
/// One share per raise-to amount.
|
||||
pub fn sized(by_size: Vec<(f64, f64)>) -> Called {
|
||||
Called { by_size, flat: None }
|
||||
}
|
||||
|
||||
/// The share for a raise to `to`; `fallback` when the size is unknown or the share
|
||||
/// is not a probability.
|
||||
pub fn share_at(&self, to: f64, fallback: f64) -> f64 {
|
||||
self.by_size
|
||||
.iter()
|
||||
.find(|(size, _)| (size - to).abs() < 1e-9)
|
||||
.map(|(_, share)| *share)
|
||||
.or(self.flat)
|
||||
.filter(|s| s.is_finite() && (0.0..=1.0).contains(s))
|
||||
.unwrap_or(fallback)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
use super::called::Called;
|
||||
use super::range_combos::{as_ranges, combos};
|
||||
use super::range_filter::{range_filter, HandFilter};
|
||||
use crate::card::Card;
|
||||
use crate::equity::monte_carlo::estimate;
|
||||
use crate::equity::query::Query;
|
||||
use crate::equity::villain_range::VillainRange;
|
||||
use crate::exploit::call_floor::call_continuation;
|
||||
use crate::exploit::range_model::range_spec;
|
||||
use crate::exploit::size_notch::size_notch;
|
||||
use crate::exploit::table_fold::pressure_ratio;
|
||||
use crate::game::{ActionKind, Spot};
|
||||
use crate::rng::Rng;
|
||||
use crate::sizing::candidates::candidates;
|
||||
|
||||
/// Trials per calling-range simulation (one per size notch, so a few per decision).
|
||||
const CALLED_TRIALS: u32 = 1000;
|
||||
|
||||
/// Hero's share against the hands that call a raise to each candidate size.
|
||||
///
|
||||
/// Each live villain keeps their modelled range (`range_spec`) plus the calling floor
|
||||
/// for that pressure (`exploit::call_floor`). Sizes in the same `size_notch` share one
|
||||
/// simulation. A size whose simulation fails is left out (the engine then falls back
|
||||
/// to the overall share). No legal raise: nothing to compute.
|
||||
pub fn called_equity(spot: &Spot) -> Called {
|
||||
if !spot.is_legal(ActionKind::Raise) {
|
||||
return Called::sized(Vec::new());
|
||||
}
|
||||
let notched: Vec<(f64, i32, f64)> = candidates(spot)
|
||||
.into_iter()
|
||||
.map(|to| {
|
||||
let ratio = pressure_ratio(spot.pot, spot.to_call, to);
|
||||
(to, size_notch(Some(ratio)), ratio)
|
||||
})
|
||||
.collect();
|
||||
let shares: Vec<(i32, Option<f64>)> = notched.iter().fold(Vec::new(), |mut done, (_, notch, ratio)| {
|
||||
if !done.iter().any(|(n, _)| n == notch) {
|
||||
done.push((*notch, simulate(spot, *notch, *ratio)));
|
||||
}
|
||||
done
|
||||
});
|
||||
Called::sized(
|
||||
notched
|
||||
.into_iter()
|
||||
.filter_map(|(to, notch, _)| {
|
||||
shares.iter().find(|(n, _)| *n == notch).and_then(|(_, s)| *s).map(|s| (to, s))
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
fn simulate(spot: &Spot, notch: i32, ratio: f64) -> Option<f64> {
|
||||
let rules = spot.rules();
|
||||
let filters: Vec<HandFilter> = spot
|
||||
.live_villains()
|
||||
.map(|v| {
|
||||
let mut spec = range_spec(spot, v);
|
||||
spec.continues.push(call_continuation(v, spot.street, ratio));
|
||||
range_filter(&spec, rules, spot.variant, &spot.board)
|
||||
})
|
||||
.collect();
|
||||
if filters.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let dead: Vec<Card> = spot.hero_cards.iter().chain(&spot.board).copied().collect();
|
||||
let lists: Vec<_> = filters.iter().map(|f| combos(&rules, f, &dead)).collect();
|
||||
let ranges: Vec<VillainRange> = as_ranges(&filters, &lists);
|
||||
let query = Query { rules, hero: &spot.hero_cards, board: &spot.board, villains: &ranges, trials: CALLED_TRIALS };
|
||||
let seed = spot.seed ^ (0xCA11_0000_u64 + (notch + 8).clamp(0, 64) as u64);
|
||||
estimate(&query, Rng::seeded(seed)).ok().map(|(e, _)| e.share)
|
||||
}
|
||||
+81
-74
@@ -1,74 +1,81 @@
|
||||
use super::from_plan::from_plan;
|
||||
use super::passive::cheapest_continue;
|
||||
use super::postflop::choose_postflop;
|
||||
use super::range_filter::{range_filter, HandFilter};
|
||||
use crate::equity::monte_carlo::estimate;
|
||||
use crate::equity::query::Query;
|
||||
use crate::equity::villain_range::VillainRange;
|
||||
use crate::exploit::adjust::adjust;
|
||||
use crate::exploit::profile::Profile;
|
||||
use crate::exploit::range_model::range_spec;
|
||||
use crate::exploit::thresholds::Thresholds;
|
||||
use crate::game::{Decision, Spot, Street};
|
||||
use crate::guard::enforce::enforce;
|
||||
use crate::preflop::advise::advise;
|
||||
use crate::read::hand_read::read;
|
||||
use crate::rng::Rng;
|
||||
|
||||
const TRIALS_TWO_CARD: u32 = 3000;
|
||||
const TRIALS_FIVE_CARD: u32 = 1500;
|
||||
|
||||
fn trials(spot: &Spot) -> u32 {
|
||||
if spot.rules().hole_cards >= 5 {
|
||||
TRIALS_FIVE_CARD
|
||||
} else {
|
||||
TRIALS_TWO_CARD
|
||||
}
|
||||
}
|
||||
|
||||
/// Postflop: read the hand, model each live villain's range, simulate, choose.
|
||||
fn postflop(spot: &Spot, thresholds: &Thresholds) -> Decision {
|
||||
let rules = spot.rules();
|
||||
let hand_read = match read(&rules, &spot.hero_cards, &spot.board) {
|
||||
Ok(r) => r,
|
||||
Err(fault) => return Decision::abstain(format!("hand read unavailable: {fault}")),
|
||||
};
|
||||
let filters: Vec<HandFilter> = spot
|
||||
.live_villains()
|
||||
.map(|v| range_filter(&range_spec(spot, v), rules, spot.variant, &spot.board))
|
||||
.collect();
|
||||
if filters.is_empty() {
|
||||
return cheapest_continue(spot).map_or_else(
|
||||
|| Decision::abstain("no live opponent and nothing to continue with"),
|
||||
|choice| Decision::new(choice, 0.5, "no live opponents: cheapest continuing action"),
|
||||
);
|
||||
}
|
||||
let ranges: Vec<VillainRange> =
|
||||
filters.iter().map(|f| VillainRange::Filter(f.as_ref())).collect();
|
||||
let query = Query {
|
||||
rules,
|
||||
hero: &spot.hero_cards,
|
||||
board: &spot.board,
|
||||
villains: &ranges,
|
||||
trials: trials(spot),
|
||||
};
|
||||
match estimate(&query, Rng::seeded(spot.seed)) {
|
||||
Ok((equity, _)) => choose_postflop(spot, &equity, thresholds, &hand_read),
|
||||
Err(fault) => Decision::abstain(format!("equity unavailable: {fault}")),
|
||||
}
|
||||
}
|
||||
|
||||
fn think(spot: &Spot) -> Decision {
|
||||
let profiles: Vec<Profile> = spot.live_villains().filter_map(|v| v.profile).collect();
|
||||
let thresholds = adjust(Thresholds::BASE, &profiles);
|
||||
match spot.street {
|
||||
Street::Preflop => from_plan(spot, &advise(spot, &thresholds)),
|
||||
Street::Flop | Street::Turn | Street::River => postflop(spot, &thresholds),
|
||||
}
|
||||
}
|
||||
|
||||
/// Full decision for a validated spot (preflop chart or postflop read/equity/EV),
|
||||
/// always passed through `guard::enforce::enforce`.
|
||||
pub fn decide(spot: &Spot) -> Decision {
|
||||
enforce(spot, think(spot))
|
||||
}
|
||||
use super::called_equity::called_equity;
|
||||
use super::from_plan::from_plan;
|
||||
use super::passive::cheapest_continue;
|
||||
use super::postflop::choose_postflop_with;
|
||||
use super::range_combos::{as_ranges, combos};
|
||||
use super::range_filter::{range_filter, HandFilter};
|
||||
use crate::card::Card;
|
||||
use crate::equity::monte_carlo::estimate;
|
||||
use crate::equity::query::Query;
|
||||
use crate::equity::villain_range::VillainRange;
|
||||
use crate::exploit::adjust::adjust;
|
||||
use crate::exploit::profile::Profile;
|
||||
use crate::exploit::range_model::range_spec;
|
||||
use crate::exploit::thresholds::Thresholds;
|
||||
use crate::game::{Decision, Spot, Street};
|
||||
use crate::guard::enforce::enforce;
|
||||
use crate::preflop::advise::advise;
|
||||
use crate::read::hand_read::read;
|
||||
use crate::rng::Rng;
|
||||
|
||||
const TRIALS_TWO_CARD: u32 = 3000;
|
||||
const TRIALS_FIVE_CARD: u32 = 1500;
|
||||
|
||||
fn trials(spot: &Spot) -> u32 {
|
||||
if spot.rules().hole_cards >= 5 {
|
||||
TRIALS_FIVE_CARD
|
||||
} else {
|
||||
TRIALS_TWO_CARD
|
||||
}
|
||||
}
|
||||
|
||||
/// Postflop: read the hand, model each live villain's range, simulate against it and
|
||||
/// against the hands that would call each raise, choose.
|
||||
fn postflop(spot: &Spot, thresholds: &Thresholds) -> Decision {
|
||||
let rules = spot.rules();
|
||||
let hand_read = match read(&rules, &spot.hero_cards, &spot.board) {
|
||||
Ok(r) => r,
|
||||
Err(fault) => return Decision::abstain(format!("hand read unavailable: {fault}")),
|
||||
};
|
||||
let filters: Vec<HandFilter> = spot
|
||||
.live_villains()
|
||||
.map(|v| range_filter(&range_spec(spot, v), rules, spot.variant, &spot.board))
|
||||
.collect();
|
||||
if filters.is_empty() {
|
||||
return cheapest_continue(spot).map_or_else(
|
||||
|| Decision::abstain("no live opponent and nothing to continue with"),
|
||||
|choice| Decision::new(choice, 0.5, "no live opponents: cheapest continuing action"),
|
||||
);
|
||||
}
|
||||
let dead: Vec<Card> = spot.hero_cards.iter().chain(&spot.board).copied().collect();
|
||||
let lists: Vec<_> = filters.iter().map(|f| combos(&rules, f, &dead)).collect();
|
||||
let ranges: Vec<VillainRange> = as_ranges(&filters, &lists);
|
||||
let query = Query {
|
||||
rules,
|
||||
hero: &spot.hero_cards,
|
||||
board: &spot.board,
|
||||
villains: &ranges,
|
||||
trials: trials(spot),
|
||||
};
|
||||
match estimate(&query, Rng::seeded(spot.seed)) {
|
||||
Ok((equity, _)) => {
|
||||
choose_postflop_with(spot, &equity, &called_equity(spot), thresholds, &hand_read)
|
||||
}
|
||||
Err(fault) => Decision::abstain(format!("equity unavailable: {fault}")),
|
||||
}
|
||||
}
|
||||
|
||||
fn think(spot: &Spot) -> Decision {
|
||||
let profiles: Vec<Profile> = spot.live_villains().filter_map(|v| v.profile).collect();
|
||||
let thresholds = adjust(Thresholds::BASE, &profiles);
|
||||
match spot.street {
|
||||
Street::Preflop => from_plan(spot, &advise(spot, &thresholds)),
|
||||
Street::Flop | Street::Turn | Street::River => postflop(spot, &thresholds),
|
||||
}
|
||||
}
|
||||
|
||||
/// Full decision for a validated spot (preflop chart or postflop read/equity/EV),
|
||||
/// always passed through `guard::enforce::enforce`.
|
||||
pub fn decide(spot: &Spot) -> Decision {
|
||||
enforce(spot, think(spot))
|
||||
}
|
||||
|
||||
+18
-14
@@ -1,14 +1,18 @@
|
||||
//! Engine: composes assessment and choice into a decision.
|
||||
|
||||
pub mod decide;
|
||||
pub mod effective;
|
||||
pub mod from_plan;
|
||||
pub mod multiway;
|
||||
pub mod options;
|
||||
pub mod passive;
|
||||
pub mod postflop;
|
||||
pub mod range_filter;
|
||||
pub mod read_label;
|
||||
pub mod semibluff;
|
||||
pub mod texture_stance;
|
||||
pub mod uncertainty;
|
||||
//! Engine: composes assessment and choice into a decision.
|
||||
|
||||
pub mod bluff;
|
||||
pub mod called;
|
||||
pub mod called_equity;
|
||||
pub mod decide;
|
||||
pub mod effective;
|
||||
pub mod from_plan;
|
||||
pub mod multiway;
|
||||
pub mod options;
|
||||
pub mod passive;
|
||||
pub mod postflop;
|
||||
pub mod range_combos;
|
||||
pub mod range_filter;
|
||||
pub mod read_label;
|
||||
pub mod semibluff;
|
||||
pub mod texture_stance;
|
||||
pub mod uncertainty;
|
||||
|
||||
+68
-28
@@ -1,8 +1,15 @@
|
||||
use super::bluff::{
|
||||
bluff_catch_blocked, branches, is_bluff_hand, never_folds, RaiseKind, PURE_BLUFF_EDGE,
|
||||
PURE_BLUFF_MIN_FOLD,
|
||||
};
|
||||
use super::called::Called;
|
||||
use super::effective::Effective;
|
||||
use super::multiway::realized_share;
|
||||
use super::semibluff::has_real_draw;
|
||||
use crate::exploit::table_fold::table_fold;
|
||||
use crate::exploit::thresholds::Thresholds;
|
||||
use crate::game::{ActionKind, Choice, Spot};
|
||||
use crate::odds::{break_even::break_even, ev, fold_equity::fold_equity};
|
||||
use crate::odds::{break_even::break_even, ev};
|
||||
use crate::read::hand_read::HandRead;
|
||||
use crate::sizing::candidates::candidates;
|
||||
|
||||
@@ -11,6 +18,8 @@ use crate::sizing::candidates::candidates;
|
||||
pub struct Scored {
|
||||
pub choice: Choice,
|
||||
pub ev: f64,
|
||||
/// Why, for raises; `None` for passive choices.
|
||||
pub kind: Option<RaiseKind>,
|
||||
}
|
||||
|
||||
/// EV of the best non-raising line, ignoring the call margin: check when free, the raw
|
||||
@@ -29,46 +38,77 @@ pub fn is_value_raise(eff: &Effective) -> bool {
|
||||
eff.raise_ok && eff.value_ok && eff.share >= eff.value_raise
|
||||
}
|
||||
|
||||
/// What kind of raise this hand could make here, if any.
|
||||
fn raise_kind(spot: &Spot, eff: &Effective, read: &HandRead) -> Option<RaiseKind> {
|
||||
let bluffable = eff.raise_ok && !never_folds(spot);
|
||||
if is_value_raise(eff) {
|
||||
Some(RaiseKind::Value)
|
||||
} else if bluffable && eff.semibluff_ok && has_real_draw(read, spot.street) {
|
||||
Some(RaiseKind::Semibluff)
|
||||
} else if bluffable && eff.villains == 1 && is_bluff_hand(read, spot) {
|
||||
Some(RaiseKind::Bluff)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Every action the rule lets hero consider, in preference order (passive first, so
|
||||
/// ties keep the cheaper line). With `eff` the effective (texture and multiway
|
||||
/// adjusted) numbers and `share = eff.share`:
|
||||
/// * check, when legal;
|
||||
/// * call, when legal and `share >= break_even + eff.call_margin`;
|
||||
/// * call, when legal, `share >= break_even + eff.call_margin`, and not a blocked bluff
|
||||
/// catch (`bluff::bluff_catch_blocked`);
|
||||
/// * fold, when legal and check is not;
|
||||
/// * each sizing candidate as a raise, when legal and either
|
||||
/// * each sizing candidate as a raise, valued over two branches (`bluff::branches`):
|
||||
/// P(all fold) from each villain's profile (`exploit::table_fold`, times
|
||||
/// `fold_equity_scale`) and the share against the calling range (`called`, realised
|
||||
/// for the table size). Kept when
|
||||
/// - it is a value raise (`is_value_raise`), or
|
||||
/// - it is a semibluff: raising is allowed, at most two villains, a real draw
|
||||
/// (`semibluff::has_real_draw`), and the raise EV is strictly above `passive_ev`.
|
||||
///
|
||||
/// The raise EV uses `fold_equity(pot, to, fold_equity_scale, live villains)`, i.e.
|
||||
/// `fe * pot + (1 - fe) * (share * final_pot - added)`. Pure bluffs never qualify.
|
||||
pub fn scored(spot: &Spot, eff: &Effective, thresholds: &Thresholds, read: &HandRead) -> Vec<Scored> {
|
||||
/// - a semibluff (real draw, at most two villains, nobody who never folds) with a
|
||||
/// positive fold branch, EV > 0 and EV above `passive_ev`, or
|
||||
/// - a pure bluff (weak hand, heads-up, nobody who never folds) with P(fold) >= 0.5,
|
||||
/// EV > 0 and EV at least `PURE_BLUFF_EDGE` pots above `passive_ev`.
|
||||
pub fn scored(
|
||||
spot: &Spot,
|
||||
eff: &Effective,
|
||||
thresholds: &Thresholds,
|
||||
read: &HandRead,
|
||||
called: &Called,
|
||||
) -> Vec<Scored> {
|
||||
let share = eff.share;
|
||||
let free = spot.is_legal(ActionKind::Check);
|
||||
let be = break_even(spot.pot, spot.to_call);
|
||||
let live = spot.live_villains().count();
|
||||
let passive = |choice, ev| Scored { choice, ev, kind: None };
|
||||
|
||||
let check = free.then(|| Scored { choice: Choice::Check, ev: ev::check(share, spot.pot) });
|
||||
let call = (spot.is_legal(ActionKind::Call) && share >= be + eff.call_margin)
|
||||
.then(|| Scored { choice: Choice::Call, ev: ev::call(share, spot.pot, spot.to_call) });
|
||||
let fold = (spot.is_legal(ActionKind::Fold) && !free)
|
||||
.then(|| Scored { choice: Choice::Fold, ev: ev::fold() });
|
||||
let check = free.then(|| passive(Choice::Check, ev::check(share, spot.pot)));
|
||||
let call = (spot.is_legal(ActionKind::Call)
|
||||
&& share >= be + eff.call_margin
|
||||
&& !bluff_catch_blocked(spot, read))
|
||||
.then(|| passive(Choice::Call, ev::call(share, spot.pot, spot.to_call)));
|
||||
let fold = (spot.is_legal(ActionKind::Fold) && !free).then(|| passive(Choice::Fold, ev::fold()));
|
||||
|
||||
let value = is_value_raise(eff);
|
||||
let semi = eff.raise_ok && eff.semibluff_ok && has_real_draw(read, spot.street);
|
||||
let kind = raise_kind(spot, eff, read).filter(|_| spot.is_legal(ActionKind::Raise));
|
||||
let floor = passive_ev(spot, share).unwrap_or(f64::NEG_INFINITY);
|
||||
let sizes = if spot.is_legal(ActionKind::Raise) && (value || semi) {
|
||||
let raises = kind.map_or_else(Vec::new, |kind| {
|
||||
candidates(spot)
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
let raises = sizes
|
||||
.into_iter()
|
||||
.map(|to| {
|
||||
let fe = fold_equity(spot.pot, to, thresholds.fold_equity_scale, live);
|
||||
Scored { choice: Choice::Raise { to }, ev: ev::raise(share, spot.pot, spot.to_call, to, fe) }
|
||||
})
|
||||
.filter(move |s| value || s.ev > floor);
|
||||
.into_iter()
|
||||
.filter_map(|to| {
|
||||
let fe = table_fold(spot, to, thresholds.fold_equity_scale);
|
||||
let against = realized_share(called.share_at(to, share), eff.villains);
|
||||
let b = branches(spot, to, fe, against);
|
||||
let keep = match kind {
|
||||
RaiseKind::Value => true,
|
||||
RaiseKind::Semibluff => b.fold > 0.0 && b.total > 0.0 && b.total > floor,
|
||||
RaiseKind::Bluff => {
|
||||
fe >= PURE_BLUFF_MIN_FOLD
|
||||
&& b.total > 0.0
|
||||
&& b.total >= floor + PURE_BLUFF_EDGE * spot.pot
|
||||
}
|
||||
};
|
||||
keep.then_some(Scored { choice: Choice::Raise { to }, ev: b.total, kind: Some(kind) })
|
||||
})
|
||||
.collect()
|
||||
});
|
||||
|
||||
check
|
||||
.into_iter()
|
||||
|
||||
+170
-153
@@ -1,153 +1,170 @@
|
||||
use super::effective::{effective, Effective};
|
||||
use super::options::{is_value_raise, passive_ev, scored, Scored};
|
||||
use super::passive::cheapest_continue;
|
||||
use super::read_label::describe;
|
||||
use super::uncertainty::abstain_confidence;
|
||||
use crate::equity::estimate::Estimate;
|
||||
use crate::exploit::thresholds::Thresholds;
|
||||
use crate::game::{ActionKind, Choice, Decision, Spot};
|
||||
use crate::odds::{break_even::break_even, ev};
|
||||
use crate::read::hand_read::HandRead;
|
||||
|
||||
fn label(choice: Choice, semibluff: bool) -> String {
|
||||
match choice {
|
||||
Choice::Raise { to } if semibluff => format!("semibluff raise to {to:.2}"),
|
||||
Choice::Raise { to } => format!("raise to {to:.2}"),
|
||||
other => other.name().to_lowercase(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Highest EV wins; the first of equals wins (options are ordered passive first).
|
||||
fn best(options: &[Scored]) -> Option<Scored> {
|
||||
options.iter().copied().fold(None, |acc, s| match acc {
|
||||
Some(b) if b.ev >= s.ev => Some(b),
|
||||
_ => Some(s),
|
||||
})
|
||||
}
|
||||
|
||||
/// EV lead of the winner over the next-best alternative, counting folding (0) and the
|
||||
/// call it passed up.
|
||||
fn lead(spot: &Spot, share: f64, options: &[Scored], winner: Scored) -> f64 {
|
||||
let rivals = options
|
||||
.iter()
|
||||
.filter(|s| s.choice != winner.choice)
|
||||
.map(|s| s.ev)
|
||||
.chain((winner.choice != Choice::Fold).then(ev::fold))
|
||||
.chain(
|
||||
(winner.choice == Choice::Fold && spot.is_legal(ActionKind::Call))
|
||||
.then(|| ev::call(share, spot.pot, spot.to_call)),
|
||||
);
|
||||
rivals
|
||||
.fold(None, |m: Option<f64>, x| Some(m.map_or(x, |m| m.max(x))))
|
||||
.map_or(winner.ev.abs(), |r| winner.ev - r)
|
||||
}
|
||||
|
||||
fn confidence(spot: &Spot, gap: f64) -> f64 {
|
||||
let scale = (spot.pot + spot.to_call).max(spot.big_blind).max(f64::MIN_POSITIVE);
|
||||
0.5 + 0.5 * (4.0 * gap.max(0.0) / scale).tanh()
|
||||
}
|
||||
|
||||
/// Opening of every reason: the read, plus the player count when multiway.
|
||||
fn headline(read: &HandRead, eff: &Effective) -> String {
|
||||
if eff.villains > 1 {
|
||||
format!("{} ({}-way)", describe(read), eff.villains)
|
||||
} else {
|
||||
describe(read)
|
||||
}
|
||||
}
|
||||
|
||||
/// `(lead, error)` when a raise leads the best passive line by less than its own EV
|
||||
/// standard error, `std_error * final pot` (the money at stake). A negative lead
|
||||
/// (the passive line is worth more) counts as inside the error.
|
||||
fn marginal_raise(
|
||||
spot: &Spot,
|
||||
eff: &Effective,
|
||||
estimate: &Estimate,
|
||||
to: f64,
|
||||
raise_ev: f64,
|
||||
) -> Option<(f64, f64)> {
|
||||
let passive = passive_ev(spot, eff.share)?;
|
||||
let gap = raise_ev - passive;
|
||||
let error = estimate.std_error.max(0.0) * ev::final_pot(spot.pot, spot.to_call, to);
|
||||
(gap < error).then_some((gap, error))
|
||||
}
|
||||
|
||||
/// The rule for a postflop spot, given an equity estimate and the hand read.
|
||||
///
|
||||
/// 1. Invalid equity: ABSTAIN with confidence 0.
|
||||
/// 2. `effective` (texture stance x multiway) gives the realised share, call margin,
|
||||
/// value threshold and which raises are allowed.
|
||||
/// 3. Score check / call / fold / value raises / semibluffs (`options::scored`) and take
|
||||
/// the maximum EV. Never folds when check is legal.
|
||||
/// 4. Uncertainty: facing fold-or-call with `|share - (break-even + call margin)| <
|
||||
/// abstain_band + 2 * std_error`, or a winning raise whose EV lead over the best
|
||||
/// passive line is below `std_error * final pot`: ABSTAIN with a small confidence
|
||||
/// (0.05..0.30, `uncertainty::abstain_confidence`).
|
||||
/// 5. Otherwise `confidence` rises with the EV lead over the runner-up, relative to the pot.
|
||||
pub fn choose_postflop(
|
||||
spot: &Spot,
|
||||
estimate: &Estimate,
|
||||
thresholds: &Thresholds,
|
||||
read: &HandRead,
|
||||
) -> Decision {
|
||||
let raw = estimate.share;
|
||||
if !(raw.is_finite() && (0.0..=1.0).contains(&raw)) {
|
||||
return Decision::abstain("equity unavailable");
|
||||
}
|
||||
let eff = effective(spot, raw, thresholds, read);
|
||||
let share = eff.share;
|
||||
let be = break_even(spot.pot, spot.to_call);
|
||||
let breakpoint = be + eff.call_margin;
|
||||
let band = thresholds.abstain_band + 2.0 * estimate.std_error.max(0.0);
|
||||
let facing_choice = spot.is_legal(ActionKind::Fold)
|
||||
&& spot.is_legal(ActionKind::Call)
|
||||
&& !spot.is_legal(ActionKind::Check);
|
||||
let head = headline(read, &eff);
|
||||
let options = scored(spot, &eff, thresholds, read);
|
||||
let winner = best(&options);
|
||||
|
||||
if let Some(Scored { choice: Choice::Raise { to }, ev: raise_ev }) = winner {
|
||||
if let Some((gap, error)) = marginal_raise(spot, &eff, estimate, to, raise_ev) {
|
||||
return Decision::new(
|
||||
Choice::Abstain,
|
||||
abstain_confidence(gap, error),
|
||||
format!("{head}: raise EV lead {gap:+.2} inside its error {error:.2}"),
|
||||
);
|
||||
}
|
||||
} else if facing_choice && (share - breakpoint).abs() < band {
|
||||
return Decision::new(
|
||||
Choice::Abstain,
|
||||
abstain_confidence((share - breakpoint).abs(), band),
|
||||
format!(
|
||||
"{head}: equity {raw:.2} (realised {share:.2}) vs break-even {be:.2} + margin {:.2}: inside uncertainty band {band:.3}",
|
||||
eff.call_margin
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
match winner {
|
||||
Some(w) => {
|
||||
let shown = match w.choice {
|
||||
Choice::Fold => ev::call(share, spot.pot, spot.to_call),
|
||||
_ => w.ev,
|
||||
};
|
||||
let name = match w.choice {
|
||||
Choice::Fold => "call".to_string(),
|
||||
other => label(other, !is_value_raise(&eff)),
|
||||
};
|
||||
Decision::new(
|
||||
w.choice,
|
||||
confidence(spot, lead(spot, share, &options, w)),
|
||||
format!("{head}: equity {raw:.2} vs break-even {be:.2}, EV {name} {shown:+.2}"),
|
||||
)
|
||||
}
|
||||
None => match cheapest_continue(spot) {
|
||||
Some(choice) => Decision::new(
|
||||
choice,
|
||||
0.5,
|
||||
format!("{head}: equity {raw:.2} vs break-even {be:.2}, only continuing action available"),
|
||||
),
|
||||
None => Decision::abstain("no actionable option"),
|
||||
},
|
||||
}
|
||||
}
|
||||
use super::bluff::RaiseKind;
|
||||
use super::called::Called;
|
||||
use super::effective::{effective, Effective};
|
||||
use super::options::{passive_ev, scored, Scored};
|
||||
use super::passive::cheapest_continue;
|
||||
use super::read_label::describe;
|
||||
use super::uncertainty::abstain_confidence;
|
||||
use crate::equity::estimate::Estimate;
|
||||
use crate::exploit::thresholds::Thresholds;
|
||||
use crate::game::{ActionKind, Choice, Decision, Spot};
|
||||
use crate::odds::{break_even::break_even, ev};
|
||||
use crate::read::hand_read::HandRead;
|
||||
|
||||
fn label(choice: Choice, kind: Option<RaiseKind>) -> String {
|
||||
match (choice, kind) {
|
||||
(Choice::Raise { to }, Some(RaiseKind::Semibluff)) => format!("semibluff raise to {to:.2}"),
|
||||
(Choice::Raise { to }, Some(RaiseKind::Bluff)) => format!("bluff raise to {to:.2}"),
|
||||
(Choice::Raise { to }, _) => format!("raise to {to:.2}"),
|
||||
(other, _) => other.name().to_lowercase(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Highest EV wins; the first of equals wins (options are ordered passive first).
|
||||
fn best(options: &[Scored]) -> Option<Scored> {
|
||||
options.iter().copied().fold(None, |acc, s| match acc {
|
||||
Some(b) if b.ev >= s.ev => Some(b),
|
||||
_ => Some(s),
|
||||
})
|
||||
}
|
||||
|
||||
/// EV lead of the winner over the next-best alternative, counting folding (0) and the
|
||||
/// call it passed up.
|
||||
fn lead(spot: &Spot, share: f64, options: &[Scored], winner: Scored) -> f64 {
|
||||
let rivals = options
|
||||
.iter()
|
||||
.filter(|s| s.choice != winner.choice)
|
||||
.map(|s| s.ev)
|
||||
.chain((winner.choice != Choice::Fold).then(ev::fold))
|
||||
.chain(
|
||||
(winner.choice == Choice::Fold && spot.is_legal(ActionKind::Call))
|
||||
.then(|| ev::call(share, spot.pot, spot.to_call)),
|
||||
);
|
||||
rivals
|
||||
.fold(None, |m: Option<f64>, x| Some(m.map_or(x, |m| m.max(x))))
|
||||
.map_or(winner.ev.abs(), |r| winner.ev - r)
|
||||
}
|
||||
|
||||
fn confidence(spot: &Spot, gap: f64) -> f64 {
|
||||
let scale = (spot.pot + spot.to_call).max(spot.big_blind).max(f64::MIN_POSITIVE);
|
||||
0.5 + 0.5 * (4.0 * gap.max(0.0) / scale).tanh()
|
||||
}
|
||||
|
||||
/// Opening of every reason: the read, plus the player count when multiway.
|
||||
fn headline(read: &HandRead, eff: &Effective) -> String {
|
||||
if eff.villains > 1 {
|
||||
format!("{} ({}-way)", describe(read), eff.villains)
|
||||
} else {
|
||||
describe(read)
|
||||
}
|
||||
}
|
||||
|
||||
/// `(lead, error)` when a raise leads the best passive line by less than its own EV
|
||||
/// standard error, `std_error * final pot` (the money at stake). A negative lead
|
||||
/// (the passive line is worth more) counts as inside the error.
|
||||
fn marginal_raise(
|
||||
spot: &Spot,
|
||||
eff: &Effective,
|
||||
estimate: &Estimate,
|
||||
to: f64,
|
||||
raise_ev: f64,
|
||||
) -> Option<(f64, f64)> {
|
||||
let passive = passive_ev(spot, eff.share)?;
|
||||
let gap = raise_ev - passive;
|
||||
let error = estimate.std_error.max(0.0) * ev::final_pot(spot.pot, spot.to_call, to);
|
||||
(gap < error).then_some((gap, error))
|
||||
}
|
||||
|
||||
/// `choose_postflop_with` when nothing is known about the calling range: the whole
|
||||
/// range calls, so the called branch uses the overall share.
|
||||
pub fn choose_postflop(
|
||||
spot: &Spot,
|
||||
estimate: &Estimate,
|
||||
thresholds: &Thresholds,
|
||||
read: &HandRead,
|
||||
) -> Decision {
|
||||
choose_postflop_with(spot, estimate, &Called::flat(estimate.share), thresholds, read)
|
||||
}
|
||||
|
||||
/// The rule for a postflop spot, given an equity estimate, the share against the
|
||||
/// calling range per raise size (`called`) and the hand read.
|
||||
///
|
||||
/// 1. Invalid equity: ABSTAIN with confidence 0.
|
||||
/// 2. `effective` (texture stance x multiway) gives the realised share, call margin,
|
||||
/// value threshold and which raises are allowed.
|
||||
/// 3. Score check / call / fold / value raises / semibluffs / bluffs (`options::scored`,
|
||||
/// raises over two branches: fold and called) and take the maximum EV. Never folds
|
||||
/// when check is legal.
|
||||
/// 4. Uncertainty: facing fold-or-call with `|share - (break-even + call margin)| <
|
||||
/// abstain_band + 2 * std_error`, or a winning raise whose EV lead over the best
|
||||
/// passive line is below `std_error * final pot`: ABSTAIN with a small confidence
|
||||
/// (0.05..0.30, `uncertainty::abstain_confidence`).
|
||||
/// 5. Otherwise `confidence` rises with the EV lead over the runner-up, relative to the pot.
|
||||
pub fn choose_postflop_with(
|
||||
spot: &Spot,
|
||||
estimate: &Estimate,
|
||||
called: &Called,
|
||||
thresholds: &Thresholds,
|
||||
read: &HandRead,
|
||||
) -> Decision {
|
||||
let raw = estimate.share;
|
||||
if !(raw.is_finite() && (0.0..=1.0).contains(&raw)) {
|
||||
return Decision::abstain("equity unavailable");
|
||||
}
|
||||
let eff = effective(spot, raw, thresholds, read);
|
||||
let share = eff.share;
|
||||
let be = break_even(spot.pot, spot.to_call);
|
||||
let breakpoint = be + eff.call_margin;
|
||||
let band = thresholds.abstain_band + 2.0 * estimate.std_error.max(0.0);
|
||||
let facing_choice = spot.is_legal(ActionKind::Fold)
|
||||
&& spot.is_legal(ActionKind::Call)
|
||||
&& !spot.is_legal(ActionKind::Check);
|
||||
let head = headline(read, &eff);
|
||||
let options = scored(spot, &eff, thresholds, read, called);
|
||||
let winner = best(&options);
|
||||
|
||||
if let Some(Scored { choice: Choice::Raise { to }, ev: raise_ev, .. }) = winner {
|
||||
if let Some((gap, error)) = marginal_raise(spot, &eff, estimate, to, raise_ev) {
|
||||
return Decision::new(
|
||||
Choice::Abstain,
|
||||
abstain_confidence(gap, error),
|
||||
format!("{head}: raise EV lead {gap:+.2} inside its error {error:.2}"),
|
||||
);
|
||||
}
|
||||
} else if facing_choice && (share - breakpoint).abs() < band {
|
||||
return Decision::new(
|
||||
Choice::Abstain,
|
||||
abstain_confidence((share - breakpoint).abs(), band),
|
||||
format!(
|
||||
"{head}: equity {raw:.2} (realised {share:.2}) vs break-even {be:.2} + margin {:.2}: inside uncertainty band {band:.3}",
|
||||
eff.call_margin
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
match winner {
|
||||
Some(w) => {
|
||||
let shown = match w.choice {
|
||||
Choice::Fold => ev::call(share, spot.pot, spot.to_call),
|
||||
_ => w.ev,
|
||||
};
|
||||
let name = match w.choice {
|
||||
Choice::Fold => "call".to_string(),
|
||||
other => label(other, w.kind),
|
||||
};
|
||||
Decision::new(
|
||||
w.choice,
|
||||
confidence(spot, lead(spot, share, &options, w)),
|
||||
format!("{head}: equity {raw:.2} vs break-even {be:.2}, EV {name} {shown:+.2}"),
|
||||
)
|
||||
}
|
||||
None => match cheapest_continue(spot) {
|
||||
Some(choice) => Decision::new(
|
||||
choice,
|
||||
0.5,
|
||||
format!("{head}: equity {raw:.2} vs break-even {be:.2}, only continuing action available"),
|
||||
),
|
||||
None => Decision::abstain("no actionable option"),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
use super::range_filter::HandFilter;
|
||||
use crate::card::{deck, Card};
|
||||
use crate::equity::villain_range::VillainRange;
|
||||
use crate::variant::Rules;
|
||||
|
||||
/// Every two-card hand outside `dead` that `filter` keeps, for direct sampling.
|
||||
/// `None` for other hand sizes (too many to enumerate) or an empty range (the
|
||||
/// simulation then falls back to the predicate and its documented fallback).
|
||||
pub fn combos(rules: &Rules, filter: &HandFilter, dead: &[Card]) -> Option<Vec<Vec<Card>>> {
|
||||
if rules.hole_cards != 2 {
|
||||
return None;
|
||||
}
|
||||
let pool = deck::without_in(rules.deck, dead);
|
||||
let kept: Vec<Vec<Card>> = (0..pool.len())
|
||||
.flat_map(|i| (i + 1..pool.len()).map(move |j| (i, j)))
|
||||
.map(|(i, j)| vec![pool[i], pool[j]])
|
||||
.filter(|hand| filter(hand))
|
||||
.collect();
|
||||
(!kept.is_empty()).then_some(kept)
|
||||
}
|
||||
|
||||
/// The simulation's view of each villain: the enumerated list where there is one,
|
||||
/// the predicate otherwise.
|
||||
pub fn as_ranges<'a>(filters: &'a [HandFilter], lists: &'a [Option<Vec<Vec<Card>>>]) -> Vec<VillainRange<'a>> {
|
||||
filters
|
||||
.iter()
|
||||
.zip(lists)
|
||||
.map(|(f, list)| match list {
|
||||
Some(hands) => VillainRange::Combos(hands),
|
||||
None => VillainRange::Filter(f.as_ref()),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
+130
-30
@@ -1,30 +1,130 @@
|
||||
use crate::card::Card;
|
||||
use crate::exploit::range_spec::RangeSpec;
|
||||
use crate::preflop::hand_percentile::percentile;
|
||||
use crate::read::draw::Draw;
|
||||
use crate::read::glance::glance;
|
||||
use crate::variant::{Rules, Variant};
|
||||
|
||||
/// A villain's range as an owned predicate over hole cards.
|
||||
pub type HandFilter = Box<dyn Fn(&[Card]) -> bool>;
|
||||
|
||||
/// Turns a `RangeSpec` into the predicate Monte Carlo samples from: a hand is in when
|
||||
/// its preflop percentile lies in `[lo, hi]` and, for every continuation, its made
|
||||
/// hand on the first `board_len` board cards is at least `min_made` — or it holds at
|
||||
/// least a gutshot when the continuation lets draws continue. A hand `glance` cannot
|
||||
/// read is out. A `board_len` beyond the board is clamped to the whole board.
|
||||
pub fn range_filter(spec: &RangeSpec, rules: Rules, variant: Variant, board: &[Card]) -> HandFilter {
|
||||
let spec = spec.clone();
|
||||
let board = board.to_vec();
|
||||
Box::new(move |hand: &[Card]| {
|
||||
let p = percentile(variant, hand);
|
||||
p >= spec.lo
|
||||
&& p <= spec.hi
|
||||
&& spec.continues.iter().all(|c| {
|
||||
match glance(&rules, hand, &board[..c.board_len.min(board.len())]) {
|
||||
Ok((made, draw)) => made >= c.min_made || (c.draws_continue && draw >= Draw::Gutshot),
|
||||
Err(_) => false,
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
use crate::card::{deck, Card};
|
||||
use crate::exploit::range_spec::{Continuation, RangeSpec};
|
||||
use crate::preflop::hand_percentile::percentile;
|
||||
use crate::read::draw::Draw;
|
||||
use crate::read::glance::glance;
|
||||
use crate::rng::Rng;
|
||||
use crate::variant::{Rules, Variant};
|
||||
|
||||
/// A villain's range as an owned predicate over hole cards.
|
||||
pub type HandFilter = Box<dyn Fn(&[Card]) -> bool>;
|
||||
|
||||
/// Hands sampled to weigh a range when the hole cards are too many to enumerate.
|
||||
const SAMPLE: usize = 600;
|
||||
const SAMPLE_SEED: u64 = 0xB1FF_5EED;
|
||||
|
||||
/// Does `hand` meet the floor of `c` on its part of the board? `None` when unreadable.
|
||||
fn meets(rules: &Rules, c: &Continuation, hand: &[Card], board: &[Card]) -> Option<bool> {
|
||||
glance(rules, hand, &board[..c.board_len.min(board.len())])
|
||||
.ok()
|
||||
.map(|(made, draw)| made >= c.min_made || (c.draws_continue && draw >= Draw::Gutshot))
|
||||
}
|
||||
|
||||
/// A fixed number in `[0, 1)` per hand and continuation, independent of card order.
|
||||
fn ticket(hand: &[Card], step: usize) -> f64 {
|
||||
let mut idx: Vec<u8> = hand.iter().map(|c| c.index()).collect();
|
||||
idx.sort_unstable();
|
||||
let key = idx.iter().fold(step as u64, |h, i| h.wrapping_mul(0x100_0000_01B3) ^ u64::from(*i));
|
||||
Rng::seeded(key).unit().0
|
||||
}
|
||||
|
||||
/// The starting hands the range is weighed over: every two-card hand, or a fixed sample.
|
||||
fn population(rules: &Rules, board: &[Card]) -> Vec<Vec<Card>> {
|
||||
let pool = deck::without_in(rules.deck, board);
|
||||
if rules.hole_cards == 2 {
|
||||
(0..pool.len())
|
||||
.flat_map(|i| (i + 1..pool.len()).map(move |j| (i, j)))
|
||||
.map(|(i, j)| vec![pool[i], pool[j]])
|
||||
.collect()
|
||||
} else {
|
||||
(0..SAMPLE)
|
||||
.scan(Rng::seeded(SAMPLE_SEED), |rng, _| {
|
||||
deck::draw(&pool, rules.hole_cards, *rng).map(|(hand, next)| {
|
||||
*rng = next;
|
||||
hand
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
/// Ticket threshold that admits the `k` lowest tickets of `below`: halfway between the
|
||||
/// k-th and the next ticket, 0 for none, above every ticket for all.
|
||||
fn threshold(mut tickets: Vec<f64>, k: usize) -> f64 {
|
||||
tickets.sort_by(|a, b| a.total_cmp(b));
|
||||
match (k, tickets.len()) {
|
||||
(0, _) => 0.0,
|
||||
(k, n) if k >= n => 1.0,
|
||||
(k, _) => 0.5 * (tickets[k - 1] + tickets[k]),
|
||||
}
|
||||
}
|
||||
|
||||
/// Ticket threshold per continuation for hands below its floor, chosen so that over the
|
||||
/// population they make up `bluff_share` of the survivors: the `b * value / (1 - b)`
|
||||
/// lowest tickets (rounded) are admitted. Second value: `true` when some continuation
|
||||
/// has no value hand at all in what is left (the band contradicts the action).
|
||||
fn acceptances(spec: &RangeSpec, rules: &Rules, variant: Variant, board: &[Card]) -> (Vec<f64>, bool) {
|
||||
if spec.continues.is_empty() {
|
||||
return (Vec::new(), false);
|
||||
}
|
||||
let start: Vec<Vec<Card>> = population(rules, board)
|
||||
.into_iter()
|
||||
.filter(|h| (spec.lo..=spec.hi).contains(&percentile(variant, h)))
|
||||
.collect();
|
||||
let init = (start, Vec::new(), false);
|
||||
let (_, qs, starved) = spec.continues.iter().enumerate().fold(init, |(alive, mut qs, starved), (step, c)| {
|
||||
let judged: Vec<(Vec<Card>, Option<bool>)> =
|
||||
alive.into_iter().map(|h| { let m = meets(rules, c, &h, board); (h, m) }).collect();
|
||||
let value = judged.iter().filter(|(_, m)| *m == Some(true)).count();
|
||||
let below: Vec<f64> =
|
||||
judged.iter().filter(|(_, m)| *m == Some(false)).map(|(h, _)| ticket(h, step)).collect();
|
||||
let b = c.bluff_share.clamp(0.0, 0.99);
|
||||
let k = if b > 0.0 { (b * value as f64 / (1.0 - b)).round() as usize } else { 0 };
|
||||
let q = threshold(below, k);
|
||||
qs.push(q);
|
||||
let next = judged
|
||||
.into_iter()
|
||||
.filter(|(h, m)| *m == Some(true) || (*m == Some(false) && ticket(h, step) < q))
|
||||
.map(|(h, _)| h)
|
||||
.collect();
|
||||
(next, qs, starved || value == 0)
|
||||
});
|
||||
(qs, starved)
|
||||
}
|
||||
|
||||
/// Upper end of the relaxed band (see `range_filter`).
|
||||
const RELAXED_HI: f64 = 0.5;
|
||||
|
||||
/// Turns a `RangeSpec` into the predicate Monte Carlo samples from: a hand is in when
|
||||
/// its preflop percentile lies in `[lo, hi]` and, for every continuation, its made
|
||||
/// hand on the first `board_len` board cards is at least `min_made` — or it holds at
|
||||
/// least a gutshot when the continuation lets draws continue — or it is one of the
|
||||
/// admitted bluffs: below the floor with a fixed per-hand ticket under the acceptance
|
||||
/// that makes bluffs `bluff_share` of the range (`acceptances`). A hand `glance` cannot
|
||||
/// read is out. A `board_len` beyond the board is clamped to the whole board.
|
||||
///
|
||||
/// When no hand of the preflop band meets some floor, the band contradicts what the
|
||||
/// villain did; the action wins and the band is relaxed to `[0, max(hi, 0.5)]`.
|
||||
pub fn range_filter(spec: &RangeSpec, rules: Rules, variant: Variant, board: &[Card]) -> HandFilter {
|
||||
let board = board.to_vec();
|
||||
let (first, starved) = acceptances(spec, &rules, variant, &board);
|
||||
let (spec, qs) = if starved {
|
||||
let relaxed = RangeSpec { lo: 0.0, hi: spec.hi.max(RELAXED_HI), continues: spec.continues.clone() };
|
||||
let (qs, _) = acceptances(&relaxed, &rules, variant, &board);
|
||||
(relaxed, qs)
|
||||
} else {
|
||||
(spec.clone(), first)
|
||||
};
|
||||
Box::new(move |hand: &[Card]| {
|
||||
let p = percentile(variant, hand);
|
||||
p >= spec.lo
|
||||
&& p <= spec.hi
|
||||
&& spec.continues.iter().zip(&qs).enumerate().all(|(step, (c, q))| {
|
||||
match meets(&rules, c, hand, &board) {
|
||||
Some(true) => true,
|
||||
Some(false) => *q > 0.0 && ticket(hand, step) < *q,
|
||||
None => false,
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user