//! The Nash test's raw material: for every information set that can be reached, the value of //! each action while every other decision stays frozen. //! //! `value[a]` is conditional on arriving at the node with that hand class: the counterfactual //! value divided by the opponent-reach mass `sum_j chance(i, j) * opp_reach(j)`. `weight` is the //! probability of getting there at all, `own_reach(i) * mass(i)`; rows with tiny weight are //! allowed to be loose because their value is multiplied by that weight everywhere else. use super::strategy::Strategy; use super::tree::Tree; use super::value_walk::{root_values, Responder}; #[derive(Clone, Debug, PartialEq)] pub struct Deviation { pub node: &'static str, pub player: usize, pub class: usize, pub weight: f64, pub frequency: Vec, pub value: Vec, } impl Deviation { /// Expected value of the frozen mix at this information set. pub fn mix_value(&self) -> f64 { self.frequency.iter().zip(&self.value).map(|(f, v)| f * v).sum() } /// How much better the best zero-frequency action is than the mix (0 if none is better). pub fn zero_frequency_gain(&self) -> f64 { let mix = self.mix_value(); self.frequency .iter() .zip(&self.value) .filter(|(f, _)| **f == 0.0) .map(|(_, v)| v - mix) .fold(0.0, f64::max) } /// How much worse than the mix the worst action played with >= 5 % is (0 if none is worse). pub fn support_shortfall(&self) -> f64 { let mix = self.mix_value(); self.frequency .iter() .zip(&self.value) .filter(|(f, _)| **f >= 0.05) .map(|(_, v)| mix - v) .fold(0.0, f64::max) } } /// One entry per decision node, hand class and acting player that has a defined conditional value. pub fn deviations(tree: &Tree, strategy: &Strategy) -> Vec { let n = tree.classes; let mut found = Vec::new(); for player in 0..2 { let mut visit = |v: &super::value_walk::Visit| { for class in 0..n { let mass: f64 = (0..n) .map(|k| { let chance = if player == 0 { tree.chance[class * n + k] } else { tree.chance[k * n + class] }; chance * v.opp[k] }) .sum(); if mass <= 0.0 { continue; } found.push(Deviation { node: v.name, player, class, weight: v.own[class] * mass, frequency: strategy.row(v.node, class).to_vec(), value: v.action_values.iter().map(|a| a[class] / mass).collect(), }); } }; root_values(tree, strategy, Responder::Follow, player, &mut visit); } found }