Files
pokerfreak/src/exploit/style.rs
T

260 lines
12 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Classifies an opponent profile into a playing style.
use super::profile::Profile;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Style {
/// No usable numbers at all.
Unknown,
/// Plays very few hands and folds to pressure.
Nit,
/// Plays many hands and raises a lot.
LooseAggressive,
/// Plays many hands passively, or almost never folds to a bet.
CallingStation,
/// Everything in between.
Regular,
}
const LOOSE_VPIP: f64 = 0.35;
const AGGRESSIVE_PFR: f64 = 0.20;
const NIT_VPIP: f64 = 0.15;
const NIT_FOLD_TO_BET: f64 = 0.65;
/// Fold-to-bet claims about a "nit" only count if the player is not clearly loose.
const NIT_MAX_VPIP_WITH_FOLDS: f64 = 0.25;
pub const STATION_FOLD_TO_BET: f64 = 0.30;
const STATION_MIN_VPIP: f64 = 0.28;
/// A calling station is loose AND postflop-passive. Above this postflop aggression frequency a
/// loose-but-not-raising player is not a station — it only looks loose preflop. Sits between the
/// reg archetype's measured 0.31 and the maniac's ~0.49; the CMA-ES nemesis measures 0.62. See the
/// `an_aggressive_loose_folder_is_not_a_station` regression test and BEFUND-nemesis-station-fehllabel.
const STATION_MAX_AGGRESSION: f64 = 0.45;
/// At or below this postflop aggression a loose-path station is trusted in FULL (`station_trust`
/// 1.0). Between here and `STATION_MAX_AGGRESSION` the station exploit is scaled down linearly — a
/// confidence ramp, not a cliff. Set above the real stations (fish 0.11, oracle 0.02) so they stay
/// bit-identical, and below the reg archetype (0.31) so a misread aggressive "station" (nemesis-2
/// aggr 0.41 → trust 0.16) barely exploits. See `station_trust` and BEFUND-nemesis-soft-trust.
const STATION_FULL_TRUST_AGGRESSION: f64 = 0.20;
/// Messschalter (einmal gelesen) `STATION_POSTFLOP=1`: the loose-passive path to `CallingStation` also
/// needs a postflop signal - fold-to-bet at most `STATION_LOOSE_MAX_FOLD` (unknown passes). A reg who
/// only opens wider (the learning HUD reg, `sim::lerner`, 15.11.: memory read -7,15 bb/100 against blind,
/// all postflop) folds like a reg and is no station, however wide it plays preflop.
/// `STATION_POSTFLOP=schmal` (15.11., node diagnosis `diag_welt DIAG_SCHALTER=station`): against a
/// player the switch would demote (`station_demoted`), hero decides both ways - station read and blind
/// (the demoted Regular is gated blind) - and plays the LESS aggressive action (`engine::decide`). The
/// full demotion won only where the station read was the more aggressive one (thin value raises into a
/// frequent folder: cbet-folder +36, nemesis-2 +38, all Raise -> Call/Check/Fold nodes) and lost only
/// where it was the more cautious one (check-raiser −6,7, polar −5,1: Fold -> Call, Call -> Raise).
fn station_mode() -> u8 {
use std::sync::OnceLock;
static MODE: OnceLock<u8> = OnceLock::new();
STATION_OVERRIDE.with(|o| o.get()).unwrap_or_else(|| {
*MODE.get_or_init(|| match std::env::var("STATION_POSTFLOP").as_deref() {
Ok("1") => 1,
Ok("schmal") => 2,
_ => 0,
})
})
}
fn station_postflop() -> bool {
station_mode() == 1
}
/// `STATION_POSTFLOP=schmal` is on (the two-way decision in `engine::decide`).
pub fn station_narrow() -> bool {
station_mode() == 2
}
/// A station by the old rule that the postflop gate demotes (it folds more than `STATION_LOOSE_MAX_FOLD`).
pub fn station_demoted(profile: &Profile) -> bool {
classify_with(profile, false) == Style::CallingStation && classify_with(profile, true) != Style::CallingStation
}
/// The current thread's override (to restore it after a two-way decision).
pub fn station_postflop_override() -> Option<u8> {
STATION_OVERRIDE.with(|o| o.get())
}
thread_local! {
static STATION_OVERRIDE: std::cell::Cell<Option<u8>> = const { std::cell::Cell::new(None) };
}
/// Diagnostics only (`diag_welt DIAG_SCHALTER=station`): force the `STATION_POSTFLOP` mode on this
/// thread (0 off, 1 on, 2 schmal), `None` = the environment switch.
pub fn set_station_postflop_override(mode: Option<u8>) {
STATION_OVERRIDE.with(|o| o.set(mode));
}
/// Fold-to-bet above which a loose-passive player is not a station under `STATION_POSTFLOP` (a reg
/// folds ~0,50-0,55, the stations of the ladder 0,20-0,35).
const STATION_LOOSE_MAX_FOLD: f64 = 0.45;
fn folds_like_a_station(fold: Option<f64>, gate: bool) -> bool {
!gate || fold.is_none_or(|b| b <= STATION_LOOSE_MAX_FOLD)
}
/// A frequency in `[0, 1]`; NaN means "not known".
fn frequency(x: Option<f64>) -> Option<f64> {
x.filter(|v| !v.is_nan()).map(|v| v.clamp(0.0, 1.0))
}
/// Thresholds (vpip/pfr/foldToBet/aggression are all clamped to `[0, 1]` first, NaN counts as missing):
/// - all missing: `Unknown`
/// - vpip >= 35 % and pfr >= 20 %: `LooseAggressive`
/// - (vpip >= 35 % AND aggression < 45 % or unknown), or foldToBet <= 30 % with vpip >= 28 % (or
/// unknown): `CallingStation`
/// - vpip <= 15 %, or foldToBet >= 65 % with vpip <= 25 % (or unknown): `Nit`
/// - else `Regular`
///
/// A station is loose AND passive: the `loose` → `CallingStation` path is gated on postflop
/// aggression (`STATION_MAX_AGGRESSION`) so a loose-preflop but aggression-heavy frequent-folder
/// (the CMA-ES nemesis: vpip 0.53, pfr 0.14, foldToBet 0.65, aggression 0.62) reads `Regular` — gated
/// to blind by `read_gate`, the proven-safe baseline — instead of being handed `earned_trust 1.0`
/// and the station exploit it then bled −448 bb/100 to. The `sticky` path (fold ≤ 30 %) is a real
/// non-folder and stays a station regardless of aggression.
pub fn classify(profile: &Profile) -> Style {
classify_with(profile, station_postflop())
}
pub fn classify_with(profile: &Profile, station_gate: bool) -> Style {
let (vpip, pfr, fold, aggr) = (
frequency(profile.vpip),
frequency(profile.pfr),
frequency(profile.fold_to_bet),
frequency(profile.aggression),
);
let loose = vpip.is_some_and(|v| v >= LOOSE_VPIP);
// Postflop-passive: a station does not fire back. Unknown aggression keeps the old behaviour.
let passive = aggr.is_none_or(|a| a < STATION_MAX_AGGRESSION);
let sticky = fold.is_some_and(|b| b <= STATION_FOLD_TO_BET)
&& vpip.is_none_or(|v| v >= STATION_MIN_VPIP);
let tight = vpip.is_some_and(|v| v <= NIT_VPIP)
|| (fold.is_some_and(|b| b >= NIT_FOLD_TO_BET)
&& vpip.is_none_or(|v| v <= NIT_MAX_VPIP_WITH_FOLDS));
if profile.fold_only || profile.pool_baseline || (vpip.is_none() && pfr.is_none() && fold.is_none()) {
Style::Unknown
} else if loose && pfr.is_some_and(|f| f >= AGGRESSIVE_PFR) {
Style::LooseAggressive
} else if (loose && passive && folds_like_a_station(fold, station_gate)) || sticky {
Style::CallingStation
} else if tight {
Style::Nit
} else {
Style::Regular
}
}
/// How much to trust a `CallingStation` read, in `[0, 1]` — the confidence-weighted generalization
/// of the binary `earned_trust` gate (`engine::read_gate` keeps a station's profile or drops it; this
/// says HOW MUCH of the station exploit to apply to what it keeps). Only meaningful for a profile
/// that already classifies `CallingStation`; the station-exploit channels (`adjust`'s value shift,
/// `style_rung`'s made-floor rung) scale their deviation from blind by this weight.
///
/// A station earns full trust on either signal: it does not fold (sticky, `fold_to_bet` ≤ 30 %) —
/// value-betting it thin is always right — OR it is postflop-passive (low `aggression`). The
/// aggression ramp governs only the loose-WIDTH path, where a high `vpip` alone made the old binary
/// gate hand full trust to aggressive frequent-folders (the CMA-ES nemesis-2, aggr 0.41, folds 56 %)
/// that then bled to the over-call. Unknown aggression keeps the old full trust (no postflop sample
/// to doubt). A genuine station (fish, oracle) returns exactly `1.0` → bit-identical to the old gate.
pub fn station_trust(profile: &Profile) -> f64 {
// A real non-folder is a station on the strongest signal; aggression does not weaken that.
if frequency(profile.fold_to_bet).is_some_and(|b| b <= STATION_FOLD_TO_BET) {
return 1.0;
}
match frequency(profile.aggression) {
None => 1.0,
Some(a) => ((STATION_MAX_AGGRESSION - a)
/ (STATION_MAX_AGGRESSION - STATION_FULL_TRUST_AGGRESSION))
.clamp(0.0, 1.0),
}
}
#[cfg(test)]
mod tests {
use super::*;
fn profile(vpip: f64, pfr: f64, fold: f64, aggr: f64) -> Profile {
Profile {
vpip: Some(vpip),
pfr: Some(pfr),
fold_to_bet: Some(fold),
aggression: Some(aggr),
..Profile::default()
}
}
/// The CMA-ES nemesis read: loose preflop (vpip 0.53) but a hyper-aggressive frequent-folder
/// (aggression 0.62, foldToBet 0.65). Width alone made the old `loose` path label it a station,
/// handing it earned_trust and the station exploit it bled −448 bb/100 to. It must read `Regular`
/// (gated to blind), not `CallingStation`.
#[test]
fn an_aggressive_loose_folder_is_not_a_station() {
let p = profile(0.53, 0.14, 0.65, 0.62);
assert_eq!(classify(&p), Style::Regular);
}
/// A genuinely loose-passive opponent (low aggression) is still a `CallingStation` — the fix
/// gates on aggression, not on preflop width, so the real station is untouched.
#[test]
fn a_loose_passive_opponent_is_still_a_station() {
let p = profile(0.70, 0.05, 0.25, 0.10);
assert_eq!(classify(&p), Style::CallingStation);
}
/// A sticky non-folder (foldToBet ≤ 30 %) stays a station regardless of aggression — not folding
/// is itself the station signal; the aggression gate applies only to the `loose` path.
#[test]
fn a_sticky_non_folder_stays_a_station_even_if_aggressive() {
let p = profile(0.40, 0.10, 0.20, 0.60);
assert_eq!(classify(&p), Style::CallingStation);
}
/// Aggression unknown (not yet measured) keeps the old width-based station read, so nothing
/// regresses for profiles without a postflop sample.
#[test]
fn unknown_aggression_keeps_the_width_based_station() {
let p = Profile { vpip: Some(0.50), pfr: Some(0.10), fold_to_bet: Some(0.45), ..Profile::default() };
assert_eq!(classify(&p), Style::CallingStation);
}
/// A genuine passive station (fish: aggr 0.11) and a non-folder earn FULL trust — the soft gate
/// is bit-identical to the old binary gate for the stations that were always right to exploit.
#[test]
fn a_passive_station_earns_full_trust() {
let fish = profile(0.55, 0.08, 0.40, 0.11); // loose path (folds 40 %), very passive
assert_eq!(station_trust(&fish), 1.0);
let non_folder = profile(0.40, 0.10, 0.20, 0.60); // sticky: never folds → full trust despite aggression
assert_eq!(station_trust(&non_folder), 1.0);
}
/// A misclassified aggressive loose "station" (nemesis-2: loose width, folds 56 %, aggr 0.41)
/// earns only a sliver of trust, so the station exploit barely fires on it.
#[test]
fn a_misread_aggressive_station_earns_little_trust() {
let nemesis2 = profile(0.65, 0.15, 0.56, 0.41);
let c = station_trust(&nemesis2);
assert!(c > 0.0 && c < 0.25, "expected a small trust, got {c}");
}
/// Trust falls monotonically as the loose-path station gets more aggressive, reaching 0 at the
/// classify boundary — a smooth handoff to `Regular` (gated blind), no cliff.
#[test]
fn loose_station_trust_ramps_down_with_aggression() {
let at = |a: f64| station_trust(&profile(0.55, 0.10, 0.50, a));
assert_eq!(at(0.20), 1.0);
assert!((at(0.325) - 0.5).abs() < 1e-9);
assert_eq!(at(0.45), 0.0);
assert!(at(0.25) > at(0.35));
}
/// Unknown aggression keeps full trust: no postflop sample is no reason to doubt the read.
#[test]
fn unknown_aggression_keeps_full_station_trust() {
let p = Profile { vpip: Some(0.50), pfr: Some(0.10), fold_to_bet: Some(0.45), ..Profile::default() };
assert_eq!(station_trust(&p), 1.0);
}
}