Die Welt soll nichts verraten. Woerter, die ein Gegenueber implizieren (echt vs. unecht, live, Mensch), sind aus Prosa, Kommentaren und Docstrings raus; Namen (mensch_ruhe_s, echte_chops) und der Altwert "echt" im Spielplan bleiben. Nur Kommentare und Docstrings angefasst, damit keine Einrueckung faellt.
185 lines
7.2 KiB
Rust
185 lines
7.2 KiB
Rust
//! Solve one multi-defender spot end to end, check it, and measure it against the one-defender
|
|
//! chart.
|
|
//!
|
|
//! - `solve_multi`: `iterations` sampled CFR+ steps from `CFR_SEED`, average strategy rounded to
|
|
//! permille (what is shipped), the sampled best-response check of `multi_walk` on that rounded
|
|
//! strategy, and -- for one and two defenders, where an exact full-width game exists -- the exact
|
|
//! exploitability of the same rounded strategy in `open_game` / `open3_game`.
|
|
//! - `exact_exploitability`: the rounded strategy mapped by node name into the exact game. With
|
|
//! one defender the map is `open`, `BB_defend` -> `defend`, `vs_BB_three_bet` -> `vs_three_bet`,
|
|
//! `BB_vs_jam` -> `vs_jam`; with two the small blind's nodes are added
|
|
//! (`SB_defend` -> `sb_defend`, `BB_overcall_SB` -> `bb_overcall`, `vs_SB_three_bet` ->
|
|
//! `vs_sb_three_bet`, `SB_vs_jam` -> `sb_vs_jam`).
|
|
//! - Deviation measures (`width`, `mean_abs_difference`): combo-weighted, so a class counts by
|
|
//! how often it is dealt. Width is the share of all combos the opener raises; the mean absolute
|
|
//! difference is the share of combos whose open frequency differs, weighted by how much.
|
|
|
|
use super::environment::Environment;
|
|
use super::frozen_multi::freeze_rows;
|
|
use super::game::Game;
|
|
use super::hand_class::{class_exists, CLASS_COUNT};
|
|
use super::multi_tree::{multi_open_tree, MultiTree};
|
|
use super::multi_walk::{multi_iterate, sampled_exploitability, Deals, MultiState, MultiStrategy, SampledCheck};
|
|
use super::open3_game::open3_tree;
|
|
use super::open_game::open_tree;
|
|
use super::permille::{from_permille, to_permille};
|
|
use super::run_plan::CFR_SEED;
|
|
use super::seat::Seat;
|
|
use super::strategy::Strategy;
|
|
use crate::fault::Fault;
|
|
use crate::rng::Rng;
|
|
|
|
/// Seed of the deals of the sampled check (the independent half uses `CHECK_SEED + 1`).
|
|
pub const CHECK_SEED: u64 = 0x4745_4745_4E50_524F;
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub struct SolvedMulti {
|
|
pub seat: Seat,
|
|
pub defenders: usize,
|
|
pub depth_bb: u8,
|
|
pub iterations: u32,
|
|
/// `(name, actions, permille of every action but fold per class)`.
|
|
pub rows: Vec<(String, u8, Vec<u16>)>,
|
|
pub open_drift_permille: u32,
|
|
pub check: SampledCheck,
|
|
pub exact: Option<f64>,
|
|
}
|
|
|
|
fn rounded(tree: &MultiTree, strategy: &MultiStrategy, env: &Environment) -> MultiStrategy {
|
|
let mut out = strategy.clone();
|
|
for (node, probs) in out.probs.iter_mut().enumerate() {
|
|
let a = tree.action_counts_at(node);
|
|
if a == 0 {
|
|
continue;
|
|
}
|
|
*probs = (0..tree.classes)
|
|
.flat_map(|c| {
|
|
if class_exists(env.rules.deck, c) {
|
|
from_permille(&to_permille(strategy.row(node, c)))
|
|
} else {
|
|
(0..a).map(|k| if k == 0 { 1.0 } else { 0.0 }).collect()
|
|
}
|
|
})
|
|
.collect();
|
|
}
|
|
out
|
|
}
|
|
|
|
fn open_permille(tree: &MultiTree, strategy: &MultiStrategy) -> Vec<u16> {
|
|
let node = tree.decision("open").unwrap_or(tree.root);
|
|
(0..tree.classes).map(|c| to_permille(strategy.row(node, c)).get(1).copied().unwrap_or(0)).collect()
|
|
}
|
|
|
|
/// Share of all combos of the deck that the opener raises.
|
|
pub fn width(env: &Environment, open: &[u16]) -> f64 {
|
|
let (num, den) = (0..CLASS_COUNT).fold((0.0, 0.0), |(n, d), c| {
|
|
let w = env.chance.combos(c) as f64;
|
|
(n + w * open.get(c).copied().unwrap_or(0) as f64 / 1000.0, d + w)
|
|
});
|
|
if den > 0.0 { num / den } else { 0.0 }
|
|
}
|
|
|
|
/// Combo-weighted mean of `|a - b|` (as a share, 0..1).
|
|
pub fn mean_abs_difference(env: &Environment, a: &[u16], b: &[u16]) -> f64 {
|
|
let (num, den) = (0..CLASS_COUNT).fold((0.0, 0.0), |(n, d), c| {
|
|
let w = env.chance.combos(c) as f64;
|
|
let diff = (a.get(c).copied().unwrap_or(0) as f64 - b.get(c).copied().unwrap_or(0) as f64).abs() / 1000.0;
|
|
(n + w * diff, d + w)
|
|
});
|
|
if den > 0.0 { num / den } else { 0.0 }
|
|
}
|
|
|
|
/// The exact game's name of a multi-tree node (one or two defenders), `None` if it has none.
|
|
pub fn exact_name(name: &str, defenders: usize) -> Option<&'static str> {
|
|
let both = match name {
|
|
"open" => Some("open"),
|
|
"BB_defend" => Some("defend"),
|
|
"vs_BB_three_bet" => Some("vs_three_bet"),
|
|
"BB_vs_jam" => Some("vs_jam"),
|
|
_ => None,
|
|
};
|
|
let two = match name {
|
|
"SB_defend" => Some("sb_defend"),
|
|
"BB_overcall_SB" => Some("bb_overcall"),
|
|
"vs_SB_three_bet" => Some("vs_sb_three_bet"),
|
|
"SB_vs_jam" => Some("sb_vs_jam"),
|
|
_ => None,
|
|
};
|
|
match defenders {
|
|
1 => both,
|
|
2 => both.or(two),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
fn mapped<G: Game>(game: &G, tree: &MultiTree, strategy: &MultiStrategy, defenders: usize) -> Result<Strategy, Fault> {
|
|
let mut out = Strategy::uniform(game);
|
|
for (node, n) in tree.nodes.iter().enumerate() {
|
|
if let super::multi_tree::MultiNode::Decision { name, .. } = n {
|
|
let target = exact_name(name, defenders)
|
|
.ok_or_else(|| Fault::Inconsistent(format!("{name} has no node in the exact game")))?;
|
|
out = out.with_rows(game, target, |c| strategy.row(node, c).to_vec())?;
|
|
}
|
|
}
|
|
Ok(out)
|
|
}
|
|
|
|
/// Exact exploitability of a multi strategy in the full-width game (one or two defenders).
|
|
pub fn exact_exploitability(env: &Environment, tree: &MultiTree, strategy: &MultiStrategy) -> Result<Option<f64>, Fault> {
|
|
let defenders = tree.players - 1;
|
|
let depth = tree.depth_bb;
|
|
match defenders {
|
|
1 => {
|
|
let game = open_tree(env, tree.seat, depth)?;
|
|
Ok(Some(game.exploitability(&mapped(&game, tree, strategy, 1)?)))
|
|
}
|
|
2 => {
|
|
let game = open3_tree(env, tree.seat, depth)?;
|
|
Ok(Some(game.exploitability(&mapped(&game, tree, strategy, 2)?)))
|
|
}
|
|
_ => Ok(None),
|
|
}
|
|
}
|
|
|
|
/// Run the sampled CFR+ and return the average strategy, plus the one at `iterations / 2`.
|
|
pub fn run_multi(tree: &MultiTree, iterations: u32) -> (MultiStrategy, MultiStrategy) {
|
|
let deals = Deals::new(tree);
|
|
let half = (iterations / 2).max(1);
|
|
let (mut state, mut rng) = (MultiState::fresh(tree), Rng::seeded(CFR_SEED));
|
|
let mut middle = None;
|
|
for t in 1..=iterations {
|
|
(state, rng) = multi_iterate(tree, &deals, state, rng);
|
|
if t == half {
|
|
middle = Some(state.average(tree));
|
|
}
|
|
}
|
|
let last = state.average(tree);
|
|
(last.clone(), middle.unwrap_or(last))
|
|
}
|
|
|
|
pub fn solve_multi(
|
|
env: &Environment,
|
|
seat: Seat,
|
|
defenders: usize,
|
|
depth_bb: u8,
|
|
iterations: u32,
|
|
check_samples: u32,
|
|
) -> Result<SolvedMulti, Fault> {
|
|
let tree = multi_open_tree(env, seat, defenders, depth_bb as f64)?;
|
|
let (average, middle) = run_multi(&tree, iterations);
|
|
let shipped = rounded(&tree, &average, env);
|
|
let drift = mean_abs_difference(env, &open_permille(&tree, &average), &open_permille(&tree, &middle));
|
|
let check = sampled_exploitability(&tree, &shipped, check_samples, CHECK_SEED);
|
|
let exact = exact_exploitability(env, &tree, &shipped)?;
|
|
Ok(SolvedMulti {
|
|
seat,
|
|
defenders,
|
|
depth_bb,
|
|
iterations,
|
|
rows: freeze_rows(&tree, &average, |c| class_exists(env.rules.deck, c)),
|
|
open_drift_permille: (drift * 1000.0).round() as u32,
|
|
check,
|
|
exact,
|
|
})
|
|
}
|