From eeb7a31e88d0765b8ebaa06b48c45e6188ee694c Mon Sep 17 00:00:00 2001 From: Marc Mintel Date: Sun, 15 Nov 2026 22:39:57 +0100 Subject: [PATCH] nemesis: Seed-Argument, Live-Schalter aus der Umgebung (sim::arm_env, aus ab_zelle gezogen, bitgleich), CMA-ES haelt POKERBENCH_THREADS ein Co-Authored-By: Claude Opus 5.5 --- src/bin/ab_zelle.rs | 72 +----------------------------------------- src/bin/nemesis.rs | 14 +++++++-- src/sim/arm_env.rs | 77 +++++++++++++++++++++++++++++++++++++++++++++ src/sim/mod.rs | 1 + src/sim/optimize.rs | 16 +++++++--- 5 files changed, 103 insertions(+), 77 deletions(-) create mode 100644 src/sim/arm_env.rs diff --git a/src/bin/ab_zelle.rs b/src/bin/ab_zelle.rs index 4913f49c..3f7a7a04 100644 --- a/src/bin/ab_zelle.rs +++ b/src/bin/ab_zelle.rs @@ -59,77 +59,7 @@ const KLASSIK: [&str; 8] = ["station", "fish", "reg", "nit", "maniac", "nemesis- /// Track switches read from the environment. Identity on the engine branch. fn apply_arm() { - // HERO=gto: the balanced bot plays hero's seat (engine-vs-bot comparison on the same deals). - rechner::sim::table::set_hero_gto(std::env::var("HERO").as_deref() == Ok("gto")); - // SIM_RAKE=mikro: the whole ladder at the live micro table (5 %, cap 10 bb, preflop raked; the - // rake leaves the table). `RAKE_LIVE=1` (read by the engine itself) prices it in the decisions. - rechner::sim::session::set_ladder_rake(std::env::var("SIM_RAKE").as_deref() == Ok("mikro")); - // RIVER_SD=1: the river showdown read (world memory) into the session read (arm). - rechner::sim::memory::set_river_showdown(std::env::var("RIVER_SD").as_deref() == Ok("1")); - // LESEN_DRAHT=hud|gezaehlt: the Memory read through the live wire's shrink, with the samples live had - // before counted reads (hud) or the true counts (gezaehlt) — plan D1b, `sim::memory::WireRead`. - { - use rechner::sim::memory::{set_wire_read, WireRead}; - set_wire_read(match std::env::var("LESEN_DRAHT").as_deref() { - // raise = GEGNER_ZAEHLUNG=raise: the HUD samples of today plus the counted raiseToBet/reraiseToRaise - // (exactly `hud`); live = today without them. - Ok("hud") | Ok("live") | Ok("raise") => WireRead::Hud, - Ok("gezaehlt") => WireRead::Counted, - _ => WireRead::Off, - }); - rechner::sim::memory::set_wire_without_counted(std::env::var("LESEN_DRAHT").as_deref() == Ok("live")); - } - // GEDAECHTNIS_FOLD=a,b: foldToBet as the memory handover decodes it (plan D2), only with LESEN_DRAHT=gezaehlt. - rechner::sim::memory::set_fold_prior(std::env::var("GEDAECHTNIS_FOLD").ok().and_then(|v| { - let mut it = v.split(',').map(|x| x.trim().parse::().ok()); - Some((it.next()??, it.next()??)) - })); - // READS=cbet,3bet,xr: the line-specific HUD reads (fold to c-bet / fold to 3-bet / check-raise). - use rechner::sim::memory::{set_line_reads, READ_CBET, READ_CHECK_RAISE, READ_THREE_BET}; - let reads = std::env::var("READS").unwrap_or_default(); - let mask = reads.split(',').fold(0u8, |m, r| match r.trim() { - "cbet" => m | READ_CBET, - "3bet" => m | READ_THREE_BET, - "xr" => m | READ_CHECK_RAISE, - _ => m, - }); - set_line_reads(mask); - // RAISE_MODEL=1: the check-raise in the tree of a bet into a check (`engine::bluff`). - rechner::engine::bluff::set_raise_model(std::env::var("RAISE_MODEL").as_deref() == Ok("1")); - // RIVER_DBR=1: the data-biased robust river counter-strategy (`engine::river_subgame::dbr_decision`). - { - use rechner::engine::river_subgame::{set_river_player, RiverPlayer}; - set_river_player(match std::env::var("RIVER_DBR").as_deref() { - Ok("1") => RiverPlayer::Dbr, - Ok("nash") => RiverPlayer::Nash, - Ok("spiel") => RiverPlayer::Game, - Ok("grund") => RiverPlayer::Grund, - _ => RiverPlayer::Off, - }); - // TURN_SPIEL=grund: the turn+river game with both ranges from the blueprint (plan E3, - // `engine::turn_game`), only where the blueprint recognises the spot. Needs GRUNDSTRATEGIE like - // RIVER_DBR=grund. Size/budget: TURN_COMBOS, TURN_CARDS, TURN_ITERS (defaults in `TurnConfig`). - // SOLVER_RIVER=1|modell (stage A, solver everywhere): `engine::river_spieler`. - rechner::engine::river_spieler::set_river_spieler(rechner::engine::river_spieler::from_env()); - rechner::engine::turn_game::set_turn_ueberall(rechner::engine::turn_game::turn_ueberall_from_env()); - rechner::engine::flop_spieler::set_flop_spieler(rechner::engine::flop_spieler::from_env()); - rechner::engine::river3_spieler::set_river3(rechner::engine::river3_spieler::from_env()); - let (turn3, flop3) = rechner::engine::multi3_spieler::from_env(); - rechner::engine::multi3_spieler::set_turn3(turn3); - rechner::engine::multi3_spieler::set_flop3(flop3); - use rechner::engine::turn_game::{set_turn_player, TurnPlayer}; - let turn_grund = std::env::var("TURN_SPIEL").as_deref() == Ok("grund"); - set_turn_player(if turn_grund { TurnPlayer::Grund } else { TurnPlayer::Off }); - // The blueprint arm waits until the blueprint run has finished (its FERTIG marker), so a - // queued arm never reads a half-trained checkpoint; then loads it once. - if turn_grund || std::env::var("RIVER_DBR").as_deref() == Ok("grund") { - let dir = std::path::PathBuf::from(std::env::var_os("GRUNDSTRATEGIE").expect("RIVER_DBR=grund / TURN_SPIEL=grund needs GRUNDSTRATEGIE")); - while !dir.join("grundstrategie").join("FERTIG").exists() { - std::thread::sleep(std::time::Duration::from_secs(60)); - } - assert!(rechner::blueprint::ranges::grundstrategie().is_some(), "blueprint did not load from {dir:?}"); - } - } + rechner::sim::arm_env::apply_env(); } fn klassik(name: &str) -> Option { diff --git a/src/bin/nemesis.rs b/src/bin/nemesis.rs index d0c99d09..c2cbd182 100644 --- a/src/bin/nemesis.rs +++ b/src/bin/nemesis.rs @@ -6,7 +6,7 @@ //! whole continuous neighbourhood. The same optimizer tunes the engine's own hyperparameters once they //! are exposed as a vector — that is the "polish, last" step the architecture names. //! -//! Run: `cargo run --release --bin nemesis`. +//! Run: `cargo run --release --bin nemesis [deals] [generations] [seed]`. use rechner::sim::optimize::worst_case_opponent; @@ -18,12 +18,22 @@ const DEALS: u32 = 1000; const GENERATIONS: u32 = 14; fn main() { + // Same engine as the ladder: the live switches come from the environment (`sim::arm_env`). + rechner::sim::arm_env::apply_env(); let deals: u32 = std::env::args().nth(1).and_then(|s| s.parse().ok()).unwrap_or(DEALS); let generations: u32 = std::env::args().nth(2).and_then(|s| s.parse().ok()).unwrap_or(GENERATIONS); + // Optional third argument: the search seed (hex with 0x, or decimal), for several independent searches. + let seed: u64 = std::env::args() + .nth(3) + .and_then(|s| match s.strip_prefix("0x") { + Some(h) => u64::from_str_radix(h, 16).ok(), + None => s.parse().ok(), + }) + .unwrap_or(SEED); println!("=== Stufe 5: der Nemesis — schlechtester Gegner der Engine (CMA-ES ueber den Regelraum) ==="); println!("{deals} Austeilungen je Auswertung, {generations} Generationen, Memory-Read.\n"); - let (r, ev) = worst_case_opponent(deals, generations, SEED); + let (r, ev) = worst_case_opponent(deals, generations, seed); println!("Nemesis-Regelwerk (realistische Schranken):"); println!(" call_margin {:.3} raise_at {:.3} bet_at {:.3}", r.call_margin, r.raise_at, r.bet_at); diff --git a/src/sim/arm_env.rs b/src/sim/arm_env.rs new file mode 100644 index 00000000..3d4dbc7d --- /dev/null +++ b/src/sim/arm_env.rs @@ -0,0 +1,77 @@ +//! The track switches every bench binary reads from the environment (`ab_zelle`, `nemesis`), in one +//! place so a leak finder runs the same engine as the ladder. Identity when no switch is set. + +/// Read the track switches from the environment and set them (see each line). +pub fn apply_env() { + // HERO=gto: the balanced bot plays hero's seat (engine-vs-bot comparison on the same deals). + crate::sim::table::set_hero_gto(std::env::var("HERO").as_deref() == Ok("gto")); + // SIM_RAKE=mikro: the whole ladder at the live micro table (5 %, cap 10 bb, preflop raked; the + // rake leaves the table). `RAKE_LIVE=1` (read by the engine itself) prices it in the decisions. + crate::sim::session::set_ladder_rake(std::env::var("SIM_RAKE").as_deref() == Ok("mikro")); + // RIVER_SD=1: the river showdown read (world memory) into the session read (arm). + crate::sim::memory::set_river_showdown(std::env::var("RIVER_SD").as_deref() == Ok("1")); + // LESEN_DRAHT=hud|gezaehlt: the Memory read through the live wire's shrink, with the samples live had + // before counted reads (hud) or the true counts (gezaehlt) — plan D1b, `sim::memory::WireRead`. + { + use crate::sim::memory::{set_wire_read, WireRead}; + set_wire_read(match std::env::var("LESEN_DRAHT").as_deref() { + // raise = GEGNER_ZAEHLUNG=raise: the HUD samples of today plus the counted raiseToBet/reraiseToRaise + // (exactly `hud`); live = today without them. + Ok("hud") | Ok("live") | Ok("raise") => WireRead::Hud, + Ok("gezaehlt") => WireRead::Counted, + _ => WireRead::Off, + }); + crate::sim::memory::set_wire_without_counted(std::env::var("LESEN_DRAHT").as_deref() == Ok("live")); + } + // GEDAECHTNIS_FOLD=a,b: foldToBet as the memory handover decodes it (plan D2), only with LESEN_DRAHT=gezaehlt. + crate::sim::memory::set_fold_prior(std::env::var("GEDAECHTNIS_FOLD").ok().and_then(|v| { + let mut it = v.split(',').map(|x| x.trim().parse::().ok()); + Some((it.next()??, it.next()??)) + })); + // READS=cbet,3bet,xr: the line-specific HUD reads (fold to c-bet / fold to 3-bet / check-raise). + use crate::sim::memory::{set_line_reads, READ_CBET, READ_CHECK_RAISE, READ_THREE_BET}; + let reads = std::env::var("READS").unwrap_or_default(); + let mask = reads.split(',').fold(0u8, |m, r| match r.trim() { + "cbet" => m | READ_CBET, + "3bet" => m | READ_THREE_BET, + "xr" => m | READ_CHECK_RAISE, + _ => m, + }); + set_line_reads(mask); + // RAISE_MODEL=1: the check-raise in the tree of a bet into a check (`engine::bluff`). + crate::engine::bluff::set_raise_model(std::env::var("RAISE_MODEL").as_deref() == Ok("1")); + // RIVER_DBR=1: the data-biased robust river counter-strategy (`engine::river_subgame::dbr_decision`). + { + use crate::engine::river_subgame::{set_river_player, RiverPlayer}; + set_river_player(match std::env::var("RIVER_DBR").as_deref() { + Ok("1") => RiverPlayer::Dbr, + Ok("nash") => RiverPlayer::Nash, + Ok("spiel") => RiverPlayer::Game, + Ok("grund") => RiverPlayer::Grund, + _ => RiverPlayer::Off, + }); + // TURN_SPIEL=grund: the turn+river game with both ranges from the blueprint (plan E3, + // `engine::turn_game`), only where the blueprint recognises the spot. Needs GRUNDSTRATEGIE like + // RIVER_DBR=grund. Size/budget: TURN_COMBOS, TURN_CARDS, TURN_ITERS (defaults in `TurnConfig`). + // SOLVER_RIVER=1|modell (stage A, solver everywhere): `engine::river_spieler`. + crate::engine::river_spieler::set_river_spieler(crate::engine::river_spieler::from_env()); + crate::engine::turn_game::set_turn_ueberall(crate::engine::turn_game::turn_ueberall_from_env()); + crate::engine::flop_spieler::set_flop_spieler(crate::engine::flop_spieler::from_env()); + crate::engine::river3_spieler::set_river3(crate::engine::river3_spieler::from_env()); + let (turn3, flop3) = crate::engine::multi3_spieler::from_env(); + crate::engine::multi3_spieler::set_turn3(turn3); + crate::engine::multi3_spieler::set_flop3(flop3); + use crate::engine::turn_game::{set_turn_player, TurnPlayer}; + let turn_grund = std::env::var("TURN_SPIEL").as_deref() == Ok("grund"); + set_turn_player(if turn_grund { TurnPlayer::Grund } else { TurnPlayer::Off }); + // The blueprint arm waits until the blueprint run has finished (its FERTIG marker), so a + // queued arm never reads a half-trained checkpoint; then loads it once. + if turn_grund || std::env::var("RIVER_DBR").as_deref() == Ok("grund") { + let dir = std::path::PathBuf::from(std::env::var_os("GRUNDSTRATEGIE").expect("RIVER_DBR=grund / TURN_SPIEL=grund needs GRUNDSTRATEGIE")); + while !dir.join("grundstrategie").join("FERTIG").exists() { + std::thread::sleep(std::time::Duration::from_secs(60)); + } + assert!(crate::blueprint::ranges::grundstrategie().is_some(), "blueprint did not load from {dir:?}"); + } + } +} diff --git a/src/sim/mod.rs b/src/sim/mod.rs index 182e98ac..a8f4b1a5 100644 --- a/src/sim/mod.rs +++ b/src/sim/mod.rs @@ -2,6 +2,7 @@ //! fixed opponents, win rate in bb/100 with a 95 % interval. Pure and deterministic; used by //! `tests/sim_strength.rs`, not by the request pipeline. +pub mod arm_env; pub mod adaptive; pub mod aivat; pub mod benchmark; diff --git a/src/sim/optimize.rs b/src/sim/optimize.rs index c1f22077..e215b785 100644 --- a/src/sim/optimize.rs +++ b/src/sim/optimize.rs @@ -94,10 +94,18 @@ pub fn minimize f64 + Sync>( cand.push((x, z, y)); } // Evaluate the independent candidates in parallel. - let fs: Vec = thread::scope(|s| { - let handles: Vec<_> = cand.iter().map(|(x, _, _)| s.spawn(|| f(x))).collect(); - handles.into_iter().map(|h| h.join().expect("objective panicked")).collect() - }); + // At most `POKERBENCH_THREADS` at once (CLAUDE.md "Messlaeufe"); unset = all λ. The order of + // the results does not depend on the chunking, so the run stays deterministic in `seed`. + let width = std::env::var("POKERBENCH_THREADS").ok().and_then(|t| t.parse::().ok()).filter(|&t| t > 0).unwrap_or(lambda); + let fs: Vec = cand + .chunks(width) + .flat_map(|chunk| { + thread::scope(|s| { + let handles: Vec<_> = chunk.iter().map(|(x, _, _)| s.spawn(|| f(x))).collect(); + handles.into_iter().map(|h| h.join().expect("objective panicked")).collect::>() + }) + }) + .collect(); evals += lambda as u32; // (f, z, y), tracking the best x seen. let mut pop: Vec<(f64, Vec, Vec)> = Vec::with_capacity(lambda);