Files
pokerfreak/src/cfr/leaf_value3.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

122 lines
4.6 KiB
Rust

//! Counterfactual value of a three-player leaf for one traverser, given both opponents' reach.
//!
//! `value[x] = scale * sum_{y, z} P(x, y, z) * reach_q[y] * reach_r[z] * u_p(x, y, z)`, where `q`
//! and `r` are the two opponents and `P` the triple deal weight of `Tree3`. The traverser's own
//! reach is not part of the value. Opponent classes with zero reach are skipped; a leaf whose
//! payoff does not depend on the third player's class reduces the inner sum to a dot product.
use super::tree3::{Outcome, Tree3};
fn others(p: usize) -> (usize, usize) {
match p {
0 => (1, 2),
1 => (0, 2),
_ => (0, 1),
}
}
fn support(reach: &[f64]) -> Vec<usize> {
(0..reach.len()).filter(|k| reach[*k] != 0.0).collect()
}
/// `sum_{y, z} P(x, y, z) reach_o[y] reach_t[z] u(x, y)` for every `x`, times `scale`.
fn with_one(tree: &Tree3, reach_o: &[f64], reach_t: &[f64], scale: f64, u: impl Fn(usize, usize) -> f64) -> Vec<f64> {
let n = tree.classes;
let g = &tree.pair;
let ys = support(reach_o);
if ys.is_empty() || tree.norm <= 0.0 || reach_t.iter().all(|r| *r == 0.0) {
return vec![0.0; n];
}
let factor = scale / tree.norm;
let mut hx = vec![0.0; n];
(0..n)
.map(|x| {
let row_x = &g[x * n..(x + 1) * n];
hx.iter_mut().zip(row_x.iter().zip(reach_t)).for_each(|(h, (a, r))| *h = a * r);
let acc: f64 = ys
.iter()
.map(|y| {
let gxy = row_x[*y] * reach_o[*y];
if gxy == 0.0 {
return 0.0;
}
let row_y = &g[y * n..(y + 1) * n];
let d: f64 = hx.iter().zip(row_y).map(|(a, b)| a * b).sum();
gxy * d * u(x, *y)
})
.sum();
acc * factor
})
.collect()
}
/// Full triple sum for leaves whose payoff depends on all three classes.
fn with_both(tree: &Tree3, p: usize, outcome: &Outcome, reach: [&[f64]; 3], scale: f64) -> Vec<f64> {
let n = tree.classes;
let g = &tree.pair;
let (q, r) = others(p);
let (ys, zs) = (support(reach[q]), support(reach[r]));
if ys.is_empty() || zs.is_empty() || tree.norm <= 0.0 {
return vec![0.0; n];
}
let factor = scale / tree.norm;
(0..n)
.map(|x| {
let row_x = &g[x * n..(x + 1) * n];
let mut acc = 0.0;
for y in &ys {
let gxy = row_x[*y] * reach[q][*y];
if gxy == 0.0 {
continue;
}
let row_y = &g[y * n..(y + 1) * n];
for z in &zs {
let w = gxy * row_x[*z] * row_y[*z] * reach[r][*z];
if w == 0.0 {
continue;
}
let mut c = [0usize; 3];
c[p] = x;
c[q] = *y;
c[r] = *z;
acc += w * tree.payoff(outcome, c)[p];
}
}
acc * factor
})
.collect()
}
/// Probability mass of reaching the leaf with each own class: the value of a payoff of 1.
pub fn reach_mass(tree: &Tree3, p: usize, reach: [&[f64]; 3], scale: f64) -> Vec<f64> {
let (q, r) = others(p);
with_one(tree, reach[q], reach[r], scale, |_, _| 1.0)
}
pub fn leaf_value(tree: &Tree3, outcome: &Outcome, p: usize, reach: [&[f64]; 3], scale: f64) -> Vec<f64> {
let (q, r) = others(p);
let own_only = |u: &dyn Fn(usize) -> f64| with_one(tree, reach[q], reach[r], scale, |x, _| u(x));
match outcome {
Outcome::Flat(u) => own_only(&|_| u[p]),
Outcome::Own { player, values, rest } if *player == p => {
own_only(&|x| values.get(x).copied().unwrap_or(0.0))
}
Outcome::Own { rest, .. } => own_only(&|_| rest[p]),
Outcome::Heads { a, b, pot, invested, share } if *a == p || *b == p => {
let n = tree.classes;
let other = if *a == p { *b } else { *a };
let third = 3 - p - other;
let mine = |x: usize, y: usize| -> f64 {
if *a == p {
share.get(x * n + y).copied().unwrap_or(0.5)
} else {
1.0 - share.get(y * n + x).copied().unwrap_or(0.5)
}
};
with_one(tree, reach[other], reach[third], scale, |x, y| pot * mine(x, y) - invested[p])
}
Outcome::Heads { invested, .. } => own_only(&|_| -invested[p]),
Outcome::Three { .. } => with_both(tree, p, outcome, reach, scale),
}
}