vorflop_selbst: error-bound rule (G lower bound in seed B minus reach-weighted continuation error) and inconsistency map by line, around motor vorflop-selbst-fehler; specs
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
1db4a40601
commit
e05d1c42bb
@@ -0,0 +1,119 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Error bound of the continuation (Hauptsession 05.12.) and the inconsistency map, around `motor vorflop-selbst-fehler`.
|
||||
|
||||
zeilen <archiv-roh.jsonl> <knoten-liste.txt> <zeilen.txt>
|
||||
input for the motor: per kept row (faithful, BR ok) and per changed non-fold move "key class code".
|
||||
regel <archiv-roh.jsonl> <knoten-liste.txt> <aus>.fehler.tsv <ziel.tsv>
|
||||
a change counts only if lower one-sided 95 % bound of G (seed B) - sum_m |pi'(m) - pi(m)| * E(m) > 0.
|
||||
karte <aus>.knoten.tsv [haende_je=1]
|
||||
per preflop line: sum of reach * R (point and upper bound) in bb/100 hands; reach is per dealt hand.
|
||||
|
||||
Lines from the tree: raises before the node r (0 open / limp, 1 vs_open, 2 vs_3bet, 3 vs_4bet, >= 4 vs_5bet_plus); with r = 0 the big blind (seat 1)
|
||||
facing limps is bb_option, the small blind facing limps vs_limp, a later seat with limps before it iso_limp, else open.
|
||||
"""
|
||||
import collections
|
||||
import math
|
||||
import os
|
||||
import sys
|
||||
|
||||
import fortsetzung as F
|
||||
|
||||
LINIE_R = {1: "vs_open", 2: "vs_3bet", 3: "vs_4bet"}
|
||||
|
||||
|
||||
def linie(sitz, r, limps):
|
||||
if r == 0:
|
||||
if sitz == 1:
|
||||
return "bb_option" if limps > 0 else "bb_ohne_limp"
|
||||
if limps > 0:
|
||||
return "vs_limp" if sitz == 0 else "iso_limp"
|
||||
return "open"
|
||||
return LINIE_R.get(r, "vs_5bet_plus")
|
||||
|
||||
|
||||
def karte(zeilen, haende_je=1.0):
|
||||
"""zeilen: dicts with sitz, raises, limps, erreich, R, R_oben -> {line: (bb/100 point, bb/100 upper, nodes)}."""
|
||||
s = collections.defaultdict(lambda: [0.0, 0.0, 0])
|
||||
for z in zeilen:
|
||||
l = linie(int(z["sitz"]), int(z["raises"]), int(z["limps"]))
|
||||
e = float(z["erreich"]) * haende_je
|
||||
s[l][0] += 100.0 * e * float(z["R"])
|
||||
s[l][1] += 100.0 * e * float(z["R_oben"])
|
||||
s[l][2] += 1
|
||||
return {k: tuple(v) for k, v in s.items()}
|
||||
|
||||
|
||||
def gilt(g_untere, verschiebung_fehler):
|
||||
"""The rule: lower bound of G minus the error budget sum |d| E must stay above 0."""
|
||||
return g_untere - verschiebung_fehler > 0.0
|
||||
|
||||
|
||||
def tsv(pfad):
|
||||
with open(pfad) as f:
|
||||
kopf = f.readline().rstrip("\n").split("\t")
|
||||
return [dict(zip(kopf, z.rstrip("\n").split("\t"))) for z in f if z.strip()]
|
||||
|
||||
|
||||
def schreibe(ziel, text):
|
||||
if os.path.exists(ziel):
|
||||
raise SystemExit("%s exists; never written over" % ziel)
|
||||
with open(ziel + ".tmp", "w") as f:
|
||||
f.write(text)
|
||||
os.replace(ziel + ".tmp", ziel)
|
||||
|
||||
|
||||
def zeilen_cmd(roh, liste, ziel):
|
||||
_, menue = F.lies_knoten(liste)
|
||||
out = []
|
||||
for z in F.behaltene(roh):
|
||||
key = int(z["key"], 16)
|
||||
for i, a in enumerate(z["namen"]):
|
||||
if abs(z["pi_neu"][a] - z["pi_alt"][a]) > 1e-12 and menue[key][i] != 0:
|
||||
out.append("%016x %d %d" % (key, z["klasse"], menue[key][i]))
|
||||
schreibe(ziel, "\n".join(sorted(set(out))) + "\n")
|
||||
print("%d (key, class, move) to bound -> %s" % (len(set(out)), ziel))
|
||||
|
||||
|
||||
def regel_cmd(roh, liste, fehler, ziel):
|
||||
_, menue = F.lies_knoten(liste)
|
||||
e = {(int(z["schluessel"], 16), int(z["klasse"]), int(z["zug"])): float(z["E"]) for z in tsv(fehler)}
|
||||
zeilen = F.behaltene(roh)
|
||||
kopf = ["linie", "position", "knoten", "klasse", "richtung", "alt", "neu", "g_b", "g_b_untere", "fehler", "g_b_untere_minus_fehler", "gilt"]
|
||||
out, n = ["\t".join(kopf)], 0
|
||||
seen = set()
|
||||
for z in zeilen:
|
||||
key = int(z["key"], 16)
|
||||
g, se, lo = F.gewinn_b(z)
|
||||
budget = 0.0
|
||||
for i, a in enumerate(z["namen"]):
|
||||
d = abs(z["pi_neu"][a] - z["pi_alt"][a])
|
||||
if d > 1e-12 and menue[key][i] != 0:
|
||||
budget += d * e[(key, z["klasse"], menue[key][i])]
|
||||
ok = gilt(lo, budget)
|
||||
n += ok
|
||||
out.append("\t".join([z["linie"], z["pos"], z["key"], z["klasse_name"], z["richtung"], F.A.mischung_text(z["pi_alt"]), F.A.mischung_text(z["pi_neu"]),
|
||||
"%.4f" % g, "%.4f" % lo, "%.4f" % budget, "%.4f" % (lo - budget), "ja" if ok else "nein"]))
|
||||
seen.add((z["key"], z["klasse"]))
|
||||
schreibe(ziel, "\n".join(out) + "\n")
|
||||
print("%d rows, %d pass (G seed B lower bound minus continuation error > 0) -> %s" % (len(zeilen), n, ziel))
|
||||
|
||||
|
||||
def karte_cmd(knoten, haende_je=1.0):
|
||||
k = karte(tsv(knoten), haende_je)
|
||||
gesamt = [sum(v[0] for v in k.values()), sum(v[1] for v in k.values())]
|
||||
print("| Linie | Knoten | Regret x Erreichbarkeit bb/100 | obere Grenze bb/100 |\n|---|---|---|---|")
|
||||
for l, (p, o, nk) in sorted(k.items(), key=lambda kv: -kv[1][0]):
|
||||
print("| %s | %d | %.3f | %.3f |" % (l, nk, p, o))
|
||||
print("| zusammen | %d | %.3f | %.3f |" % (sum(v[2] for v in k.values()), gesamt[0], gesamt[1]))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
c, rest = sys.argv[1], sys.argv[2:]
|
||||
if c == "zeilen":
|
||||
zeilen_cmd(*rest)
|
||||
elif c == "regel":
|
||||
regel_cmd(*rest)
|
||||
elif c == "karte":
|
||||
karte_cmd(rest[0], float(rest[1].split("=", 1)[1]) if len(rest) > 1 else 1.0)
|
||||
else:
|
||||
raise SystemExit(__doc__)
|
||||
@@ -76,6 +76,12 @@ def fortsetzung_ok(sc, baum, weg, code, cache):
|
||||
return cache[praefix]
|
||||
|
||||
|
||||
def behaltene(roh_pfad):
|
||||
"""Faithful rows whose BR rule holds in the check seed (the 94 of the strict reading)."""
|
||||
zeilen = A.entscheidungszeilen(S.archiv_zeilen(roh_pfad), {z["i"]: z for z in V.lies(os.path.join(os.path.dirname(roh_pfad), "entscheidungen.jsonl"))})
|
||||
return [z for z in zeilen if z["treu"] and (not z["br"] or z["br"]["gilt"])]
|
||||
|
||||
|
||||
def gewinn_b(x):
|
||||
n = x["namen"]
|
||||
d = {a: x["pi_neu"][a] - x["pi_alt"][a] for a in n}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
"""Spec of the error-bound rule and the inconsistency map."""
|
||||
import unittest
|
||||
|
||||
import fehler_schranke as FS
|
||||
|
||||
|
||||
class Spec(unittest.TestCase):
|
||||
def test_change_counts_only_if_lower_bound_survives_the_error_budget(self):
|
||||
self.assertTrue(FS.gilt(0.5, 0.2))
|
||||
self.assertFalse(FS.gilt(0.5, 0.5))
|
||||
self.assertFalse(FS.gilt(-0.1, 0.0))
|
||||
|
||||
def test_lines_from_raises_limps_and_seat(self):
|
||||
self.assertEqual(FS.linie(3, 0, 0), "open")
|
||||
self.assertEqual(FS.linie(4, 0, 1), "iso_limp")
|
||||
self.assertEqual(FS.linie(0, 0, 2), "vs_limp")
|
||||
self.assertEqual(FS.linie(1, 0, 1), "bb_option")
|
||||
self.assertEqual(FS.linie(1, 1, 0), "vs_open")
|
||||
self.assertEqual(FS.linie(2, 2, 0), "vs_3bet")
|
||||
self.assertEqual(FS.linie(5, 4, 0), "vs_5bet_plus")
|
||||
|
||||
def test_map_is_reach_times_regret_per_100_hands_by_line(self):
|
||||
z = [{"sitz": "3", "raises": "0", "limps": "0", "erreich": "0.5", "R": "0.02", "R_oben": "0.05"},
|
||||
{"sitz": "2", "raises": "0", "limps": "0", "erreich": "0.25", "R": "0.04", "R_oben": "0.04"},
|
||||
{"sitz": "1", "raises": "1", "limps": "0", "erreich": "0.1", "R": "1.0", "R_oben": "2.0"}]
|
||||
k = FS.karte(z)
|
||||
self.assertAlmostEqual(k["open"][0], 100 * (0.5 * 0.02 + 0.25 * 0.04))
|
||||
self.assertAlmostEqual(k["open"][1], 100 * (0.5 * 0.05 + 0.25 * 0.04))
|
||||
self.assertEqual(k["open"][2], 2)
|
||||
self.assertAlmostEqual(k["vs_open"][0], 10.0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user