frist::notbremse(): Uhrzeit seit begin() ueber KERN_NOTBREMSE_MS -> left_ms 0, kein Solver startet, laufende Solver-Schleifen (turn_game, flop/river/river3/multi3, solver_kern) geben ab, die Heuristik entscheidet. Gesetzt nur von entscheiden/decide.py (Live-Pfad); roentgen.kern_aufruf entfernt sie, Mess- und Pflichtpfade bleiben rein in Rechenzeit. Pflichtfaelle: Kern in BelowNormal, Frist 30 s je Fall (vorher Idle, 275 s gesamt, brach unter Volllast ab). Nachweis: Bremse 1 ms -> 0/20 Solver, ohne -> 20/20. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
188 lines
7.0 KiB
Rust
188 lines
7.0 KiB
Rust
//! "Solver everywhere", the MULTIWAY turn and flop: 3- and 4-handed spots decided by Monte-Carlo CFR
|
||
//! over real combos across the remaining streets (`cfr::multi3`), the sibling of the multiway river
|
||
//! (`engine::river3_spieler`, same ranges, line replay and approximations). Switches `SOLVER_TURN3`
|
||
//! and `SOLVER_FLOP3` (default off: no decision, the engine is bit-identical).
|
||
//!
|
||
//! - **Turn game:** turn betting (33/75 % pot, all-in, one raise ×3), `TURN_SLOTS` river cards, river
|
||
//! betting (75 % pot, all-in, one raise), showdown.
|
||
//! - **Flop game:** flop betting (as the turn above), `FLOP_SLOTS` turn cards, turn betting (75 %,
|
||
//! all-in, one raise), the river dealt and checked down (as stage C, no river betting).
|
||
//! - **Ranges** per player from the range model of the line (gated reads), at most `MAX_COMBOS`.
|
||
//! - **Time:** `frist::TURN_MS` / `frist::FLOP_MS` per decision, own caps `TURN_BUDGET_MS` /
|
||
//! `FLOP_BUDGET_MS`; under `MIN_ITERS` iterations the heuristic decides.
|
||
//!
|
||
//! Named approximations: no equilibrium guarantee with three players; stacks capped at the smallest
|
||
//! live stack (no side pots); the next street's card from a few slots; one size on later streets.
|
||
|
||
use crate::cfr::multi3::{GameM, NodeM, SolverM};
|
||
use crate::cfr::river3::Menu3;
|
||
use crate::cfr::river_game::Act;
|
||
use crate::engine::river3_spieler::{acting_order, model_hands, street_line3};
|
||
use crate::engine::river_subgame::legal_choice;
|
||
use crate::game::{Decision, Spot, Street, Villain};
|
||
use crate::hand::fast7;
|
||
use crate::rng::Rng;
|
||
use std::sync::atomic::{AtomicBool, Ordering};
|
||
|
||
static TURN3: AtomicBool = AtomicBool::new(false);
|
||
static FLOP3: AtomicBool = AtomicBool::new(false);
|
||
|
||
pub fn set_turn3(on: bool) {
|
||
TURN3.store(on, Ordering::Relaxed);
|
||
}
|
||
|
||
pub fn set_flop3(on: bool) {
|
||
FLOP3.store(on, Ordering::Relaxed);
|
||
}
|
||
|
||
pub fn turn3() -> bool {
|
||
TURN3.load(Ordering::Relaxed)
|
||
}
|
||
|
||
pub fn flop3() -> bool {
|
||
FLOP3.load(Ordering::Relaxed)
|
||
}
|
||
|
||
fn on(key: &str) -> bool {
|
||
matches!(std::env::var(key).as_deref(), Ok("1") | Ok("an"))
|
||
}
|
||
|
||
/// `SOLVER_TURN3=1`, `SOLVER_FLOP3=1`.
|
||
pub fn from_env() -> (bool, bool) {
|
||
(on("SOLVER_TURN3"), on("SOLVER_FLOP3"))
|
||
}
|
||
|
||
/// Iteration cap (and the count under `SOLVER_ZEIT=fest`).
|
||
pub const ITERS: u32 = 12_000;
|
||
/// Fewest iterations a decision may rest on.
|
||
pub const MIN_ITERS: u32 = 1_000;
|
||
/// Own caps of one solve, inside the street's decision total.
|
||
pub const TURN_BUDGET_MS: f64 = 1050.0;
|
||
pub const FLOP_BUDGET_MS: f64 = 1700.0;
|
||
/// Most combos per player.
|
||
pub const MAX_COMBOS: usize = 200;
|
||
pub const TURN_SLOTS: usize = 8;
|
||
pub const FLOP_SLOTS: usize = 6;
|
||
|
||
fn later() -> Menu3 {
|
||
Menu3 { fracs: vec![0.75], raise_mult: 3.0, max_raises: 1, allin: true }
|
||
}
|
||
|
||
pub struct SolvedM {
|
||
pub row: Vec<f64>,
|
||
pub acts: Vec<Act>,
|
||
pub iterations: u64,
|
||
pub millis: f64,
|
||
pub players: usize,
|
||
pub nodes: usize,
|
||
pub combos: Vec<usize>,
|
||
}
|
||
|
||
/// Solve this 3- or 4-handed turn or flop (gated spot) and return hero's average strategy at its node.
|
||
pub fn solve_multi3(spot: &Spot) -> Option<SolvedM> {
|
||
let villains = spot.live_villains().count();
|
||
let (cards, cap_ms, total, slots) = match spot.street {
|
||
Street::Turn => (4, TURN_BUDGET_MS, crate::engine::frist::TURN_MS, TURN_SLOTS),
|
||
Street::Flop => (3, FLOP_BUDGET_MS, crate::engine::frist::FLOP_MS, FLOP_SLOTS),
|
||
_ => return None,
|
||
};
|
||
if spot.board.len() != cards || spot.hero_cards.len() != 2 || !(2..=3).contains(&villains) {
|
||
return None;
|
||
}
|
||
let budget = crate::engine::frist::budget_ms(cap_ms, total);
|
||
if budget <= 0.0 {
|
||
return None;
|
||
}
|
||
let started = std::time::Instant::now();
|
||
let cpu0 = crate::engine::frist::thread_cpu_ms();
|
||
let order = acting_order(spot)?;
|
||
let (line, c) = street_line3(spot, &order, spot.street)?;
|
||
let me = order.iter().position(|s| *s == spot.hero_seat)?;
|
||
let stack_of = |seat: u8| -> f64 {
|
||
if seat == spot.hero_seat {
|
||
spot.hero_stack
|
||
} else {
|
||
spot.live_villains().find(|v| v.seat == seat).map_or(0.0, |v| v.stack)
|
||
}
|
||
};
|
||
let cap = order.iter().enumerate().map(|(i, s)| stack_of(*s) + c[i]).fold(f64::INFINITY, f64::min);
|
||
let pot = spot.pot - c.iter().sum::<f64>();
|
||
if pot <= 0.0 || !cap.is_finite() || cap <= c.iter().copied().fold(0.0, f64::max) {
|
||
return None;
|
||
}
|
||
let board = fast7::mask(&spot.board);
|
||
let hero_mask = fast7::mask(&spot.hero_cards);
|
||
let mut hands = Vec::with_capacity(order.len());
|
||
for seat in &order {
|
||
let h = if *seat == spot.hero_seat {
|
||
model_hands(spot, &Villain { seat: *seat, stack: spot.hero_stack, folded: false, profile: None }, board, Some(hero_mask), MAX_COMBOS)?
|
||
} else {
|
||
let v = spot.live_villains().find(|v| v.seat == *seat)?;
|
||
model_hands(spot, v, board, None, MAX_COMBOS)?
|
||
};
|
||
hands.push(h);
|
||
}
|
||
let game = GameM::build(order.len(), pot, cap, &[Menu3::standard(), later()], slots);
|
||
let at = game.follow(&line)?;
|
||
let NodeM::Dec { player, acts, .. } = &game.nodes[at] else { return None };
|
||
if *player != me {
|
||
return None;
|
||
}
|
||
let acts = acts.clone();
|
||
let nodes = game.nodes.len();
|
||
let combos: Vec<usize> = hands.iter().map(|h| h.masks.len()).collect();
|
||
let hero_index = hands[me].masks.iter().position(|m| *m == hero_mask)?;
|
||
let mut solver = SolverM::new(game, hands, board, spot.seed ^ 0x9e37_79b9_7f4a_7c15);
|
||
let mut done = 0u32;
|
||
while done < ITERS {
|
||
solver.iterate();
|
||
done += 1;
|
||
if done % 50 == 0 && crate::engine::frist::thread_cpu_ms() - cpu0 > budget || crate::engine::frist::notbremse() {
|
||
break;
|
||
}
|
||
}
|
||
if done < MIN_ITERS {
|
||
return None;
|
||
}
|
||
let row = solver.row(at, hero_index);
|
||
Some(SolvedM { row, acts, iterations: solver.iterations(), millis: started.elapsed().as_secs_f64() * 1e3, players: order.len(), nodes, combos })
|
||
}
|
||
|
||
/// The live multiway turn/flop decision, or `None` and the heuristic keeps it.
|
||
pub fn multi3_decision(raw: &Spot) -> Option<Decision> {
|
||
let on = match raw.street {
|
||
Street::Turn => turn3(),
|
||
Street::Flop => flop3(),
|
||
_ => false,
|
||
};
|
||
if !on || raw.hero_cards.len() != 2 || raw.live_villains().count() < 2 {
|
||
return None;
|
||
}
|
||
let gated = crate::engine::read_gate::gate_reads(raw);
|
||
let s = solve_multi3(&gated)?;
|
||
let (draw, _) = Rng::seeded(gated.seed).unit();
|
||
let mut acc = 0.0;
|
||
let mut pick = s.acts.len() - 1;
|
||
for (k, p) in s.row.iter().enumerate() {
|
||
acc += p;
|
||
if draw < acc {
|
||
pick = k;
|
||
break;
|
||
}
|
||
}
|
||
let act = s.acts[pick];
|
||
let p = s.row[pick];
|
||
let street = if gated.street == Street::Turn { "turn3" } else { "flop3" };
|
||
Some(Decision::new(
|
||
legal_choice(&gated, act),
|
||
p.clamp(0.0, 1.0),
|
||
format!(
|
||
"solver {street} ({} players): {:?} combos, {} nodes, {} iters, {act:?} p {p:.2}, {:.0} ms",
|
||
s.players, s.combos, s.nodes, s.iterations, s.millis
|
||
),
|
||
))
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests;
|