Repository navigation
Expand file tree
/
Copy pathelo.py
More file actions
236 lines (185 loc) · 7.98 KB
/
Copy pathelo.py
File metadata and controls
236 lines (185 loc) · 7.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
"""
ELO rating system for hexgo agents.
- K=32, standard chess formula
- Agents registered by name+config string
- Results persisted to elo.json after every game
- run_match() plays N games between two agents, alternating colour
"""
import json
import math
import random
import time
from pathlib import Path
from typing import Protocol
from game import HexGame
ELO_FILE = Path(__file__).parent / "elo.json"
K = 32
DEFAULT_RATING = 1200.0
# ── Agent protocol ────────────────────────────────────────────────────────────
class Agent(Protocol):
name: str
def choose_move(self, game: HexGame) -> tuple[int, int]: ...
# ── Concrete agents ───────────────────────────────────────────────────────────
class RandomAgent:
def __init__(self):
self.name = "random"
def choose_move(self, game: HexGame) -> tuple[int, int]:
return random.choice(game.legal_moves())
class EisensteinGreedyAgent:
"""
Algorithmic agent grounded in the Eisenstein integer (Z[omega]) ontology.
Scores each candidate move by the maximum chain it would create or block
along any of the three unit-direction axes of Z[omega]:
u1 = (1,0) q-axis
u2 = (0,1) r-axis
u3 = (1,-1) diagonal axis
No lookahead, no learned weights — pure greedy chain extension.
Used as a curriculum adversary (forces the net to beat structured play)
and as a permanent ELO baseline to measure progress against a known ontology.
defensive=False : maximise own chain length only
defensive=True : max(own extension, blocking opponent's best chain)
"""
_AXES = ((1, 0), (0, 1), (1, -1))
def __init__(self, name: str = "eisenstein_greedy", defensive: bool = False):
self.name = name
self.defensive = defensive
def choose_move(self, game: HexGame) -> tuple[int, int]:
player = game.current_player
opponent = 3 - player
best_move, best_score = None, -1
for q, r in game.legal_moves():
own = self._chain_if_placed(game, q, r, player)
block = self._chain_if_placed(game, q, r, opponent) if self.defensive else 0
score = max(own, block)
if score > best_score:
best_score, best_move = score, (q, r)
return best_move or random.choice(game.legal_moves())
def _chain_if_placed(self, game: HexGame, q: int, r: int, player: int) -> int:
"""Longest run player would have along any Z[omega] axis if placed at (q,r)."""
best = 1
for dq, dr in self._AXES:
count = 1
for sign in (1, -1):
nq, nr = q + sign * dq, r + sign * dr
while game.board.get((nq, nr)) == player:
count += 1
nq += sign * dq
nr += sign * dr
best = max(best, count)
return best
class MCTSAgent:
def __init__(self, sims: int = 100):
self.sims = sims
self.name = f"mcts_{sims}"
def choose_move(self, game: HexGame) -> tuple[int, int]:
from mcts import mcts
return mcts(game, self.sims)
class NetAgent:
"""MCTS agent using a trained neural net for policy+value."""
def __init__(self, net, sims: int = 100, name: str = "net"):
self.net = net
self.sims = sims
self.name = name
def choose_move(self, game: HexGame) -> tuple[int, int]:
from mcts import mcts_with_net
return mcts_with_net(game, self.net, self.sims)
# ── ELO tracker ───────────────────────────────────────────────────────────────
class ELO:
def __init__(self):
self.ratings: dict[str, float] = {}
self.history: list[dict] = []
self._load()
def _load(self):
if ELO_FILE.exists():
data = json.loads(ELO_FILE.read_text())
self.ratings = data.get("ratings", {})
self.history = data.get("history", [])
def save(self):
ELO_FILE.write_text(json.dumps(
{"ratings": self.ratings, "history": self.history}, indent=2))
def rating(self, name: str) -> float:
return self.ratings.get(name, DEFAULT_RATING)
def expected(self, a: str, b: str) -> float:
return 1 / (1 + 10 ** ((self.rating(b) - self.rating(a)) / 400))
def update(self, winner: str, loser: str, draw: bool = False):
ra, rb = self.rating(winner), self.rating(loser)
ea = self.expected(winner, loser)
sa = 0.5 if draw else 1.0
self.ratings[winner] = ra + K * (sa - ea)
self.ratings[loser] = rb + K * ((1 - sa) - (1 - ea))
def record(self, a: str, b: str, winner_name: str | None, moves: int, duration: float):
draw = winner_name is None
if not draw:
loser = b if winner_name == a else a
self.update(winner_name, loser, draw=False)
else:
self.update(a, b, draw=True)
self.history.append({
"a": a, "b": b, "winner": winner_name,
"moves": moves, "duration": round(duration, 2),
"ratings": {a: round(self.rating(a), 1), b: round(self.rating(b), 1)},
})
self.save()
def leaderboard(self) -> list[tuple[str, float]]:
return sorted(self.ratings.items(), key=lambda x: -x[1])
# ── Match runner ──────────────────────────────────────────────────────────────
def play_game(agent1: Agent, agent2: Agent, max_moves: int = 300) -> dict:
"""
Play one game. agent1=player1(X), agent2=player2(O).
Returns {winner_name, moves, duration}.
"""
game = HexGame()
agents = {1: agent1, 2: agent2}
t0 = time.perf_counter()
move_count = 0
while game.winner is None and move_count < max_moves:
legal = game.legal_moves()
if not legal:
break
agent = agents[game.current_player]
move = agent.choose_move(game)
game.make(*move)
move_count += 1
dur = time.perf_counter() - t0
winner_name = None
if game.winner == 1:
winner_name = agent1.name
elif game.winner == 2:
winner_name = agent2.name
return {"winner_name": winner_name, "moves": move_count, "duration": dur}
def run_match(agent_a: Agent, agent_b: Agent, n_games: int = 10,
elo: ELO | None = None, verbose: bool = True) -> dict:
"""
Play n_games between agent_a and agent_b, alternating colours.
Returns summary {wins_a, wins_b, draws, ratings}.
"""
if elo is None:
elo = ELO()
wins = {agent_a.name: 0, agent_b.name: 0, None: 0}
for i in range(n_games):
# Alternate who plays X
if i % 2 == 0:
p1, p2 = agent_a, agent_b
else:
p1, p2 = agent_b, agent_a
result = play_game(p1, p2)
wn = result["winner_name"]
wins[wn] += 1
elo.record(agent_a.name, agent_b.name, wn, result["moves"], result["duration"])
if verbose:
print(f" G{i+1:02d} winner={wn or 'draw':12s} "
f"moves={result['moves']:3d} {result['duration']:.1f}s "
f"ELO {agent_a.name}={elo.rating(agent_a.name):.0f} "
f"{agent_b.name}={elo.rating(agent_b.name):.0f}")
return {
f"wins_{agent_a.name}": wins[agent_a.name],
f"wins_{agent_b.name}": wins[agent_b.name],
"draws": wins[None],
"ratings": elo.leaderboard(),
}
if __name__ == "__main__":
print("Running: random vs mcts_50 (4 games)\n")
result = run_match(RandomAgent(), MCTSAgent(50), n_games=4)
print("\nLeaderboard:")
for name, r in result["ratings"]:
print(f" {name:20s} {r:.0f}")