Files
pokerfreak/src/engine/called_equity.rs
T

74 lines
3.0 KiB
Rust

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, top_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 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);
let fold_only = v.profile.is_some_and(|p| p.fold_only);
let call = if fold_only { top_call_continuation } else { call_continuation };
spec.continues.push(call(v, spot.street, ratio, spot.to_call > 0.0));
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)
}