Files

119 lines
4.5 KiB
Python

#!/usr/bin/env python3
"""Wer hat postflop entschieden? Anteil Solver-Kern gegen Faustregel je Lauf.
Liest die echten Züge aus `betrieb/scratch/shadow*.jsonl`. Ein Zug zählt als Solver, wenn sein Grund
mit `solver-kern` beginnt, sonst als Faustregel. Gezählt je Straße und heads-up/multiway, dazu die
häufigsten Faustregel-Gründe (erste Wörter), damit jede Lücke einen Namen hat.
Ziel: Faustregel-Anteil 0. Jeder Lauf mit Faustregel-Zügen schreibt eine Zeile nach
archiv/audit/anteil-warnung.log.
python3 lesen/kern_anteil.py [--pid N]
python3 lesen/kern_anteil.py --nach-lauf --pid N [--wurzel BAUM]
-> archiv/audit/anteil-<pid>.md
Nur Aggregate, keine Namen.
"""
import argparse
import collections
import glob
import json
import os
import sys
def protokolle(wurzel):
return sorted(glob.glob(os.path.join(wurzel, "betrieb", "scratch", "shadow*.jsonl")))
def grundklasse(grund):
"""Kurzname einer Faustregel: der Text vor dem ersten Doppelpunkt, gekürzt."""
g = str(grund or "").split(":")[0].strip()
return (g[:60] or "(ohne Grund)")
def zug(d, pid=None):
"""-> (strasse, 'hu'|'mw', solver: bool, grund) oder None."""
if not isinstance(d, dict) or (pid is not None and d.get("pid") != pid):
return None
q, a = d.get("anfrage"), d.get("antwort")
if not isinstance(q, dict) or not isinstance(a, dict):
return None
strasse = str(q.get("street") or "").upper()
if strasse in ("", "PREFLOP"):
return None
gegner = [v for v in (q.get("villains") or []) if isinstance(v, dict) and not v.get("folded")]
grund = str(a.get("reason") or "")
return strasse, ("mw" if len(gegner) > 1 else "hu"), grund.startswith("solver-kern"), grund
def zuege(pfade, pid=None):
aus = []
for pfad in pfade:
try:
datei = open(pfad, encoding="utf-8", errors="replace")
except OSError:
continue
with datei:
for zeile in datei:
if '"antwort"' not in zeile:
continue
try:
z = zug(json.loads(zeile), pid)
except ValueError:
continue
if z is not None:
aus.append(z)
return aus
def bericht(zs, titel):
zeilen = [f"# {titel}", "", "Postflop-Entscheidungen: Solver-Kern gegen Faustregel. Ziel: Faustregel 0.", "",
"| Straße | Lage | Züge | Faustregel | Anteil |", "|---|---|---:|---:|---:|"]
zahl = collections.Counter((s, l) for s, l, _, _ in zs)
faust = collections.Counter((s, l) for s, l, solver, _ in zs if not solver)
for s in ("FLOP", "TURN", "RIVER"):
for lage in ("hu", "mw"):
n = zahl[(s, lage)]
if n:
zeilen.append(f"| {s} | {lage} | {n} | {faust[(s, lage)]} | {100 * faust[(s, lage)] / n:.0f} % |")
n, f = len(zs), sum(faust.values())
zeilen.append(f"| alle | | {n} | {f} | {100 * f / n if n else 0:.0f} % |")
gruende = collections.Counter(grundklasse(g) for _, _, solver, g in zs if not solver)
if gruende:
zeilen += ["", "Häufigste Faustregel-Gründe:", ""]
zeilen += [f"- {k} ({v})" for k, v in gruende.most_common(10)]
return "\n".join(zeilen) + "\n", {"zuege": n, "faustregel": f}
def nach_lauf(pid, wurzel):
ordner = os.path.join(wurzel, "archiv", "audit")
os.makedirs(ordner, exist_ok=True)
text, z = bericht(zuege(protokolle(wurzel), pid), f"Kern-Anteil Lauf {pid}")
with open(os.path.join(ordner, f"anteil-{pid}.md"), "w", encoding="utf-8", newline="\n") as f:
f.write(text)
if z["faustregel"]:
with open(os.path.join(ordner, "anteil-warnung.log"), "a", encoding="utf-8", newline="\n") as f:
f.write(f"Lauf {pid}: Faustregel {z['faustregel']} von {z['zuege']} Postflop-Zügen "
f"({100 * z['faustregel'] / z['zuege']:.0f} %)\n")
return z
def main(argv=None):
p = argparse.ArgumentParser(description="Anteil Solver-Kern gegen Faustregel")
p.add_argument("--nach-lauf", action="store_true")
p.add_argument("--pid", type=int)
p.add_argument("--wurzel", default=os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
a = p.parse_args(argv)
if a.nach_lauf:
if a.pid is None:
p.error("--nach-lauf braucht --pid")
nach_lauf(a.pid, a.wurzel)
return 0
text, _ = bericht(zuege(protokolle(a.wurzel), a.pid), "Kern-Anteil")
sys.stdout.write(text)
return 0
if __name__ == "__main__":
sys.exit(main())