|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | + |
| 5 | +import numpy as np |
| 6 | + |
| 7 | +from game.actions import ACTION_SPACE |
| 8 | +from training.config_runtime import cfg_bool, cfg_int |
| 9 | +from training.selfplay_runtime import compute_action_probs, heuristic_move |
| 10 | + |
| 11 | +if TYPE_CHECKING: |
| 12 | + from engine.mcts import MCTS |
| 13 | + from model.system import AtaxxZero |
| 14 | + |
| 15 | + |
| 16 | +def _play_eval_episode( |
| 17 | + mcts: MCTS, |
| 18 | + rng: np.random.Generator, |
| 19 | + heuristic_level: str, |
| 20 | +) -> int: |
| 21 | + from game.board import AtaxxBoard |
| 22 | + |
| 23 | + board = AtaxxBoard() |
| 24 | + root = None |
| 25 | + model_player = 1 if float(rng.random()) >= 0.5 else -1 |
| 26 | + while not board.is_game_over(): |
| 27 | + if board.current_player == model_player: |
| 28 | + probs, root = compute_action_probs( |
| 29 | + board=board, |
| 30 | + mcts=mcts, |
| 31 | + root=root, |
| 32 | + add_noise=False, |
| 33 | + temperature=0.0, |
| 34 | + ) |
| 35 | + action_idx = int(np.argmax(probs)) |
| 36 | + board.step(ACTION_SPACE.decode(action_idx)) |
| 37 | + root = mcts.advance_root(root, action_idx) |
| 38 | + continue |
| 39 | + move = heuristic_move(board, rng, heuristic_level) |
| 40 | + board.step(move) |
| 41 | + root = mcts.advance_root(root, ACTION_SPACE.encode(move)) |
| 42 | + winner = board.get_result() |
| 43 | + if winner == model_player: |
| 44 | + return 1 |
| 45 | + if winner == 0: |
| 46 | + return 0 |
| 47 | + return -1 |
| 48 | + |
| 49 | + |
| 50 | +def evaluate_model( |
| 51 | + system: AtaxxZero, |
| 52 | + device: str, |
| 53 | + games: int, |
| 54 | + sims: int, |
| 55 | + c_puct: float, |
| 56 | + heuristic_level: str, |
| 57 | + seed: int, |
| 58 | +) -> dict[str, float | int | str]: |
| 59 | + from engine.mcts import MCTS |
| 60 | + |
| 61 | + system.eval() |
| 62 | + system.to(device) |
| 63 | + mcts = MCTS( |
| 64 | + model=system.model, |
| 65 | + c_puct=c_puct, |
| 66 | + n_simulations=sims, |
| 67 | + device=device, |
| 68 | + use_amp=cfg_bool("mcts_use_amp"), |
| 69 | + cache_size=max(0, cfg_int("mcts_cache_size")), |
| 70 | + leaf_batch_size=max(1, cfg_int("mcts_leaf_batch_size")), |
| 71 | + ) |
| 72 | + rng = np.random.default_rng(seed=seed) |
| 73 | + wins = 0 |
| 74 | + losses = 0 |
| 75 | + draws = 0 |
| 76 | + for _ in range(games): |
| 77 | + outcome = _play_eval_episode(mcts, rng, heuristic_level) |
| 78 | + if outcome > 0: |
| 79 | + wins += 1 |
| 80 | + elif outcome < 0: |
| 81 | + losses += 1 |
| 82 | + else: |
| 83 | + draws += 1 |
| 84 | + score = (wins + 0.5 * draws) / max(1, games) |
| 85 | + return { |
| 86 | + "games": games, |
| 87 | + "wins": wins, |
| 88 | + "losses": losses, |
| 89 | + "draws": draws, |
| 90 | + "score": score, |
| 91 | + "heuristic_level": heuristic_level, |
| 92 | + "sims": sims, |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | +__all__ = [ |
| 97 | + "evaluate_model", |
| 98 | +] |
0 commit comments