|
|
|
@@ -1,494 +1,503 @@
|
|
|
|
|
//! Scenario S1 of the blueprint (design `DESIGN-E2-grundstrategie.md`): 100 bb, the button opens to
|
|
|
|
|
//! 2.5 bb, the small blind folds, the big blind calls; flop pot 5 bb, heads-up postflop. The ranges
|
|
|
|
|
//! are the 6-max ones (button open 41.5 %, big-blind call 57.3 % of combos), the commonest pot at
|
|
|
|
|
//! the live tables. Player 0 = big blind (first to act on every
|
|
|
|
|
//! street), player 1 = button.
|
|
|
|
|
//!
|
|
|
|
|
//! Bet abstraction: flop 33 / 75 %, turn 50 / 100 %, river 33 / 75 / 125 % of the pot, all-in as a
|
|
|
|
|
//! bet everywhere; raises to 3x the bet faced and all-in; at most 3 bets (bet + 2 raises) a street.
|
|
|
|
|
//! Hands come from the engine's own preflop charts (button open, big-blind call at 100 bb), cards
|
|
|
|
|
//! by the card abstraction (`abstraktion`): flop / turn classes from the tables, river by EHS.
|
|
|
|
|
|
|
|
|
|
use super::abstraktion::{combo_index, combos, river_class, river_ehs, CLASSES};
|
|
|
|
|
use super::iso::{self, Street};
|
|
|
|
|
use super::mccfr::{Baum, Knoten, Spiel, Zufall};
|
|
|
|
|
use super::szenario::Szenario;
|
|
|
|
|
use crate::card::Card;
|
|
|
|
|
use crate::cfr::hand_class::class_of;
|
|
|
|
|
use crate::hand::fast7;
|
|
|
|
|
use crate::preflop::generated_lookup::{open_frequency, vs_open, vs_three_bet};
|
|
|
|
|
use crate::variant::Variant;
|
|
|
|
|
|
|
|
|
|
pub const STACK: f64 = 100.0;
|
|
|
|
|
pub const OPEN: f64 = 2.5;
|
|
|
|
|
pub const MAX_EINSAETZE: usize = 3;
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
|
|
|
pub enum Aktion {
|
|
|
|
|
Check,
|
|
|
|
|
Bet(f64),
|
|
|
|
|
Call,
|
|
|
|
|
Raise(f64),
|
|
|
|
|
Fold,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
|
|
|
pub enum Ende {
|
|
|
|
|
/// `verlierer` folded; `c` = total contributions of both (preflop included).
|
|
|
|
|
Fold { verlierer: usize, c: [f64; 2] },
|
|
|
|
|
/// Equal contributions, cards decide.
|
|
|
|
|
Showdown { c: f64 },
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The S1 betting tree with the action of every edge (`aktion[child]` = how the child was reached).
|
|
|
|
|
pub struct Einsatzbaum {
|
|
|
|
|
pub baum: Baum,
|
|
|
|
|
pub enden: Vec<Ende>,
|
|
|
|
|
pub aktion: Vec<Option<Aktion>>,
|
|
|
|
|
/// Contributions of both players (preflop included) when the node is reached.
|
|
|
|
|
pub beitrag: Vec<[f64; 2]>,
|
|
|
|
|
/// Parent of every node (`usize::MAX` for the root).
|
|
|
|
|
pub eltern: Vec<usize>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Bauer {
|
|
|
|
|
sz: Szenario,
|
|
|
|
|
knoten: Vec<Knoten>,
|
|
|
|
|
enden: Vec<Ende>,
|
|
|
|
|
aktion: Vec<Option<Aktion>>,
|
|
|
|
|
beitrag: Vec<[f64; 2]>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Bauer {
|
|
|
|
|
/// A bet / raise to `to` that leaves (almost) nothing behind is the all-in (`Szenario::allin_ab`).
|
|
|
|
|
fn rund(&self, to: f64) -> f64 {
|
|
|
|
|
match self.sz.allin_ab {
|
|
|
|
|
Some(f) if to >= f * self.sz.stapel => self.sz.stapel,
|
|
|
|
|
_ => to,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ende(&mut self, e: Ende, a: Aktion) -> usize {
|
|
|
|
|
self.enden.push(e);
|
|
|
|
|
self.knoten.push(Knoten::Ende(self.enden.len() - 1));
|
|
|
|
|
self.aktion.push(Some(a));
|
|
|
|
|
self.beitrag.push(match e {
|
|
|
|
|
Ende::Fold { c, .. } => c,
|
|
|
|
|
Ende::Showdown { c } => [c, c],
|
|
|
|
|
});
|
|
|
|
|
self.knoten.len() - 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Street `s` is over with equal contributions `c`.
|
|
|
|
|
fn strasse_zu(&mut self, s: usize, c: f64, a: Aktion) -> usize {
|
|
|
|
|
if s == 2 || c >= self.sz.stapel {
|
|
|
|
|
self.ende(Ende::Showdown { c }, a)
|
|
|
|
|
} else {
|
|
|
|
|
self.zug(s + 1, [c, c], 0, 0, false, Some(a))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Decision of `p` on street `s`; `einsaetze` bets so far on this street; `geprueft` = player 0
|
|
|
|
|
/// checked (only possible with no bet).
|
|
|
|
|
fn zug(&mut self, s: usize, c: [f64; 2], p: usize, einsaetze: usize, geprueft: bool, a: Option<Aktion>) -> usize {
|
|
|
|
|
let me = self.knoten.len();
|
|
|
|
|
self.knoten.push(Knoten::Zug { spieler: p, strasse: s, kinder: Vec::new() });
|
|
|
|
|
self.aktion.push(a);
|
|
|
|
|
self.beitrag.push(c);
|
|
|
|
|
let o = 1 - p;
|
|
|
|
|
let pot = c[0] + c[1];
|
|
|
|
|
let facing = c[o] - c[p];
|
|
|
|
|
let mut kinder = Vec::new();
|
|
|
|
|
if facing <= 0.0 {
|
|
|
|
|
kinder.push(if geprueft || p == 1 { self.strasse_zu(s, c[p], Aktion::Check) } else { self.zug(s, c, o, 0, true, Some(Aktion::Check)) });
|
|
|
|
|
let stapel = self.sz.stapel;
|
|
|
|
|
let mut sizes: Vec<f64> = self.sz.einsaetze[s].iter().map(|f| self.rund((c[p] + f * pot).min(stapel))).collect();
|
|
|
|
|
sizes.push(stapel);
|
|
|
|
|
sizes.dedup_by(|x, y| (*x - *y).abs() < 1e-9);
|
|
|
|
|
for to in sizes {
|
|
|
|
|
if to > c[p] {
|
|
|
|
|
let mut n = c;
|
|
|
|
|
n[p] = to;
|
|
|
|
|
kinder.push(self.zug(s, n, o, einsaetze + 1, false, Some(Aktion::Bet(to))));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
kinder.push(self.ende(Ende::Fold { verlierer: p, c }, Aktion::Fold));
|
|
|
|
|
kinder.push(self.strasse_zu(s, c[o], Aktion::Call));
|
|
|
|
|
if einsaetze < self.sz.max_einsaetze && c[o] < self.sz.stapel {
|
|
|
|
|
let stapel = self.sz.stapel;
|
|
|
|
|
let mut sizes = vec![self.rund((c[p] + self.sz.raise * facing).min(stapel)), stapel];
|
|
|
|
|
sizes.dedup_by(|x, y| (*x - *y).abs() < 1e-9);
|
|
|
|
|
for to in sizes {
|
|
|
|
|
let mut n = c;
|
|
|
|
|
n[p] = to;
|
|
|
|
|
kinder.push(self.zug(s, n, o, einsaetze + 1, false, Some(Aktion::Raise(to))));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if let Knoten::Zug { kinder: k, .. } = &mut self.knoten[me] {
|
|
|
|
|
*k = kinder;
|
|
|
|
|
}
|
|
|
|
|
me
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Einsatzbaum {
|
|
|
|
|
pub fn s1() -> Einsatzbaum {
|
|
|
|
|
Einsatzbaum::neu(Szenario::s1())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The betting tree of a scenario: player 0 (out of position) acts first on every street.
|
|
|
|
|
pub fn neu(sz: &Szenario) -> Einsatzbaum {
|
|
|
|
|
let mut b = Bauer { sz: *sz, knoten: Vec::new(), enden: Vec::new(), aktion: Vec::new(), beitrag: Vec::new() };
|
|
|
|
|
let wurzel = b.zug(0, [sz.beitrag(), sz.beitrag()], 0, 0, false, None);
|
|
|
|
|
let mut eltern = vec![usize::MAX; b.knoten.len()];
|
|
|
|
|
for (i, k) in b.knoten.iter().enumerate() {
|
|
|
|
|
if let Knoten::Zug { kinder, .. } = k {
|
|
|
|
|
kinder.iter().for_each(|c| eltern[*c] = i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Einsatzbaum { baum: Baum { knoten: b.knoten, wurzel }, enden: b.enden, aktion: b.aktion, beitrag: b.beitrag, eltern }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Card abstraction tables; `None` = one class on that street (for tests without tables).
|
|
|
|
|
pub struct Abstraktion {
|
|
|
|
|
pub flop: Option<(Street, Vec<u8>)>,
|
|
|
|
|
pub turn: Option<(Street, Vec<u8>)>,
|
|
|
|
|
pub river: Vec<f32>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Abstraktion {
|
|
|
|
|
/// Load `flop.u8`, `turn.u8`, `river.f32` from `dir` (built by `bin/abstraktion`).
|
|
|
|
|
pub fn laden(dir: &std::path::Path) -> std::io::Result<Abstraktion> {
|
|
|
|
|
let lies = |n: &str| std::fs::read(dir.join(n));
|
|
|
|
|
let flop = lies("flop.u8")?;
|
|
|
|
|
let turn = lies("turn.u8")?;
|
|
|
|
|
let river = lies("river.f32")?.chunks(4).map(|b| f32::from_le_bytes([b[0], b[1], b[2], b[3]])).collect();
|
|
|
|
|
let (s3, s4) = (Street::new(3), Street::new(4));
|
|
|
|
|
assert_eq!(flop.len(), s3.count(), "flop table does not match the flop street");
|
|
|
|
|
assert_eq!(turn.len(), s4.count(), "turn table does not match the turn street");
|
|
|
|
|
Ok(Abstraktion { flop: Some((s3, flop)), turn: Some((s4, turn)), river })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// One dealt hand: cards and the class of both players on every street.
|
|
|
|
|
pub struct Deal {
|
|
|
|
|
pub hand: [u64; 2],
|
|
|
|
|
pub board: [u64; 5],
|
|
|
|
|
pub klasse: [[u8; 3]; 2],
|
|
|
|
|
/// +1 player 0 wins the showdown, -1 player 1, 0 split.
|
|
|
|
|
pub sieger: i8,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The game of one scenario (`Szenario`); `S1` is its original name and the S1 scenario.
|
|
|
|
|
pub type S1 = Grund;
|
|
|
|
|
|
|
|
|
|
pub struct Grund {
|
|
|
|
|
pub sz: &'static Szenario,
|
|
|
|
|
pub baum: Einsatzbaum,
|
|
|
|
|
pub abstraktion: Abstraktion,
|
|
|
|
|
pub alle: Vec<u64>,
|
|
|
|
|
/// Preflop weight per combo of each player (0 = out of position, 1 = in position); S1: [big
|
|
|
|
|
/// blind call, button open].
|
|
|
|
|
pub gewicht: [Vec<f64>; 2],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Range weight per combo of `combos()` from a chart frequency per hand class.
|
|
|
|
|
fn gewichte(alle: &[u64], f: impl Fn(Card, Card) -> f64) -> Vec<f64> {
|
|
|
|
|
let deck = Card::parse_list("2c 3c 4c 5c 6c 7c 8c 9c Tc Jc Qc Kc Ac 2d 3d 4d 5d 6d 7d 8d 9d Td Jd Qd Kd Ad 2h 3h 4h 5h 6h 7h 8h 9h Th Jh Qh Kh Ah 2s 3s 4s 5s 6s 7s 8s 9s Ts Js Qs Ks As").unwrap();
|
|
|
|
|
let karte = |bit: u64| *deck.iter().find(|c| fast7::bit(**c) == bit).unwrap();
|
|
|
|
|
alle.iter().map(|h| f(karte(h & h.wrapping_neg()), karte(h & (h - 1)))).collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Grund {
|
|
|
|
|
pub fn neu(abstraktion: Abstraktion) -> Grund {
|
|
|
|
|
Grund::mit(Szenario::s1(), abstraktion)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The game of scenario `sz` with ranges from the engine's preflop charts at its stack:
|
|
|
|
|
/// the opener's open frequency (times its call frequency against the 3-bet in a 3-bet pot) and
|
|
|
|
|
/// the big blind's call (3-bet) frequency against that open.
|
|
|
|
|
pub fn mit(sz: &'static Szenario, abstraktion: Abstraktion) -> Grund {
|
|
|
|
|
let alle = combos();
|
|
|
|
|
let (depth, opener) = (sz.stapel, sz.eroeffner);
|
|
|
|
|
let klasse = |a: Card, b: Card| class_of(a, b).unwrap();
|
|
|
|
|
let (eroeffner, verteidiger) = match sz.dreibet {
|
|
|
|
|
None => (
|
|
|
|
|
gewichte(&alle, |a, b| open_frequency(Variant::Nlhe, opener, depth, klasse(a, b)).map_or(0.0, |f| f.raise)),
|
|
|
|
|
gewichte(&alle, |a, b| vs_open(Variant::Nlhe, opener, depth, klasse(a, b)).map_or(0.0, |f| f.call)),
|
|
|
|
|
),
|
|
|
|
|
Some(_) => (
|
|
|
|
|
gewichte(&alle, |a, b| {
|
|
|
|
|
let o = open_frequency(Variant::Nlhe, opener, depth, klasse(a, b)).map_or(0.0, |f| f.raise);
|
|
|
|
|
o * vs_three_bet(Variant::Nlhe, opener, depth, klasse(a, b)).map_or(0.0, |f| f.call)
|
|
|
|
|
}),
|
|
|
|
|
gewichte(&alle, |a, b| vs_open(Variant::Nlhe, opener, depth, klasse(a, b)).map_or(0.0, |f| f.raise)),
|
|
|
|
|
),
|
|
|
|
|
};
|
|
|
|
|
let mut gewicht = [Vec::new(), Vec::new()];
|
|
|
|
|
gewicht[sz.eroeffner_spieler()] = eroeffner;
|
|
|
|
|
gewicht[sz.verteidiger_spieler()] = verteidiger;
|
|
|
|
|
Grund { sz, baum: Einsatzbaum::neu(sz), abstraktion, alle, gewicht }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn hand_ziehen(&self, p: usize, tot: u64, rng: &mut Zufall) -> u64 {
|
|
|
|
|
loop {
|
|
|
|
|
let i = rng.below(self.alle.len());
|
|
|
|
|
let h = self.alle[i];
|
|
|
|
|
if h & tot == 0 && rng.unit() < self.gewicht[p][i] {
|
|
|
|
|
return h;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Grund {
|
|
|
|
|
/// Class of `hand` on `strasse` with the street's board `board` (3, 4 or 5 cards); on the river
|
|
|
|
|
/// `ehs` must be `river_ehs(board)`.
|
|
|
|
|
pub fn klasse_von(&self, hand: u64, board: u64, strasse: usize, ehs: &[f32]) -> usize {
|
|
|
|
|
match strasse {
|
|
|
|
|
0 => self.abstraktion.flop.as_ref().map_or(0, |(s, t)| t[s.index(board, hand).unwrap() as usize] as usize),
|
|
|
|
|
1 => self.abstraktion.turn.as_ref().map_or(0, |(s, t)| t[s.index(board, hand).unwrap() as usize] as usize),
|
|
|
|
|
_ => river_class(ehs[combo_index(hand)], &self.abstraktion.river) as usize,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Spiel for Grund {
|
|
|
|
|
type Deal = Deal;
|
|
|
|
|
|
|
|
|
|
fn baum(&self) -> &Baum {
|
|
|
|
|
&self.baum.baum
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn klassen(&self, _: usize) -> usize {
|
|
|
|
|
CLASSES
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ziehen(&self, rng: &mut Zufall) -> Deal {
|
|
|
|
|
let h1 = self.hand_ziehen(1, 0, rng);
|
|
|
|
|
let h0 = self.hand_ziehen(0, h1, rng);
|
|
|
|
|
let deck = iso::deck();
|
|
|
|
|
let mut tot = h0 | h1;
|
|
|
|
|
let mut board = [0u64; 5];
|
|
|
|
|
for b in board.iter_mut() {
|
|
|
|
|
loop {
|
|
|
|
|
let c = deck[rng.below(52)];
|
|
|
|
|
if c & tot == 0 {
|
|
|
|
|
*b = c;
|
|
|
|
|
tot |= c;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let flop = board[0] | board[1] | board[2];
|
|
|
|
|
let turn = flop | board[3];
|
|
|
|
|
let river = turn | board[4];
|
|
|
|
|
let ehs = river_ehs(river, &self.alle);
|
|
|
|
|
let hand = [h0, h1];
|
|
|
|
|
let klasse = hand.map(|h| {
|
|
|
|
|
let f = self.abstraktion.flop.as_ref().map_or(0, |(s, t)| t[s.index(flop, h).unwrap() as usize]);
|
|
|
|
|
let t = self.abstraktion.turn.as_ref().map_or(0, |(s, t)| t[s.index(turn, h).unwrap() as usize]);
|
|
|
|
|
[f, t, river_class(ehs[combo_index(h)], &self.abstraktion.river)]
|
|
|
|
|
});
|
|
|
|
|
let (a, b) = (fast7::score(river | h0), fast7::score(river | h1));
|
|
|
|
|
Deal { hand, board, klasse, sieger: (a > b) as i8 - (a < b) as i8 }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn klasse(&self, d: &Deal, spieler: usize, strasse: usize) -> usize {
|
|
|
|
|
d.klasse[spieler][strasse] as usize
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn wert(&self, d: &Deal, ende: usize) -> [f64; 2] {
|
|
|
|
|
match self.baum.enden[ende] {
|
|
|
|
|
Ende::Fold { verlierer, c } => {
|
|
|
|
|
let v = c[verlierer];
|
|
|
|
|
if verlierer == 0 { [-v, v] } else { [v, -v] }
|
|
|
|
|
}
|
|
|
|
|
Ende::Showdown { c } => {
|
|
|
|
|
let v = c * d.sieger as f64;
|
|
|
|
|
[v, -v]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
use crate::blueprint::abstraktion::river_cuts;
|
|
|
|
|
use crate::blueprint::mccfr::{rechnen, Tabellen};
|
|
|
|
|
use crate::rng::split_mix::Rng;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn the_s1_tree_is_well_formed() {
|
|
|
|
|
let t = Einsatzbaum::s1();
|
|
|
|
|
let zuege = t.baum.knoten.iter().filter(|k| matches!(k, Knoten::Zug { .. })).count();
|
|
|
|
|
eprintln!("S1: {} Knoten, {} Zuege, {} Enden", t.baum.knoten.len(), zuege, t.enden.len());
|
|
|
|
|
for e in &t.enden {
|
|
|
|
|
match e {
|
|
|
|
|
Ende::Fold { verlierer, c } => {
|
|
|
|
|
assert!(c[*verlierer] < c[1 - verlierer], "only the player facing a bet folds");
|
|
|
|
|
assert!(c.iter().all(|x| *x <= STACK + 1e-9));
|
|
|
|
|
}
|
|
|
|
|
Ende::Showdown { c } => assert!(*c >= OPEN && *c <= STACK + 1e-9),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Per street at most 3 bets: no path has a 4th Bet/Raise in a row without a street change.
|
|
|
|
|
fn tiefe(t: &Einsatzbaum, k: usize, s: usize, n: usize) {
|
|
|
|
|
if let Knoten::Zug { strasse, kinder, .. } = &t.baum.knoten[k] {
|
|
|
|
|
let n = if *strasse != s { 0 } else { n };
|
|
|
|
|
for c in kinder {
|
|
|
|
|
let m = n + matches!(t.aktion[*c], Some(Aktion::Bet(_) | Aktion::Raise(_))) as usize;
|
|
|
|
|
assert!(m <= MAX_EINSAETZE);
|
|
|
|
|
tiefe(t, *c, *strasse, m);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
tiefe(&t, t.baum.wurzel, 0, 0);
|
|
|
|
|
assert!(zuege < 20_000, "{zuege} decisions");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn the_preflop_ranges_are_the_engine_charts() {
|
|
|
|
|
let s = S1::neu(Abstraktion { flop: None, turn: None, river: river_cuts(50, 1) });
|
|
|
|
|
let anteil = |p: usize| s.gewicht[p].iter().sum::<f64>() / 1326.0;
|
|
|
|
|
let (bb, btn) = (anteil(0), anteil(1));
|
|
|
|
|
eprintln!("BB call {:.3}, BTN open {:.3}", bb, btn);
|
|
|
|
|
assert!(btn > 0.30 && btn < 0.70, "button open {btn}");
|
|
|
|
|
assert!(bb > 0.15 && bb < 0.70, "big blind call {bb}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// S1 through the scenario family is the S1 of old: same tree shape, same entries (the
|
|
|
|
|
/// checkpoint `s1.ckpt` has `(regret + sum) = 2 * 8 * entries` bytes), same ranges.
|
|
|
|
|
#[test]
|
|
|
|
|
fn s1_through_the_family_is_unchanged() {
|
|
|
|
|
let t = Einsatzbaum::s1();
|
|
|
|
|
let n = t.baum.knoten.iter().map(|k| if let Knoten::Zug { kinder, .. } = k { CLASSES * kinder.len() } else { 0 }).sum::<usize>();
|
|
|
|
|
let tab = Tabellen::neu(&S1::neu(Abstraktion { flop: None, turn: None, river: river_cuts(50, 1) }));
|
|
|
|
|
assert_eq!(tab.eintraege(), n);
|
|
|
|
|
eprintln!("S1: {} Knoten, {} Eintraege", t.baum.knoten.len(), n);
|
|
|
|
|
// The real checkpoint, if `GRUNDSTRATEGIE` names a directory with it: it must still load.
|
|
|
|
|
if let Some(dir) = std::env::var_os("GRUNDSTRATEGIE") {
|
|
|
|
|
let ckpt = std::path::Path::new(&dir).join("grundstrategie").join("s1.ckpt");
|
|
|
|
|
if ckpt.exists() {
|
|
|
|
|
let mut tab = tab;
|
|
|
|
|
assert!(tab.datei_lesen(&ckpt), "s1.ckpt does not match the S1 tree any more");
|
|
|
|
|
eprintln!("s1.ckpt loads: {} rounds", tab.runden);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ohne_tabellen(sz: &'static Szenario) -> Grund {
|
|
|
|
|
Grund::mit(sz, Abstraktion { flop: None, turn: None, river: river_cuts(50, 1) })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Every scenario: tree well-formed (only the player facing a bet folds, contributions within
|
|
|
|
|
/// the stack, at most the scenario's bets a street, out of position acts first), values
|
|
|
|
|
/// zero-sum on legal deals.
|
|
|
|
|
#[test]
|
|
|
|
|
fn every_scenario_tree_is_well_formed_and_zero_sum() {
|
|
|
|
|
use crate::blueprint::szenario::ALLE;
|
|
|
|
|
for sz in ALLE.iter() {
|
|
|
|
|
let s = ohne_tabellen(sz);
|
|
|
|
|
let t = &s.baum;
|
|
|
|
|
let zuege = t.baum.knoten.iter().filter(|k| matches!(k, Knoten::Zug { .. })).count();
|
|
|
|
|
let eintraege = t.baum.knoten.iter().map(|k| if let Knoten::Zug { kinder, .. } = k { CLASSES * kinder.len() } else { 0 }).sum::<usize>();
|
|
|
|
|
eprintln!("{}: {} Knoten, {} Zuege, {} Enden, {} Eintraege = {:.0} MB (2 Tabellen, f64)", sz.id, t.baum.knoten.len(), zuege, t.enden.len(), eintraege, eintraege as f64 * 16.0 / 1e6);
|
|
|
|
|
let Knoten::Zug { spieler, strasse, .. } = &t.baum.knoten[t.baum.wurzel] else { panic!() };
|
|
|
|
|
assert_eq!((*spieler, *strasse), (0, 0), "out of position acts first");
|
|
|
|
|
assert_eq!(t.beitrag[t.baum.wurzel], [sz.beitrag(); 2]);
|
|
|
|
|
for e in &t.enden {
|
|
|
|
|
match e {
|
|
|
|
|
Ende::Fold { verlierer, c } => {
|
|
|
|
|
assert!(c[*verlierer] < c[1 - verlierer], "{}: only the player facing a bet folds", sz.id);
|
|
|
|
|
assert!(c.iter().all(|x| *x <= sz.stapel + 1e-9));
|
|
|
|
|
}
|
|
|
|
|
Ende::Showdown { c } => assert!(*c >= sz.beitrag() && *c <= sz.stapel + 1e-9),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fn tiefe(t: &Einsatzbaum, k: usize, s: usize, n: usize, max: usize) {
|
|
|
|
|
if let Knoten::Zug { strasse, kinder, .. } = &t.baum.knoten[k] {
|
|
|
|
|
let n = if *strasse != s { 0 } else { n };
|
|
|
|
|
for c in kinder {
|
|
|
|
|
let m = n + matches!(t.aktion[*c], Some(Aktion::Bet(_) | Aktion::Raise(_))) as usize;
|
|
|
|
|
assert!(m <= max);
|
|
|
|
|
tiefe(t, *c, *strasse, m, max);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
tiefe(t, t.baum.wurzel, 0, 0, sz.max_einsaetze);
|
|
|
|
|
let mut rng = Zufall(Rng::seeded(5));
|
|
|
|
|
for _ in 0..50 {
|
|
|
|
|
let d = s.ziehen(&mut rng);
|
|
|
|
|
assert_eq!((d.hand[0] | d.hand[1] | d.board.iter().fold(0, |m, c| m | c)).count_ones(), 9);
|
|
|
|
|
for e in 0..t.enden.len() {
|
|
|
|
|
let w = s.wert(&d, e);
|
|
|
|
|
assert_eq!(w[0], -w[1]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Per scenario a short run lowers LBR below the uniform strategy's (river-only abstraction,
|
|
|
|
|
/// so flop / turn stay blind; the point is that positions, seats and ranges are consistent).
|
|
|
|
|
fn lbr_faellt(id: &str) {
|
|
|
|
|
use crate::blueprint::lbr::lbr;
|
|
|
|
|
let s = ohne_tabellen(Szenario::finde(id).unwrap());
|
|
|
|
|
let mut tab = Tabellen::neu(&s);
|
|
|
|
|
let roh = lbr(&s, &tab, 1000, 2, 11);
|
|
|
|
|
let t = std::time::Instant::now();
|
|
|
|
|
let (runden, periode) = (10u64, 10_000u64);
|
|
|
|
|
rechnen(&s, &mut tab, runden, periode, 2, 3);
|
|
|
|
|
let sek = t.elapsed().as_secs_f64();
|
|
|
|
|
let nachher = lbr(&s, &tab, 1000, 2, 11);
|
|
|
|
|
eprintln!(
|
|
|
|
|
"{id}: {} Eintraege; {:.0} Iterationen/s (2 Threads); LBR uniform {:+.0} ±{:.0} -> trained {:+.0} ±{:.0} bb/100 (OOP {:+.0} / IP {:+.0})",
|
|
|
|
|
tab.eintraege(), (runden * periode) as f64 / sek, roh.bb100, roh.ci95, nachher.bb100, nachher.ci95, nachher.je_sitz[0], nachher.je_sitz[1]
|
|
|
|
|
);
|
|
|
|
|
assert!(roh.bb100 - roh.ci95 > 300.0, "{id}: uniform must be very exploitable ({:+.0})", roh.bb100);
|
|
|
|
|
assert!(nachher.bb100 < roh.bb100, "{id}: {} vs {}", nachher.bb100, roh.bb100);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn lbr_falls_s1() {
|
|
|
|
|
lbr_faellt("s1");
|
|
|
|
|
}
|
|
|
|
|
#[test]
|
|
|
|
|
fn lbr_falls_sb() {
|
|
|
|
|
lbr_faellt("sb");
|
|
|
|
|
}
|
|
|
|
|
#[test]
|
|
|
|
|
fn lbr_falls_co() {
|
|
|
|
|
lbr_faellt("co");
|
|
|
|
|
}
|
|
|
|
|
#[test]
|
|
|
|
|
fn lbr_falls_btn3() {
|
|
|
|
|
lbr_faellt("btn3");
|
|
|
|
|
}
|
|
|
|
|
#[test]
|
|
|
|
|
fn lbr_falls_co3() {
|
|
|
|
|
lbr_faellt("co3");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A short run on the real tree: deals are legal, values zero-sum, the tables move.
|
|
|
|
|
#[test]
|
|
|
|
|
fn s1_runs_and_stays_zero_sum() {
|
|
|
|
|
let s = S1::neu(Abstraktion { flop: None, turn: None, river: river_cuts(50, 1) });
|
|
|
|
|
let mut rng = Zufall(Rng::seeded(9));
|
|
|
|
|
for _ in 0..200 {
|
|
|
|
|
let d = s.ziehen(&mut rng);
|
|
|
|
|
let all = d.hand[0] | d.hand[1] | d.board.iter().fold(0, |m, c| m | c);
|
|
|
|
|
assert_eq!(all.count_ones(), 9);
|
|
|
|
|
for e in 0..s.baum.enden.len() {
|
|
|
|
|
let w = s.wert(&d, e);
|
|
|
|
|
assert_eq!(w[0], -w[1]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let mut tab = Tabellen::neu(&s);
|
|
|
|
|
rechnen(&s, &mut tab, 2, 200, 1, 3);
|
|
|
|
|
assert!(tab.sichern().0.iter().any(|r| *r != 0.0));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//! Scenario S1 of the blueprint (design `DESIGN-E2-grundstrategie.md`): 100 bb, the button opens to
|
|
|
|
|
//! 2.5 bb, the small blind folds, the big blind calls; flop pot 5 bb, heads-up postflop. The ranges
|
|
|
|
|
//! are the 6-max ones (button open 41.5 %, big-blind call 57.3 % of combos), the commonest pot at
|
|
|
|
|
//! the live tables. Player 0 = big blind (first to act on every
|
|
|
|
|
//! street), player 1 = button.
|
|
|
|
|
//!
|
|
|
|
|
//! Bet abstraction: flop 33 / 75 %, turn 50 / 100 %, river 33 / 75 / 125 % of the pot, all-in as a
|
|
|
|
|
//! bet everywhere; raises to 3x the bet faced and all-in; at most 3 bets (bet + 2 raises) a street.
|
|
|
|
|
//! Hands come from the engine's own preflop charts (button open, big-blind call at 100 bb), cards
|
|
|
|
|
//! by the card abstraction (`abstraktion`): flop / turn classes from the tables, river by EHS.
|
|
|
|
|
|
|
|
|
|
use super::abstraktion::{combo_index, combos, river_class, river_ehs, CLASSES};
|
|
|
|
|
use super::iso::{self, Street};
|
|
|
|
|
use super::mccfr::{Baum, Knoten, Spiel, Zufall};
|
|
|
|
|
use super::szenario::Szenario;
|
|
|
|
|
use crate::card::Card;
|
|
|
|
|
use crate::cfr::hand_class::class_of;
|
|
|
|
|
use crate::hand::fast7;
|
|
|
|
|
use crate::preflop::generated_lookup::{open_frequency, vs_open, vs_three_bet};
|
|
|
|
|
use crate::variant::Variant;
|
|
|
|
|
|
|
|
|
|
pub const STACK: f64 = 100.0;
|
|
|
|
|
pub const OPEN: f64 = 2.5;
|
|
|
|
|
pub const MAX_EINSAETZE: usize = 3;
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
|
|
|
pub enum Aktion {
|
|
|
|
|
Check,
|
|
|
|
|
Bet(f64),
|
|
|
|
|
Call,
|
|
|
|
|
Raise(f64),
|
|
|
|
|
Fold,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
|
|
|
|
pub enum Ende {
|
|
|
|
|
/// `verlierer` folded; `c` = total contributions of both (preflop included).
|
|
|
|
|
Fold { verlierer: usize, c: [f64; 2] },
|
|
|
|
|
/// Equal contributions, cards decide.
|
|
|
|
|
Showdown { c: f64 },
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The S1 betting tree with the action of every edge (`aktion[child]` = how the child was reached).
|
|
|
|
|
pub struct Einsatzbaum {
|
|
|
|
|
pub baum: Baum,
|
|
|
|
|
pub enden: Vec<Ende>,
|
|
|
|
|
pub aktion: Vec<Option<Aktion>>,
|
|
|
|
|
/// Contributions of both players (preflop included) when the node is reached.
|
|
|
|
|
pub beitrag: Vec<[f64; 2]>,
|
|
|
|
|
/// Parent of every node (`usize::MAX` for the root).
|
|
|
|
|
pub eltern: Vec<usize>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Bauer {
|
|
|
|
|
sz: Szenario,
|
|
|
|
|
knoten: Vec<Knoten>,
|
|
|
|
|
enden: Vec<Ende>,
|
|
|
|
|
aktion: Vec<Option<Aktion>>,
|
|
|
|
|
beitrag: Vec<[f64; 2]>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Bauer {
|
|
|
|
|
/// A bet / raise to `to` that leaves (almost) nothing behind is the all-in (`Szenario::allin_ab`).
|
|
|
|
|
fn rund(&self, to: f64) -> f64 {
|
|
|
|
|
match self.sz.allin_ab {
|
|
|
|
|
Some(f) if to >= f * self.sz.stapel => self.sz.stapel,
|
|
|
|
|
_ => to,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ende(&mut self, e: Ende, a: Aktion) -> usize {
|
|
|
|
|
self.enden.push(e);
|
|
|
|
|
self.knoten.push(Knoten::Ende(self.enden.len() - 1));
|
|
|
|
|
self.aktion.push(Some(a));
|
|
|
|
|
self.beitrag.push(match e {
|
|
|
|
|
Ende::Fold { c, .. } => c,
|
|
|
|
|
Ende::Showdown { c } => [c, c],
|
|
|
|
|
});
|
|
|
|
|
self.knoten.len() - 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Street `s` is over with equal contributions `c`.
|
|
|
|
|
fn strasse_zu(&mut self, s: usize, c: f64, a: Aktion) -> usize {
|
|
|
|
|
if s == 2 || c >= self.sz.stapel {
|
|
|
|
|
self.ende(Ende::Showdown { c }, a)
|
|
|
|
|
} else {
|
|
|
|
|
self.zug(s + 1, [c, c], 0, 0, false, Some(a))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Decision of `p` on street `s`; `einsaetze` bets so far on this street; `geprueft` = player 0
|
|
|
|
|
/// checked (only possible with no bet).
|
|
|
|
|
fn zug(&mut self, s: usize, c: [f64; 2], p: usize, einsaetze: usize, geprueft: bool, a: Option<Aktion>) -> usize {
|
|
|
|
|
let me = self.knoten.len();
|
|
|
|
|
self.knoten.push(Knoten::Zug { spieler: p, strasse: s, kinder: Vec::new() });
|
|
|
|
|
self.aktion.push(a);
|
|
|
|
|
self.beitrag.push(c);
|
|
|
|
|
let o = 1 - p;
|
|
|
|
|
let pot = c[0] + c[1];
|
|
|
|
|
let facing = c[o] - c[p];
|
|
|
|
|
let mut kinder = Vec::new();
|
|
|
|
|
if facing <= 0.0 {
|
|
|
|
|
kinder.push(if geprueft || p == 1 { self.strasse_zu(s, c[p], Aktion::Check) } else { self.zug(s, c, o, 0, true, Some(Aktion::Check)) });
|
|
|
|
|
let stapel = self.sz.stapel;
|
|
|
|
|
let mut sizes: Vec<f64> = self.sz.einsaetze[s].iter().map(|f| self.rund((c[p] + f * pot).min(stapel))).collect();
|
|
|
|
|
sizes.push(stapel);
|
|
|
|
|
sizes.dedup_by(|x, y| (*x - *y).abs() < 1e-9);
|
|
|
|
|
for to in sizes {
|
|
|
|
|
if to > c[p] {
|
|
|
|
|
let mut n = c;
|
|
|
|
|
n[p] = to;
|
|
|
|
|
kinder.push(self.zug(s, n, o, einsaetze + 1, false, Some(Aktion::Bet(to))));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
kinder.push(self.ende(Ende::Fold { verlierer: p, c }, Aktion::Fold));
|
|
|
|
|
kinder.push(self.strasse_zu(s, c[o], Aktion::Call));
|
|
|
|
|
if einsaetze < self.sz.max_einsaetze && c[o] < self.sz.stapel {
|
|
|
|
|
let stapel = self.sz.stapel;
|
|
|
|
|
let mut sizes = vec![self.rund((c[p] + self.sz.raise * facing).min(stapel)), stapel];
|
|
|
|
|
sizes.dedup_by(|x, y| (*x - *y).abs() < 1e-9);
|
|
|
|
|
for to in sizes {
|
|
|
|
|
let mut n = c;
|
|
|
|
|
n[p] = to;
|
|
|
|
|
kinder.push(self.zug(s, n, o, einsaetze + 1, false, Some(Aktion::Raise(to))));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if let Knoten::Zug { kinder: k, .. } = &mut self.knoten[me] {
|
|
|
|
|
*k = kinder;
|
|
|
|
|
}
|
|
|
|
|
me
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Einsatzbaum {
|
|
|
|
|
pub fn s1() -> Einsatzbaum {
|
|
|
|
|
Einsatzbaum::neu(Szenario::s1())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The betting tree of a scenario: player 0 (out of position) acts first on every street.
|
|
|
|
|
pub fn neu(sz: &Szenario) -> Einsatzbaum {
|
|
|
|
|
let mut b = Bauer { sz: *sz, knoten: Vec::new(), enden: Vec::new(), aktion: Vec::new(), beitrag: Vec::new() };
|
|
|
|
|
let wurzel = b.zug(0, [sz.beitrag(), sz.beitrag()], 0, 0, false, None);
|
|
|
|
|
let mut eltern = vec![usize::MAX; b.knoten.len()];
|
|
|
|
|
for (i, k) in b.knoten.iter().enumerate() {
|
|
|
|
|
if let Knoten::Zug { kinder, .. } = k {
|
|
|
|
|
kinder.iter().for_each(|c| eltern[*c] = i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Einsatzbaum { baum: Baum { knoten: b.knoten, wurzel }, enden: b.enden, aktion: b.aktion, beitrag: b.beitrag, eltern }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Card abstraction tables; `None` = one class on that street (for tests without tables).
|
|
|
|
|
pub struct Abstraktion {
|
|
|
|
|
pub flop: Option<(Street, Vec<u8>)>,
|
|
|
|
|
pub turn: Option<(Street, Vec<u8>)>,
|
|
|
|
|
pub river: Vec<f32>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Abstraktion {
|
|
|
|
|
/// Load `flop.u8`, `turn.u8`, `river.f32` from `dir` (built by `bin/abstraktion`).
|
|
|
|
|
pub fn laden(dir: &std::path::Path) -> std::io::Result<Abstraktion> {
|
|
|
|
|
let lies = |n: &str| std::fs::read(dir.join(n));
|
|
|
|
|
let flop = lies("flop.u8")?;
|
|
|
|
|
let turn = lies("turn.u8")?;
|
|
|
|
|
let river = lies("river.f32")?.chunks(4).map(|b| f32::from_le_bytes([b[0], b[1], b[2], b[3]])).collect();
|
|
|
|
|
let (s3, s4) = (Street::new(3), Street::new(4));
|
|
|
|
|
assert_eq!(flop.len(), s3.count(), "flop table does not match the flop street");
|
|
|
|
|
assert_eq!(turn.len(), s4.count(), "turn table does not match the turn street");
|
|
|
|
|
Ok(Abstraktion { flop: Some((s3, flop)), turn: Some((s4, turn)), river })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// One dealt hand: cards and the class of both players on every street.
|
|
|
|
|
pub struct Deal {
|
|
|
|
|
pub hand: [u64; 2],
|
|
|
|
|
pub board: [u64; 5],
|
|
|
|
|
pub klasse: [[u8; 3]; 2],
|
|
|
|
|
/// +1 player 0 wins the showdown, -1 player 1, 0 split.
|
|
|
|
|
pub sieger: i8,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The game of one scenario (`Szenario`); `S1` is its original name and the S1 scenario.
|
|
|
|
|
pub type S1 = Grund;
|
|
|
|
|
|
|
|
|
|
pub struct Grund {
|
|
|
|
|
pub sz: &'static Szenario,
|
|
|
|
|
pub baum: Einsatzbaum,
|
|
|
|
|
pub abstraktion: Abstraktion,
|
|
|
|
|
pub alle: Vec<u64>,
|
|
|
|
|
/// Preflop weight per combo of each player (0 = out of position, 1 = in position); S1: [big
|
|
|
|
|
/// blind call, button open].
|
|
|
|
|
pub gewicht: [Vec<f64>; 2],
|
|
|
|
|
/// Diagnostic: deal this flop always (bit mask of 3 cards).
|
|
|
|
|
pub flop_fest: Option<u64>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Range weight per combo of `combos()` from a chart frequency per hand class.
|
|
|
|
|
fn gewichte(alle: &[u64], f: impl Fn(Card, Card) -> f64) -> Vec<f64> {
|
|
|
|
|
let deck = Card::parse_list("2c 3c 4c 5c 6c 7c 8c 9c Tc Jc Qc Kc Ac 2d 3d 4d 5d 6d 7d 8d 9d Td Jd Qd Kd Ad 2h 3h 4h 5h 6h 7h 8h 9h Th Jh Qh Kh Ah 2s 3s 4s 5s 6s 7s 8s 9s Ts Js Qs Ks As").unwrap();
|
|
|
|
|
let karte = |bit: u64| *deck.iter().find(|c| fast7::bit(**c) == bit).unwrap();
|
|
|
|
|
alle.iter().map(|h| f(karte(h & h.wrapping_neg()), karte(h & (h - 1)))).collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Grund {
|
|
|
|
|
pub fn neu(abstraktion: Abstraktion) -> Grund {
|
|
|
|
|
Grund::mit(Szenario::s1(), abstraktion)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The game of scenario `sz` with ranges from the engine's preflop charts at its stack:
|
|
|
|
|
/// the opener's open frequency (times its call frequency against the 3-bet in a 3-bet pot) and
|
|
|
|
|
/// the big blind's call (3-bet) frequency against that open.
|
|
|
|
|
pub fn mit(sz: &'static Szenario, abstraktion: Abstraktion) -> Grund {
|
|
|
|
|
let alle = combos();
|
|
|
|
|
let (depth, opener) = (sz.stapel, sz.eroeffner);
|
|
|
|
|
let klasse = |a: Card, b: Card| class_of(a, b).unwrap();
|
|
|
|
|
let (eroeffner, verteidiger) = match sz.dreibet {
|
|
|
|
|
None => (
|
|
|
|
|
gewichte(&alle, |a, b| open_frequency(Variant::Nlhe, opener, depth, klasse(a, b)).map_or(0.0, |f| f.raise)),
|
|
|
|
|
gewichte(&alle, |a, b| vs_open(Variant::Nlhe, opener, depth, klasse(a, b)).map_or(0.0, |f| f.call)),
|
|
|
|
|
),
|
|
|
|
|
Some(_) => (
|
|
|
|
|
gewichte(&alle, |a, b| {
|
|
|
|
|
let o = open_frequency(Variant::Nlhe, opener, depth, klasse(a, b)).map_or(0.0, |f| f.raise);
|
|
|
|
|
o * vs_three_bet(Variant::Nlhe, opener, depth, klasse(a, b)).map_or(0.0, |f| f.call)
|
|
|
|
|
}),
|
|
|
|
|
gewichte(&alle, |a, b| vs_open(Variant::Nlhe, opener, depth, klasse(a, b)).map_or(0.0, |f| f.raise)),
|
|
|
|
|
),
|
|
|
|
|
};
|
|
|
|
|
let mut gewicht = [Vec::new(), Vec::new()];
|
|
|
|
|
gewicht[sz.eroeffner_spieler()] = eroeffner;
|
|
|
|
|
gewicht[sz.verteidiger_spieler()] = verteidiger;
|
|
|
|
|
Grund { sz, baum: Einsatzbaum::neu(sz), abstraktion, alle, gewicht, flop_fest: None }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn hand_ziehen(&self, p: usize, tot: u64, rng: &mut Zufall) -> u64 {
|
|
|
|
|
loop {
|
|
|
|
|
let i = rng.below(self.alle.len());
|
|
|
|
|
let h = self.alle[i];
|
|
|
|
|
if h & tot == 0 && rng.unit() < self.gewicht[p][i] {
|
|
|
|
|
return h;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Grund {
|
|
|
|
|
/// Class of `hand` on `strasse` with the street's board `board` (3, 4 or 5 cards); on the river
|
|
|
|
|
/// `ehs` must be `river_ehs(board)`.
|
|
|
|
|
pub fn klasse_von(&self, hand: u64, board: u64, strasse: usize, ehs: &[f32]) -> usize {
|
|
|
|
|
match strasse {
|
|
|
|
|
0 => self.abstraktion.flop.as_ref().map_or(0, |(s, t)| t[s.index(board, hand).unwrap() as usize] as usize),
|
|
|
|
|
1 => self.abstraktion.turn.as_ref().map_or(0, |(s, t)| t[s.index(board, hand).unwrap() as usize] as usize),
|
|
|
|
|
_ => river_class(ehs[combo_index(hand)], &self.abstraktion.river) as usize,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Spiel for Grund {
|
|
|
|
|
type Deal = Deal;
|
|
|
|
|
|
|
|
|
|
fn baum(&self) -> &Baum {
|
|
|
|
|
&self.baum.baum
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn klassen(&self, _: usize) -> usize {
|
|
|
|
|
CLASSES
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ziehen(&self, rng: &mut Zufall) -> Deal {
|
|
|
|
|
let fest = self.flop_fest.unwrap_or(0);
|
|
|
|
|
let h1 = self.hand_ziehen(1, fest, rng);
|
|
|
|
|
let h0 = self.hand_ziehen(0, h1 | fest, rng);
|
|
|
|
|
let deck = iso::deck();
|
|
|
|
|
let mut tot = h0 | h1 | fest;
|
|
|
|
|
let mut board = [0u64; 5];
|
|
|
|
|
let mut f = fest;
|
|
|
|
|
for b in board.iter_mut() {
|
|
|
|
|
if f != 0 {
|
|
|
|
|
*b = f & f.wrapping_neg();
|
|
|
|
|
f &= f - 1;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
loop {
|
|
|
|
|
let c = deck[rng.below(52)];
|
|
|
|
|
if c & tot == 0 {
|
|
|
|
|
*b = c;
|
|
|
|
|
tot |= c;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let flop = board[0] | board[1] | board[2];
|
|
|
|
|
let turn = flop | board[3];
|
|
|
|
|
let river = turn | board[4];
|
|
|
|
|
let ehs = river_ehs(river, &self.alle);
|
|
|
|
|
let hand = [h0, h1];
|
|
|
|
|
let klasse = hand.map(|h| {
|
|
|
|
|
let f = self.abstraktion.flop.as_ref().map_or(0, |(s, t)| t[s.index(flop, h).unwrap() as usize]);
|
|
|
|
|
let t = self.abstraktion.turn.as_ref().map_or(0, |(s, t)| t[s.index(turn, h).unwrap() as usize]);
|
|
|
|
|
[f, t, river_class(ehs[combo_index(h)], &self.abstraktion.river)]
|
|
|
|
|
});
|
|
|
|
|
let (a, b) = (fast7::score(river | h0), fast7::score(river | h1));
|
|
|
|
|
Deal { hand, board, klasse, sieger: (a > b) as i8 - (a < b) as i8 }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn klasse(&self, d: &Deal, spieler: usize, strasse: usize) -> usize {
|
|
|
|
|
d.klasse[spieler][strasse] as usize
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn wert(&self, d: &Deal, ende: usize) -> [f64; 2] {
|
|
|
|
|
match self.baum.enden[ende] {
|
|
|
|
|
Ende::Fold { verlierer, c } => {
|
|
|
|
|
let v = c[verlierer];
|
|
|
|
|
if verlierer == 0 { [-v, v] } else { [v, -v] }
|
|
|
|
|
}
|
|
|
|
|
Ende::Showdown { c } => {
|
|
|
|
|
let v = c * d.sieger as f64;
|
|
|
|
|
[v, -v]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
use crate::blueprint::abstraktion::river_cuts;
|
|
|
|
|
use crate::blueprint::mccfr::{rechnen, Tabellen};
|
|
|
|
|
use crate::rng::split_mix::Rng;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn the_s1_tree_is_well_formed() {
|
|
|
|
|
let t = Einsatzbaum::s1();
|
|
|
|
|
let zuege = t.baum.knoten.iter().filter(|k| matches!(k, Knoten::Zug { .. })).count();
|
|
|
|
|
eprintln!("S1: {} Knoten, {} Zuege, {} Enden", t.baum.knoten.len(), zuege, t.enden.len());
|
|
|
|
|
for e in &t.enden {
|
|
|
|
|
match e {
|
|
|
|
|
Ende::Fold { verlierer, c } => {
|
|
|
|
|
assert!(c[*verlierer] < c[1 - verlierer], "only the player facing a bet folds");
|
|
|
|
|
assert!(c.iter().all(|x| *x <= STACK + 1e-9));
|
|
|
|
|
}
|
|
|
|
|
Ende::Showdown { c } => assert!(*c >= OPEN && *c <= STACK + 1e-9),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Per street at most 3 bets: no path has a 4th Bet/Raise in a row without a street change.
|
|
|
|
|
fn tiefe(t: &Einsatzbaum, k: usize, s: usize, n: usize) {
|
|
|
|
|
if let Knoten::Zug { strasse, kinder, .. } = &t.baum.knoten[k] {
|
|
|
|
|
let n = if *strasse != s { 0 } else { n };
|
|
|
|
|
for c in kinder {
|
|
|
|
|
let m = n + matches!(t.aktion[*c], Some(Aktion::Bet(_) | Aktion::Raise(_))) as usize;
|
|
|
|
|
assert!(m <= MAX_EINSAETZE);
|
|
|
|
|
tiefe(t, *c, *strasse, m);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
tiefe(&t, t.baum.wurzel, 0, 0);
|
|
|
|
|
assert!(zuege < 20_000, "{zuege} decisions");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn the_preflop_ranges_are_the_engine_charts() {
|
|
|
|
|
let s = S1::neu(Abstraktion { flop: None, turn: None, river: river_cuts(50, 1) });
|
|
|
|
|
let anteil = |p: usize| s.gewicht[p].iter().sum::<f64>() / 1326.0;
|
|
|
|
|
let (bb, btn) = (anteil(0), anteil(1));
|
|
|
|
|
eprintln!("BB call {:.3}, BTN open {:.3}", bb, btn);
|
|
|
|
|
assert!(btn > 0.30 && btn < 0.70, "button open {btn}");
|
|
|
|
|
assert!(bb > 0.15 && bb < 0.70, "big blind call {bb}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// S1 through the scenario family is the S1 of old: same tree shape, same entries (the
|
|
|
|
|
/// checkpoint `s1.ckpt` has `(regret + sum) = 2 * 8 * entries` bytes), same ranges.
|
|
|
|
|
#[test]
|
|
|
|
|
fn s1_through_the_family_is_unchanged() {
|
|
|
|
|
let t = Einsatzbaum::s1();
|
|
|
|
|
let n = t.baum.knoten.iter().map(|k| if let Knoten::Zug { kinder, .. } = k { CLASSES * kinder.len() } else { 0 }).sum::<usize>();
|
|
|
|
|
let tab = Tabellen::neu(&S1::neu(Abstraktion { flop: None, turn: None, river: river_cuts(50, 1) }));
|
|
|
|
|
assert_eq!(tab.eintraege(), n);
|
|
|
|
|
eprintln!("S1: {} Knoten, {} Eintraege", t.baum.knoten.len(), n);
|
|
|
|
|
// The real checkpoint, if `GRUNDSTRATEGIE` names a directory with it: it must still load.
|
|
|
|
|
if let Some(dir) = std::env::var_os("GRUNDSTRATEGIE") {
|
|
|
|
|
let ckpt = std::path::Path::new(&dir).join("grundstrategie").join("s1.ckpt");
|
|
|
|
|
if ckpt.exists() {
|
|
|
|
|
let mut tab = tab;
|
|
|
|
|
assert!(tab.datei_lesen(&ckpt), "s1.ckpt does not match the S1 tree any more");
|
|
|
|
|
eprintln!("s1.ckpt loads: {} rounds", tab.runden);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ohne_tabellen(sz: &'static Szenario) -> Grund {
|
|
|
|
|
Grund::mit(sz, Abstraktion { flop: None, turn: None, river: river_cuts(50, 1) })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Every scenario: tree well-formed (only the player facing a bet folds, contributions within
|
|
|
|
|
/// the stack, at most the scenario's bets a street, out of position acts first), values
|
|
|
|
|
/// zero-sum on legal deals.
|
|
|
|
|
#[test]
|
|
|
|
|
fn every_scenario_tree_is_well_formed_and_zero_sum() {
|
|
|
|
|
use crate::blueprint::szenario::ALLE;
|
|
|
|
|
for sz in ALLE.iter() {
|
|
|
|
|
let s = ohne_tabellen(sz);
|
|
|
|
|
let t = &s.baum;
|
|
|
|
|
let zuege = t.baum.knoten.iter().filter(|k| matches!(k, Knoten::Zug { .. })).count();
|
|
|
|
|
let eintraege = t.baum.knoten.iter().map(|k| if let Knoten::Zug { kinder, .. } = k { CLASSES * kinder.len() } else { 0 }).sum::<usize>();
|
|
|
|
|
eprintln!("{}: {} Knoten, {} Zuege, {} Enden, {} Eintraege = {:.0} MB (2 Tabellen, f64)", sz.id, t.baum.knoten.len(), zuege, t.enden.len(), eintraege, eintraege as f64 * 16.0 / 1e6);
|
|
|
|
|
let Knoten::Zug { spieler, strasse, .. } = &t.baum.knoten[t.baum.wurzel] else { panic!() };
|
|
|
|
|
assert_eq!((*spieler, *strasse), (0, 0), "out of position acts first");
|
|
|
|
|
assert_eq!(t.beitrag[t.baum.wurzel], [sz.beitrag(); 2]);
|
|
|
|
|
for e in &t.enden {
|
|
|
|
|
match e {
|
|
|
|
|
Ende::Fold { verlierer, c } => {
|
|
|
|
|
assert!(c[*verlierer] < c[1 - verlierer], "{}: only the player facing a bet folds", sz.id);
|
|
|
|
|
assert!(c.iter().all(|x| *x <= sz.stapel + 1e-9));
|
|
|
|
|
}
|
|
|
|
|
Ende::Showdown { c } => assert!(*c >= sz.beitrag() && *c <= sz.stapel + 1e-9),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fn tiefe(t: &Einsatzbaum, k: usize, s: usize, n: usize, max: usize) {
|
|
|
|
|
if let Knoten::Zug { strasse, kinder, .. } = &t.baum.knoten[k] {
|
|
|
|
|
let n = if *strasse != s { 0 } else { n };
|
|
|
|
|
for c in kinder {
|
|
|
|
|
let m = n + matches!(t.aktion[*c], Some(Aktion::Bet(_) | Aktion::Raise(_))) as usize;
|
|
|
|
|
assert!(m <= max);
|
|
|
|
|
tiefe(t, *c, *strasse, m, max);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
tiefe(t, t.baum.wurzel, 0, 0, sz.max_einsaetze);
|
|
|
|
|
let mut rng = Zufall(Rng::seeded(5));
|
|
|
|
|
for _ in 0..50 {
|
|
|
|
|
let d = s.ziehen(&mut rng);
|
|
|
|
|
assert_eq!((d.hand[0] | d.hand[1] | d.board.iter().fold(0, |m, c| m | c)).count_ones(), 9);
|
|
|
|
|
for e in 0..t.enden.len() {
|
|
|
|
|
let w = s.wert(&d, e);
|
|
|
|
|
assert_eq!(w[0], -w[1]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Per scenario a short run lowers LBR below the uniform strategy's (river-only abstraction,
|
|
|
|
|
/// so flop / turn stay blind; the point is that positions, seats and ranges are consistent).
|
|
|
|
|
fn lbr_faellt(id: &str) {
|
|
|
|
|
use crate::blueprint::lbr::lbr;
|
|
|
|
|
let s = ohne_tabellen(Szenario::finde(id).unwrap());
|
|
|
|
|
let mut tab = Tabellen::neu(&s);
|
|
|
|
|
let roh = lbr(&s, &tab, 1000, 2, 11);
|
|
|
|
|
let t = std::time::Instant::now();
|
|
|
|
|
let (runden, periode) = (10u64, 10_000u64);
|
|
|
|
|
rechnen(&s, &mut tab, runden, periode, 2, 3);
|
|
|
|
|
let sek = t.elapsed().as_secs_f64();
|
|
|
|
|
let nachher = lbr(&s, &tab, 1000, 2, 11);
|
|
|
|
|
eprintln!(
|
|
|
|
|
"{id}: {} Eintraege; {:.0} Iterationen/s (2 Threads); LBR uniform {:+.0} ±{:.0} -> trained {:+.0} ±{:.0} bb/100 (OOP {:+.0} / IP {:+.0})",
|
|
|
|
|
tab.eintraege(), (runden * periode) as f64 / sek, roh.bb100, roh.ci95, nachher.bb100, nachher.ci95, nachher.je_sitz[0], nachher.je_sitz[1]
|
|
|
|
|
);
|
|
|
|
|
assert!(roh.bb100 - roh.ci95 > 300.0, "{id}: uniform must be very exploitable ({:+.0})", roh.bb100);
|
|
|
|
|
assert!(nachher.bb100 < roh.bb100, "{id}: {} vs {}", nachher.bb100, roh.bb100);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn lbr_falls_s1() {
|
|
|
|
|
lbr_faellt("s1");
|
|
|
|
|
}
|
|
|
|
|
#[test]
|
|
|
|
|
fn lbr_falls_sb() {
|
|
|
|
|
lbr_faellt("sb");
|
|
|
|
|
}
|
|
|
|
|
#[test]
|
|
|
|
|
fn lbr_falls_co() {
|
|
|
|
|
lbr_faellt("co");
|
|
|
|
|
}
|
|
|
|
|
#[test]
|
|
|
|
|
fn lbr_falls_btn3() {
|
|
|
|
|
lbr_faellt("btn3");
|
|
|
|
|
}
|
|
|
|
|
#[test]
|
|
|
|
|
fn lbr_falls_co3() {
|
|
|
|
|
lbr_faellt("co3");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A short run on the real tree: deals are legal, values zero-sum, the tables move.
|
|
|
|
|
#[test]
|
|
|
|
|
fn s1_runs_and_stays_zero_sum() {
|
|
|
|
|
let s = S1::neu(Abstraktion { flop: None, turn: None, river: river_cuts(50, 1) });
|
|
|
|
|
let mut rng = Zufall(Rng::seeded(9));
|
|
|
|
|
for _ in 0..200 {
|
|
|
|
|
let d = s.ziehen(&mut rng);
|
|
|
|
|
let all = d.hand[0] | d.hand[1] | d.board.iter().fold(0, |m, c| m | c);
|
|
|
|
|
assert_eq!(all.count_ones(), 9);
|
|
|
|
|
for e in 0..s.baum.enden.len() {
|
|
|
|
|
let w = s.wert(&d, e);
|
|
|
|
|
assert_eq!(w[0], -w[1]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let mut tab = Tabellen::neu(&s);
|
|
|
|
|
rechnen(&s, &mut tab, 2, 200, 1, 3);
|
|
|
|
|
assert!(tab.sichern().0.iter().any(|r| *r != 0.0));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|