Files
pokerfreak/src/cfr/solve.rs
T
mmintel 836c16c9a4 Sprechweise geputzt: kein echt/live/Mensch mehr in Kommentaren und Texten
Die Welt soll nichts verraten. Woerter, die ein Gegenueber implizieren (echt vs. unecht,
live, Mensch), sind aus Prosa, Kommentaren und Docstrings raus; Namen (mensch_ruhe_s,
echte_chops) und der Altwert "echt" im Spielplan bleiben. Nur Kommentare und Docstrings
angefasst, damit keine Einrueckung faellt.
2026-11-01 22:11:05 +01:00

68 lines
2.6 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::game::Game;
use super::solver_state::SolverState;
use super::strategy::{average_strategy, Strategy};
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<G: Game + ?Sized>(tree: &G, 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) = tree.step(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, tree.exploitability(&average)));
}
}
(state, rng, samples, curve)
});
let strategy = average_strategy(tree, &state);
let final_exploitability = tree.exploitability(&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 }
}