trioskosmos commited on
Commit
2e185f9
·
verified ·
1 Parent(s): e95dcfe

Upload ai/utils/monitor_self_play.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. ai/utils/monitor_self_play.py +127 -0
ai/utils/monitor_self_play.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import sys
4
+
5
+ # Add project root to path
6
+ sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
7
+
8
+ import engine_rust
9
+
10
+ from ai.utils.benchmark_decks import parse_deck
11
+
12
+
13
+ def monitor_game():
14
+ db_path = "engine/data/cards_compiled.json"
15
+ with open(db_path, "r", encoding="utf-8") as f:
16
+ db_content = f.read()
17
+ db_json = json.loads(db_content)
18
+ db = engine_rust.PyCardDatabase(db_content)
19
+
20
+ # Use simple starter decks
21
+ p_deck = [124, 127] * 20
22
+ p_lives = [1024, 1025, 1027]
23
+ p_energy = [20000] * 10
24
+
25
+ # Setup Agents
26
+ game = engine_rust.PyGameState(db)
27
+
28
+ # Setup Decks (Need correct structure)
29
+ deck0, lives0, energy0 = parse_deck(
30
+ "ai/decks/aqours_cup.txt", db_json["member_db"], db_json["live_db"], db_json.get("energy_db", {})
31
+ )
32
+ deck1, lives1, energy1 = parse_deck(
33
+ "ai/decks/muse_cup.txt", db_json["member_db"], db_json["live_db"], db_json.get("energy_db", {})
34
+ )
35
+
36
+ game.initialize_game(deck0, deck1, energy0, energy1, lives0, lives1)
37
+
38
+ # Setup Agents
39
+ mcts = engine_rust.PyHybridMCTS("ai/models/alphanet.onnx", 0.3)
40
+
41
+ def decode_action(aid, game_p):
42
+ if aid == 0:
43
+ return "Pass/Confirm"
44
+ if 1 <= aid <= 180:
45
+ rel = aid - 1
46
+ h_idx = rel // 3
47
+ s_idx = rel % 3
48
+ if h_idx < len(game_p.hand):
49
+ cid = game_p.hand[h_idx]
50
+ c_name = db_json["member_db"].get(str(cid), {}).get("name", "Unknown")
51
+ return f"Play {c_name} to Slot {s_idx}"
52
+ if 200 <= aid <= 230:
53
+ rel = aid - 200
54
+ s_idx = rel // 10
55
+ return f"Activate Ability on Slot {s_idx}"
56
+ if 300 <= aid <= 360:
57
+ h_idx = aid - 300
58
+ if h_idx < len(game_p.hand):
59
+ cid = game_p.hand[h_idx]
60
+ c_name = db_json["member_db"].get(str(cid), {}).get("name", "Unknown")
61
+ return f"Mulligan Toggle: {c_name}"
62
+ if 400 <= aid <= 460:
63
+ h_idx = aid - 400
64
+ if h_idx < len(game_p.hand):
65
+ cid = game_p.hand[h_idx]
66
+ c_name = db_json["live_db"].get(str(cid), {}).get("name", "Unknown")
67
+ return f"Set Live: {c_name}"
68
+ return f"Unknown({aid})"
69
+
70
+ print("Starting Monitored Game...")
71
+
72
+ step = 0
73
+ while not game.is_terminal() and step < 400: # High limit for trace
74
+ cp_idx = game.current_player
75
+ p0 = game.get_player(0)
76
+ p1 = game.get_player(1)
77
+ cur_p = game.get_player(cp_idx)
78
+
79
+ print(f"\n[Step {step}] Turn {game.turn} | Player {cp_idx} | Phase {game.phase}")
80
+ print(f" P0 Score: {p0.score} | Hand: {len(p0.hand)} | Lives: {len(p0.success_lives)}")
81
+ print(f" P1 Score: {p1.score} | Hand: {len(p1.hand)} | Lives: {len(p1.success_lives)}")
82
+
83
+ is_interactive = game.phase in [-1, 0, 4, 5]
84
+
85
+ if is_interactive:
86
+ # Show Hand Contents
87
+ print(f" Hand Context ({len(cur_p.hand)} cards):")
88
+ for i, cid in enumerate(cur_p.hand):
89
+ m = db_json["member_db"].get(str(cid))
90
+ l = db_json["live_db"].get(str(cid))
91
+ c_name = m["name"] if m else (l["name"] if l else "Unknown")
92
+ c_type = "Member" if m else ("Live" if l else "Energy/Other")
93
+ print(f" [{i}] {c_name} ({c_type}) [ID: {cid}]")
94
+
95
+ legal_ids = game.get_legal_action_ids()
96
+ legal_desc = [decode_action(aid, cur_p) for aid in legal_ids]
97
+ print(f" Legal Actions ({len(legal_ids)}):")
98
+ for aid, desc in zip(legal_ids, legal_desc):
99
+ print(f" - {aid}: {desc}")
100
+
101
+ # Use Hybrid MCTS
102
+ suggestions = mcts.get_suggestions(game, 1000)
103
+ best_action = int(suggestions[0][0])
104
+
105
+ print(f" [Hybrid] Decision: {best_action} ({decode_action(best_action, cur_p)})")
106
+ print(" Probabilities:")
107
+ for a, s, v in suggestions[:5]:
108
+ print(f" - {a} ({decode_action(a, cur_p)}): Score {s:.3f} | Visits {v}")
109
+
110
+ try:
111
+ game.step(best_action)
112
+ except Exception as e:
113
+ print(f"!!! Error stepping {best_action}: {e}")
114
+ break
115
+ else:
116
+ # Auto-step non-interactive phases
117
+ game.step(0)
118
+
119
+ step += 1
120
+
121
+ print("\n--- MONITOR END ---")
122
+ winner = game.get_winner()
123
+ print(f"Final Result: {'Draw' if winner == 2 else f'Player {winner} Wins'} in {step} steps")
124
+
125
+
126
+ if __name__ == "__main__":
127
+ monitor_game()