"""AB_SPOT (29.11.): A/B an der Entscheidung - Muenze, Unterschied, B-Prozess mit eigener Umgebung, gleicher Seed, Frist, Log. Ersatz-Motor (kein echter Motor, keine Tische): River antwortet er FOLD, mit ERSATZ_B=1 CALL; im Grund steht der Seed. ERSATZ_LANGSAM=1 rechnet 3 s (B kommt zu spaet). Aus (kein AB_SPOT): kein B-Prozess, kein Feld `ab`.""" import json, os, queue, sys, tempfile, threading, time sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "entscheiden")) import abspot as A import schatten as S d = tempfile.mkdtemp(prefix="ab-spot-") ersatz = os.path.join(d, "motor.py") open(ersatz, "w", encoding="utf-8").write( "import sys, json, os, time\n" "for z in sys.stdin:\n" " q = json.loads(z)\n" " if os.environ.get('ERSATZ_PROTOKOLL'):\n" " open(os.environ['ERSATZ_PROTOKOLL'], 'a').write(z)\n" " if os.environ.get('ERSATZ_LANGSAM') == '1':\n" " time.sleep(3)\n" " b = os.environ.get('ERSATZ_B') == '1' and q.get('street') == 'RIVER'\n" " print(json.dumps({'action': 'CALL' if b else 'FOLD', 'size': None, 'confidence': 1.0, 'reason': str(q.get('seed'))}), flush=True)\n") def frage(strasse="RIVER"): return json.dumps({"variant": "NLHE", "street": strasse, "legalActions": ["FOLD", "CALL"], "history": [], "pot": 0.4, "toCall": 0.1, "heroStack": 1.9, "bigBlind": 0.02}) def aufraeumen(): for k in list(os.environ): if k.startswith("AB_SPOT") or k.startswith("ERSATZ"): os.environ.pop(k) # --- reine Teile --------------------------------------------------------------------------------------------------------- aufraeumen() assert A.konfig({}) is None and A.konfig({"AB_SPOT": " "}) is None # aus assert A.konfig({"AB_SPOT": "X", "AB_SPOT_EXE": "Z:/fehlt/nicht.exe"}) is None # B-Exe fehlt: aus assert A.konfig({"AB_SPOT": "X", "AB_SPOT_EXE": __file__})["exe"] == __file__ # eigene Exe fuer B assert A.konfig({"AB_SPOT": "X"})["exe"] is None k = A.konfig({"AB_SPOT": "MOTOR_RNR", "MOTOR_RNR": "1"}) assert k["b"] == {"MOTOR_RNR": "0"} and k["p"] == 0.5 and k["salz"] == "MOTOR_RNR" # Vorgabe: Schalter umgedreht k = A.konfig({"AB_SPOT": "MOTOR_RNR", "AB_SPOT_B": "MOTOR_RNR=1; MOTOR_RNR_MODELL=pool", "AB_SPOT_P": "2"}) assert k["b"] == {"MOTOR_RNR": "1", "MOTOR_RNR_MODELL": "pool"} and k["p"] == 1.0 # Muenze: fest je (Salz, Raum, Hand), ohne Uhr; gleichverteilt assert A.muenze("s", "r", "1") == A.muenze("s", "r", "1") != A.muenze("t", "r", "1") u = [A.muenze("s", "r", str(i)) for i in range(4000)] assert all(0 <= x < 1 for x in u) and abs(sum(x < 0.5 for x in u) / 4000 - 0.5) < 0.03 # Unterschied: Art, oder Betrag um mehr als 10 % assert A.unterschied({"action": "FOLD"}, {"action": "CALL"}) assert not A.unterschied({"action": "BET", "size": 1.0}, {"action": "RAISE", "size": 1.05}) assert A.unterschied({"action": "RAISE", "size": 1.0}, {"action": "RAISE", "size": 1.2}) assert not A.unterschied({"action": "CHECK"}, {"action": "CHECK"}) q = json.loads(frage()) r = A.waehle({"name": "X", "p": 1.0, "salz": "X"}, q, "r", "h", {"action": "FOLD"}, {"action": "CALL"}) assert r["action"] == "CALL" and r["ab"]["arm"] == "B" and r["ab"]["art"] == "spot" and r["ab"]["a"]["action"] == "FOLD" r = A.waehle({"name": "X", "p": 0.0, "salz": "X"}, q, "r", "h", {"action": "FOLD"}, {"action": "CALL"}) assert r["action"] == "FOLD" and r["ab"]["arm"] == "A" r = A.waehle({"name": "X", "p": 1.0, "salz": "X"}, q, "r", "h", {"action": "FOLD"}, None, "B zu spaet") assert r["action"] == "FOLD" and r["ab"]["art"] == "nicht_gezogen" and r["ab"]["warum"] == "B zu spaet" assert r["ab"]["pot"] == 0.4 and r["ab"]["stack"] == 1.9 and r["ab"]["toCall"] == 0.1 # --- aus: wie heute ------------------------------------------------------------------------------------------------------ aufraeumen() log = os.path.join(d, "aus.jsonl") m = S.Schatten([sys.executable, ersatz], ziel=log, spielt=True) assert m.ab is None and m.b_bahnen == () r = m.zug(frage(), raum="r", hand="1", frist=5) assert r["action"] == "FOLD" and "ab" not in r, r time.sleep(0.2) zeilen = [json.loads(l) for l in open(log, encoding="utf-8")] assert all(z.get("gespielt") != "ab_b" for z in zeilen) # --- an: zwei Arme, gleicher Seed, Muenze je Hand ------------------------------------------------------------------------ aufraeumen() os.environ.update({"AB_SPOT": "ERSATZ_B", "AB_SPOT_B": "ERSATZ_B=1", "AB_SPOT_SALZ": "probe"}) log = os.path.join(d, "an.jsonl") m = S.Schatten([sys.executable, ersatz], ziel=log, spielt=True) assert len(m.b_bahnen) == 2 arme = {} for i in range(12): hand = str(1000 + i) r = m.zug(frage(), raum="r", hand=hand, frist=5) ab = r["ab"] assert ab["art"] == "spot" and ab["schalter"] == "ERSATZ_B", ab assert ab["a"]["action"] == "FOLD" and ab["b"]["action"] == "CALL" assert ab["a"]["reason"] == ab["b"]["reason"], ab # derselbe Seed in beiden Armen soll = "B" if A.muenze("probe", "r", hand) < 0.5 else "A" assert ab["arm"] == soll and r["action"] == ("CALL" if soll == "B" else "FOLD"), (ab, r) arme[soll] = arme.get(soll, 0) + 1 r2 = m.zug(frage(), raum="r", hand=hand, frist=5) # zweiter Spot derselben Hand: derselbe Arm assert r2["ab"]["arm"] == soll assert set(arme) == {"A", "B"}, arme r = m.zug(frage("FLOP"), raum="r", hand="1", frist=5) # beide gleich: A, keine Muenze assert r["ab"]["art"] == "gleich" and r["ab"]["arm"] is None and r["action"] == "FOLD" time.sleep(0.3) zeilen = [json.loads(l) for l in open(log, encoding="utf-8")] b = [z for z in zeilen if z.get("gespielt") == "ab_b"] assert b and all("q" not in z and z.get("bahn") == "ab_b" for z in b) # B-Zeilen ohne doppelte Anfrage assert any(z.get("gespielt") == "motor" and "q" in z for z in zeilen) # --- B zu spaet: A spielt, nicht gezogen, Frist haelt --------------------------------------------------------------------- aufraeumen() os.environ.update({"AB_SPOT": "ERSATZ_B", "AB_SPOT_B": "ERSATZ_B=1;ERSATZ_LANGSAM=1"}) m = S.Schatten([sys.executable, ersatz], ziel=os.path.join(d, "spaet.jsonl"), spielt=True) m.zug(frage("FLOP"), raum="r", hand="w", frist=5) # Prozesse warm t = time.time() r = m.zug(frage(), raum="r", hand="x", frist=1.0) assert time.time() - t < 1.5, time.time() - t assert r["action"] == "FOLD" and r["ab"]["art"] == "nicht_gezogen" and r["ab"]["warum"] == "B zu spaet", r aufraeumen() # --- AB_SPOT_VERFALL: eine abgelaufene B-Anfrage geht nicht mehr an den Motor ------------------------------------------------ aufraeumen() assert A.konfig({"AB_SPOT": "X"})["verfall"] is False # Vorgabe aus assert A.konfig({"AB_SPOT": "X", "AB_SPOT_VERFALL": "0"})["verfall"] is False assert A.konfig({"AB_SPOT": "X", "AB_SPOT_VERFALL": "1"})["verfall"] is True def b_anfrage(m, bis_in, hand="v", bahn=0): """Eine B-Anfrage direkt in die B-Bahn; bis_in < 0: die Frist ist schon vorbei.""" zb = {"da": threading.Event(), "ab": False, "bis": time.time() + bis_in, "seed": 7} m.b_bahnen[bahn].put_nowait((time.time() - 1.0, json.loads(frage()), None, "r", hand, zb)) zb["da"].wait(5) return zb def zeilen_von(pfad): time.sleep(0.2) return [json.loads(l) for l in open(pfad, encoding="utf-8")] for verfall in ("0", "1"): aufraeumen() os.environ.update({"AB_SPOT": "ERSATZ_B", "AB_SPOT_B": "ERSATZ_B=1", "AB_SPOT_VERFALL": verfall}) log = os.path.join(d, "verfall%s.jsonl" % verfall) os.environ["ERSATZ_PROTOKOLL"] = prot = os.path.join(d, "verfall%s.motor" % verfall) m = S.Schatten([sys.executable, ersatz], ziel=log, spielt=True) assert m.ab["verfall"] is (verfall == "1") zb = b_anfrage(m, -1.0) # abgelaufen z = [x for x in zeilen_von(log) if x.get("bahn") == "ab_b"] if verfall == "0": # aus: wie heute, an den Motor assert zb["r"]["action"] == "CALL" and zb["grund"] is None, zb assert len(z) == 1 and "motor" in z[0] and "verfallen" not in z[0], z assert getattr(m, "b_verfallen", 0) == 0 else: # an: nicht gesendet, Grund, Zaehler, eine Zeile assert "r" not in zb and zb["grund"] == "B zu spaet" and zb["da"].is_set(), zb assert m.b_verfallen == 1 assert len(z) == 1 and z[0]["verfallen"] is True and "motor" not in z[0], z assert z[0]["raum"] == "r" and z[0]["hand"] == "v" and z[0]["wartet_s"] >= 1.0 and "zeit" in z[0], z zb = b_anfrage(m, 5.0, "w") # nicht abgelaufen: wie heute assert zb["r"]["action"] == "CALL" and zb["grund"] is None and m.b_verfallen == 1, zb m.b_bahnen[0].put_nowait((time.time() - 9, {"_hand": {"hand": "h1"}, "_schreibe": 0}, None, "", "", None)) zb = b_anfrage(m, -1.0, "x") assert m.b_verfallen == 2 and "r" not in zb assert '"_hand"' in open(prot, encoding="utf-8").read() # die Buchung kam beim Motor an # B-Bahn 2 (Turn/River) genauso, A-Bahn unberuehrt: eine abgelaufene A-Frage wird weiter beantwortet zb = b_anfrage(m, -1.0, "y", bahn=1) assert zb["grund"] == "B zu spaet" and m.b_verfallen == 3 r = m.zug(frage(), raum="r", hand="z", frist=5) assert r["action"] in ("FOLD", "CALL") and r["ab"]["art"] in ("spot", "gleich"), r aufraeumen() # --- AB_SPOT_FLOP_TIEF: der Flop in die zweite B-Bahn, Preflop allein in der ersten --------------------------------------------- aufraeumen() assert A.konfig({"AB_SPOT": "X"})["flop_tief"] is False # Vorgabe aus assert A.konfig({"AB_SPOT": "X", "AB_SPOT_FLOP_TIEF": "0"})["flop_tief"] is False assert A.konfig({"AB_SPOT": "X", "AB_SPOT_FLOP_TIEF": "1"})["flop_tief"] is True def bahn_von(tief, strasse): """In welche B-Bahn (0/1) geht eine Anfrage der Strasse? Leere Ersatz-Bahnen, damit kein Arbeiter sie leert.""" aufraeumen() os.environ.update({"AB_SPOT": "ERSATZ_B", "AB_SPOT_B": "ERSATZ_B=1"}) if tief: os.environ["AB_SPOT_FLOP_TIEF"] = "1" m = S.Schatten([sys.executable, ersatz], ziel=os.path.join(d, "tief.jsonl"), spielt=True) m.b_bahnen = (queue.Queue(1), queue.Queue(1)) zb = m._b_anstossen(json.loads(frage(strasse)), "r", "h", {"bis": time.time() + 5, "seed": 1}) return [i for i in (0, 1) if m.b_bahnen[i].qsize() == 1], zb for tief, soll in ((False, {"PREFLOP": 0, "FLOP": 0, "TURN": 1, "RIVER": 1}), (True, {"PREFLOP": 0, "FLOP": 1, "TURN": 1, "RIVER": 1})): for strasse, bahn in soll.items(): assert bahn_von(tief, strasse)[0] == [bahn], (tief, strasse) m = S.Schatten([sys.executable, ersatz], ziel=os.path.join(d, "tief.jsonl"), spielt=True) # B-Bahn voll: wie heute m.b_bahnen = (queue.Queue(1), queue.Queue(1)) m._b_anstossen(json.loads(frage("FLOP")), "r", "h", {"bis": time.time() + 5, "seed": 1}) zb = m._b_anstossen(json.loads(frage("FLOP")), "r", "h", {"bis": time.time() + 5, "seed": 1}) assert zb["grund"] == "B-Bahn voll" and zb["da"].is_set() # --- AB_SPOT_FRUEH: Abbild bei jeder neuen Strassenkarte als Vorsuche in die B-Bahn der Strasse -------------------------------- aufraeumen() assert A.konfig({"AB_SPOT": "X"})["frueh"] is False # Vorgabe aus assert A.konfig({"AB_SPOT": "X", "AB_SPOT_FRUEH": "0"})["frueh"] is False assert A.konfig({"AB_SPOT": "X", "AB_SPOT_FRUEH": "1"})["frueh"] is True def frueh_motor(frueh, tief=True, log="frueh.jsonl"): """Motor mit B-Bahnen und A-Bahnen, die niemand leert (die Abbilder bleiben dort sichtbar).""" aufraeumen() os.environ.update({"AB_SPOT": "ERSATZ_B", "AB_SPOT_B": "ERSATZ_B=1"}) if frueh is not None: os.environ["AB_SPOT_FRUEH"] = frueh if tief: os.environ["AB_SPOT_FLOP_TIEF"] = "1" m = S.Schatten([sys.executable, ersatz], ziel=os.path.join(d, log), spielt=True) m.b_bahnen = (queue.Queue(4), queue.Queue(4)) m.schlange, m.tief = queue.Queue(4), queue.Queue(4) return m def bahnen(m): return [m.b_bahnen[0].qsize(), m.b_bahnen[1].qsize(), m.schlange.qsize(), m.tief.qsize()] # aus: nichts geht an B, auch nicht an A (Standard) m = frueh_motor(None) assert m.ab["frueh"] is False and S.ab_frueh_an() is False for st in ("FLOP", "TURN", "RIVER"): m.ab_frueh(frage(st), "r", "h1") assert bahnen(m) == [0, 0, 0, 0] and m.frueh_wartend == 0 # an: je Strassenkarte genau ein Abbild in die Bahn, in die die echte Anfrage der Strasse spaeter geht for st in ("FLOP", "TURN", "RIVER"): m = frueh_motor("1") m.befehl = [sys.executable, ersatz, "dienst", d] # wie der echte Befehl: [3] ist der Strategie-Ordner (zustaendig liest ihn) S._schatten = m try: assert S.ab_frueh_an() is True S.ab_frueh(frage(st), "r", "h1") finally: S._schatten = None assert bahnen(m) == [0, 1, 0, 0], (st, bahnen(m)) # zweite B-Bahn; A-Bahnen unberuehrt assert m.frueh_wartend == 0 # A zaehlt nur seine eigenen Vorsuchen t, q, kern, raum, hand, z = m.b_bahnen[1].get_nowait() assert q["street"] == st and raum == "r" and hand == "h1" and kern is None assert z["frueh"] is True and z["bis"] > time.time() + 10 and not z["ab"] and not z["da"].is_set(), z # ohne AB_SPOT_FLOP_TIEF liefe der Flop in der Bahn mit Preflop: dort nie ein Vorabbild (es hielte die Preflop-Zuege auf) m = frueh_motor("1", tief=False) m.ab_frueh(frage("FLOP"), "r", "h1") assert bahnen(m) == [0, 0, 0, 0] and m.b_frueh["ohne_bahn"] == 1 m.ab_frueh(frage("TURN"), "r", "h1") assert bahnen(m) == [0, 1, 0, 0] # Preflop und fremde Lagen bekommen nie ein Abbild m = frueh_motor("1") m.ab_frueh(frage("PREFLOP"), "r", "h1") m.ab_frueh(json.dumps(dict(json.loads(frage("TURN")), variant="PLO")), "r", "h1") m.ab_frueh("kein json", "r", "h1") assert bahnen(m) == [0, 0, 0, 0] # Bahn nicht leer: das Abbild entfaellt und wird gezaehlt, die wartende echte Anfrage bleibt allein m = frueh_motor("1") echt = (time.time(), json.loads(frage("TURN")), None, "r", "h0", {"da": threading.Event(), "ab": False, "bis": time.time() + 5}) m.b_bahnen[1].put_nowait(echt) m.ab_frueh(frage("TURN"), "r", "h1") assert bahnen(m) == [0, 1, 0, 0] and m.b_bahnen[1].get_nowait() is echt assert m.b_frueh["besetzt"] == 1 and m.b_frueh["gelegt"] == 0 # der Motor spielt nicht: nichts m = frueh_motor("1") m.spielt = False m.ab_frueh(frage("TURN"), "r", "h1") assert bahnen(m) == [0, 0, 0, 0] # Arbeiter: Abbild ohne Zug im Protokoll, eine kompakte Zeile; ein Abbild einer vorbeigegangenen Strasse wird nicht gerechnet aufraeumen() os.environ.update({"AB_SPOT": "ERSATZ_B", "AB_SPOT_B": "ERSATZ_B=1", "AB_SPOT_FRUEH": "1", "AB_SPOT_FLOP_TIEF": "1"}) os.environ["ERSATZ_PROTOKOLL"] = prot = os.path.join(d, "frueh.motor") log = os.path.join(d, "frueh-arbeiter.jsonl") m = S.Schatten([sys.executable, ersatz], ziel=log, spielt=True) m.ab_frueh(frage("FLOP"), "r", "h1") # gilt: Stand des Tisches ist (h1, FLOP) warte_bis = time.time() + 10 while time.time() < warte_bis and not [x for x in zeilen_von(log) if x.get("frueh")]: time.sleep(0.1) z = [x for x in zeilen_von(log) if x.get("frueh")] assert len(z) == 1 and z[0]["bahn"] == "ab_b" and z[0]["raum"] == "r" and z[0]["hand"] == "h1" and z[0]["strasse"] == "FLOP", z assert isinstance(z[0]["rechnet_s"], float) and z[0]["wartet_s"] >= 0 and 0 < len(z[0]["grund"]) <= 80 and "gespielt" not in z[0], z gesendet = [json.loads(l) for l in open(prot, encoding="utf-8")] assert len(gesendet) == 1 and gesendet[0]["suche_frueh"] is True and gesendet[0]["street"] == "FLOP" # die Strasse ist vorbei (der Tisch steht schon am Turn): nicht rechnen, Zeile mit Grund m.b_stand["r"] = ("h1", "TURN", False) m.b_bahnen[1].put_nowait((time.time(), json.loads(frage("FLOP")), None, "r", "h1", {"da": threading.Event(), "ab": False, "bis": time.time() + 60, "frueh": True})) # die echte Anfrage der Strasse ist schon da: sie rechnet selbst, das Abbild haelt sie nicht auf m.b_stand["r"] = ("h1", "TURN", True) m.b_bahnen[1].put_nowait((time.time(), json.loads(frage("TURN")), None, "r", "h1", {"da": threading.Event(), "ab": False, "bis": time.time() + 60, "frueh": True})) time.sleep(1.0) z = [x for x in zeilen_von(log) if x.get("frueh")] assert len(z) == 3 and [bool(x.get("verworfen")) for x in z] == [False, True, True], z assert m.b_frueh["alt"] == 2 assert len([json.loads(l) for l in open(prot, encoding="utf-8")]) == 1 # nur das erste Abbild kam beim Motor an # eine echte Anfrage setzt den Stand und gibt wie immer einen Zug zurueck (kein Feld frueh, "ab" im Ergebnis) r = m.zug(frage("FLOP"), raum="r", hand="h2", frist=5) assert r["action"] == "FOLD" and r["ab"]["art"] in ("gleich", "spot") and m.b_stand["r"] == ("h2", "FLOP", True), (r, m.b_stand) aufraeumen() print("ab_spot_spec: gruen")