Repository navigation
Expand file tree
/
Copy pathwatch_server.py
More file actions
124 lines (93 loc) · 3.87 KB
/
Copy pathwatch_server.py
File metadata and controls
124 lines (93 loc) · 3.87 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
import os
import math
import torch
import numpy as np
from flask import Flask, request, jsonify
from flask_cors import CORS
import torch.nn.functional as F
import c2048
from game import Game2048
from model import StrategyNN, ValueNN
# Import it EXACTLY as it is, no modifications needed
from expectimax import get_best_move
app = Flask(__name__)
CORS(app)
# --- 1. Init Engine & Hardware ---
c2048.init_tables()
# FORCE CPU ENTIRELY
device = torch.device("cpu")
print(f"Server starting strictly on {device}...")
# --- 2. Load Models ---
policy_nn = StrategyNN().to(device)
value_nn = ValueNN().to(device)
if os.path.exists("checkpoints/policy_latest.pt"):
state_dict = torch.load("checkpoints/policy_latest.pt", map_location=device, weights_only=True)
if 'model_state_dict' in state_dict:
state_dict = state_dict['model_state_dict']
policy_nn.load_state_dict(state_dict)
if os.path.exists("checkpoints/value_latest.pt"):
value_nn.load_state_dict(torch.load("checkpoints/value_latest.pt", map_location=device, weights_only=True))
policy_nn = torch.jit.script(policy_nn.eval())
value_nn = torch.jit.script(value_nn.eval())
# --- 3. The Evaluator (Mimics train_expert_iteration.py) ---
class LocalEvaluator:
"""
Exposes the exact same .get_policy() and .get_value() interface
that expectimax.py expects from the RemoteEvaluator.
"""
def __init__(self, policy_net, value_net, dev):
self.policy_net = policy_net
self.value_net = value_net
self.dev = dev
@torch.no_grad()
def get_policy(self, boards_arr):
tensor = torch.from_numpy(c2048.batch_boards_to_tensor(boards_arr)).view(-1, 256).to(self.dev)
# Removed .cpu() as it's already on the CPU
return self.policy_net(tensor).numpy()
@torch.no_grad()
def get_value(self, boards_arr):
tensor = torch.from_numpy(c2048.batch_boards_to_tensor(boards_arr)).view(-1, 256).to(self.dev)
# Removed .cpu() as it's already on the CPU
return self.value_net(tensor).squeeze(-1).numpy()
evaluator = LocalEvaluator(policy_nn, value_nn, device)
# --- Helper ---
def parse_board(board_data):
"""Converts Tampermonkey 2D arrays/lists into the C++ 64-bit integer."""
if isinstance(board_data, int): return board_data
board_int = 0
if isinstance(board_data, list):
flat_board = board_data if not isinstance(board_data[0], list) else [val for row in board_data for val in row]
for i, val in enumerate(flat_board):
if val > 0: board_int |= (int(math.log2(val)) << (i * 4))
return board_int
# --- 4. Endpoints ---
@app.route('/get_move', methods=['POST'])
def get_move():
data = request.json
board_int = parse_board(data.get('board'))
game = Game2048(board=board_int)
# EXACTLY how it is called in train_expert_iteration.py
best_action = get_best_move(game, evaluator, depth=data.get('depth', 6), top_k_moves=3)
return jsonify({"action": int(best_action)})
@app.route('/analyze', methods=['POST'])
def analyze_board():
"""Kept extremely simple. Just returns raw NN data."""
data = request.json
board_int = parse_board(data.get('board'))
board_arr = np.array([board_int], dtype=np.uint64)
# 1. Raw Value NN
raw_value = evaluator.get_value(board_arr)[0]
# 2. Raw Policy NN
logits = evaluator.get_policy(board_arr)[0]
probs = F.softmax(torch.tensor(logits), dim=0).numpy().tolist()
actions = ["Up", "Down", "Left", "Right"]
raw_policy = {actions[i]: round(probs[i] * 100, 1) for i in range(4)}
# 3. Scratched Tree Evals (Dummy data so Tampermonkey doesn't crash)
tree_evals = {"Up": "N/A", "Down": "N/A", "Left": "N/A", "Right": "N/A"}
return jsonify({
"expected_survival": round(float(raw_value), 1),
"raw_policy": raw_policy,
"tree_evals": tree_evals
})
if __name__ == "__main__":
app.run(host="127.0.0.1", port=8000)