EP, MP, CO and BTN now play three-player games against small and big blind (push/fold and open/call/3-bet trees, CFR+ for three players, exploitability summed over all three best responses). Deal weights and three-way shares are documented approximations. Short deck gets its own equity table and charts from the same generator (--deck short). Open width shrinks in EP/MP (e.g. EP 100bb 0.291 -> 0.249, MP 40bb 0.357 -> 0.293); frozen exploitability stays at 0-4 micro-bb (full) and 0-9 micro-bb (short). Lookup and advice cover SB vs open/jam, BB after an SB call and the opener vs an SB 3-bet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
260 lines
10 KiB
Rust
260 lines
10 KiB
Rust
//! Traversals of a three-player tree: the CFR+ step, evaluation of a fixed strategy, best
|
|
//! response, exploitability and the Nash test's per-infoset action values.
|
|
//!
|
|
//! Same scheme as the two-player `iterate` / `value_walk`: vectorised over hand classes, values
|
|
//! counterfactual (summed over both opponents' holdings, weighted by deal probability and their
|
|
//! reach, without the traverser's own reach). Chance branches scale a common factor.
|
|
//!
|
|
//! CFR+ update: regrets floored at zero, players updated in turn (0, 1, 2), each against the
|
|
//! strategies already updated in this iteration, average strategy weighted by the iteration.
|
|
//! For three players CFR has no equilibrium guarantee; whether the result is one is measured
|
|
//! (`exploitability`, `deviations`), not assumed.
|
|
|
|
use super::deviation::Deviation;
|
|
use super::game::Game;
|
|
use super::leaf_value3::{leaf_value, reach_mass};
|
|
use super::regret_matching::matched;
|
|
use super::solver_state::SolverState;
|
|
use super::strategy::Strategy;
|
|
use super::tree3::{Node3, Tree3};
|
|
use crate::rng::Rng;
|
|
|
|
type Reach = [Vec<f64>; 3];
|
|
|
|
fn view(reach: &Reach) -> [&[f64]; 3] {
|
|
[&reach[0], &reach[1], &reach[2]]
|
|
}
|
|
|
|
fn add(total: &mut [f64], value: &[f64]) {
|
|
total.iter_mut().zip(value).for_each(|(t, v)| *t += v);
|
|
}
|
|
|
|
struct Learn<'a> {
|
|
tree: &'a Tree3,
|
|
player: usize,
|
|
weight: f64,
|
|
regret: &'a mut Vec<Vec<f64>>,
|
|
sums: &'a mut Vec<Vec<f64>>,
|
|
}
|
|
|
|
impl Learn<'_> {
|
|
fn walk(&mut self, node: usize, reach: &Reach, scale: f64) -> Vec<f64> {
|
|
let tree = self.tree;
|
|
let n = tree.classes;
|
|
match tree.nodes.get(node) {
|
|
None => vec![0.0; n],
|
|
Some(Node3::Terminal(outcome)) => leaf_value(tree, outcome, self.player, view(reach), scale),
|
|
Some(Node3::Chance(branches)) => branches.iter().fold(vec![0.0; n], |mut total, (p, child)| {
|
|
let value = self.walk(*child, reach, scale * p);
|
|
add(&mut total, &value);
|
|
total
|
|
}),
|
|
Some(Node3::Decision { player, children, .. }) => {
|
|
let actions = children.len();
|
|
if actions == 0 {
|
|
return vec![0.0; n];
|
|
}
|
|
let sigma = matched(&self.regret[node], actions);
|
|
let owner = *player;
|
|
let values: Vec<Vec<f64>> = children
|
|
.iter()
|
|
.enumerate()
|
|
.map(|(a, child)| {
|
|
let mut next = reach.clone();
|
|
next[owner].iter_mut().enumerate().for_each(|(c, r)| *r *= sigma[c * actions + a]);
|
|
self.walk(*child, &next, scale)
|
|
})
|
|
.collect();
|
|
if owner != self.player {
|
|
return values.iter().fold(vec![0.0; n], |mut total, v| {
|
|
add(&mut total, v);
|
|
total
|
|
});
|
|
}
|
|
let own = &reach[owner];
|
|
(0..n)
|
|
.map(|i| {
|
|
let node_value: f64 = (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).max(0.0);
|
|
self.sums[node][i * actions + a] += self.weight * own[i] * sigma[i * actions + a];
|
|
}
|
|
node_value
|
|
})
|
|
.collect()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// One CFR+ iteration as a pure fold step; the generator passes through (full width).
|
|
pub fn iterate3(tree: &Tree3, 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];
|
|
let reach: Reach = [ones.clone(), ones.clone(), ones];
|
|
for player in 0..3 {
|
|
let mut pass = Learn { tree, player, weight: next as f64, regret: &mut regret, sums: &mut sums };
|
|
pass.walk(tree.root, &reach, 1.0);
|
|
}
|
|
}
|
|
(SolverState::from_parts(next, regret, sums), rng)
|
|
}
|
|
|
|
/// What a traverser's decision node looked like during an evaluation walk.
|
|
pub(crate) struct Visit3<'a> {
|
|
pub node: usize,
|
|
pub name: &'static str,
|
|
pub reach: &'a Reach,
|
|
pub scale: f64,
|
|
/// `[action][class]`
|
|
pub action_values: &'a [Vec<f64>],
|
|
}
|
|
|
|
struct Evaluate<'a> {
|
|
tree: &'a Tree3,
|
|
strategy: &'a Strategy,
|
|
player: usize,
|
|
best: bool,
|
|
}
|
|
|
|
impl Evaluate<'_> {
|
|
fn probability(&self, node: usize, class: usize, action: usize) -> f64 {
|
|
self.strategy.probability(node, class, action)
|
|
}
|
|
|
|
fn walk<F: FnMut(&Visit3)>(&self, node: usize, reach: &Reach, scale: f64, visit: &mut F) -> Vec<f64> {
|
|
let tree = self.tree;
|
|
let n = tree.classes;
|
|
match tree.nodes.get(node) {
|
|
None => vec![0.0; n],
|
|
Some(Node3::Terminal(outcome)) => leaf_value(tree, outcome, self.player, view(reach), scale),
|
|
Some(Node3::Chance(branches)) => branches.iter().fold(vec![0.0; n], |mut total, (p, child)| {
|
|
let value = self.walk(*child, reach, scale * p, visit);
|
|
add(&mut total, &value);
|
|
total
|
|
}),
|
|
Some(Node3::Decision { player, name, children }) => {
|
|
let owner = *player;
|
|
let mine = owner == self.player;
|
|
let values: Vec<Vec<f64>> = children
|
|
.iter()
|
|
.enumerate()
|
|
.map(|(a, child)| {
|
|
let mut next = reach.clone();
|
|
if !(mine && self.best) {
|
|
next[owner]
|
|
.iter_mut()
|
|
.enumerate()
|
|
.for_each(|(c, r)| *r *= self.probability(node, c, a));
|
|
}
|
|
self.walk(*child, &next, scale, visit)
|
|
})
|
|
.collect();
|
|
if !mine {
|
|
return values.iter().fold(vec![0.0; n], |mut total, v| {
|
|
add(&mut total, v);
|
|
total
|
|
});
|
|
}
|
|
visit(&Visit3 { node, name, reach, scale, action_values: &values });
|
|
(0..n)
|
|
.map(|i| {
|
|
let v = if self.best {
|
|
values.iter().map(|v| v[i]).fold(f64::NEG_INFINITY, f64::max)
|
|
} else {
|
|
(0..children.len()).map(|a| self.probability(node, i, a) * values[a][i]).sum()
|
|
};
|
|
if v.is_finite() { v } else { 0.0 }
|
|
})
|
|
.collect()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn root_values3<F: FnMut(&Visit3)>(tree: &Tree3, strategy: &Strategy, player: usize, best: bool, visit: &mut F) -> Vec<f64> {
|
|
if !tree.is_playable() {
|
|
return vec![0.0; tree.classes];
|
|
}
|
|
let ones = vec![1.0; tree.classes];
|
|
let reach: Reach = [ones.clone(), ones.clone(), ones];
|
|
Evaluate { tree, strategy, player, best }.walk(tree.root, &reach, 1.0, visit)
|
|
}
|
|
|
|
/// Expected result of `player` when everybody follows `strategy`.
|
|
pub fn game_value3(tree: &Tree3, strategy: &Strategy, player: usize) -> f64 {
|
|
root_values3(tree, strategy, player.min(2), false, &mut |_| {}).iter().sum()
|
|
}
|
|
|
|
/// What `player` wins by best-responding while the other two keep `strategy`.
|
|
pub fn best_response_gain3(tree: &Tree3, strategy: &Strategy, player: usize) -> f64 {
|
|
let player = player.min(2);
|
|
let best: f64 = root_values3(tree, strategy, player, true, &mut |_| {}).iter().sum();
|
|
best - game_value3(tree, strategy, player)
|
|
}
|
|
|
|
/// Sum of the three best-response gains (0 exactly at a Nash equilibrium).
|
|
pub fn exploitability3(tree: &Tree3, strategy: &Strategy) -> f64 {
|
|
(0..3).map(|p| best_response_gain3(tree, strategy, p)).sum()
|
|
}
|
|
|
|
/// One entry per decision node, hand class and acting player that can be reached at all: the
|
|
/// conditional value of every action with everything else frozen (see `deviation`).
|
|
pub fn deviations3(tree: &Tree3, strategy: &Strategy) -> Vec<Deviation> {
|
|
let mut found = Vec::new();
|
|
for player in 0..3 {
|
|
let mut visit = |v: &Visit3| {
|
|
let mass = reach_mass(tree, player, view(v.reach), v.scale);
|
|
for (class, m) in mass.iter().enumerate() {
|
|
if *m <= 0.0 {
|
|
continue;
|
|
}
|
|
found.push(Deviation {
|
|
node: v.name,
|
|
player,
|
|
class,
|
|
weight: v.reach[player][class] * m,
|
|
frequency: strategy.row(v.node, class).to_vec(),
|
|
value: v.action_values.iter().map(|a| a[class] / m).collect(),
|
|
});
|
|
}
|
|
};
|
|
root_values3(tree, strategy, player, false, &mut visit);
|
|
}
|
|
found
|
|
}
|
|
|
|
impl Game for Tree3 {
|
|
fn classes(&self) -> usize {
|
|
self.classes
|
|
}
|
|
|
|
fn action_counts(&self) -> Vec<usize> {
|
|
self.nodes
|
|
.iter()
|
|
.map(|n| match n {
|
|
Node3::Decision { children, .. } => children.len(),
|
|
_ => 0,
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
fn decision_name(&self, node: usize) -> Option<&'static str> {
|
|
match self.nodes.get(node) {
|
|
Some(Node3::Decision { name, .. }) => Some(name),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
fn step(&self, state: SolverState, rng: Rng) -> (SolverState, Rng) {
|
|
iterate3(self, state, rng)
|
|
}
|
|
|
|
fn exploitability(&self, strategy: &Strategy) -> f64 {
|
|
exploitability3(self, strategy)
|
|
}
|
|
}
|