Files
pokerfreak/src/cfr/frozen_multi.rs
T
mmintelandClaude Opus 5.5 fbb3c1312a Vorflop-CFR mit einem bis vier Verteidigern, Short-Deck-Charts belegt
Mehrere Verteidiger: Opener gegen die letzten k Plätze (k = 1..4), gleiche
Abstraktion, Chance-Sampling-CFR+ mit exaktem Card Removal, jede Annahme im
Kopf von generated_charts_multi.rs. Beleg: Abweichung gegen die
Ein-Verteidiger-Charts je Spot, Gegenprobe exakt (k <= 2) und gesampelt
(k = 3, 4), alle unter 0,01 bb.

Short Deck: Vollständigkeit aller Positionen und Randhände, alle
All-in-Entscheidungen gegen direkte Showdowns mit echten Karten geprüft
(0 falsch in 69 Knoten).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 21:05:55 +01:00

144 lines
5.6 KiB
Rust

//! The shapes of the frozen multi-defender tables (`preflop/generated_charts_multi.rs`) and the
//! way back to strategies.
//!
//! A row is one decision node by name (`multi_tree`). Per hand class it stores the permille of
//! every action but the first (fold); the first is `1000 - rest`. Each value is two base-32 digits
//! (`0-9a-v`, 0..=1023), so a row of a three-action node is 169 x 4 characters. Classes missing
//! from the deck are 0 (fold).
use super::hand_class::CLASS_COUNT;
use super::multi_tree::MultiTree;
use super::multi_walk::MultiStrategy;
use super::permille::{from_permille, to_permille};
use super::seat::Seat;
use crate::fault::Fault;
const DIGITS: &[u8; 32] = b"0123456789abcdefghijklmnopqrstuv";
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct MultiRow {
pub name: &'static str,
pub actions: u8,
pub data: &'static str,
}
/// One solved spot: first actor `seat`, `defenders` modelled players behind it.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct MultiSpot {
pub seat: Seat,
pub defenders: u8,
pub depth_bb: u8,
/// Sampled CFR+ iterations (one deal each).
pub iterations: u32,
/// Combo-weighted mean change of the open chart between iteration N/2 and N, permille.
pub open_drift_permille: u32,
/// Deals per pass of the sampled check.
pub check_samples: u32,
/// Sum of the best-response gains on the deals that chose them, micro-bb per deal.
pub in_sample_micro_bb: i32,
/// The same responses on independent deals, micro-bb per deal (noise can push it below 0).
pub holdout_micro_bb: i32,
/// Exact full-width exploitability (`open_game` / `open3_game`), one and two defenders only.
pub exact_micro_bb: Option<u32>,
pub rows: &'static [MultiRow],
}
/// The one-defender open chart of a seat (first actor against the big blind alone,
/// `open_game`), solved full width: the baseline the multi-defender charts are compared with.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct BaselineSpot {
pub seat: Seat,
pub depth_bb: u8,
pub iterations: u32,
/// Raise permille per class, two base-32 digits each.
pub open: &'static str,
}
pub fn encode(values: &[u16]) -> String {
values
.iter()
.flat_map(|v| {
let v = (*v).min(1023) as usize;
[DIGITS[v / 32] as char, DIGITS[v % 32] as char]
})
.collect()
}
pub fn decode(data: &str) -> Result<Vec<u16>, Fault> {
let digit = |b: u8| -> Result<u16, Fault> {
DIGITS.iter().position(|d| *d == b).map(|p| p as u16).ok_or_else(|| Fault::Invalid {
field: "multi chart".into(),
detail: format!("not a base-32 digit: {}", b as char),
})
};
let bytes = data.as_bytes();
if !bytes.len().is_multiple_of(2) {
return Err(Fault::Invalid { field: "multi chart".into(), detail: "odd length".into() });
}
bytes.chunks(2).map(|p| Ok(digit(p[0])? * 32 + digit(p[1])?)).collect()
}
/// Permille rows `[class][action]` of a frozen row.
pub fn permille_rows(row: &MultiRow) -> Result<Vec<Vec<u16>>, Fault> {
let a = row.actions as usize;
let values = decode(row.data)?;
if a < 2 || values.len() != CLASS_COUNT * (a - 1) {
return Err(Fault::Invalid { field: row.name.into(), detail: "wrong row length".into() });
}
values
.chunks(a - 1)
.map(|rest| {
let sum: u16 = rest.iter().sum();
if sum > 1000 {
return Err(Fault::Invalid { field: row.name.into(), detail: "frequencies above 1000".into() });
}
Ok(std::iter::once(1000 - sum).chain(rest.iter().copied()).collect())
})
.collect()
}
/// Rows of a strategy, rounded to permille, in the frozen layout (`masked`: classes outside the
/// deck fold).
pub fn freeze_rows(tree: &MultiTree, strategy: &MultiStrategy, exists: impl Fn(usize) -> bool) -> Vec<(String, u8, Vec<u16>)> {
tree.nodes
.iter()
.enumerate()
.filter_map(|(node, n)| match n {
super::multi_tree::MultiNode::Decision { name, children, .. } => {
let a = children.len();
let values = (0..tree.classes)
.flat_map(|c| {
let rounded = if exists(c) { to_permille(strategy.row(node, c)) } else { vec![0; a] };
rounded.into_iter().skip(1).collect::<Vec<u16>>()
})
.collect();
Some((name.clone(), a as u8, values))
}
_ => None,
})
.collect()
}
/// The frozen strategy over the tree (every decision node must have a row of the same width).
pub fn multi_strategy(tree: &MultiTree, rows: &[MultiRow]) -> Result<MultiStrategy, Fault> {
let mut strategy = MultiStrategy::uniform(tree);
for row in rows {
let node = tree.decision(row.name).ok_or_else(|| Fault::Missing(format!("decision node {}", row.name)))?;
if strategy.actions[node] != row.actions as usize {
return Err(Fault::Invalid { field: row.name.into(), detail: "wrong number of actions".into() });
}
strategy.probs[node] = permille_rows(row)?.iter().flat_map(|r| from_permille(r)).collect();
}
let covered = rows.len() == tree.decision_names().len();
if !covered {
return Err(Fault::Inconsistent("the table does not cover every decision node".into()));
}
Ok(strategy)
}
/// Raise permille of the opener per class.
pub fn open_row(rows: &[MultiRow]) -> Result<Vec<u16>, Fault> {
let row = rows.iter().find(|r| r.name == "open").ok_or_else(|| Fault::Missing("open".into()))?;
Ok(permille_rows(row)?.iter().map(|r| r[1]).collect())
}