//! 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 { (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 { 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 { 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 { 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 { 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), } }