//! Two opponents that MOVE, for the long-session ladder (heads-up, `ab_zelle` rows `lerner*` / //! `wechsler*`): the realistic middle between the fixed model opponents (`sim::reader`, `sim::welt`) //! and the LBR (which knows hero's strategy at once). //! //! - **Lerner**: a regular with a HUD on hero. It sees only what a real player sees: hero's actions //! and the cards hero shows at a showdown — never a mucked hand. The HUD counts with forgetting //! (every count decays by `Lernart::vergessen` per hand), each rate is shrunk toward a reference //! by a Beta prior and moved only by the part of its deviation that survives one standard error //! (thin samples move nothing). From that it deviates from its base ruleset: bluffs more when hero //! folds too much, value-bets thinner when hero calls too much, 3-bets lighter when hero folds to //! 3-bets or opens too much, calls the river lighter when hero's shown river bets are bluffs. Every //! deviation is capped (`KAPPE_*`), so the learner never turns into an absurdly exploitable bot. //! - **Wechsler**: plays one fixed opponent for `haende` hands, then another (nit → maniac, fish → //! reg ...). Shows how fast hero's read switches and what a stale read costs. //! //! The learner is played through `Opponent::Lerner(Zug)`: a `Copy` snapshot of its adapted rules, //! re-derived from the HUD before every hand (the `sim::adaptive` pattern). `Zug::statisch(base)` //! plays bit-identically to `Opponent::Styled(base)` (spec), so a learner that never adapts IS the //! fixed ruleset, and the session loop (`session`) reproduces `session::deal_samples_memory` deal by //! deal (spec) — the old ladder is untouched. use super::memory::SessionMemory; use super::opponent::{Act, Opponent, View}; use super::reader::{strength, Reading, Rules, BLUFF_BELOW, OPEN_PRICE}; use super::session::{mix, rate, Measurement, ReadMode}; use super::table::{deal, play_hand_history, HandRead, HandSetup, TableCost}; use crate::card::Card; use crate::game::{line, ActionRecord, SeenAction, Street}; use crate::rng::Rng; const EPS: f64 = 1e-9; /// Caps of the deviations (absolute, in the threshold's own unit). Upward bluffing at most +0.25, /// value at most 0.10 thinner, 3-bet at most 0.15 lighter, river calls at most 0.12 lighter/tighter. const KAPPE_BLUFF_HOCH: f64 = 0.25; const KAPPE_BLUFF_RUNTER: f64 = 0.08; const KAPPE_WERT_DUENN: f64 = 0.10; const KAPPE_WERT_DICK: f64 = 0.05; const KAPPE_DREI_LEICHT: f64 = 0.15; const KAPPE_DREI_SCHWER: f64 = 0.05; const KAPPE_OPEN: f64 = 0.15; const KAPPE_GEGEN_DREI: f64 = 0.12; const KAPPE_RIVER: f64 = 0.12; const KAPPE_XR: f64 = 0.10; /// Pseudo-observations of the Beta prior that pulls each HUD rate toward its reference. const PRIOR_N: f64 = 10.0; /// The learner's adapted rules for one hand. `rules.bluff` is its flop bluff, `bluff_spaet` turn and /// river; `three_bet_at` its preflop re-raise threshold (the base ruleset re-raises from `raise_at`); /// `call_vs_drei` its calling margin preflop when it already put in more than the big blind (it faces /// a 3-bet); `call_river` its calling margin on the river. #[derive(Clone, Copy, Debug, PartialEq)] pub struct Zug { pub rules: Rules, pub bluff_spaet: f64, pub three_bet_at: f64, pub call_vs_drei: f64, pub call_river: f64, } impl Zug { /// No adaptation: plays exactly `reader::act_with(base, ..)`. pub fn statisch(base: Rules) -> Zug { Zug { rules: base, bluff_spaet: base.bluff, three_bet_at: base.raise_at, call_vs_drei: base.call_margin, call_river: base.call_margin } } } /// The learner's action: `reader::act_with`'s threshold logic with the street-specific fields of /// `Zug`. Consumes the rng exactly like `act_with` (one unit only on a bluff decision). pub fn act(z: &Zug, view: View, reading: Reading, street: Street, rng: Rng) -> (Act, Rng) { let r = &z.rules; let s = reading.strength; let to_call = view.to_call; let raise_from = match (reading.preflop && to_call <= OPEN_PRICE, to_call > 0.0) { (true, _) => r.open_at, (false, true) if reading.preflop => z.three_bet_at, (false, true) => r.raise_at, (false, false) => r.bet_at, }; let raise = Act::RaiseTo(reading.own_bet + to_call + r.size * (reading.pot + to_call)); let can_raise = view.min_raise_to.is_some(); if can_raise && s >= raise_from { return (raise, rng); } if to_call > 0.0 { let margin = if reading.preflop && reading.own_bet > OPEN_PRICE + EPS { z.call_vs_drei } else if street == Street::River { z.call_river } else { r.call_margin }; let price = to_call / (reading.pot + to_call); return (if s >= price + margin { Act::Call } else { Act::Fold }, rng); } if can_raise && !reading.preflop && s < BLUFF_BELOW { let bluff = if street == Street::Flop { r.bluff } else { z.bluff_spaet }; let (u, rng) = rng.unit(); return (if u < bluff { raise } else { Act::Check }, rng); } (Act::Check, rng) } /// One HUD rate with forgetting: decayed counts of chances `n` and hits `k`. #[derive(Clone, Copy, Debug, Default, PartialEq)] pub struct Rate { pub n: f64, pub k: f64, } impl Rate { fn add(&mut self, hit: bool) { self.n += 1.0; if hit { self.k += 1.0; } } fn decay(&mut self, l: f64) { self.n *= l; self.k *= l; } /// Posterior mean under a Beta prior of `PRIOR_N` observations at `reference`. pub fn mean(&self, reference: f64) -> f64 { (self.k + reference * PRIOR_N) / (self.n + PRIOR_N) } /// The deviation from `reference` that survives one standard error, signed (0 inside it). pub fn dev(&self, reference: f64) -> f64 { let m = self.mean(reference); let sd = (m * (1.0 - m) / (self.n + PRIOR_N + 1.0)).sqrt(); let d = m - reference; d.signum() * (d.abs() - sd).max(0.0) } } /// The references: the point where the learner does not deviate (a sound heads-up regular). pub mod bezug { pub const SB_OPEN: f64 = 0.75; pub const FOLD_TO_OPEN: f64 = 0.40; pub const THREE_BET: f64 = 0.12; pub const FOLD_TO_THREE: f64 = 0.50; pub const FOLD_TO_CBET: f64 = 0.45; pub const FOLD_TO_BET: f64 = 0.45; pub const RIVER_CALL: f64 = 0.55; pub const CHECK_RAISE: f64 = 0.10; pub const RIVER_BLUFF: f64 = 0.30; } /// What the learner sees of hero, hand by hand. #[derive(Clone, Copy, Debug, Default, PartialEq)] pub struct Hud { /// Hands observed (not decayed): the warmup counter. pub hands: u32, pub vpip: Rate, pub pfr: Rate, /// Hero first in on the small blind (the heads-up button): opened? pub sb_open: Rate, /// Hero on the big blind facing an open: folded? pub fold_to_open: Rate, /// Hero on the big blind facing an open: re-raised? pub three_bet: Rate, pub fold_to_three: Rate, pub fold_to_cbet: Rate, /// Facing any other postflop bet or raise: folded? pub fold_to_bet: Rate, /// Facing a river bet or raise: called (or raised)? pub river_call: Rate, pub check_raise: Rate, /// Showdowns. pub wtsd: Rate, /// Hero bet or raised the river and showed down: was it a bluff (equity < 0.5 against a random hand)? pub river_bluff: Rate, } impl Hud { /// Fold one finished hand at a table of `players` into the HUD: hero's actions (`hero_seat` /// 1-based) and, if hero reached a showdown, hero's shown cards. Every count first decays by /// `vergessen`. Heads-up `sb_open` is the small blind's open; at a bigger table it is hero's /// open when first in (no voluntary chips before it). pub fn observe(&mut self, history: &[ActionRecord], hero_seat: u8, players: usize, hero_hole: [Card; 2], board: &[Card; 5], vergessen: f64) { for r in [ &mut self.vpip, &mut self.pfr, &mut self.sb_open, &mut self.fold_to_open, &mut self.three_bet, &mut self.fold_to_three, &mut self.fold_to_cbet, &mut self.fold_to_bet, &mut self.river_call, &mut self.check_raise, &mut self.wtsd, &mut self.river_bluff, ] { r.decay(vergessen); } self.hands += 1; let mut first_pre = true; let mut river_aggr = false; for (k, r) in history.iter().enumerate() { if r.seat != hero_seat || matches!(r.action, SeenAction::PostSmallBlind | SeenAction::PostBigBlind | SeenAction::PostAnte) { continue; } let before = &history[..k]; let folded = r.action == SeenAction::Fold; let aggressive = r.action.is_aggressive() && r.amount.unwrap_or(0.0) > street_max(before, r.street) + EPS; match r.street { Street::Preflop => { if first_pre { first_pre = false; let raises_before = before.iter().filter(|x| x.street == Street::Preflop && x.action.is_aggressive()).count(); let voluntary = !folded && (r.action != SeenAction::Check); self.vpip.add(voluntary); self.pfr.add(aggressive); let first_in = if players == 2 { hero_seat == 1 } else { !before.iter().any(|x| x.street == Street::Preflop && matches!(x.action, SeenAction::Call | SeenAction::Bet | SeenAction::Raise | SeenAction::AllIn)) }; if raises_before == 0 && first_in { self.sb_open.add(aggressive); } if raises_before == 1 && line::opener(before).is_some_and(|o| o != hero_seat) { self.fold_to_open.add(folded); self.three_bet.add(aggressive); } } if line::facing_three_bet(before, hero_seat) { self.fold_to_three.add(folded); } } street => { let facing = street_max(before, street) > own_amount(before, street, hero_seat) + EPS; if street == Street::Flop && line::facing_cbet(before, hero_seat) { self.fold_to_cbet.add(folded); } else if facing { self.fold_to_bet.add(folded); } if street == Street::River && facing { self.river_call.add(!folded); } if line::check_raise_chance(before, hero_seat, street) { self.check_raise.add(aggressive); } if street == Street::River && aggressive { river_aggr = true; } } } } // A showdown: hero never folded and somebody else was still in; only then are hero's cards seen. let folded = |s: u8| history.iter().any(|r| r.seat == s && r.action == SeenAction::Fold); let showdown = !folded(hero_seat) && (1..=players as u8).any(|s| s != hero_seat && !folded(s)); self.wtsd.add(showdown); if showdown && river_aggr { let seed = 0x1E42_5EED ^ (self.hands as u64).wrapping_mul(0x9E37_79B9_7F4A_7C15); self.river_bluff.add(strength(&hero_hole, board, Rng::seeded(seed)) < 0.5); } } /// One line per rate: name, decayed chances, posterior mean (for the session's `.hud` file). pub fn zeilen(&self) -> Vec { use bezug::*; let row = |name: &str, r: &Rate, reference: f64| format!("{name} {:.1} {:.3} {:+.3}", r.n, r.mean(reference), r.dev(reference)); vec![ format!("hands {}", self.hands), row("vpip", &self.vpip, 0.5), row("pfr", &self.pfr, 0.4), row("sb_open", &self.sb_open, SB_OPEN), row("fold_to_open", &self.fold_to_open, FOLD_TO_OPEN), row("three_bet", &self.three_bet, THREE_BET), row("fold_to_three", &self.fold_to_three, FOLD_TO_THREE), row("fold_to_cbet", &self.fold_to_cbet, FOLD_TO_CBET), row("fold_to_bet", &self.fold_to_bet, FOLD_TO_BET), row("river_call", &self.river_call, RIVER_CALL), row("check_raise", &self.check_raise, CHECK_RAISE), row("wtsd", &self.wtsd, 0.3), row("river_bluff", &self.river_bluff, RIVER_BLUFF), ] } } /// The largest street amount anyone has put in on `street` so far (posts included preflop). fn street_max(before: &[ActionRecord], street: Street) -> f64 { before.iter().filter(|r| r.street == street).filter_map(|r| r.amount).fold(0.0, f64::max) } fn own_amount(before: &[ActionRecord], street: Street, seat: u8) -> f64 { before.iter().filter(|r| r.street == street && r.seat == seat).filter_map(|r| r.amount).fold(0.0, f64::max) } /// The deviation channels (`Lernart::kanaele`), one bit each, so the cost of every single deviation /// can be measured against the never-adapting control on the same deals. pub mod kanal { /// Preflop: open (steal) wider against a big blind that folds too much. pub const OPEN: u8 = 1; /// Preflop: 3-bet lighter, and call 3-bets lighter against a hero that 3-bets too much. pub const DREI: u8 = 2; /// Flop bluff (vs fold-to-c-bet, minus check-raise). pub const BLUFF_FLOP: u8 = 4; /// Turn and river bluff (vs fold-to-bet). pub const BLUFF_SPAET: u8 = 8; /// Value threshold (thinner against a caller). pub const WERT: u8 = 16; /// River calling margin (vs hero's shown river bluff share). pub const RIVER: u8 = 32; pub const ALLE: u8 = 63; /// Channel names as the ladders spell them (`lerner-k-`, `LERNER_KANAELE=drei,river`). pub const NAMEN: [(&str, u8); 6] = [("open", OPEN), ("drei", DREI), ("flop", BLUFF_FLOP), ("spaet", BLUFF_SPAET), ("wert", WERT), ("river", RIVER)]; /// `open,drei,...` to the bit mask; empty, `alle` or unknown names alone give `ALLE`. pub fn parse(s: &str) -> u8 { let k = s.split(',').filter_map(|w| NAMEN.iter().find(|(n, _)| *n == w.trim()).map(|x| x.1)).fold(0, |a, b| a | b); if k == 0 { ALLE } else { k } } } /// How the learner learns: `gain` scales every deviation (0 = never adapts), `vergessen` is the /// per-hand decay of the HUD counts (0.998 ≈ a 500-hand window), `warmup` the hands before it adapts, /// `kanaele` which deviations it uses (`kanal`). #[derive(Clone, Copy, Debug, PartialEq)] pub struct Lernart { pub gain: f64, pub vergessen: f64, pub warmup: u32, pub kanaele: u8, } impl Lernart { pub const STANDARD: Lernart = Lernart { gain: 1.0, vergessen: 0.998, warmup: 50, kanaele: kanal::ALLE }; /// Never adapts: the paired control (plays `Opponent::Styled(base)` hand for hand). pub const STATISCH: Lernart = Lernart { gain: 0.0, vergessen: 0.998, warmup: 50, kanaele: kanal::ALLE }; /// Only the deviations in `kanaele`. pub const fn nur(kanaele: u8) -> Lernart { Lernart { gain: 1.0, vergessen: 0.998, warmup: 50, kanaele } } } /// The learner's rules for the next hand from its HUD on hero. Every move is capped. pub fn zug(base: Rules, hud: &Hud, art: Lernart) -> Zug { if art.gain == 0.0 || hud.hands < art.warmup { return Zug::statisch(base); } use bezug::*; let g = art.gain; let d_cbet = hud.fold_to_cbet.dev(FOLD_TO_CBET); let d_bet = hud.fold_to_bet.dev(FOLD_TO_BET); let d_three_fold = hud.fold_to_three.dev(FOLD_TO_THREE); let d_open = hud.sb_open.dev(SB_OPEN); let d_fold_open = hud.fold_to_open.dev(FOLD_TO_OPEN); let d_three = hud.three_bet.dev(THREE_BET); let d_river_call = hud.river_call.dev(RIVER_CALL); let d_xr = hud.check_raise.dev(CHECK_RAISE); let d_rb = hud.river_bluff.dev(RIVER_BLUFF); let unit = |x: f64| x.clamp(0.0, 1.0); let mut r = base; // Bluff more into a folder, less into a caller; a check-raiser takes flop bluffs away. r.bluff = unit(base.bluff + (g * d_cbet).clamp(-KAPPE_BLUFF_RUNTER, KAPPE_BLUFF_HOCH) - (g * d_xr).clamp(0.0, KAPPE_XR)); let bluff_spaet = unit(base.bluff + (g * d_bet).clamp(-KAPPE_BLUFF_RUNTER, KAPPE_BLUFF_HOCH)); // Value thinner against a caller (fold-to-bet low, river calls high), a touch thicker vs a folder. r.bet_at = unit(base.bet_at + (g * (0.5 * d_bet - 0.3 * d_river_call)).clamp(-KAPPE_WERT_DUENN, KAPPE_WERT_DICK)); // Steal wider against a big blind that folds too much. r.open_at = unit(base.open_at - (g * 0.5 * d_fold_open).clamp(-KAPPE_DREI_SCHWER, KAPPE_OPEN)); // 3-bet lighter against an opener that folds to 3-bets or opens too much. let three_bet_at = unit(base.raise_at - (g * (0.6 * d_three_fold + 0.3 * d_open)).clamp(-KAPPE_DREI_SCHWER, KAPPE_DREI_LEICHT)); // Call 3-bets lighter against a hero that 3-bets too much. let call_vs_drei = (base.call_margin - (g * 0.5 * d_three).clamp(-KAPPE_DREI_SCHWER, KAPPE_GEGEN_DREI)).max(0.0); // Call the river lighter when hero's shown river bets are bluffs, tighter when they never are. let call_river = (base.call_margin - (g * 0.6 * d_rb).clamp(-KAPPE_RIVER, KAPPE_RIVER)).max(0.0); // A channel that is off keeps the base value exactly. let on = |k: u8| art.kanaele & k != 0; let s = Zug::statisch(base); if !on(kanal::BLUFF_FLOP) { r.bluff = base.bluff; } if !on(kanal::WERT) { r.bet_at = base.bet_at; } if !on(kanal::OPEN) { r.open_at = base.open_at; } let drei = on(kanal::DREI); Zug { rules: r, bluff_spaet: if on(kanal::BLUFF_SPAET) { bluff_spaet } else { s.bluff_spaet }, three_bet_at: if drei { three_bet_at } else { s.three_bet_at }, call_vs_drei: if drei { call_vs_drei } else { s.call_vs_drei }, call_river: if on(kanal::RIVER) { call_river } else { s.call_river }, } } /// A moving opponent of the long-session ladder. #[derive(Clone, Copy, Debug, PartialEq)] pub enum Gegner { /// A HUD regular on `base` that adapts by `art`. Lerner { base: Rules, art: Lernart }, /// `vor` for the first `haende` hands, then `nach`. Wechsler { vor: Opponent, nach: Opponent, haende: u32 }, } /// What one long session produced. pub struct Sitzung { /// Hero's luck-corrected result per deal (bb), as `session::deal_samples` stores it. pub samples: Vec, pub m: Measurement, /// Hero's read on the opponent at the end. pub memory: SessionMemory, /// The learner's HUD on hero at the end (`None` for the switcher). pub hud: Option, /// Per deal the same luck-corrected result split by the street the hand ended on (preflop, /// flop, turn, river); the four add up to `samples`. pub strassen: Vec<[f64; 4]>, } /// One heads-up session of `deals` deals against ONE moving opponent, each deal played from both /// seats (duplicate). Same deal and hand seeds as `session::deal_samples_memory`, so a learner that /// never adapts and a switcher that never switches reproduce it bit for bit (spec). pub fn session(gegner: Gegner, deals: u32, stack_bb: f64, seed: u64, read: ReadMode) -> Sitzung { const PLAYERS: u8 = 2; let mut samples = Vec::with_capacity(deals as usize); let mut raw = Vec::with_capacity(deals as usize); let mut m = Measurement { hands: 0, bb_per_100: 0.0, ci95: 0.0, raw_bb_per_100: 0.0, raw_ci95: 0.0, decisions: 0, abstains: 0, invalid: 0, rake_bb_per_100: 0.0, }; let mut memory = SessionMemory::new(); let mut hud = Hud::default(); let mut played = 0u32; let mut strassen = Vec::with_capacity(deals as usize); for d in 0..deals { let (cards, _) = deal(PLAYERS as usize, Rng::seeded(mix(seed, d as u64))); let (mut sum, mut rsum) = (0.0, 0.0); let mut by_street = [0.0; 4]; for hero_seat in 1..=PLAYERS { let opponent = match gegner { Gegner::Lerner { base, art } => Opponent::Lerner(zug(base, &hud, art)), Gegner::Wechsler { vor, nach, haende } => { if played < haende { vor } else { nach } } }; let hand_read = match read { ReadMode::Oracle => HandRead::Oracle, ReadMode::Blind => HandRead::Given(None), ReadMode::Memory | ReadMode::Hud => HandRead::Given(memory.read()), }; let hand = HandSetup { deal: &cards, hero_seat, opponent, stack_bb, seed: mix(seed ^ 0xA5A5, (d as u64) << 8 | hero_seat as u64), read: hand_read, }; let (r, history) = play_hand_history(&hand, TableCost::FREE); if read == ReadMode::Memory { memory.observe_hand(&history, hero_seat as usize - 1, PLAYERS as usize); } if let Gegner::Lerner { art, .. } = gegner { hud.observe(&history, hero_seat, PLAYERS as usize, cards.holes[hero_seat as usize - 1], &cards.board, art.vergessen); } played += 1; let end = history.last().map_or(0, |x| match x.street { Street::Preflop => 0, Street::Flop => 1, Street::Turn => 2, Street::River => 3, }); by_street[end] += (r.hero_net - r.luck) / PLAYERS as f64; sum += r.hero_net - r.luck; rsum += r.hero_net; m.hands += 1; m.decisions += r.hero_decisions; m.abstains += r.abstains; m.invalid += r.invalid; } samples.push(sum / PLAYERS as f64); raw.push(rsum / PLAYERS as f64); strassen.push(by_street); } (m.bb_per_100, m.ci95) = rate(&samples); (m.raw_bb_per_100, m.raw_ci95) = rate(&raw); let hud = matches!(gegner, Gegner::Lerner { .. }).then_some(hud); Sitzung { samples, m, memory, hud, strassen } } #[cfg(test)] mod tests { use super::*; use crate::sim::reader::{act_with, Style}; use crate::sim::session::{deal_samples_memory, Setup}; fn h(street: Street, seat: u8, action: SeenAction, amount: Option) -> ActionRecord { ActionRecord { street, seat, action, amount } } #[test] fn statisch_spielt_wie_act_with() { for style in [Style::Reg, Style::Nit, Style::Maniac, Style::Fish] { let base = style.rules(); let z = Zug::statisch(base); for (i, (to_call, own, pre, street)) in [ (0.5, 0.5, true, Street::Preflop), (2.0, 1.0, true, Street::Preflop), (6.0, 2.5, true, Street::Preflop), (0.0, 0.0, false, Street::Flop), (3.0, 0.0, false, Street::Turn), (0.0, 0.0, false, Street::River), (5.0, 0.0, false, Street::River), ] .into_iter() .enumerate() { for s in [0.1, 0.3, 0.5, 0.6, 0.7, 0.85, 0.95] { let view = View { to_call, min_raise_to: Some(own + to_call + 1.0), max_raise_to: Some(100.0) }; let reading = Reading { strength: s, pot: 6.0, own_bet: own, preflop: pre }; let rng = Rng::seeded(i as u64 * 31 + (s * 100.0) as u64); let a = act(&z, view, reading, street, rng); let b = act_with(base, view, reading, rng); assert_eq!(a.0, b.0); assert_eq!(a.1.next_u64().0, b.1.next_u64().0, "rng consumed alike"); } } } } #[test] fn hud_zaehlt_was_ein_beobachter_sieht() { // Hero = seat 1 (SB) opens, villain 3-bets, hero folds. let hist = vec![ h(Street::Preflop, 1, SeenAction::PostSmallBlind, Some(0.5)), h(Street::Preflop, 2, SeenAction::PostBigBlind, Some(1.0)), h(Street::Preflop, 1, SeenAction::Raise, Some(2.5)), h(Street::Preflop, 2, SeenAction::Raise, Some(8.0)), h(Street::Preflop, 1, SeenAction::Fold, None), ]; let deal = crate::sim::table::deal(2, Rng::seeded(7)).0; let mut hud = Hud::default(); hud.observe(&hist, 1, 2, deal.holes[0], &deal.board, 1.0); assert_eq!(hud.sb_open, Rate { n: 1.0, k: 1.0 }); assert_eq!(hud.fold_to_three, Rate { n: 1.0, k: 1.0 }); assert_eq!(hud.pfr, Rate { n: 1.0, k: 1.0 }); assert_eq!(hud.wtsd, Rate { n: 1.0, k: 0.0 }); assert_eq!(hud.three_bet.n, 0.0); // Hero = seat 2 (BB) faces the open, calls; flop c-bet, hero folds. let hist = vec![ h(Street::Preflop, 1, SeenAction::PostSmallBlind, Some(0.5)), h(Street::Preflop, 2, SeenAction::PostBigBlind, Some(1.0)), h(Street::Preflop, 1, SeenAction::Raise, Some(2.5)), h(Street::Preflop, 2, SeenAction::Call, Some(2.5)), h(Street::Flop, 2, SeenAction::Check, None), h(Street::Flop, 1, SeenAction::Bet, Some(3.0)), h(Street::Flop, 2, SeenAction::Fold, None), ]; let mut hud = Hud::default(); hud.observe(&hist, 2, 2, deal.holes[1], &deal.board, 1.0); assert_eq!(hud.fold_to_open, Rate { n: 1.0, k: 0.0 }); assert_eq!(hud.three_bet, Rate { n: 1.0, k: 0.0 }); assert_eq!(hud.fold_to_cbet, Rate { n: 1.0, k: 1.0 }); assert_eq!(hud.check_raise, Rate { n: 1.0, k: 0.0 }); assert_eq!(hud.vpip, Rate { n: 1.0, k: 1.0 }); assert_eq!(hud.sb_open.n, 0.0); } #[test] fn vergessen_und_duenne_stichprobe() { let mut r = Rate::default(); r.add(true); r.add(true); // two folds of two: inside one standard error of the prior -> no move assert_eq!(r.dev(0.45), 0.0); for _ in 0..200 { r.add(true); } assert!(r.dev(0.45) > 0.4); r.decay(0.5); assert!((r.n - 101.0).abs() < 1e-9); } #[test] fn kanalnamen() { assert_eq!(kanal::parse("drei,river"), kanal::DREI | kanal::RIVER); assert_eq!(kanal::parse("spaet"), kanal::BLUFF_SPAET); assert_eq!(kanal::parse(""), kanal::ALLE); assert_eq!(kanal::parse("alle"), kanal::ALLE); assert_eq!(kanal::NAMEN.iter().fold(0, |a, x| a | x.1), kanal::ALLE); } #[test] fn abweichungen_sind_gedeckelt() { let base = Style::Reg.rules(); let mut hud = Hud { hands: 10_000, ..Hud::default() }; let all = Rate { n: 10_000.0, k: 10_000.0 }; let none = Rate { n: 10_000.0, k: 0.0 }; // hero folds to everything hud.fold_to_cbet = all; hud.fold_to_bet = all; hud.fold_to_three = all; hud.fold_to_open = all; hud.sb_open = all; hud.river_bluff = all; hud.three_bet = all; let z = zug(base, &hud, Lernart { gain: 100.0, ..Lernart::STANDARD }); assert!((z.rules.bluff - (base.bluff + KAPPE_BLUFF_HOCH)).abs() < 1e-12); assert!((z.bluff_spaet - (base.bluff + KAPPE_BLUFF_HOCH)).abs() < 1e-12); assert!((z.three_bet_at - (base.raise_at - KAPPE_DREI_LEICHT)).abs() < 1e-12); assert!((z.rules.open_at - (base.open_at - KAPPE_OPEN)).abs() < 1e-12); assert!((z.call_river - (base.call_margin - KAPPE_RIVER)).abs() < 1e-12); // hero calls everything: value thinner, bluffs down, capped hud.fold_to_cbet = none; hud.fold_to_bet = none; hud.river_call = all; let z = zug(base, &hud, Lernart { gain: 100.0, ..Lernart::STANDARD }); assert!((z.rules.bet_at - (base.bet_at - KAPPE_WERT_DUENN)).abs() < 1e-12); assert!((z.bluff_spaet - (base.bluff - KAPPE_BLUFF_RUNTER)).abs() < 1e-12); // every channel off: the base exactly; one channel on moves only its own fields assert_eq!(zug(base, &hud, Lernart { gain: 100.0, ..Lernart::nur(0) }), Zug::statisch(base)); let d = zug(base, &hud, Lernart { gain: 100.0, ..Lernart::nur(kanal::DREI) }); assert_eq!((d.rules, d.bluff_spaet, d.call_river), (base, base.bluff, base.call_margin)); assert!(d.three_bet_at < base.raise_at); // warmup and gain 0: no move assert_eq!(zug(base, &Hud { hands: 10, ..hud }, Lernart::STANDARD), Zug::statisch(base)); assert_eq!(zug(base, &hud, Lernart::STATISCH), Zug::statisch(base)); } #[test] fn nie_lernend_ist_die_alte_leiter_bitgleich() { let base = Style::Reg.rules(); for read in [ReadMode::Memory, ReadMode::Blind] { let old = Setup { players: 2, opponent: Opponent::Styled(base), deals: 40, stack_bb: 100.0, seed: 0x5EED_0001, read }; let (a, ma, _) = deal_samples_memory(&old); let s = session(Gegner::Lerner { base, art: Lernart::STATISCH }, 40, 100.0, 0x5EED_0001, read); assert_eq!(a, s.samples); assert_eq!(ma.decisions, s.m.decisions); for (x, st) in s.samples.iter().zip(&s.strassen) { assert!((x - st.iter().sum::()).abs() < 1e-9); } let reg = Opponent::Reader(Style::Reg); let old = Setup { opponent: reg, ..old }; let (a, _, _) = deal_samples_memory(&old); let s = session(Gegner::Wechsler { vor: reg, nach: reg, haende: 20 }, 40, 100.0, 0x5EED_0001, read); assert_eq!(a, s.samples); } } #[test] fn wechsler_wechselt_nach_n_haenden() { let nit = Opponent::Reader(Style::Nit); let man = Opponent::Reader(Style::Maniac); let seed = 0x5EED_0002; let w = session(Gegner::Wechsler { vor: nit, nach: man, haende: 40 }, 40, 100.0, seed, ReadMode::Blind); let a = session(Gegner::Wechsler { vor: nit, nach: nit, haende: 0 }, 40, 100.0, seed, ReadMode::Blind); let b = session(Gegner::Wechsler { vor: man, nach: man, haende: 0 }, 40, 100.0, seed, ReadMode::Blind); // blind hero: the deals before the switch are the nit's, after it the maniac's assert_eq!(w.samples[..20], a.samples[..20]); assert_eq!(w.samples[20..], b.samples[20..]); } }