Files
pokerfreak/src/cfr/solve.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

70 lines
2.7 KiB
Rust

//! Run CFR+ for a fixed number of iterations and record how it converged.
//!
//! Two records, both about the average strategy:
//! - `checkpoints`: exploitability (in payoff units) at five iteration counts,
//! `N/100, N/20, N/5, N/2, N` (the last one is always `N`);
//! - `stable_since`: the argmax chart (`chart_signature`, permille-rounded) is sampled every
//! `N/200` iterations (at least every iteration); `stable_since` is the first sampled
//! iteration from which every later sample equals the final chart. A chart that only settles
//! at the very end reports `N`.
use super::chart_signature::signature;
use super::exploitability::exploitability;
use super::iterate::iterate;
use super::solver_state::SolverState;
use super::strategy::{average_strategy, Strategy};
use super::tree::Tree;
use crate::rng::Rng;
#[derive(Clone, Debug, PartialEq)]
pub struct Solved {
pub iterations: u32,
pub strategy: Strategy,
/// Five `(iteration, exploitability)` pairs.
pub checkpoints: Vec<(u32, f64)>,
pub stable_since: u32,
pub final_exploitability: f64,
}
fn checkpoint_iterations(n: u32) -> [u32; 5] {
[n / 100, n / 20, n / 5, n / 2, n].map(|k| k.max(1).min(n.max(1)))
}
pub fn solve(tree: &Tree, iterations: u32, rng: Rng) -> Solved {
let stride = (iterations / 200).max(1);
let marks = checkpoint_iterations(iterations);
let start = (SolverState::fresh(tree), rng, Vec::new(), Vec::new());
let (state, _, samples, curve) = (1..=iterations).fold(start, |(state, rng, mut samples, mut curve), t| {
let (state, rng) = iterate(tree, state, rng);
let sampled = t % stride == 0 || t == iterations;
let marked = marks.contains(&t);
if sampled || marked {
let average = average_strategy(tree, &state);
if sampled {
samples.push((t, signature(&average)));
}
if marked {
curve.push((t, exploitability(tree, &average)));
}
}
(state, rng, samples, curve)
});
let strategy = average_strategy(tree, &state);
let final_exploitability = exploitability(tree, &strategy);
let checkpoints: Vec<(u32, f64)> = marks
.iter()
.map(|m| curve.iter().find(|(t, _)| t == m).copied().unwrap_or((*m, final_exploitability)))
.collect();
let final_chart = samples.last().map(|(_, s): &(u32, Vec<u8>)| s.clone());
let stable_since = match final_chart {
Some(chart) => samples
.iter()
.rev()
.take_while(|(_, s)| *s == chart)
.last()
.map_or(iterations, |(t, _)| *t),
None => iterations,
};
Solved { iterations, strategy, checkpoints, stable_since, final_exploitability }
}