Files
pokerfreak/src/cfr/environment.rs
T
mmintelandClaude Opus 5.5 3d65e856f6 Rake-aware CFR and charts: rake rule, ante situation, regenerated preflop charts
Secures the worktree's uncommitted state before merging into main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 10:36:11 +01:00

92 lines
3.5 KiB
Rust

//! Everything a game builder needs to know about the deck: rules, exact deal weights, the
//! equity matrix and the strength order of the classes.
use super::chance_table::{chance_table, ChanceTable};
use super::chart_deck::ChartDeck;
use super::equity_matrix::EquityMatrix;
use super::frozen_equity::frozen_equity_for;
use super::hand_class::{classes_in, CLASS_COUNT};
use crate::fault::Fault;
use crate::rake::{ChartRake, CHART_ANTES_BB};
use crate::variant::{rules, Rules};
#[derive(Clone, Debug)]
pub struct Environment {
pub rules: Rules,
pub equity: EquityMatrix,
pub chance: ChanceTable,
/// Classes of the deck, strongest first: by all-in equity against a uniformly random hand.
pub order: Vec<usize>,
/// What the leaves pay. The hold'em tables are derived with the measured rake; short deck has
/// no measured table and stays without.
pub rake: ChartRake,
/// Antes of the whole table, bb: dead money in every pot, the winner of a fold takes them too.
/// A constant per player does not change a decision, so nobody's own ante is subtracted.
pub antes: f64,
}
impl Environment {
/// Two-card games only (the classes are two-card hands).
pub fn new(rules: &Rules, equity: EquityMatrix) -> Result<Environment, Fault> {
if rules.hole_cards != 2 {
return Err(Fault::Invalid {
field: "rules".into(),
detail: "hand classes need exactly two hole cards".into(),
});
}
let chance = chance_table(rules.deck);
let vs_random = |c: usize| -> f64 {
let (num, den) = (0..CLASS_COUNT).fold((0.0, 0.0), |(n, d), j| {
let p = chance.probability(c, j);
(n + p * equity.get(c, j), d + p)
});
if den > 0.0 {
num / den
} else {
0.0
}
};
let mut order = classes_in(rules.deck);
order.sort_by(|a, b| {
vs_random(*b).partial_cmp(&vs_random(*a)).unwrap_or(std::cmp::Ordering::Equal).then(a.cmp(b))
});
Ok(Environment { rules: *rules, equity, chance, order, rake: ChartRake::None, antes: 0.0 })
}
/// Full deck, hold'em rules, the frozen equity table, the measured rake and antes.
pub fn frozen() -> Environment {
Environment::frozen_for(ChartDeck::Full)
}
/// `frozen` without rake and antes: a zero-sum game.
pub fn frozen_free_table() -> Environment {
Environment { rake: ChartRake::None, antes: 0.0, ..Environment::frozen() }
}
/// The deck's rules and its frozen equity table.
pub fn frozen_for(deck: ChartDeck) -> Environment {
let variant = deck.variant();
let (rake, antes) = match deck {
ChartDeck::Full => (ChartRake::Measured, CHART_ANTES_BB),
ChartDeck::Short => (ChartRake::None, 0.0),
};
let env = Environment::new(&rules(variant), frozen_equity_for(deck)).unwrap_or_else(|_| {
// Unreachable: both rule sets have two hole cards. Kept total instead of panicking.
let r = rules(variant);
Environment {
rules: r,
equity: EquityMatrix::neutral(),
chance: chance_table(r.deck),
order: classes_in(r.deck),
rake: ChartRake::None,
antes: 0.0,
}
});
Environment { rake, antes, ..env }
}
pub fn classes(&self) -> usize {
CLASS_COUNT
}
}