//! A synthetic opponent population: a distribution over `reader::Rules`, so the lab measures the //! engine against thousands of INDIVIDUAL opponents drawn around a few archetype means, not five //! fixed clones. Each draw carries a hidden archetype label the engine never sees — it must infer //! the type from observed actions (`exploit::posterior`), and that inference is the exploitation //! signal the fixed-bot ladder structurally hides (it hands over a converged oracle read). //! //! Deterministic given a seed. The `micro` prior is a plausible micro-stakes field; Stufe 1 //! calibrates the archetype weights and spreads to real player data (`lesen/profil.py`, //! `lesen/gedaechtnis.py`) so the synthetic field matches real humans. use super::opponent::Opponent; use super::reader::{Rules, Style}; use crate::rng::Rng; /// One cluster of the field: a Style's rules as the mean, with a weight (its share of the field). #[derive(Clone, Copy, Debug)] pub struct Archetype { pub label: &'static str, pub mean: Rules, pub weight: f64, } /// A drawn opponent: its ruleset plus the hidden archetype it came from. The label is for scoring /// the engine's inference, never shown to the engine. #[derive(Clone, Copy, Debug)] pub struct Draw { pub rules: Rules, pub label: &'static str, } impl Draw { /// The opponent the table plays (read-driven, no authored oracle profile). pub fn opponent(&self) -> Opponent { Opponent::Styled(self.rules) } } /// Per-parameter standard deviation of the within-archetype spread. Kept smaller than the /// between-archetype distance, so clusters stay distinct but every draw is an individual. #[derive(Clone, Copy, Debug)] pub struct Spread { pub call_margin: f64, pub raise_at: f64, pub bet_at: f64, pub open_at: f64, pub bluff: f64, pub size: f64, } impl Spread { pub const DEFAULT: Spread = Spread { call_margin: 0.04, raise_at: 0.05, bet_at: 0.05, open_at: 0.05, bluff: 0.06, size: 0.12, }; } #[derive(Clone, Debug)] pub struct Population { pub archetypes: Vec, pub spread: Spread, } impl Population { /// A plausible micro-stakes field as the starting prior (calibrated later): mostly fish and /// regs, some nits, a few maniacs. Means are the named `Style` rules. pub fn micro() -> Population { Population { archetypes: vec![ Archetype { label: "fish", mean: Style::Fish.rules(), weight: 0.35 }, Archetype { label: "reg", mean: Style::Reg.rules(), weight: 0.30 }, Archetype { label: "nit", mean: Style::Nit.rules(), weight: 0.20 }, Archetype { label: "maniac", mean: Style::Maniac.rules(), weight: 0.15 }, ], spread: Spread::DEFAULT, } } /// Draw one opponent: pick an archetype by weight, perturb each threshold by a clamped Gaussian. pub fn sample(&self, rng: Rng) -> (Draw, Rng) { let (arch, rng) = self.pick(rng); let m = arch.mean; let s = self.spread; let (call_margin, rng) = jitter(m.call_margin, s.call_margin, 0.0, 0.45, rng); let (raise_at, rng) = jitter(m.raise_at, s.raise_at, 0.55, 0.97, rng); let (bet_at, rng) = jitter(m.bet_at, s.bet_at, 0.45, 0.90, rng); let (open_at, rng) = jitter(m.open_at, s.open_at, 0.45, 0.85, rng); let (bluff, rng) = jitter(m.bluff, s.bluff, 0.0, 0.60, rng); let (size, rng) = jitter(m.size, s.size, 0.25, 1.30, rng); let rules = Rules { call_margin, raise_at, bet_at, open_at, bluff, size }; (Draw { rules, label: arch.label }, rng) } fn pick(&self, rng: Rng) -> (Archetype, Rng) { let total: f64 = self.archetypes.iter().map(|a| a.weight).sum::().max(1e-12); let (u, rng) = rng.unit(); let mut acc = 0.0; for a in &self.archetypes { acc += a.weight / total; if u <= acc { return (*a, rng); } } (*self.archetypes.last().expect("non-empty population"), rng) } } /// A Gaussian jitter around `mean` with sd `sd`, clamped to `[lo, hi]`. Box-Muller from two units. pub(crate) fn jitter(mean: f64, sd: f64, lo: f64, hi: f64, rng: Rng) -> (f64, Rng) { let (z, rng) = normal(rng); ((mean + sd * z).clamp(lo, hi), rng) } pub(crate) fn normal(rng: Rng) -> (f64, Rng) { let (u1, rng) = rng.unit(); let (u2, rng) = rng.unit(); let u1 = u1.max(1e-12); ((-2.0 * u1.ln()).sqrt() * (std::f64::consts::TAU * u2).cos(), rng) } #[cfg(test)] mod tests { use super::*; fn draws(pop: &Population, n: usize, seed: u64) -> Vec { let mut rng = Rng::seeded(seed); (0..n) .map(|_| { let (d, next) = pop.sample(rng); rng = next; d }) .collect() } #[test] fn deterministic_given_seed() { let pop = Population::micro(); let a = draws(&pop, 100, 7); let b = draws(&pop, 100, 7); assert_eq!(a.len(), b.len()); for (x, y) in a.iter().zip(&b) { assert_eq!(x.rules, y.rules); assert_eq!(x.label, y.label); } } #[test] fn draws_stay_in_bounds() { let pop = Population::micro(); for d in draws(&pop, 5000, 11) { let r = d.rules; assert!((0.0..=0.45).contains(&r.call_margin), "{:?}", r); assert!((0.55..=0.97).contains(&r.raise_at)); assert!((0.45..=0.90).contains(&r.bet_at)); assert!((0.45..=0.85).contains(&r.open_at)); assert!((0.0..=0.60).contains(&r.bluff)); assert!((0.25..=1.30).contains(&r.size)); } } #[test] fn label_frequencies_track_weights() { let pop = Population::micro(); let n = 20000; let ds = draws(&pop, n, 23); for a in &pop.archetypes { let share = ds.iter().filter(|d| d.label == a.label).count() as f64 / n as f64; println!("{}: want {:.2} got {:.3}", a.label, a.weight, share); assert!((share - a.weight).abs() < 0.03, "{} off: {share:.3}", a.label); } } #[test] fn clusters_stay_distinct() { // Fish call almost anything (tiny call_margin); nits need a big edge. The drawn clusters must // preserve that ordering on average, else the spread has blurred the archetypes away. let pop = Population::micro(); let ds = draws(&pop, 8000, 31); let mean = |label: &str, f: fn(&Rules) -> f64| { let v: Vec = ds.iter().filter(|d| d.label == label).map(|d| f(&d.rules)).collect(); v.iter().sum::() / v.len().max(1) as f64 }; let fish_call = mean("fish", |r| r.call_margin); let nit_call = mean("nit", |r| r.call_margin); let maniac_bluff = mean("maniac", |r| r.bluff); let nit_bluff = mean("nit", |r| r.bluff); println!("call_margin fish {fish_call:.3} < nit {nit_call:.3}; bluff maniac {maniac_bluff:.3} > nit {nit_bluff:.3}"); assert!(fish_call < nit_call - 0.1, "clusters blurred on call_margin"); assert!(maniac_bluff > nit_bluff + 0.1, "clusters blurred on bluff"); } }