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>
117 lines
4.2 KiB
Rust
117 lines
4.2 KiB
Rust
//! Evaluation traversals without learning: expected value of a fixed strategy, best response,
|
|
//! and the per-action values at every information set (for the deviation report).
|
|
//!
|
|
//! Values are counterfactual in the same sense as in `iterate`: for the traverser they are summed
|
|
//! over the opponent's holdings weighted by chance and the opponent's reach, and exclude the
|
|
//! traverser's own reach.
|
|
|
|
use super::strategy::Strategy;
|
|
use super::terminal_value::terminal_value;
|
|
use super::tree::{Node, Tree};
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
pub(crate) enum Responder {
|
|
/// The traverser plays the given strategy.
|
|
Follow,
|
|
/// The traverser picks, per hand class, the best action everywhere.
|
|
Best,
|
|
}
|
|
|
|
/// What a traverser's decision node looked like during the walk.
|
|
pub(crate) struct Visit<'a> {
|
|
pub node: usize,
|
|
pub name: &'static str,
|
|
pub own: &'a [f64],
|
|
pub opp: &'a [f64],
|
|
/// `[action][class]`
|
|
pub action_values: &'a [Vec<f64>],
|
|
}
|
|
|
|
struct Walker<'a> {
|
|
tree: &'a Tree,
|
|
strategy: &'a Strategy,
|
|
responder: Responder,
|
|
player: usize,
|
|
}
|
|
|
|
impl Walker<'_> {
|
|
fn walk<F: FnMut(&Visit)>(&self, node: usize, own: &[f64], opp: &[f64], visit: &mut F) -> Vec<f64> {
|
|
let n = self.tree.classes;
|
|
match self.tree.nodes.get(node) {
|
|
None => vec![0.0; n],
|
|
Some(Node::Terminal(payoff)) => terminal_value(payoff, &self.tree.chance, n, self.player, opp),
|
|
Some(Node::Chance(branches)) => {
|
|
let mut total = vec![0.0; n];
|
|
for (p, child) in branches {
|
|
let scaled: Vec<f64> = opp.iter().map(|x| x * p).collect();
|
|
let value = self.walk(*child, own, &scaled, visit);
|
|
total.iter_mut().zip(&value).for_each(|(t, v)| *t += v);
|
|
}
|
|
total
|
|
}
|
|
Some(Node::Decision { player: owner, name, children }) => {
|
|
if *owner == self.player {
|
|
self.own_decision(node, name, children, own, opp, visit)
|
|
} else {
|
|
let mut total = vec![0.0; n];
|
|
for (a, child) in children.iter().enumerate() {
|
|
let reach: Vec<f64> =
|
|
(0..n).map(|j| opp[j] * self.strategy.probability(node, j, a)).collect();
|
|
let value = self.walk(*child, own, &reach, visit);
|
|
total.iter_mut().zip(&value).for_each(|(t, v)| *t += v);
|
|
}
|
|
total
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn own_decision<F: FnMut(&Visit)>(
|
|
&self,
|
|
node: usize,
|
|
name: &'static str,
|
|
children: &[usize],
|
|
own: &[f64],
|
|
opp: &[f64],
|
|
visit: &mut F,
|
|
) -> Vec<f64> {
|
|
let n = self.tree.classes;
|
|
let sigma = |class: usize, a: usize| self.strategy.probability(node, class, a);
|
|
let values: Vec<Vec<f64>> = children
|
|
.iter()
|
|
.enumerate()
|
|
.map(|(a, child)| {
|
|
let reach: Vec<f64> = match self.responder {
|
|
Responder::Follow => (0..n).map(|i| own[i] * sigma(i, a)).collect(),
|
|
Responder::Best => own.to_vec(),
|
|
};
|
|
self.walk(*child, &reach, opp, visit)
|
|
})
|
|
.collect();
|
|
visit(&Visit { node, name, own, opp, action_values: &values });
|
|
(0..n)
|
|
.map(|i| match self.responder {
|
|
Responder::Follow => (0..children.len()).map(|a| sigma(i, a) * values[a][i]).sum(),
|
|
Responder::Best => values.iter().map(|v| v[i]).fold(f64::NEG_INFINITY, f64::max),
|
|
})
|
|
.map(|v: f64| if v.is_finite() { v } else { 0.0 })
|
|
.collect()
|
|
}
|
|
}
|
|
|
|
/// Value vector of `player` from the root with full reach; `visit` sees every decision node of
|
|
/// that player.
|
|
pub(crate) fn root_values<F: FnMut(&Visit)>(
|
|
tree: &Tree,
|
|
strategy: &Strategy,
|
|
responder: Responder,
|
|
player: usize,
|
|
visit: &mut F,
|
|
) -> Vec<f64> {
|
|
if !tree.is_playable() {
|
|
return vec![0.0; tree.classes];
|
|
}
|
|
let ones = vec![1.0; tree.classes];
|
|
Walker { tree, strategy, responder, player }.walk(tree.root, &ones, &ones, visit)
|
|
}
|