Files
pokerfreak/src/sim/gto.rs
T
mmintelandClaude Opus 4.8 1492fb6a38 Synthetik-Labor: Population, Zwillings-Tor, adaptive Liga, Optimizer, Sim-to-Real
Ersetzt die irrefuehrende Oracle-Leiter durch eine ehrliche Messmethodik
(Oracle aus, Read via Memory/Posterior gegen eine gezogene Gegner-Population).

Stufen:
- 0 Exploitation-Luecke: +18.98 bb/100 gegen die gezogene Population
- 1 Kalibrierung an echte HUD-Daten (lesen/kalibrierung.py)
- 2 Zwillings-Tor (EGTA + Exploitability-Waechter): Offense +43.95,
  Boden gto -36.25 (verliert gegen Balancierte - die Oracle-verdeckte Wurzel)
- 3 Adaptive Liga L0-L3 (reaktiv / modellierend / Liga)
- 4 Sim-to-Real: HUD-Gap (sim::simreal) + Outcome-Abgleich (lesen/liveabgleich.py):
  real -266.75 vs Sim +43.95 => Gap -311; A/B-Pipeline + Runbook fertig,
  nur der geldechte Live-Lauf unter beiden Armen bleibt offen
- 5 Optimizer (Diagonal-CMA-ES, parallel): Nemesis -414.81 bb/100

Befunde und Architektur in docs/notizen/. Lab-Tests gruen
(Rust lib 92/0, python liveabgleich/kalibrierung/gegnersignal gruen).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-11 09:57:32 +01:00

381 lines
15 KiB
Rust

