Drei getestete/bewiesene Arbeitsstraenge dieser Session (128/128 Lib gruen): - EV-neutrales Size-Mix (engine::postflop): das Postflop-Sizing war reiner EV-Argmax (deterministisch und damit lesbar). Es mischt jetzt seeded unter den Raise-Sizes, deren EV innerhalb 0.5 Standardfehlern des besten liegt (unter dem Modell nicht unterscheidbar) - nur Raises, nie check/call/fold. Gepaart gegen die deterministische Baseline bewiesen EV-neutral (-0.23 +/-0.59 bb/100, 112 Spots gemischt, jede Zeile ununterscheidbar von 0); der winzige Negativ-Punktschaetzer ist konstruktionsbedingt (Abweichung nur weg vom Argmax) und band-getrieben. Schalter SIZE_MIX / SIZE_MIX_BAND. Befund: docs/notizen/BEFUND-size-mix-ev-neutral.md. - Stage B Turn+River-Subgame-Solver (cfr::turn_river, engine::turn_subgame) plus Per-Street-Aggression in der Belief (Profile turn_/river_aggression, sim::memory-Zaehler, believed_villain nimmt die Aggression als Parameter). Der Live-Turn-Fold-Veto wurde nach gepaarter Messung ZURUECKGENOMMEN (regressiert nemesis-1; Wurzel = draw-blinder Turn-Bucket, nicht Belief-Granularitaet -> Stage C). Solver- und Belief-Infra bleibt als getestete, Ladder-neutrale Primitive. Befund: docs/notizen/BEFUND-stage-b-turn-river-solver.md. - Turnier-Staerke-Leiter (sim::turnier_leiter, bin turnier_leiter, icm::risk): ICM-$-gepaarte Leiter, die die ICM-Linie gegen Chip-EV beweist, Cash bit-identisch. Befunde: docs/notizen/BEFUND-turnier-*.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
218 lines
11 KiB
Rust
218 lines
11 KiB
Rust
//! A heads-up TURN subgame as a `Tree`, depth-limited to one betting round with the value of the
|
|
//! river folded into the leaves. The betting shape is identical to the river tree (`cfr::river`):
|
|
//! hero acts first, one bet size, no raises. The difference is the terminal that would be a river
|
|
//! showdown: here the turn is NOT the last street, so the three "reach the river" terminals carry a
|
|
//! **leaf value matrix** `V[i * buckets + j]` = hero's expected chips over the whole river *given*
|
|
//! the turn buckets `(i, j)`, instead of a static showdown.
|
|
//!
|
|
//! This is the DeepStack/endgame construction: solve only the turn decisions, and evaluate each
|
|
//! river-reaching leaf by the equilibrium value of the river subgame it leads to (precomputed by the
|
|
//! caller, averaged over the runout, via the existing `cfr::river` solver). Because the CFR core
|
|
//! (`solve`, `terminal_value`, `kl_br`, `exploitability`) is street-agnostic and `terminal_value`
|
|
//! reads an arbitrary `Payoff::Pair`, no core change is needed — only this builder and the leaf
|
|
//! precompute. When the leaf is set to a plain monotone showdown, this tree is byte-identical to
|
|
//! `cfr::river::tree_with_chance` (see tests), so the turn solve degrades gracefully to the river one.
|
|
//!
|
|
//! Buckets are TURN strength classes (0 = weakest), `chance[i * buckets + j]` the joint turn deal
|
|
//! table with card removal already inside. Payoffs are hero's (player 0) net in chips, zero-sum, each
|
|
//! player treated as having put `p / 2` into the turn pot: a turn fold moves the dead pot `±p/2`; a
|
|
//! river-reaching leaf pays the precomputed `V`, which itself nets the river play to hero's chips.
|
|
|
|
use super::solve::{solve, Solved};
|
|
use super::strategy::Strategy;
|
|
use super::tree::{Node, Payoff, Tree};
|
|
use crate::rng::Rng;
|
|
|
|
/// A solved heads-up turn subgame and the turn-bucket count it was built over.
|
|
pub struct TurnSolve {
|
|
pub solved: Solved,
|
|
pub buckets: usize,
|
|
}
|
|
|
|
impl TurnSolve {
|
|
/// Hero's action probabilities at the root (0 = check, 1 = bet) for a hand in turn `bucket`.
|
|
pub fn hero_root(&self, bucket: usize) -> Vec<f64> {
|
|
self.solved.strategy.row(0, bucket).to_vec()
|
|
}
|
|
|
|
/// Hero's action probabilities facing a turn bet after checking (node "hero_vs_bet", index 4:
|
|
/// 0 = fold, 1 = call) for a hand in turn `bucket`.
|
|
pub fn hero_vs_bet(&self, bucket: usize) -> Vec<f64> {
|
|
self.solved.strategy.row(4, bucket).to_vec()
|
|
}
|
|
}
|
|
|
|
/// Build the heads-up turn tree over `buckets` turn-strength buckets with an explicit joint deal
|
|
/// table `chance[i * buckets + j] = P(hero turn-bucket i, villain turn-bucket j)` (card removal
|
|
/// inside), turn pot `p`, and two river leaf-value matrices (each `buckets * buckets`, hero's net
|
|
/// chips over the river):
|
|
/// - `leaf_check`: the river reached via a turn CHECK-CHECK (river starts at pot `p`),
|
|
/// - `leaf_bet`: the river reached via a CALLED turn bet (river starts at the larger pot).
|
|
///
|
|
/// Node 0 is hero's root decision (check, bet). Shape mirrors `cfr::river::tree_with_chance`; the
|
|
/// bet size enters only through the leaf matrices the caller computed, so it is not a parameter here.
|
|
pub fn tree_with_leaf(buckets: usize, chance: Vec<f64>, p: f64, leaf_check: Vec<f64>, leaf_bet: Vec<f64>) -> Tree {
|
|
debug_assert_eq!(chance.len(), buckets * buckets, "chance table must be buckets^2");
|
|
debug_assert_eq!(leaf_check.len(), buckets * buckets, "leaf_check must be buckets^2");
|
|
debug_assert_eq!(leaf_bet.len(), buckets * buckets, "leaf_bet must be buckets^2");
|
|
let nodes = vec![
|
|
Node::Decision { player: 0, name: "hero", children: vec![1, 2] }, // 0: check -> 1, bet -> 2
|
|
Node::Decision { player: 1, name: "v_check", children: vec![3, 4] }, // 1: v check -> 3, v bet -> 4
|
|
Node::Decision { player: 1, name: "v_bet", children: vec![5, 6] }, // 2: v fold -> 5, v call -> 6
|
|
Node::Terminal(Payoff::Pair(leaf_check)), // 3: turn check-check -> river
|
|
Node::Decision { player: 0, name: "hero_vs_bet", children: vec![7, 8] }, // 4: hero fold -> 7, call -> 8
|
|
Node::Terminal(Payoff::Flat(p / 2.0)), // 5: hero bet, villain folds (uncalled)
|
|
Node::Terminal(Payoff::Pair(leaf_bet.clone())), // 6: hero bet, villain calls -> river
|
|
Node::Terminal(Payoff::Flat(-p / 2.0)), // 7: hero checks, villain bets, hero folds
|
|
Node::Terminal(Payoff::Pair(leaf_bet)), // 8: hero checks, villain bets, hero calls -> river
|
|
];
|
|
Tree { classes: buckets, chance, nodes, root: 0 }
|
|
}
|
|
|
|
/// Hero's (player 0) payoff at a terminal for a fixed class pair `(i, j)` — the raw per-deal result,
|
|
/// before the chance/opponent-reach weighting the solver applies.
|
|
fn hero_payoff(payoff: &Payoff, n: usize, i: usize, j: usize) -> f64 {
|
|
match payoff {
|
|
Payoff::Flat(c) => *c,
|
|
Payoff::Own(v) => v.get(i).copied().unwrap_or(0.0),
|
|
Payoff::Pair(m) => m.get(i * n + j).copied().unwrap_or(0.0),
|
|
Payoff::Raked { pair, .. } => pair.get(i * n + j).copied().unwrap_or(0.0),
|
|
}
|
|
}
|
|
|
|
/// Hero's expected net chips for every class pair `(i, j)` under a fixed `strategy`:
|
|
/// `out[i * classes + j] = E[hero payoff | hero class i, villain class j, both follow `strategy`]`.
|
|
///
|
|
/// A forward walk conditioned on the pair (no chance/reach weighting — that is applied outside, by
|
|
/// the parent tree's own `chance` table). This is exactly the matrix a depth-limited parent street
|
|
/// needs as a `Payoff::Pair` leaf: solve the river subgame, then `pair_values` reads the equilibrium
|
|
/// value per river-bucket pair. Invariant (checked in tests): weighting `out` by the tree's `chance`
|
|
/// reproduces `value_walk::root_values(Follow, 0)`.
|
|
pub fn pair_values(tree: &Tree, strategy: &Strategy) -> Vec<f64> {
|
|
let n = tree.classes;
|
|
let mut out = vec![0.0; n * n];
|
|
for i in 0..n {
|
|
for j in 0..n {
|
|
out[i * n + j] = eval_pair(tree, strategy, tree.root, i, j);
|
|
}
|
|
}
|
|
out
|
|
}
|
|
|
|
/// Expected hero payoff from `node` for the fixed class pair `(i, j)` under `strategy`.
|
|
fn eval_pair(tree: &Tree, strategy: &Strategy, node: usize, i: usize, j: usize) -> f64 {
|
|
match &tree.nodes[node] {
|
|
Node::Terminal(payoff) => hero_payoff(payoff, tree.classes, i, j),
|
|
Node::Chance(branches) => branches.iter().map(|(p, child)| p * eval_pair(tree, strategy, *child, i, j)).sum(),
|
|
Node::Decision { player, children, .. } => {
|
|
let class = if *player == 0 { i } else { j };
|
|
children
|
|
.iter()
|
|
.enumerate()
|
|
.map(|(a, child)| strategy.probability(node, class, a) * eval_pair(tree, strategy, *child, i, j))
|
|
.sum()
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Solve a heads-up turn subgame built by `tree_with_leaf`. Runs `iterations` of the shared CFR+ core.
|
|
pub fn solve_turn(buckets: usize, chance: Vec<f64>, p: f64, leaf_check: Vec<f64>, leaf_bet: Vec<f64>, iterations: u32, seed: u64) -> TurnSolve {
|
|
let t = tree_with_leaf(buckets, chance, p, leaf_check, leaf_bet);
|
|
TurnSolve { solved: solve(&t, iterations, Rng::seeded(seed)), buckets }
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::cfr::river::tree_with_chance;
|
|
use crate::cfr::value_walk::{root_values, Responder};
|
|
|
|
const ITERS: u32 = 20_000;
|
|
const SEED: u64 = 0x5EED_7117;
|
|
|
|
/// A monotone showdown matrix for total pot `q` (the river builder's private `showdown`, inlined).
|
|
fn showdown_vec(buckets: usize, q: f64) -> Vec<f64> {
|
|
let mut pair = vec![0.0; buckets * buckets];
|
|
for i in 0..buckets {
|
|
for j in 0..buckets {
|
|
pair[i * buckets + j] = match i.cmp(&j) {
|
|
std::cmp::Ordering::Greater => q / 2.0,
|
|
std::cmp::Ordering::Less => -q / 2.0,
|
|
std::cmp::Ordering::Equal => 0.0,
|
|
};
|
|
}
|
|
}
|
|
pair
|
|
}
|
|
|
|
/// Normalised joint deal table `hero[i] * villain[j]` (the river builder's private `chance`).
|
|
fn joint(hero: &[f64], villain: &[f64]) -> Vec<f64> {
|
|
let n = hero.len();
|
|
let mut t = vec![0.0; n * n];
|
|
let mut total = 0.0;
|
|
for i in 0..n {
|
|
for j in 0..n {
|
|
let w = hero[i].max(0.0) * villain[j].max(0.0);
|
|
t[i * n + j] = w;
|
|
total += w;
|
|
}
|
|
}
|
|
if total > 0.0 {
|
|
t.iter_mut().for_each(|x| *x /= total);
|
|
} else {
|
|
t.iter_mut().for_each(|x| *x = 1.0 / (n * n) as f64);
|
|
}
|
|
t
|
|
}
|
|
|
|
/// With the river folded in as a plain showdown (no river betting), the turn tree is byte-identical
|
|
/// to the river tree — `tree_with_leaf` is a faithful generalisation, so the depth-limited solve
|
|
/// degrades exactly to the one-street solve.
|
|
#[test]
|
|
fn showdown_leaf_reproduces_the_river_tree_exactly() {
|
|
let buckets = 3;
|
|
let (p, s) = (2.0, 1.0);
|
|
let chance = joint(&[0.5, 0.0, 0.5], &[0.0, 1.0, 0.0]);
|
|
let turn = tree_with_leaf(buckets, chance.clone(), p, showdown_vec(buckets, p), showdown_vec(buckets, p + 2.0 * s));
|
|
let river = tree_with_chance(buckets, chance, p, s);
|
|
assert_eq!(turn, river, "a showdown leaf must reproduce the river tree node-for-node");
|
|
}
|
|
|
|
/// `pair_values` reads back the exact leaf the pure strategy forces: hero always checks, villain
|
|
/// always checks → the check-check leaf, for every pair.
|
|
#[test]
|
|
fn pair_values_reads_back_the_forced_leaf() {
|
|
let buckets = 3;
|
|
let p = 2.0;
|
|
let leaf_check: Vec<f64> = (0..buckets * buckets).map(|k| k as f64 + 1.0).collect(); // arbitrary, distinct
|
|
let leaf_bet = vec![-9.0; buckets * buckets];
|
|
let tree = tree_with_leaf(buckets, joint(&[1.0, 1.0, 1.0], &[1.0, 1.0, 1.0]), p, leaf_check.clone(), leaf_bet);
|
|
// Pure: hero checks (action 0 at "hero"), villain checks (action 0 at "v_check").
|
|
let pure = Strategy::pure(&tree, |_, _| 0); // every node's action 0: hero check, villain check
|
|
let got = pair_values(&tree, &pure);
|
|
assert_eq!(got, leaf_check, "check-check must land on the check-check leaf for every pair");
|
|
}
|
|
|
|
/// The core invariant tying `pair_values` to the solver's own value: weighting the per-pair matrix
|
|
/// by the tree's `chance` reproduces `root_values(Follow, hero)` exactly. This is what lets a parent
|
|
/// street trust `pair_values` as its leaf.
|
|
#[test]
|
|
fn pair_values_weighted_by_chance_equals_root_values() {
|
|
let buckets = 3;
|
|
let (p, s) = (2.0, 1.0);
|
|
let chance = joint(&[0.4, 0.2, 0.4], &[0.1, 0.6, 0.3]);
|
|
let turn = tree_with_leaf(buckets, chance.clone(), p, showdown_vec(buckets, p), showdown_vec(buckets, p + 2.0 * s));
|
|
let solved = solve(&turn, ITERS, Rng::seeded(SEED));
|
|
let pv = pair_values(&turn, &solved.strategy);
|
|
let rv = root_values(&turn, &solved.strategy, Responder::Follow, 0, &mut |_| {});
|
|
for i in 0..buckets {
|
|
let weighted: f64 = (0..buckets).map(|j| chance[i * buckets + j] * pv[i * buckets + j]).sum();
|
|
assert!(
|
|
(weighted - rv[i]).abs() < 1e-9,
|
|
"class {i}: chance-weighted pair_values {weighted} != root_values {}",
|
|
rv[i]
|
|
);
|
|
}
|
|
}
|
|
}
|