delete handler.py
Browse files- handler.py +0 -115
handler.py
DELETED
|
@@ -1,115 +0,0 @@
|
|
| 1 |
-
import sys
|
| 2 |
-
import json
|
| 3 |
-
import random
|
| 4 |
-
from pathlib import Path
|
| 5 |
-
|
| 6 |
-
model_path = Path("/model")
|
| 7 |
-
if str(model_path) not in sys.path:
|
| 8 |
-
sys.path.insert(0, str(model_path))
|
| 9 |
-
|
| 10 |
-
import chess_mcvs
|
| 11 |
-
|
| 12 |
-
class EndpointHandler:
|
| 13 |
-
def __init__(self, path: str, game_model: dict = None):
|
| 14 |
-
self.path = Path(path)
|
| 15 |
-
self.config = game_model or {}
|
| 16 |
-
|
| 17 |
-
# Load both config files
|
| 18 |
-
for cfg_file in ["config_model.json", "config_data.json"]:
|
| 19 |
-
p = self.path / cfg_file
|
| 20 |
-
if p.exists():
|
| 21 |
-
try:
|
| 22 |
-
self.config.update(json.loads(p.read_text(encoding="utf-8")))
|
| 23 |
-
except:
|
| 24 |
-
pass
|
| 25 |
-
|
| 26 |
-
self.lambda_zone = float(self.config.get("lambda_zone", 0.6))
|
| 27 |
-
self.k_zone = int(self.config.get("k_zone", 8))
|
| 28 |
-
self.time_per_move = float(self.config.get("time_per_move", 3.0))
|
| 29 |
-
self.temperature = float(self.config.get("temperature", 0.25))
|
| 30 |
-
|
| 31 |
-
# === Load Zone Database ===
|
| 32 |
-
self.zone_db = None
|
| 33 |
-
zone_path = self.path / "chess_zone_db.npz"
|
| 34 |
-
if not zone_path.exists():
|
| 35 |
-
zone_path = Path("/data") / "chess_zone_db.npz" # sandbox mount
|
| 36 |
-
|
| 37 |
-
if zone_path.exists():
|
| 38 |
-
try:
|
| 39 |
-
self.zone_db = chess_mcvs.HilbertOrderedZoneDatabase(filepath=str(zone_path))
|
| 40 |
-
print(f"โ
Zone DB LOADED | W={len(self.zone_db.winning_matrices)} | "
|
| 41 |
-
f"L={len(self.zone_db.losing_matrices)} | D={len(self.zone_db.draw_matrices)}",
|
| 42 |
-
file=sys.stderr)
|
| 43 |
-
except Exception as e:
|
| 44 |
-
print(f"โ Zone DB failed: {e}", file=sys.stderr)
|
| 45 |
-
else:
|
| 46 |
-
print("โ ๏ธ chess_zone_db.npz NOT FOUND", file=sys.stderr)
|
| 47 |
-
|
| 48 |
-
# === Create MCVSSearcher (same as your local experiment) ===
|
| 49 |
-
self.searcher = chess_mcvs.MCVSSearcher(
|
| 50 |
-
policy_net=None,
|
| 51 |
-
value_net=None,
|
| 52 |
-
zone_db=self.zone_db,
|
| 53 |
-
use_nets=False,
|
| 54 |
-
lambda_zone=self.lambda_zone,
|
| 55 |
-
k_zone=self.k_zone,
|
| 56 |
-
cpuct=float(self.config.get("cpuct", 1.5)),
|
| 57 |
-
dirichlet_alpha=float(self.config.get("dirichlet_alpha", 0.3)),
|
| 58 |
-
dirichlet_noise_fraction=0.25,
|
| 59 |
-
device="cpu"
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
print(f"๐ Handler ready | lambda_zone={self.lambda_zone} | time={self.time_per_move}s | temp={self.temperature}",
|
| 63 |
-
file=sys.stderr)
|
| 64 |
-
|
| 65 |
-
def __call__(self, data: dict):
|
| 66 |
-
inputs = data.get("inputs", data)
|
| 67 |
-
fen = inputs.get("fen")
|
| 68 |
-
player = inputs.get("player", "w")
|
| 69 |
-
time_budget = float(inputs.get("time_budget", self.time_per_move))
|
| 70 |
-
|
| 71 |
-
print(f"๐ Search start | FEN={fen[:40]}... | time_budget={time_budget:.1f}s", file=sys.stderr)
|
| 72 |
-
|
| 73 |
-
game = chess_mcvs.Chess()
|
| 74 |
-
game.board.set_fen(fen)
|
| 75 |
-
if str(player).lower().startswith("b"):
|
| 76 |
-
game.board.turn = False
|
| 77 |
-
|
| 78 |
-
# Run the same searcher you used in your experiment
|
| 79 |
-
visits, sims = self.searcher.search_with_time_budget(game, time_budget)
|
| 80 |
-
|
| 81 |
-
print(f"๐ Search done โ {sims} simulations, explored {len(visits)} moves", file=sys.stderr)
|
| 82 |
-
|
| 83 |
-
if visits:
|
| 84 |
-
# Use temperature like in your successful local tests
|
| 85 |
-
if self.temperature > 0.01:
|
| 86 |
-
moves = list(visits.items())
|
| 87 |
-
counts = [c for _, c in moves]
|
| 88 |
-
logits = [c ** (1.0 / self.temperature) for c in counts]
|
| 89 |
-
total = sum(logits)
|
| 90 |
-
probs = [l / total for l in logits]
|
| 91 |
-
|
| 92 |
-
r = random.random()
|
| 93 |
-
cum = 0.0
|
| 94 |
-
for (move, _), p in zip(moves, probs):
|
| 95 |
-
cum += p
|
| 96 |
-
if r <= cum:
|
| 97 |
-
chosen = move
|
| 98 |
-
break
|
| 99 |
-
else:
|
| 100 |
-
chosen = moves[-1][0]
|
| 101 |
-
else:
|
| 102 |
-
chosen = max(visits.items(), key=lambda x: x[1])[0]
|
| 103 |
-
|
| 104 |
-
print(f"โ
Final move: {chosen.uci()}", file=sys.stderr)
|
| 105 |
-
return {"move": chosen.uci()}
|
| 106 |
-
else:
|
| 107 |
-
print("โ ๏ธ No visits, using fallback", file=sys.stderr)
|
| 108 |
-
legal = list(game.get_legal_moves())
|
| 109 |
-
move = max(legal, key=lambda m: game.board.is_capture(m)) if legal else legal[0]
|
| 110 |
-
return {"move": move.uci()}
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
def get_move(data: dict, path: str = ".", game_model: dict = None):
|
| 114 |
-
h = EndpointHandler(path=path, game_model=game_model)
|
| 115 |
-
return h(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|