Files
pokerfreak/src/cfr/tree3.rs
T
mmintelandClaude Opus 5.5 7f40fa49a0 CFR charts with both blinds as defenders, short deck tables
EP, MP, CO and BTN now play three-player games against small and big
blind (push/fold and open/call/3-bet trees, CFR+ for three players,
exploitability summed over all three best responses). Deal weights and
three-way shares are documented approximations. Short deck gets its own
equity table and charts from the same generator (--deck short).

Open width shrinks in EP/MP (e.g. EP 100bb 0.291 -> 0.249, MP 40bb
0.357 -> 0.293); frozen exploitability stays at 0-4 micro-bb (full) and
0-9 micro-bb (short). Lookup and advice cover SB vs open/jam, BB after
an SB call and the opener vs an SB 3-bet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 15:02:45 +01:00

129 lines
5.1 KiB
Rust

//! A three-player game as data: the same public tree as `tree` (decision nodes are played per
//! hand class), with a payoff for every player at each leaf and chance weights over class triples.
//!
//! **Deal weights.** `P(i, j, k) = g(i, j) * g(i, k) * g(j, k) / norm` with
//! `g(i, j) = disjoint combo pairs (i, j) / sqrt(combos(i) * combos(j))`. Card removal between any
//! two players is exact; an overlap of all three hands (AK, AQ, AJ share the ace) is approximated
//! by the product of the pairwise compatibilities. The weight is symmetric in the three classes.
//!
//! **Three-way showdowns** use the pairwise equity matrix (`three_way_shares`): no three-way
//! equity table is generated.
use super::environment::Environment;
use super::hand_class::CLASS_COUNT;
use super::three_way::three_way_shares;
#[derive(Clone, Debug, PartialEq)]
pub enum Outcome {
/// Every player's result is fixed.
Flat([f64; 3]),
/// `player`'s result depends on its own class (`values`), the others get `rest`.
Own { player: usize, values: Vec<f64>, rest: [f64; 3] },
/// Showdown between `a` and `b`: `a` receives `pot * share[i_a * classes + i_b]`, `b` the rest
/// of the pot; every player's own `invested` chips are subtracted (the third player folded).
Heads { a: usize, b: usize, pot: f64, invested: [f64; 3], share: Vec<f64> },
/// Three-way showdown: the pot is split by `three_way_shares` with the realisation `weights`
/// and `spread` (all-in: weights 1, spread 1).
Three { pot: f64, invested: [f64; 3], weights: [f64; 3], spread: f64 },
}
#[derive(Clone, Debug, PartialEq)]
pub enum Node3 {
Decision { player: usize, name: &'static str, children: Vec<usize> },
/// Branches `(probability, child)`; probabilities should sum to 1.
Chance(Vec<(f64, usize)>),
Terminal(Outcome),
}
#[derive(Clone, Debug, PartialEq)]
pub struct Tree3 {
pub classes: usize,
/// `g(i, j)`, row-major, symmetric.
pub pair: Vec<f64>,
/// Sum of `g(i, j) g(i, k) g(j, k)` over all triples.
pub norm: f64,
/// Pairwise all-in equity `e(i, j)`, row-major.
pub equity: Vec<f64>,
pub nodes: Vec<Node3>,
pub root: usize,
}
/// `g` and its normaliser for the deck of `env`.
pub fn triple_weights(env: &Environment) -> (Vec<f64>, f64) {
let n = CLASS_COUNT;
let combos: Vec<f64> = (0..n).map(|c| env.chance.combos(c) as f64).collect();
let pair: Vec<f64> = (0..n * n)
.map(|k| {
let (i, j) = (k / n, k % n);
let scale = (combos[i] * combos[j]).sqrt();
if scale > 0.0 {
env.chance.disjoint_combos(i, j) as f64 / scale
} else {
0.0
}
})
.collect();
let norm = (0..n)
.map(|i| {
let row_i = &pair[i * n..(i + 1) * n];
(0..n)
.map(|j| {
let row_j = &pair[j * n..(j + 1) * n];
row_i[j] * row_i.iter().zip(row_j).map(|(a, b)| a * b).sum::<f64>()
})
.sum::<f64>()
})
.sum();
(pair, norm)
}
impl Tree3 {
pub fn new(env: &Environment, nodes: Vec<Node3>, root: usize) -> Tree3 {
let (pair, norm) = triple_weights(env);
let n = CLASS_COUNT;
let equity = (0..n * n).map(|k| env.equity.get(k / n, k % n)).collect();
Tree3 { classes: n, pair, norm, equity, nodes, root }
}
/// Probability of the deal `(i, j, k)` (any order of the players).
pub fn weight(&self, i: usize, j: usize, k: usize) -> f64 {
let n = self.classes;
if i >= n || j >= n || k >= n || self.norm <= 0.0 {
return 0.0;
}
self.pair[i * n + j] * self.pair[i * n + k] * self.pair[j * n + k] / self.norm
}
/// Every player's result at a leaf for the classes `c` (indexed by player).
pub fn payoff(&self, outcome: &Outcome, c: [usize; 3]) -> [f64; 3] {
let n = self.classes;
match outcome {
Outcome::Flat(u) => *u,
Outcome::Own { player, values, rest } => {
let mut u = *rest;
if let Some(slot) = u.get_mut(*player) {
*slot = values.get(c[*player]).copied().unwrap_or(0.0);
}
u
}
Outcome::Heads { a, b, pot, invested, share } => {
let s = share.get(c[*a] * n + c[*b]).copied().unwrap_or(0.5);
let mut u = invested.map(|x| -x);
u[*a] += pot * s;
u[*b] += pot * (1.0 - s);
u
}
Outcome::Three { pot, invested, weights, spread } => {
let e = |x: usize, y: usize| self.equity[c[x] * n + c[y]];
let shares = three_way_shares([[0.5, e(0, 1), e(0, 2)], [e(1, 0), 0.5, e(1, 2)], [e(2, 0), e(2, 1), 0.5]], *weights, *spread);
[0, 1, 2].map(|p| pot * shares[p] - invested[p])
}
}
}
/// The tree can be traversed.
pub fn is_playable(&self) -> bool {
self.classes > 0 && self.pair.len() == self.classes * self.classes && self.root < self.nodes.len()
}
}