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>
100 lines
4.3 KiB
Rust
100 lines
4.3 KiB
Rust
//! One CFR+ iteration as a pure fold step `(state, rng) -> (state, rng)`.
|
|
//!
|
|
//! Full-width, vectorised over hand classes: every deal is enumerated exactly through the chance
|
|
//! table, so no random numbers are drawn and the generator passes through unchanged (it is part
|
|
//! of the signature so a sampling variant can replace this step without touching callers).
|
|
//! Update rule: CFR+ with alternating players -- regrets are floored at zero, the average
|
|
//! strategy is weighted linearly by the iteration number (Tammelin 2014), player 0 is updated
|
|
//! first and player 1 then responds to the updated strategy.
|
|
//!
|
|
//! The mutation below is local to this function: the input state is consumed, a new one returned.
|
|
|
|
use super::regret_matching::matched;
|
|
use super::solver_state::SolverState;
|
|
use super::terminal_value::terminal_value;
|
|
use super::tree::{Node, Tree};
|
|
use crate::rng::Rng;
|
|
|
|
pub fn iterate(tree: &Tree, state: SolverState, rng: Rng) -> (SolverState, Rng) {
|
|
let (iteration, mut regret, mut sums) = state.into_parts();
|
|
let next = iteration.saturating_add(1);
|
|
if tree.is_playable() && regret.len() == tree.nodes.len() {
|
|
let ones = vec![1.0; tree.classes];
|
|
for player in 0..2 {
|
|
let mut pass = Pass { tree, player, weight: next as f64, regret: &mut regret, sums: &mut sums };
|
|
pass.walk(tree.root, &ones, &ones);
|
|
}
|
|
}
|
|
(SolverState::from_parts(next, regret, sums), rng)
|
|
}
|
|
|
|
struct Pass<'a> {
|
|
tree: &'a Tree,
|
|
player: usize,
|
|
weight: f64,
|
|
regret: &'a mut Vec<Vec<f64>>,
|
|
sums: &'a mut Vec<Vec<f64>>,
|
|
}
|
|
|
|
impl Pass<'_> {
|
|
/// Counterfactual values of the traversing player at `node`, one per class.
|
|
fn walk(&mut self, node: usize, own: &[f64], opp: &[f64]) -> Vec<f64> {
|
|
let tree = self.tree;
|
|
let n = tree.classes;
|
|
match tree.nodes.get(node) {
|
|
None => vec![0.0; n],
|
|
Some(Node::Terminal(payoff)) => terminal_value(payoff, &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);
|
|
total.iter_mut().zip(&value).for_each(|(t, v)| *t += v);
|
|
}
|
|
total
|
|
}
|
|
Some(Node::Decision { player, children, .. }) => {
|
|
let actions = children.len();
|
|
if actions == 0 {
|
|
return vec![0.0; n];
|
|
}
|
|
let sigma = matched(&self.regret[node], actions);
|
|
if *player == self.player {
|
|
self.own_node(node, children, &sigma, own, opp)
|
|
} 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] * sigma[j * actions + a]).collect();
|
|
let value = self.walk(*child, own, &reach);
|
|
total.iter_mut().zip(&value).for_each(|(t, v)| *t += v);
|
|
}
|
|
total
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn own_node(&mut self, node: usize, children: &[usize], sigma: &[f64], own: &[f64], opp: &[f64]) -> Vec<f64> {
|
|
let n = self.tree.classes;
|
|
let actions = children.len();
|
|
let values: Vec<Vec<f64>> = children
|
|
.iter()
|
|
.enumerate()
|
|
.map(|(a, child)| {
|
|
let reach: Vec<f64> = (0..n).map(|i| own[i] * sigma[i * actions + a]).collect();
|
|
self.walk(*child, &reach, opp)
|
|
})
|
|
.collect();
|
|
let mut node_value = vec![0.0; n];
|
|
for i in 0..n {
|
|
node_value[i] = (0..actions).map(|a| sigma[i * actions + a] * values[a][i]).sum();
|
|
for a in 0..actions {
|
|
let r = &mut self.regret[node][i * actions + a];
|
|
*r = (*r + values[a][i] - node_value[i]).max(0.0);
|
|
self.sums[node][i * actions + a] += self.weight * own[i] * sigma[i * actions + a];
|
|
}
|
|
}
|
|
node_value
|
|
}
|
|
}
|