Files
pokerfreak/src/rake/live.rs
T
mmintelandClaude Opus 5.5 c5076ddc5f Rake-Deckel nach Spielerzahl: RAKE_LIVE=sitze (Kern) und lesen/rake.regel_cent (Fremdpruefung Befund 3)
- Kern: RAKE_LIVE=sitze = wie 1 plus Deckel nach ausgeteilten Spielern (2: 5 Cent, 3-4: 12 Cent, 5+: 20 Cent).
  RAKE_LIVE=1 unveraendert: Identitaet 0/3000 (golive) und 0/1961 (eigen). Mit sitze nur Abweichungen bei 2-4 Spielern
  (27 Aktions-/Groessen-Kipps auf 4961 Anfragen, Liste in fremd-out/rake).
- lesen/rake.py: deckel_cent/regel_cent; gegen den Raum 7550 eigene + 1953 hhDealer-Haende 0 Abweichungen (vorher 15 + 4).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 17:37:20 +01:00

222 lines
8.4 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! The rake inside the live decision (`RAKE_LIVE`).
//!
//! Every money comparison of a cash decision — pot odds, call / check / raise EV, the fold branch of a
//! bet, the MDF floor's break-even, the river veto's tree, the open-jam price — is about the pot hero
//! *takes home*, not the pot on the table. At NL 0.01/0.02 the room keeps 5 % of every pot (cap 10 bb,
//! preflop pots too: `RakeRule::MICRO_BB`), so a call that breaks even on the raw pot loses.
//!
//! **Switch.** `RAKE_LIVE=1` (read once). Off by default: then no scope is ever entered, `net` returns
//! its argument unchanged and every result is bit-identical to the engine without this module.
//!
//! **Seat cap (`RAKE_LIVE=sitze`).** The room's cap depends on the players dealt in (measured 23.11.,
//! `messung/fremd/4_regeln/ERGEBNIS.md`, NL 0.01/0.02): 5-6 players 20 cents (10 bb), 3-4 players 12
//! cents (6 bb), 2 players 5 cents (2.5 bb); 7550 own and 1953 hhDealer hands match to the cent.
//! `RAKE_LIVE=sitze` is `RAKE_LIVE=1` plus that cap (the spot's `seats`, the players of the hand);
//! with `RAKE_LIVE=1` and at five or more seats nothing differs from the flat 10 bb, bit for bit.
//!
//! **Scope.** The rake only exists at a cash table: `engine::decide::decide_with` enters a `Scope` for a
//! spot without a tournament situation (tournaments pay no rake per pot). The scope is thread-local and
//! restored on drop, so parallel simulations and nested decisions stay independent. Inside it the pot
//! is in the spot's chips; the rule is in big blinds, so the scope carries the spot's big blind.
//!
//! **Model.** The rake of a pot is the rule applied to the pot as it would end *now* (heads-up cap,
//! flop dealt or not makes no difference at this stake). Later streets are not modelled, exactly as
//! the EV formulas of `odds::ev` do not model them.
//!
//! **Smooth, not to the cent.** The table rounds to the cent (0.5 bb), but a decision does not know
//! the pot the hand will end with, and over that uncertainty the rounding averages out: the expected
//! rake is 5 % of the pot up to the cap (`DECISION_RULE`). Pricing the rounded step instead puts
//! artificial jumps of 0.5 bb between neighbouring sizes (a pot bet into 2 bb paid 0.5, two thirds
//! paid 0) and pushed value bets to the smaller size: measured against the station, −0.19 bb/100
//! (`messung/rake/URTEIL.md`). The simulator's table (`TableCost::MICRO`) keeps the exact cents.
use super::{HandEnd, RakeRule};
use std::cell::Cell;
use std::sync::OnceLock;
/// The rule the live tables take (big blinds), to the cent.
pub const LIVE_RULE: RakeRule = RakeRule::MICRO_BB;
/// The rule a decision prices with: the live rule without the rounding step (its expectation).
pub const DECISION_RULE: RakeRule = RakeRule { step: 0.0, ..LIVE_RULE };
/// `RAKE_LIVE=1` switches the rake into the live decision. Read once per process.
pub fn enabled() -> bool {
static ON: OnceLock<bool> = OnceLock::new();
*ON.get_or_init(|| matches!(std::env::var("RAKE_LIVE").as_deref(), Ok("1") | Ok("sitze")))
}
/// `RAKE_LIVE=sitze`: the cap follows the players of the hand (`cap_bb`). Read once per process.
pub fn seat_cap_enabled() -> bool {
static ON: OnceLock<bool> = OnceLock::new();
*ON.get_or_init(|| std::env::var("RAKE_LIVE").as_deref() == Ok("sitze"))
}
/// The room's cap in big blinds for a hand with `players` dealt in (NL 0.01/0.02: 20 / 12 / 5 cents).
pub fn cap_bb(players: u8) -> f64 {
match players {
0 | 1 => LIVE_RULE.cap,
2 => 2.5,
3 | 4 => 6.0,
_ => LIVE_RULE.cap,
}
}
thread_local! {
/// Big blind of the cash spot being decided on this thread; `None` = no rake.
static BIG_BLIND: Cell<Option<f64>> = const { Cell::new(None) };
/// Players of the hand when the seat cap is on; `None` = the flat cap.
static SEATS: Cell<Option<u8>> = const { Cell::new(None) };
}
/// While alive, `net` and `of_pot` rake pots in chips of a table with this big blind.
#[must_use]
pub struct Scope {
previous: Option<f64>,
previous_seats: Option<u8>,
}
impl Scope {
/// Enter unconditionally (tests, measurement tools). A non-positive or broken big blind means no
/// rake.
pub fn enter(big_blind: f64) -> Scope {
let value = (big_blind.is_finite() && big_blind > 0.0).then_some(big_blind);
Scope { previous: BIG_BLIND.with(|b| b.replace(value)), previous_seats: SEATS.with(|s| s.replace(None)) }
}
/// Enter with the seat cap of a table with `players` dealt in (tests, `cash_scope`).
pub fn enter_seats(big_blind: f64, players: u8) -> Scope {
let scope = Scope::enter(big_blind);
SEATS.with(|s| s.set(Some(players)));
scope
}
/// No rake inside, whatever the outer scope said (a tournament decision nested in a cash run).
pub fn none() -> Scope {
Scope { previous: BIG_BLIND.with(|b| b.replace(None)), previous_seats: SEATS.with(|s| s.replace(None)) }
}
}
impl Drop for Scope {
fn drop(&mut self) {
BIG_BLIND.with(|b| b.set(self.previous));
SEATS.with(|s| s.set(self.previous_seats));
}
}
/// The scope of a cash decision: entered only when `RAKE_LIVE` is on, `None` otherwise (and then
/// nothing changes anywhere).
pub fn cash_scope(big_blind: f64) -> Option<Scope> {
enabled().then(|| Scope::enter(big_blind))
}
/// `cash_scope` for a spot with `players` dealt in: under `RAKE_LIVE=sitze` the cap follows them, under
/// `RAKE_LIVE=1` the scope is the one of `cash_scope` (flat cap).
pub fn cash_scope_seats(big_blind: f64, players: u8) -> Option<Scope> {
enabled().then(|| if seat_cap_enabled() { Scope::enter_seats(big_blind, players) } else { Scope::enter(big_blind) })
}
/// Is a rake scope active on this thread?
pub fn active() -> bool {
BIG_BLIND.with(Cell::get).is_some()
}
/// Rake of `pot` (chips) at the current table; 0 outside a scope.
pub fn of_pot(pot: f64) -> f64 {
match BIG_BLIND.with(Cell::get) {
None => 0.0,
Some(bb) if pot.is_finite() && pot > 0.0 => {
let rule = match SEATS.with(Cell::get) {
Some(players) if cap_bb(players) != LIVE_RULE.cap => {
let cap = cap_bb(players);
RakeRule { cap, cap_more_than_three: cap, ..DECISION_RULE }
}
_ => DECISION_RULE,
};
rule.of_hand(pot / bb, 2, HandEnd::FlopDealt) * bb
}
Some(_) => 0.0,
}
}
/// What the winner of `pot` takes home. Outside a scope `pot` itself, unchanged to the bit.
pub fn net(pot: f64) -> f64 {
if BIG_BLIND.with(Cell::get).is_none() {
return pot;
}
pot - of_pot(pot)
}
#[cfg(test)]
mod tests {
use super::*;
fn near(a: f64, b: f64) {
assert!((a - b).abs() < 1e-12, "{a} != {b}");
}
#[test]
fn without_a_scope_nothing_is_raked_and_the_pot_is_returned_bit_for_bit() {
for pot in [0.0, 0.03, 0.11, 1.0, 7.123_456_789, 1e9, f64::NAN] {
assert_eq!(net(pot).to_bits(), pot.to_bits());
assert_eq!(of_pot(pot), 0.0);
}
assert!(!active());
}
#[test]
fn a_decision_prices_five_percent_smooth_capped_at_twenty_cents() {
let _s = Scope::enter(0.02);
near(of_pot(0.10), 0.005);
near(of_pot(0.33), 0.0165);
near(of_pot(3.38), 0.169);
near(of_pot(4.03), 0.20);
near(of_pot(50.0), 0.20);
near(net(1.0), 0.95);
}
#[test]
fn the_scope_is_restored_on_drop_and_nests() {
{
let _a = Scope::enter(0.02);
assert!(active());
{
let _b = Scope::none();
assert!(!active());
}
assert!(active());
}
assert!(!active());
}
#[test]
fn the_seat_cap_follows_the_players_of_the_hand() {
{
let _s = Scope::enter_seats(0.02, 6);
near(of_pot(50.0), 0.20);
near(of_pot(3.38), 0.169);
}
{
let _s = Scope::enter_seats(0.02, 4);
near(of_pot(50.0), 0.12);
near(of_pot(1.0), 0.05);
}
{
let _s = Scope::enter_seats(0.02, 2);
near(of_pot(50.0), 0.05);
}
assert!(!active());
let _s = Scope::enter(0.02);
near(of_pot(50.0), 0.20);
}
#[test]
fn in_big_blinds_neighbouring_sizes_pay_no_step() {
let _s = Scope::enter(1.0);
near(of_pot(4.67), 0.2335);
near(of_pot(6.0), 0.3);
near(of_pot(20.0), 1.0);
near(of_pot(400.0), 10.0);
}
}