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

57 lines
2.0 KiB
Rust

//! The class-vs-class equity matrix, stored as the upper triangle of integers (1/10 000).
//!
//! Only `i <= j` is stored: the all-in equity of `j` against `i` is exactly one minus the
//! equity of `i` against `j` (ties count half), and a class against itself is 1/2 by symmetry
//! of the deal. `from_triangle` rebuilds the full antisymmetric matrix.
use super::hand_class::CLASS_COUNT;
use crate::fault::Fault;
pub const TRIANGLE_LEN: usize = CLASS_COUNT * (CLASS_COUNT + 1) / 2;
const SCALE: f64 = 10_000.0;
/// Row-major pairs `(i, j)` with `i <= j`, in storage order.
pub fn triangle_pairs() -> Vec<(usize, usize)> {
(0..CLASS_COUNT).flat_map(|i| (i..CLASS_COUNT).map(move |j| (i, j))).collect()
}
#[derive(Clone, Debug, PartialEq)]
pub struct EquityMatrix {
values: Vec<f64>,
}
impl EquityMatrix {
/// Every matchup a coin flip. Used as the fallback when a table cannot be read.
pub fn neutral() -> EquityMatrix {
EquityMatrix { values: vec![0.5; CLASS_COUNT * CLASS_COUNT] }
}
pub fn from_triangle(triangle: &[u16]) -> Result<EquityMatrix, Fault> {
let invalid = |detail: &str| Fault::Invalid { field: "equity table".into(), detail: detail.into() };
if triangle.len() != TRIANGLE_LEN {
return Err(invalid("wrong length"));
}
if triangle.iter().any(|v| *v as f64 > SCALE) {
return Err(invalid("equity above 100 %"));
}
let mut values = vec![0.5; CLASS_COUNT * CLASS_COUNT];
for (k, (i, j)) in triangle_pairs().into_iter().enumerate() {
if i != j {
let v = triangle[k] as f64 / SCALE;
values[i * CLASS_COUNT + j] = v;
values[j * CLASS_COUNT + i] = 1.0 - v;
}
}
Ok(EquityMatrix { values })
}
/// Equity of class `i` against class `j`; 0.5 outside the grid.
pub fn get(&self, i: usize, j: usize) -> f64 {
if i < CLASS_COUNT && j < CLASS_COUNT {
self.values[i * CLASS_COUNT + j]
} else {
0.5
}
}
}