Files
pokerfreak/src/cfr/deviation.rs
T
mmintelandClaude Opus 5.5 a7b32cf425 Add CFR preflop chart generator, frozen charts and lookup
Pure CFR+ core over a 169-class abstraction, push/fold and open/call/3-bet games,
Nash/Kuhn/plausibility/lookup tests, chartgen binary, frozen equity and charts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 13:05:07 +01:00

82 lines
2.9 KiB
Rust

//! 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<f64>,
pub value: Vec<f64>,
}
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<Deviation> {
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
}