while _w != "/" lief ausserhalb eines Baums mit lesen/ endlos (Wurzel ist C:\). Jetzt: Abbruch, wenn dirname(_w) == _w. zeit_tells, zeit_tells_spec, denkzeit_dicht_spec. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
178 lines
8.0 KiB
Python
178 lines
8.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Zeit dicht: die eigene Denkzeit verraet die Hand nicht (hand/denkzeit.py: zeitlage).
|
|
|
|
Befund (docs/notizen/BEFUND-timing.md): die alte Zeit hing an der PRIVATEN Knappheit (Konfidenz,
|
|
Equity gegen Pot-Odds). Dicht wird sie je oeffentlicher Lage aus der gemessenen Verteilung gezogen.
|
|
Geprueft wird:
|
|
* gleiche oeffentliche Lage + gleicher Seed -> gleiche Zeit, egal wie gut die Hand ist
|
|
* die Gegenprobe (ZEIT_DICHT=0) haengt weiter an der Hand - der Test unterscheidet also
|
|
* der Durchsatz bleibt: mittlere Zeit dicht = alt (auf Zuegen, deren echte Knappheit der
|
|
gemessenen Verteilung folgt), je Lage
|
|
* die Entscheidung bleibt unberuehrt (Anfrage/Antwort unveraendert), Seed-genau
|
|
|
|
python3 tests/denkzeit_dicht_spec.py -v
|
|
"""
|
|
import os as _os, sys as _sys
|
|
_w = _os.path.dirname(_os.path.abspath(__file__))
|
|
while _os.path.dirname(_w) != _w and not _os.path.isdir(_os.path.join(_w, "lesen")):
|
|
_w = _os.path.dirname(_w)
|
|
for _u in ("lesen", "entscheiden", "hand", "betrieb"):
|
|
_q = _os.path.join(_w, _u)
|
|
if _os.path.isdir(_q) and _q not in _sys.path:
|
|
_sys.path.insert(0, _q)
|
|
import copy
|
|
import os
|
|
import random
|
|
import statistics
|
|
import unittest
|
|
|
|
import denkzeit
|
|
|
|
FLUSS = {"street": "RIVER", "pot": 0.40, "toCall": 0.20, "bigBlind": 0.02, "heroSeat": 3,
|
|
"history": [{"seat": 3, "action": "CALL", "amount": 0.06}]}
|
|
FLUSS_FREI = dict(FLUSS, toCall=0.0)
|
|
VORFLOP = {"street": "PREFLOP", "pot": 0.03, "toCall": 0.02, "bigBlind": 0.02, "heroSeat": 3,
|
|
"history": []}
|
|
|
|
|
|
class _Umgebung(unittest.TestCase):
|
|
def setUp(self):
|
|
self._alt = os.environ.get("ZEIT_DICHT")
|
|
|
|
def tearDown(self):
|
|
if self._alt is None:
|
|
os.environ.pop("ZEIT_DICHT", None)
|
|
else:
|
|
os.environ["ZEIT_DICHT"] = self._alt
|
|
|
|
def zeit(self, anfrage, antwort, seed, dicht=True, profil=None):
|
|
os.environ["ZEIT_DICHT"] = "1" if dicht else "0"
|
|
return denkzeit.gedenkzeit_ms(anfrage, antwort, profil or denkzeit.profil("schnell"),
|
|
random.Random(seed), takt=16)
|
|
|
|
|
|
class Schalter(_Umgebung):
|
|
def test_vorgabe_ist_dicht(self):
|
|
os.environ.pop("ZEIT_DICHT", None)
|
|
self.assertTrue(denkzeit.dicht())
|
|
|
|
def test_null_schaltet_zurueck_auf_die_alte_zeit(self):
|
|
os.environ["ZEIT_DICHT"] = "0"
|
|
antwort = {"action": "CALL", "confidence": 0.55, "equity": 0.33}
|
|
k, b = denkzeit.zeitlage(FLUSS, antwort, random.Random(1))
|
|
self.assertEqual(k, denkzeit.knappheit(FLUSS, antwort))
|
|
self.assertEqual(b, denkzeit.band(FLUSS, antwort))
|
|
|
|
|
|
class OhneHandinformation(_Umgebung):
|
|
"""Was der Gegner sieht (Strasse, Aktion, Einsatz, Pot), bestimmt die Zeit - die Hand nicht."""
|
|
|
|
HAENDE = [{"confidence": 0.99, "equity": 0.95}, {"confidence": 0.51, "equity": 0.34},
|
|
{"confidence": 0.70, "equity": 0.10}, {"confidence": 0.5}, {}]
|
|
|
|
def test_call_gleiche_zeit_ob_nuts_oder_hauchduenn(self):
|
|
for seed in range(40):
|
|
zeiten = {self.zeit(FLUSS, dict(h, action="CALL"), seed) for h in self.HAENDE}
|
|
self.assertEqual(len(zeiten), 1, (seed, zeiten))
|
|
|
|
def test_bet_gleiche_zeit_ob_value_oder_bluff(self):
|
|
for seed in range(40):
|
|
zeiten = {self.zeit(FLUSS_FREI, dict(h, action="BET"), seed) for h in self.HAENDE}
|
|
self.assertEqual(len(zeiten), 1, (seed, zeiten))
|
|
|
|
def test_vorflop_raise_gleiche_zeit_ob_rein_oder_gemischt(self):
|
|
for seed in range(40):
|
|
rein = self.zeit(VORFLOP, {"action": "RAISE", "confidence": 1.0}, seed)
|
|
gemischt = self.zeit(VORFLOP, {"action": "RAISE", "confidence": 0.35}, seed)
|
|
self.assertEqual(rein, gemischt)
|
|
|
|
def test_gegenprobe_die_alte_zeit_haengt_an_der_hand(self):
|
|
klar = [self.zeit(FLUSS, {"action": "CALL", "confidence": 0.99, "equity": 0.95}, s, dicht=False)
|
|
for s in range(300)]
|
|
knapp = [self.zeit(FLUSS, {"action": "CALL", "confidence": 0.51, "equity": 0.34}, s, dicht=False)
|
|
for s in range(300)]
|
|
self.assertGreater(statistics.mean(knapp), 1.3 * statistics.mean(klar))
|
|
|
|
def test_oeffentliche_lage_wirkt_weiter(self):
|
|
"""Ein grosser Pot dauert laenger als ein kleiner - das sieht jeder."""
|
|
gross = dict(FLUSS, pot=1.20)
|
|
a = {"action": "CALL"}
|
|
lang = statistics.mean(self.zeit(gross, a, s) for s in range(400))
|
|
kurz = statistics.mean(self.zeit(FLUSS, a, s) for s in range(400))
|
|
self.assertGreater(lang, kurz)
|
|
|
|
|
|
class Verteilung(unittest.TestCase):
|
|
def test_gezogene_knappheit_folgt_der_gemessenen(self):
|
|
zufall = random.Random(11)
|
|
for (strasse, aktion, gegen), (n, q) in denkzeit.KNAPP_QUANTILE.items():
|
|
if aktion == "*" or n < denkzeit.KNAPP_MINDEST:
|
|
continue
|
|
anfrage = {"street": "PREFLOP" if strasse == "PREFLOP" else "TURN",
|
|
"toCall": 0.02 if gegen else 0.0}
|
|
w = [denkzeit.scheinknappheit(anfrage, {"action": aktion}, zufall) for _ in range(4000)]
|
|
# Verteilungsfunktion: unter dem i-ten 5-%-Quantil liegen ~ i*5 % der Zuege (Stufen der
|
|
# Tabelle mit gleichen Werten zaehlen ganz mit) - und nie etwas ausserhalb der Spanne
|
|
for i in (5, 10, 15, 18, 19):
|
|
unter = sum(1 for x in w if x <= q[i] + 1e-12) / len(w)
|
|
oben = max(j for j in range(len(q)) if q[j] <= q[i] + 1e-12) / 20.0
|
|
self.assertTrue(i / 20.0 - 0.03 <= unter <= oben + 0.03, (strasse, aktion, gegen, i, unter))
|
|
self.assertTrue(q[0] - 1e-9 <= min(w) and max(w) <= q[-1] + 1e-9)
|
|
|
|
def test_unbekannte_aktion_faellt_auf_die_strasse(self):
|
|
k = denkzeit.scheinknappheit(FLUSS, {"action": "ABSTAIN"}, random.Random(2))
|
|
self.assertTrue(0.0 <= k <= 1.0)
|
|
|
|
def test_genau_eine_zufallszahl(self):
|
|
a, b = random.Random(5), random.Random(5)
|
|
denkzeit.scheinknappheit(FLUSS, {"action": "CALL"}, a)
|
|
b.random()
|
|
self.assertEqual(a.random(), b.random())
|
|
|
|
|
|
class Durchsatz(_Umgebung):
|
|
"""Zuege, deren ECHTE Knappheit der gemessenen Verteilung folgt: dicht kostet im Mittel gleich
|
|
viel Zeit wie alt (12-Tische-Zeitmodell: der Takt bleibt)."""
|
|
|
|
def mittel(self, anfrage, aktion, dicht, n=3000, tempo="schnell"):
|
|
zufall = random.Random(17)
|
|
quelle = random.Random(23)
|
|
profil = denkzeit.profil(tempo)
|
|
summe = 0.0
|
|
for _ in range(n):
|
|
k = denkzeit.scheinknappheit(anfrage, {"action": aktion}, quelle)
|
|
antwort = {"action": aktion, "confidence": 1.0 - k} # ohne Equity: knappheit = 1 - Konfidenz
|
|
os.environ["ZEIT_DICHT"] = "1" if dicht else "0"
|
|
summe += denkzeit.gedenkzeit_ms(anfrage, antwort, profil, zufall, takt=16)
|
|
return summe / n
|
|
|
|
def test_mittlere_zeit_bleibt_je_lage(self):
|
|
for anfrage, aktion in ((FLUSS, "CALL"), (FLUSS, "FOLD"), (FLUSS_FREI, "CHECK"),
|
|
(FLUSS_FREI, "BET"), (VORFLOP, "RAISE"), (VORFLOP, "CALL")):
|
|
for tempo in ("schnell", "normal"):
|
|
alt = self.mittel(anfrage, aktion, False, tempo=tempo)
|
|
neu = self.mittel(anfrage, aktion, True, tempo=tempo)
|
|
self.assertAlmostEqual(neu / alt, 1.0, delta=0.05, msg=(aktion, tempo, alt, neu))
|
|
|
|
|
|
class EntscheidungUnberuehrt(_Umgebung):
|
|
def test_anfrage_und_antwort_bleiben_wie_sie_sind(self):
|
|
anfrage = copy.deepcopy(FLUSS)
|
|
antwort = {"action": "CALL", "confidence": 0.6, "equity": 0.4, "size": None}
|
|
vorher = (copy.deepcopy(anfrage), copy.deepcopy(antwort))
|
|
self.zeit(anfrage, antwort, 3)
|
|
self.assertEqual((anfrage, antwort), vorher)
|
|
|
|
def test_gleicher_seed_gleiche_zeit(self):
|
|
a = {"action": "CALL", "confidence": 0.6}
|
|
self.assertEqual(self.zeit(FLUSS, a, 9), self.zeit(FLUSS, a, 9))
|
|
|
|
def test_haelt_die_uhr(self):
|
|
for s in range(200):
|
|
self.assertLessEqual(self.zeit(dict(FLUSS, pot=5.0), {"action": "CALL"}, s),
|
|
16 * 1000 * denkzeit.UHR_ANTEIL + 1e-6)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|