//! A near-GTO scripted opponent: the strong measurement target the weak styles cannot be. It plays
//! RANGES, not hand-strength thresholds: every decision first ranks its hand inside its own range
//! (`range_pct`, 0 = best), then plays that rank by balanced frequencies.
//!
//! - Preflop: position charts (open / call / 3-bet value + bluff / 4-bet / jam) on the 169-class
//! percentile; heads-up the button opens ~80 %, the big blind defends ~70 %.
//! - Betting: value from the top of the range, bluffs from the draw / air band at the ratio the bet
//! size asks for (bet 2/3 pot: 0.4 bluff per value, so the caller is indifferent). Medium hands
//! check (showdown value, protected by checking back), the nuts sometimes trap.
//! - Facing a bet: defends the minimum-defence share `pot / (pot + bet)` of its range, no more, no
//! less, narrowed by each earlier street it already paid; raises the very top plus a thin bluff
//! band. Against a big bet it folds more, against a small one it calls more - price decides.
//! - In position it bets thinner than out of position; the preflop aggressor c-bets more than the
//! caller leads; multiway it tightens and stops bluffing.
//!
//! It is not a solver: ranks come from equity against a random hand (draws included), the ranges
//! are fixed charts. But it has no single-street leak a heuristic can farm: it neither over-folds
//! nor over-calls, and its bluffs are as frequent as its value bets allow.
use super::opponent::{Act, View};
use crate::card::{deck, Card};
use crate::game::{ActionRecord, SeenAction, Street};
use crate::hand::showdown::best;
use crate::preflop::holdem_percentile::holdem_percentile;
use crate::rng::Rng;
use crate::variant::HoleUsage;
/// Range hands the own hand is ranked against.
const RANGE_SAMPLES: usize = 36;
/// Runouts per range hand when the board is not complete.
const RUNOUTS: usize = 20;
/// Chance a street's call is carried by the narrower range on the next street.
const STREET_REACH: f64 = 0.62;
/// The betting shape of a balanced opponent: its bet size (pot fraction) and how many bluffs it mixes
/// in per value bet. Several shapes give the ladder independent balanced targets (so a defensive fix
/// must generalise, not overfit one sizing). All shapes stay balanced - the bluff ratio matches the
/// size's minimum-defence indifference - so none has a single-street leak a heuristic can farm.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Shape {
pub bet_size: f64,
pub bluff_per_value: f64,
}
impl Shape {
/// Two-thirds-pot bettor, 0.4 bluffs per value (the original `Gto`). Byte-identical to the
/// constants it replaces, so the existing `heads-up gto` measurement is unchanged.
pub const BALANCED: Shape = Shape { bet_size: 2.0 / 3.0, bluff_per_value: 0.4 };
/// Small-ball half-pot bettor, value-leaning (0.3 bluffs per value): a tighter, more
/// value-weighted river betting range - the hardest case for a hero that over-calls marginal
/// made hands, and an independent balanced target.
pub const SMALL: Shape = Shape { bet_size: 0.5, bluff_per_value: 0.3 };
}
pub struct Ctx<'a> {
pub hole: &'a [Card],
pub board: &'a [Card],
/// Equity against one random hand (the shared `Reading` signal).
pub strength: f64,
/// Whole pot, bets of this street included.
pub pot: f64,
pub own_bet: f64,
pub stack: f64,
/// 0-based seat; 0 is the button.
pub seat: usize,
pub players: usize,
pub live: usize,
/// Acts last among the live players after the flop.
pub in_position: bool,
pub history: &'a [ActionRecord],
pub seed: u64,
/// Bet size and bluff mix of this balanced opponent.
pub shape: Shape,
}
struct Pre {
/// Raises made before this action (blinds are not raises).
level: usize,
last_raiser: Option<u8>,
limpers: usize,
max_bet: f64,
}
fn preflop_facts(history: &[ActionRecord]) -> Pre {
let mut pre = Pre { level: 0, last_raiser: None, limpers: 0, max_bet: 0.0 };
for r in history.iter().filter(|r| r.street == Street::Preflop) {
let amount = r.amount.unwrap_or(0.0);
match r.action {
SeenAction::PostAnte => {}
SeenAction::PostSmallBlind | SeenAction::PostBigBlind => pre.max_bet = pre.max_bet.max(amount),
SeenAction::Call if amount <= 1.0 + 1e-9 => pre.limpers += 1,
_ if amount > pre.max_bet + 1e-9 => {
pre.level += 1;
pre.last_raiser = Some(r.seat);
pre.max_bet = amount;
}
_ => {}
}
}
pre
}
/// Distance behind the button: 0 button, 1 small blind, 2 big blind, then early to late.
fn open_share(ctx: &Ctx) -> f64 {
let n = ctx.players;
let seat = ctx.seat;
if n == 2 {
return if seat == 0 { 0.80 } else { 1.0 };
}
match seat {
0 => 0.45,
1 => 0.40,
2 => 1.0,
s => match n - s {
1 => 0.27,
2 => 0.21,
_ => 0.16,
},
}
}
/// Share of hands called against one open, by seat.
fn call_share(ctx: &Ctx) -> f64 {
let n = ctx.players;
match (n, ctx.seat) {
(2, 1) => 0.70,
(_, 2) => 0.50,
(_, 0) => 0.12,
(_, 1) => 0.05,
(_, s) if n - s == 1 => 0.10,
_ => 0.08,
}
}
fn raise_to(ctx: &Ctx, to: f64) -> Act {
Act::RaiseTo(to.min(ctx.stack + ctx.own_bet))
}
fn preflop(view: View, ctx: &Ctx, pre: &Pre, rng: Rng) -> (Act, Rng) {
let q = holdem_percentile(ctx.hole);
let (u, rng) = rng.unit();
let can_raise = view.min_raise_to.is_some();
let hu = ctx.players == 2;
let act = match pre.level {
0 => {
let share = open_share(ctx);
let free = view.to_call <= 0.0;
if free {
// Big blind with the option: iso-raise the top, else check.
if can_raise && pre.limpers > 0 && q <= 0.14 { raise_to(ctx, 3.5 + pre.limpers as f64) } else { Act::Check }
} else if can_raise && pre.limpers > 0 && q <= share * 0.8 {
raise_to(ctx, 3.5 + pre.limpers as f64)
} else if can_raise && pre.limpers == 0 && q <= share {
raise_to(ctx, if ctx.seat == 1 && !hu { 3.0 } else { 2.5 })
} else if q <= share * 0.5 && view.to_call <= 0.5 + 1e-9 {
Act::Call
} else {
Act::Fold
}
}
1 => {
let x = pre.max_bet;
let scale = (2.5 / x).clamp(0.3, 1.2);
let value = if hu { 0.09 } else { 0.035 };
let (lo, hi, p) = if hu { (0.30, 0.50, 0.30) } else { (0.28, 0.42, 0.28) };
let call = call_share(ctx) * scale;
let three_bet = (if ctx.in_position_preflop() { 3.0 } else { 3.6 }) * x;
if can_raise && q <= value {
raise_to(ctx, three_bet)
} else if can_raise && q >= lo && q <= hi && u < p * scale {
raise_to(ctx, three_bet)
} else if q <= call {
Act::Call
} else {
Act::Fold
}
}
2 => {
let x = pre.max_bet;
let scale = (8.0 / x).clamp(0.3, 1.3);
let (value, call) = if hu { (0.035, 0.20) } else { (0.02, 0.12) };
if can_raise && q <= value {
raise_to(ctx, 2.3 * x)
} else if can_raise && q >= 0.15 && q <= 0.30 && u < 0.12 * scale {
raise_to(ctx, 2.3 * x)
} else if q <= call * scale {
Act::Call
} else {
Act::Fold
}
}
_ => {
if q <= 0.02 {
if can_raise && pre.max_bet < ctx.stack * 0.5 { raise_to(ctx, ctx.stack + ctx.own_bet) } else { Act::Call }
} else {
Act::Fold
}
}
};
(act, rng)
}
impl Ctx<'_> {
/// Preflop, "in position" over the opener: the button, or a seat after the raiser. Coarse.
fn in_position_preflop(&self) -> bool {
self.seat == 0 || (self.players > 2 && self.seat >= 3 && self.seat + 1 == self.players)
}
}
/// How often this seat has already paid a postflop bet.
fn prior_calls(ctx: &Ctx, street: Street) -> i32 {
ctx.history
.iter()
.filter(|r| r.street >= Street::Flop && r.street < street && r.seat as usize == ctx.seat + 1 && r.action == SeenAction::Call)
.count() as i32
}
/// Share of all hands this seat still holds after its own preflop actions (its range).
fn range_share(ctx: &Ctx, pre: &Pre) -> f64 {
let mine = ctx.seat as u8 + 1;
let last = ctx.history.iter().rev().find(|r| r.street == Street::Preflop && r.seat == mine && !matches!(r.action, SeenAction::PostAnte | SeenAction::PostSmallBlind | SeenAction::PostBigBlind));
match last.map(|r| r.action) {
Some(SeenAction::Raise | SeenAction::Bet | SeenAction::AllIn) if pre.last_raiser == Some(mine) => {
if pre.level <= 1 { open_share(ctx).min(0.8) } else { 0.07 }
}
Some(SeenAction::Call) => {
if pre.level <= 1 { call_share(ctx).max(0.10) } else { 0.10 }
}
_ => 0.9,
}
}
fn candidate_equity(cand: &[Card], board: &[Card], rng: Rng) -> (f64, Rng) {
let mut known = cand.to_vec();
known.extend_from_slice(board);
let pool = deck::without(&known);
let missing = 5 - board.len();
let mut rng = rng;
let mut won = 0.0;
for _ in 0..RUNOUTS {
let (cards, next) = deck::draw(&pool, 2 + missing, rng).expect("enough cards");
rng = next;
let mut full = board.to_vec();
full.extend_from_slice(&cards[2..]);
let mine = best(HoleUsage::BestFiveOfAll, cand, &full).expect("valid cards");
let theirs = best(HoleUsage::BestFiveOfAll, &cards[..2], &full).expect("valid cards");
won += match mine.cmp(&theirs) {
std::cmp::Ordering::Greater => 1.0,
std::cmp::Ordering::Equal => 0.5,
std::cmp::Ordering::Less => 0.0,
};
}
(won / RUNOUTS as f64, rng)
}
/// Share of the own range (preflop-chart hands) that beats this hand on the current board:
/// 0 is the nuts, 1 the worst. River: exact showdown ranks; earlier streets: equity against a random
/// hand, which counts draws.
fn range_pct(ctx: &Ctx, share: f64, rng: Rng) -> (f64, Rng) {
let mut known = ctx.hole.to_vec();
known.extend_from_slice(ctx.board);
let pool = deck::without(&known);
let river = ctx.board.len() == 5;
let mine_showdown = river.then(|| best(HoleUsage::BestFiveOfAll, ctx.hole, ctx.board).expect("valid cards"));
let mut rng = rng;
let (mut beaten_by, mut seen) = (0.0, 0usize);
let mut attempts = 0;
while seen < RANGE_SAMPLES && attempts < 600 {
attempts += 1;
let (i, r) = rng.below(pool.len() as u64);
let (mut j, r) = r.below(pool.len() as u64 - 1);
rng = r;
if j >= i {
j += 1;
}
let cand = [pool[i as usize], pool[j as usize]];
if holdem_percentile(&cand) > share {
continue;
}
seen += 1;
beaten_by += match &mine_showdown {
Some(mine) => {
let theirs = best(HoleUsage::BestFiveOfAll, &cand, ctx.board).expect("valid cards");
match theirs.cmp(mine) {
std::cmp::Ordering::Greater => 1.0,
std::cmp::Ordering::Equal => 0.5,
std::cmp::Ordering::Less => 0.0,
}
}
None => {
let (eq, r) = candidate_equity(&cand, ctx.board, rng);
rng = r;
if eq > ctx.strength { 1.0 } else if eq == ctx.strength { 0.5 } else { 0.0 }
}
};
}
((if seen == 0 { 0.5 } else { beaten_by / seen as f64 }), rng)
}
fn postflop(view: View, ctx: &Ctx, pre: &Pre, rng: Rng) -> (Act, Rng) {
let street = match ctx.board.len() {
3 => Street::Flop,
4 => Street::Turn,
_ => Street::River,
};
let river = street == Street::River;
let (p, rng) = range_pct(ctx, range_share(ctx, pre), rng);
let (u, rng) = rng.unit();
let (u2, rng) = rng.unit();
let can_raise = view.min_raise_to.is_some();
let multi = ctx.live > 2;
let bet_to = |frac: f64| raise_to(ctx, ctx.own_bet + view.to_call + frac * (ctx.pot + view.to_call));
let (pool_lo, pool_hi) = if river { (0.72, 1.0) } else { (0.55, 0.92) };
if view.to_call <= 0.0 {
if !can_raise {
return (Act::Check, rng);
}
let aggressor = pre.last_raiser == Some(ctx.seat as u8 + 1);
let mut value = match (aggressor, ctx.in_position) {
(true, false) => 0.28,
(true, true) => 0.33,
(false, true) => 0.30,
(false, false) => 0.16,
};
let mut bluffs = ctx.shape.bluff_per_value * value;
if multi {
value *= 0.6;
bluffs = ctx.shape.bluff_per_value * value * 0.3;
}
let bluff_prob = bluffs / (pool_hi - pool_lo);
let act = if p <= 0.03 && !river && u < 0.25 {
Act::Check
} else if p <= value || (p >= pool_lo && p <= pool_hi && u2 < bluff_prob) {
bet_to(ctx.shape.bet_size)
} else {
Act::Check
};
return (act, rng);
}
// Facing a bet or raise: defend the minimum-defence share of the range.
let before = (ctx.pot - view.to_call).max(1e-9);
let mdf = before / ctx.pot;
let reach = STREET_REACH.powi(prior_calls(ctx, street));
let defend = mdf * reach * if multi { 0.85 } else { 1.0 };
let faced_raise = ctx.own_bet > 0.0;
let raise_value = if faced_raise { 0.03 } else if river { 0.06 } else { 0.05 };
let all_in_price = view.to_call >= ctx.stack - 1e-9;
let act = if can_raise && !all_in_price && p <= raise_value && u < 0.9 {
raise_to(ctx, 3.0 * (ctx.own_bet + view.to_call))
} else if can_raise
&& !all_in_price
&& !faced_raise
&& !multi
&& p > defend
&& p >= (if river { 0.85 } else { 0.55 })
&& p <= (if river { 1.0 } else { 0.92 })
&& u < 0.07
{
raise_to(ctx, 3.0 * (ctx.own_bet + view.to_call))
} else if p <= defend {
Act::Call
} else {
Act::Fold
};
(act, rng)
}
pub fn act(view: View, ctx: &Ctx, rng: Rng) -> (Act, Rng) {
let pre = preflop_facts(ctx.history);
if ctx.board.is_empty() {
preflop(view, ctx, &pre, rng)
} else {
// The ranking has its own stream so that it does not depend on the hand's other randomness.
let (act, _) = postflop(view, ctx, &pre, Rng::seeded(ctx.seed));
let (_, rng) = rng.next_u64();
(act, rng)
}
}