diff --git a/messung/farm/nacht-0112/werkzeuge/farm_splash_wuerfel.py b/messung/farm/nacht-0112/werkzeuge/farm_splash_wuerfel.py new file mode 100644 index 00000000..38859ba2 --- /dev/null +++ b/messung/farm/nacht-0112/werkzeuge/farm_splash_wuerfel.py @@ -0,0 +1,97 @@ +# Wuerfel-Test Splash (03.12.): echte Splash-Vorflop-Lagen direkt an den Splash-Blueprintdienst (127.0.0.1:47135): seine Mischung ({"op":"probs"}) +# gegen die Haeufigkeit gezogener Zuege ({"op":"act"} mit je K verschiedenen 32-Bit-Saaten). Dazu dieselbe Lage ohne Splash (dead_money weg): +# zeigt, ob das tote Geld ueberhaupt ankommt (Mischung darf sich aendern, die Tabelle ist dieselbe - Vorflop also meist gleich). +# Anfrage wie die Bruecke (Kontext::aus fuer Vorflop): BB = 100 Chips, Sitze 0..n-1 in Tischreihenfolge, Splash -> dead_money. +# python -X utf8 farm_splash_wuerfel.py [--n 40] [--k 60] [--port 47135] +import sys; sys.dont_write_bytecode = True +import argparse, collections, json, random, socket + +R = ["TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING", "ACE"] +F = ["CLUBS", "DIAMONDS", "HEARTS", "SPADES"] + + +def karte(v): + return "23456789TJQKA"[R.index(v["rank"])] + "cdhs"[F.index(v["suit"])] + + +def anfrage(q): + """Vertrag (Vorflop) -> seine ActRequest, wie Kontext::aus; None, wenn die Lage nicht passt.""" + bb = q["bigBlind"]; c = lambda x: round(x / bb * 100) + h = q["history"]; held = q["heroSeat"] + sitze = sorted({v["seat"] for v in q["villains"]} | {held} | {x["seat"] for x in h}) + pos = {s: i for i, s in enumerate(sitze)}; n = len(sitze) + sb = next(pos[x["seat"]] for x in h if x["action"] == "POST_SB") + haus, drin, auf, hist, bb_da = 0, [0] * n, [0] * n, [], False + for x in h: + a, s = x["action"], pos[x["seat"]] + if a == "POST_ANTE": + if x.get("splash"): + haus += c(x["amount"]); continue + return None + if a == "POST_BB" and bb_da: + return None + bb_da |= a == "POST_BB" + typ = {"POST_SB": "post_sb", "POST_BB": "post_bb", "FOLD": "fold", "CHECK": "check", "CALL": "call", "BET": "bet", "RAISE": "raise", "ALL_IN": "all_in"}[a] + e = {"seat": s, "type": typ} + if a in ("POST_SB", "POST_BB", "BET", "RAISE", "ALL_IN", "CALL") and x.get("amount") is not None: + t = c(x["amount"]); drin[s] += t - auf[s]; auf[s] = t + if a != "CALL": + e["to"] = t + hist.append(e) + stand = {pos[held]: c(q["heroStack"])} + for v in q["villains"]: + stand[pos[v["seat"]]] = c(v["stack"]) + spieler = [{"seat": s, "stack_start": stand[s] + drin[s]} for s in range(n)] + r = {"small_blind": 50, "big_blind": 100, "button_seat": sb if n == 2 else (sb + n - 1) % n, "hero_seat": pos[held], + "hole_cards": [karte(k) for k in q["heroCards"]], "board": [], "players": spieler, "history": hist, + "options": {"seed": 1, "include_probs": False}} + if haus: + r["dead_money"] = haus + return r + + +def schluessel(a): + return (a.get("type"), a.get("to")) + + +def main(): + ap = argparse.ArgumentParser(); ap.add_argument("ein"); ap.add_argument("aus"); ap.add_argument("--n", type=int, default=40) + ap.add_argument("--k", type=int, default=60); ap.add_argument("--port", type=int, default=47135); o = ap.parse_args() + s = socket.create_connection(("127.0.0.1", o.port), timeout=60); f = s.makefile("rw", encoding="utf-8") + def frage(v): + f.write(json.dumps(v) + "\n"); f.flush(); return json.loads(f.readline()) + lagen = [json.loads(z) for z in open(o.ein, encoding="utf-8")] + lagen = [x for x in lagen if x.get("art") == "splash" and x["q"]["street"] == "PREFLOP"][: o.n] + rnd = random.Random(20261203); aus = []; tv_summe = 0.0; geaendert = 0 + for x in lagen: + req = anfrage(x["q"]) + if req is None: + continue + pr = frage({"op": "probs", "req": req}) + if not pr.get("ok"): + aus.append({"hand": x["hand"], "fehler": pr}); continue + mix = {(p["type"], p.get("to")): p["p"] for p in pr["probs"]} + zug = collections.Counter() + for _ in range(o.k): + r2 = dict(req, options={"seed": rnd.getrandbits(32), "include_probs": False}) + a = frage({"op": "act", "req": r2}) + zug[schluessel(a["response"]["action"])] += 1 + tv = 0.5 * sum(abs(mix.get(k, 0) - zug.get(k, 0) / o.k) for k in set(mix) | set(zug)) + ohne = dict(req); ohne.pop("dead_money", None) + p0 = frage({"op": "probs", "req": ohne}) + mix0 = {(p["type"], p.get("to")): p["p"] for p in p0.get("probs", [])} + d0 = 0.5 * sum(abs(mix.get(k, 0) - mix0.get(k, 0)) for k in set(mix) | set(mix0)) + geaendert += d0 > 1e-9; tv_summe += tv + aus.append({"hand": x["hand"], "mischung": {str(k): v for k, v in mix.items()}, "gezogen": {str(k): v for k, v in zug.items()}, + "tv_abstand": round(tv, 3), "abstand_ohne_splash": round(d0, 4)}) + gut = [a for a in aus if "tv_abstand" in a] + summe = {"lagen": len(gut), "fehler": len(aus) - len(gut), "ziehungen_je_lage": o.k, + "tv_mittel": round(tv_summe / max(1, len(gut)), 3), "tv_max": max((a["tv_abstand"] for a in gut), default=None), + "tv_erwartet_bei_k": "Zufallsrauschen etwa 0,05-0,10 bei k=60", + "mischung_aendert_sich_mit_splash": geaendert} + json.dump({"summe": summe, "lagen": aus}, open(o.aus, "w", encoding="utf-8"), ensure_ascii=False, indent=1) + print("WUERFEL " + json.dumps(summe, ensure_ascii=False)) + + +if __name__ == "__main__": + main()