#!/usr/bin/env python3 """Spezifikation entscheiden/eval7: der schnelle 7-Karten-Wert `score7` und die Monte-Carlo-Equity `equity_mc` (fuer den Preflop-All-in der All-in-EV, lesen/allin_ev). Zwei Beweise: 1) score7 == best7 (Ground Truth) ueber zehntausende Zufallshaende -> der schnelle Pfad ist beweisbar gleichwertig zur 21-Kombinationen-Enumeration. Ein Unterschied waere ein falscher Showdown-Gewinner und damit falsche Equity. 2) equity_mc ist UNVERZERRT: auf einem Board, wo equity_vs exakt bezahlbar ist (Flop/Turn), liegt der MC-Schaetzer im MC-Rauschen am exakten Wert. Unverzerrte Equity -> mittelwerttreue All-in-EV. python3 tests/eval7_mc_spec.py -v """ import gc import os import random import sys import unittest _w = os.path.dirname(os.path.abspath(__file__)) while _w != "/" and not os.path.isdir(os.path.join(_w, "entscheiden")): _w = os.path.dirname(_w) sys.path.append(os.path.join(_w, "entscheiden")) import eval7 class Score7GleichBest7(unittest.TestCase): def test_zufallshaende_identisch(self): rng = random.Random(0) deck = list(eval7.VOLLDECK) n, bad = 15000, [] gc.disable() try: for _ in range(n): h = rng.sample(deck, 7) a, b = eval7.score7(h), eval7.best7(h) if a != b and len(bad) < 5: bad.append((h, a, b)) finally: gc.enable() self.assertEqual(bad, [], f"score7 != best7 bei: {bad}") def test_strukturierte_kategorien(self): # je Kategorie eine Hand, bei der die 5 besten aus 7 nicht trivial sind faelle = [ "As Ks Qs Js Ts 2h 3d", # Straight Flush "As Ad Ac Ah Kd 2c 3s", # Vierling + Kicker "Ac Ad Ks Kh Qd Qc 2s", # drei Paare AA/KK/QQ -> bestes ist zwei Paare AAKK + Q-Kicker "2h 3h 4h 5h 6h As Kd", # Straight Flush tief (nicht Wheel) + hohe Beikarten "Ah 2d 3c 4s 5h 9d Kc", # Wheel-Straight "Ah Kh Qh 2h 7h 3d 4s", # Flush, 5 aus Herz "Ac Ad 7h 7s Kd Qc 2h", # zwei Paare + Kicker ] for f in faelle: h = eval7.karten(f) self.assertEqual(eval7.score7(h), eval7.best7(h), f) class EquityMcUnverzerrt(unittest.TestCase): def test_flop_mc_nahe_exakt(self): # Flop AA vs KQ auf K72: exakt billig (990 Runouts). MC muss im Rauschen danebenliegen. board = eval7.karten("Kc 7h 2s") ex = eval7.equity_vs("Ac Ad", board, ["Kh Qd"]) mc = eval7.equity_mc("Ac Ad", board, ["Kh Qd"], laeufe=3000, seed=1) self.assertAlmostEqual(mc, ex, delta=0.03) # 3000 Laeufe -> ~0.9 % 1-Sigma def test_board_voll_ist_exakt(self): # 5 Karten -> kein Wurf mehr offen: equity_mc faellt auf equity_vs zurueck (deterministisch). self.assertEqual(eval7.equity_mc("Kc 7s", eval7.karten("Qc 2h Kd 6d 8d"), ["Ad Qh"]), 1.0) def test_seed_reproduzierbar(self): a = eval7.equity_mc("Ah Ks", [], ["Qc Jd"], laeufe=1500, seed=42) b = eval7.equity_mc("Ah Ks", [], ["Qc Jd"], laeufe=1500, seed=42) self.assertEqual(a, b) # gleicher Seed -> bit-identisch c = eval7.equity_mc("Ah Ks", [], ["Qc Jd"], laeufe=1500, seed=43) self.assertNotEqual(a, c) # anderer Seed -> andere Ziehung def test_preflop_aa_vs_kk_plausibel(self): # AA vs KK preflop ~0.82 (Lehrbuch). MC mit 4000 Laeufen muss nah dran sein. eq = eval7.equity_mc("Ac Ad", [], ["Kc Kh"], laeufe=4000, seed=7) self.assertGreater(eq, 0.78) self.assertLess(eq, 0.86) if __name__ == "__main__": unittest.main()