Aus worktree-agent-ae0a46ebbcdc54d8b (dda39a4). Kurzmessung bis 12 M Iterationen: -30 bb/100, im Rauschen. S1 alt Endstand: +187.4 +-6.9 bb/100 (400k Haende, Runde 400). Tor fuer den Langlauf: LBR nach 400 Runden deutlich unter +187 und Richtung <30; sonst laut Plan Strang E stoppen. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
252 lines
9.8 KiB
Rust
252 lines
9.8 KiB
Rust
//! Board-texture refinement of the card abstraction (plan E2, gate "Abstraktion verfeinern, einmal";
|
|
//! finding `BEFUND-E2-plateau.md`).
|
|
//!
|
|
//! The plain abstraction gives a flop / turn hand one of 256 board-blind equity classes, so a
|
|
//! decision node has one action distribution per class for ALL boards. Here the information set of
|
|
//! a flop / turn decision is `(texture group of the public board, equity class)`:
|
|
//!
|
|
//! - the equity class is the old table class (strength ordered, 0..255), merged to `k` classes by
|
|
//! `class * k / 256` (neighbouring strengths), so memory and sparsity stay sane;
|
|
//! - the texture group is a dense id of a feature tuple of the board as a SET of cards (imperfect
|
|
//! recall, no deal order, so it is a pure function of the board mask and needs no table file):
|
|
//! - flop (3 cards): suit pattern (rainbow / two-tone / monotone), pairedness (none / pair /
|
|
//! trips), high card (A / K / Q-J / T-8 / lower), straight-draw wetness (number of five-rank
|
|
//! windows holding 3 board ranks: 0 / 1-2 / 3+);
|
|
//! - turn (4 cards): suit pattern (1111 / 211 / 22 / 31 / 4: flush draw, flush made, none),
|
|
//! pairedness (none / pair / two pair or trips / quads), most board ranks in one five-rank
|
|
//! window (a straight needs only a card / is open), high card (A / K / Q-T / lower). The turn
|
|
//! card's effect (flush-completing, pairing, straight-completing, overcard) is carried by the
|
|
//! difference of these features to the flop's, which the betting line (the node) already
|
|
//! separates by street and by the flop group's own strategy.
|
|
//!
|
|
//! The river stays as it was (EHS quantiles, 256 classes): a river hand's equity already is its
|
|
//! rank against the whole range on THIS board.
|
|
//!
|
|
//! A refined blueprint is a separate artefact: checkpoint `<id>-t.ckpt` (the old `<id>.ckpt` keeps
|
|
//! loading and running bit-identically). `Textur::standard()` is the version the ranges loader
|
|
//! expects; `Textur::neu` builds variants for experiments.
|
|
|
|
use super::iso;
|
|
|
|
/// One refinement: how coarse the groups are and how many equity classes each group keeps.
|
|
#[derive(Clone, Debug)]
|
|
pub struct Textur {
|
|
pub grob: bool,
|
|
/// Equity classes per group on the flop / turn (a divisor-friendly number <= 256).
|
|
pub k_flop: usize,
|
|
pub k_turn: usize,
|
|
/// Dense group id by feature code (`u16::MAX` = code never occurs).
|
|
flop_dicht: Vec<u16>,
|
|
turn_dicht: Vec<u16>,
|
|
pub flop_gruppen: usize,
|
|
pub turn_gruppen: usize,
|
|
}
|
|
|
|
fn lanes(m: u64) -> [u32; 4] {
|
|
[(m & 0x1FFF) as u32, ((m >> 16) & 0x1FFF) as u32, ((m >> 32) & 0x1FFF) as u32, ((m >> 48) & 0x1FFF) as u32]
|
|
}
|
|
|
|
/// Rank bit mask (13 bits, ace high = bit 12) and per-rank card counts of a board.
|
|
fn raenge(board: u64) -> (u32, [u8; 13], [u32; 4]) {
|
|
let l = lanes(board);
|
|
let mut cnt = [0u8; 13];
|
|
let mut union = 0u32;
|
|
for lane in l {
|
|
union |= lane;
|
|
for r in 0..13 {
|
|
cnt[r] += ((lane >> r) & 1) as u8;
|
|
}
|
|
}
|
|
(union, cnt, l)
|
|
}
|
|
|
|
/// Distinct ranks inside each of the ten five-rank windows (A-5 wheel included): max and the number
|
|
/// of windows holding at least 3.
|
|
fn fenster(union: u32) -> (u32, u32) {
|
|
let mut best = 0;
|
|
let mut viele = 0;
|
|
for w in 0..10 {
|
|
let mask: u32 = if w == 9 { 0b1_0000_0000_1111 } else { 0b11111 << w };
|
|
let c = (union & mask).count_ones();
|
|
best = best.max(c);
|
|
if c >= 3 {
|
|
viele += 1;
|
|
}
|
|
}
|
|
(best, viele)
|
|
}
|
|
|
|
fn hoch(union: u32, grob: bool) -> usize {
|
|
let r = 31 - union.leading_zeros(); // 0..12, ace = 12
|
|
if grob {
|
|
return (r >= 11) as usize; // A or K
|
|
}
|
|
match r {
|
|
12 => 0,
|
|
11 => 1,
|
|
10 | 9 => 2,
|
|
6..=8 => 3,
|
|
_ => 4,
|
|
}
|
|
}
|
|
|
|
/// Feature code of a flop board.
|
|
fn flop_code(board: u64, grob: bool) -> usize {
|
|
let (union, _, l) = raenge(board);
|
|
let n = union.count_ones() as usize; // 3, 2, 1
|
|
let maxsuit = l.iter().map(|x| x.count_ones()).max().unwrap() as usize; // 1..3
|
|
let (_, viele) = fenster(union);
|
|
let nass = if viele == 0 { 0 } else if viele <= 2 { 1 } else { 2 };
|
|
let paar = 3 - n; // 0 none, 1 pair, 2 trips
|
|
if grob {
|
|
((maxsuit - 1) * 2 + (paar > 0) as usize) * 3 + nass
|
|
} else {
|
|
(((maxsuit - 1) * 3 + paar) * 5 + hoch(union, false)) * 3 + nass
|
|
}
|
|
}
|
|
|
|
/// Feature code of a turn board.
|
|
fn turn_code(board: u64, grob: bool) -> usize {
|
|
let (union, cnt, l) = raenge(board);
|
|
let mut s: Vec<u32> = l.iter().map(|x| x.count_ones()).collect();
|
|
s.sort_unstable_by(|a, b| b.cmp(a));
|
|
let muster = match (s[0], s[1]) {
|
|
(1, _) => 0,
|
|
(2, 1) => 1,
|
|
(2, _) => 2,
|
|
(3, _) => 3,
|
|
_ => 4,
|
|
};
|
|
let n = union.count_ones(); // 4 none, 3 pair, 2 two pair / trips, 1 quads
|
|
let paar = match n {
|
|
4 => 0,
|
|
3 => 1,
|
|
2 => 2,
|
|
_ => 3,
|
|
};
|
|
debug_assert!(cnt.iter().map(|c| *c as u32).sum::<u32>() == 4);
|
|
let (fen, _) = fenster(union);
|
|
let strasse = (fen.max(2) - 2) as usize; // 0: <=2, 1: 3, 2: 4
|
|
if grob {
|
|
(muster * 2 + (paar > 0) as usize) * 3 + strasse
|
|
} else {
|
|
(((muster * 4 + paar) * 3 + strasse) * 4) + hoch(union, false).min(3)
|
|
}
|
|
}
|
|
|
|
impl Textur {
|
|
/// A refinement with `grob` groups and `k_flop` / `k_turn` equity classes per group.
|
|
pub fn neu(grob: bool, k_flop: usize, k_turn: usize) -> Textur {
|
|
assert!((1..=256).contains(&k_flop) && (1..=256).contains(&k_turn));
|
|
let dicht = |boards: Vec<u64>, code: fn(u64, bool) -> usize| {
|
|
let mut codes: Vec<usize> = boards.iter().map(|b| code(*b, grob)).collect();
|
|
codes.sort_unstable();
|
|
codes.dedup();
|
|
let mut v = vec![u16::MAX; codes.last().map_or(0, |m| m + 1)];
|
|
for (i, c) in codes.iter().enumerate() {
|
|
v[*c] = i as u16;
|
|
}
|
|
(v, codes.len())
|
|
};
|
|
let (flop_dicht, flop_gruppen) = dicht(iso::subsets(3), flop_code);
|
|
let (turn_dicht, turn_gruppen) = dicht(iso::subsets(4), turn_code);
|
|
Textur { grob, k_flop, k_turn, flop_dicht, turn_dicht, flop_gruppen, turn_gruppen }
|
|
}
|
|
|
|
/// The version the ranges loader and `grundtextur` use for `<id>-t.ckpt`.
|
|
pub fn standard() -> Textur {
|
|
Textur::neu(false, 64, 64)
|
|
}
|
|
|
|
/// Information-set classes of a street (flop 0, turn 1); the river is not refined.
|
|
pub fn klassen(&self, strasse: usize) -> Option<usize> {
|
|
match strasse {
|
|
0 => Some(self.flop_gruppen * self.k_flop),
|
|
1 => Some(self.turn_gruppen * self.k_turn),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
/// Texture group of a flop (3 cards) or turn (4 cards) board.
|
|
pub fn gruppe(&self, board: u64, strasse: usize) -> usize {
|
|
if strasse == 0 {
|
|
self.flop_dicht[flop_code(board, self.grob)] as usize
|
|
} else {
|
|
self.turn_dicht[turn_code(board, self.grob)] as usize
|
|
}
|
|
}
|
|
|
|
/// Refined class of a hand whose plain equity class on this street is `klasse` (0..255).
|
|
pub fn verfeinern(&self, klasse: usize, board: u64, strasse: usize) -> usize {
|
|
let k = if strasse == 0 { self.k_flop } else { self.k_turn };
|
|
self.gruppe(board, strasse) * k + klasse * k / 256
|
|
}
|
|
}
|
|
|
|
/// `true` if the checkpoint at `pfad` has exactly `eintraege` entries (header: rounds, length), so
|
|
/// it can be loaded into tables of that size without the length assertion firing.
|
|
pub fn ckpt_passt(pfad: &std::path::Path, eintraege: usize) -> bool {
|
|
use std::io::Read;
|
|
let Ok(mut f) = std::fs::File::open(pfad) else { return false };
|
|
let mut kopf = [0u8; 16];
|
|
if f.read_exact(&mut kopf).is_err() {
|
|
return false;
|
|
}
|
|
let n = u64::from_le_bytes(kopf[8..16].try_into().unwrap()) as usize;
|
|
n == eintraege && std::fs::metadata(pfad).map_or(false, |m| m.len() as usize == 16 + 16 * n)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::card::Card;
|
|
use crate::hand::fast7;
|
|
|
|
fn m(t: &str) -> u64 {
|
|
fast7::mask(&Card::parse_list(t).unwrap())
|
|
}
|
|
|
|
#[test]
|
|
fn group_counts_are_sane() {
|
|
for grob in [false, true] {
|
|
let t = Textur::neu(grob, 64, 64);
|
|
eprintln!("grob={grob}: {} Flop-Gruppen, {} Turn-Gruppen", t.flop_gruppen, t.turn_gruppen);
|
|
assert!(t.flop_gruppen >= 10 && t.flop_gruppen <= 135);
|
|
assert!(t.turn_gruppen >= 15 && t.turn_gruppen <= 320);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn suit_renaming_keeps_the_group_and_textures_differ() {
|
|
let t = Textur::standard();
|
|
// Suit renaming.
|
|
assert_eq!(t.gruppe(m("Ah 7h 2c"), 0), t.gruppe(m("As 7s 2d"), 0));
|
|
assert_eq!(t.gruppe(m("Ah 7h 2c 9d"), 1), t.gruppe(m("Ac 7c 2d 9h"), 1));
|
|
// Genuinely different textures get different groups.
|
|
let g = |b: &str| t.gruppe(m(b), 0);
|
|
assert_ne!(g("Ks 7d 2c"), g("Ks 7s 2s"), "rainbow vs monotone");
|
|
assert_ne!(g("Ks 7d 2c"), g("Ks Kd 2c"), "unpaired vs paired");
|
|
assert_ne!(g("Ks 7d 2c"), g("9s 8d 7c"), "dry vs connected");
|
|
assert_ne!(g("Ks 7d 2c"), g("7s 5d 2c"), "high card");
|
|
let u = |b: &str| t.gruppe(m(b), 1);
|
|
assert_ne!(u("Ks 7d 2c 9h"), u("Ks 7s 2s 9s"), "no flush vs flush made");
|
|
assert_ne!(u("Ks 7d 2c 9h"), u("Ks 7d 2c 7h"), "pairing card");
|
|
assert_ne!(u("Ks 7d 2c 9h"), u("Ks 7d 8c 9h"), "straight possible");
|
|
}
|
|
|
|
#[test]
|
|
fn refined_classes_stay_inside_the_street_space() {
|
|
let t = Textur::standard();
|
|
for (strasse, n) in [(0usize, 3usize), (1, 4)] {
|
|
let max = t.klassen(strasse).unwrap();
|
|
for b in iso::subsets(n).iter().step_by(97) {
|
|
for c in [0usize, 1, 100, 255] {
|
|
assert!(t.verfeinern(c, *b, strasse) < max);
|
|
}
|
|
}
|
|
}
|
|
// Neighbouring strengths merge, order is kept.
|
|
assert!(t.verfeinern(255, m("Ks 7d 2c"), 0) > t.verfeinern(0, m("Ks 7d 2c"), 0));
|
|
}
|
|
}
|