Update selfchess.py
Browse files- selfchess.py +62 -42
selfchess.py
CHANGED
|
@@ -87,8 +87,43 @@ class NN1(nn.Module):
|
|
| 87 |
x = self.neurons(x)
|
| 88 |
return x
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
model = NN1().to(device)
|
| 91 |
optimizer = optim.Adam(model.parameters(), lr=CONFIG["learning_rate"])
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
try:
|
| 94 |
model.load_state_dict(torch.load(CONFIG["model_path"], map_location=device))
|
|
@@ -119,56 +154,41 @@ def get_evaluation(board):
|
|
| 119 |
else:
|
| 120 |
return -evaluation
|
| 121 |
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
"""
|
| 124 |
-
Monte Carlo
|
| 125 |
-
|
| 126 |
"""
|
| 127 |
-
#
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
san_moves = []
|
| 134 |
|
| 135 |
-
|
| 136 |
-
if depth == 0 or board.is_game_over():
|
| 137 |
-
return get_evaluation(board)
|
| 138 |
|
| 139 |
-
|
| 140 |
-
|
|
|
|
| 141 |
board.push(move)
|
| 142 |
-
|
|
|
|
| 143 |
board.pop()
|
| 144 |
-
|
| 145 |
-
alpha = max(alpha, eval)
|
| 146 |
-
if alpha >= beta:
|
| 147 |
-
break
|
| 148 |
-
return max_eval
|
| 149 |
-
|
| 150 |
-
move_scores = {}
|
| 151 |
-
|
| 152 |
-
for _ in range(simulations):
|
| 153 |
-
# Optionally start from a random legal move (Monte Carlo rollout)
|
| 154 |
-
move = random.choice(list(board.legal_moves))
|
| 155 |
-
|
| 156 |
-
# If san_moves.txt is not empty, prefer moves from it when possible
|
| 157 |
-
move_san = board.san(move)
|
| 158 |
-
if san_moves and move_san in san_moves:
|
| 159 |
-
move = chess.Move.from_uci(board.parse_san(move_san).uci())
|
| 160 |
-
|
| 161 |
-
board.push(move)
|
| 162 |
-
score = -negamax(board, depth - 1, -float('inf'), float('inf'))
|
| 163 |
-
board.pop()
|
| 164 |
-
|
| 165 |
-
move_scores[move] = move_scores.get(move, 0) + score
|
| 166 |
-
|
| 167 |
-
# Pick move with highest average score
|
| 168 |
-
best_move = max(move_scores.items(), key=lambda x: x[1] / simulations)[0]
|
| 169 |
-
return best_move
|
| 170 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
|
| 174 |
|
|
|
|
| 87 |
x = self.neurons(x)
|
| 88 |
return x
|
| 89 |
|
| 90 |
+
lass Policy(nn.Module):
|
| 91 |
+
def __init__(self):
|
| 92 |
+
super().__init__()
|
| 93 |
+
self.embedding = nn.Embedding(13, 32)
|
| 94 |
+
self.attention = nn.MultiheadAttention(embed_dim=32, num_heads=16)
|
| 95 |
+
self.neu = 256
|
| 96 |
+
self.neurons = nn.Sequential(
|
| 97 |
+
nn.Linear(64*32, self.neu),
|
| 98 |
+
nn.ReLU(),
|
| 99 |
+
nn.Linear(self.neu, self.neu),
|
| 100 |
+
nn.ReLU(),
|
| 101 |
+
nn.Linear(self.neu, self.neu),
|
| 102 |
+
nn.ReLU(),
|
| 103 |
+
nn.Linear(self.neu, self.neu),
|
| 104 |
+
nn.ReLU(),
|
| 105 |
+
nn.Linear(self.neu, 128),
|
| 106 |
+
nn.ReLU(),
|
| 107 |
+
nn.Linear(128, 29275),
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
def forward(self, x):
|
| 111 |
+
x = chess.Board(x)
|
| 112 |
+
color = x.turn
|
| 113 |
+
x = board_to_tensor(x)
|
| 114 |
+
x = self.embedding(x)
|
| 115 |
+
x = x.permute(1, 0, 2)
|
| 116 |
+
attn_output, _ = self.attention(x, x, x)
|
| 117 |
+
x = attn_output.permute(1, 0, 2).contiguous()
|
| 118 |
+
x = x.view(x.size(0), -1)
|
| 119 |
+
x = self.neurons(x) * color
|
| 120 |
+
return x
|
| 121 |
+
|
| 122 |
model = NN1().to(device)
|
| 123 |
optimizer = optim.Adam(model.parameters(), lr=CONFIG["learning_rate"])
|
| 124 |
+
policy = Policy().to(device)
|
| 125 |
+
polweight = torch.load("NeoChess/chessy_policy.pth",map_location=device,weights_only=False)
|
| 126 |
+
policy.load_state_dict(polweight)
|
| 127 |
|
| 128 |
try:
|
| 129 |
model.load_state_dict(torch.load(CONFIG["model_path"], map_location=device))
|
|
|
|
| 154 |
else:
|
| 155 |
return -evaluation
|
| 156 |
|
| 157 |
+
with open("/usr/local/python/3.12.1/lib/python3.12/site-packages/torchrl/envs/custom/san_moves.txt", "r") as f:
|
| 158 |
+
uci_to_index = {line.strip(): i for i, line in enumerate(f)}
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
def search(board ,depth ,policy_net=policy, simulations=100, temperature=1.0, device="cpu"):
|
| 162 |
"""
|
| 163 |
+
Monte Carlo search using policy network for move selection
|
| 164 |
+
and value network via get_evaluation().
|
| 165 |
"""
|
| 166 |
+
# Convert board FEN to tensor for policy_net
|
| 167 |
+
depth
|
| 168 |
+
with torch.no_grad():
|
| 169 |
+
fen_tensor = torch.tensor([board.fen()], device=device) # Adjust if your FEN encoding differs
|
| 170 |
+
logits = policy_net(fen_tensor)["logits"].squeeze(0)
|
| 171 |
+
probs = torch.softmax(logits / temperature, dim=-1).cpu().numpy()
|
|
|
|
| 172 |
|
| 173 |
+
move_scores = {move: 0 for move in board.legal_moves}
|
|
|
|
|
|
|
| 174 |
|
| 175 |
+
for move in board.legal_moves:
|
| 176 |
+
total_eval = 0
|
| 177 |
+
for _ in range(simulations):
|
| 178 |
board.push(move)
|
| 179 |
+
eval_score = get_evaluation(board) # Uses value net
|
| 180 |
+
total_eval += eval_score
|
| 181 |
board.pop()
|
| 182 |
+
move_scores[move] = total_eval / simulations
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
+
# Weight evaluations by policy prior
|
| 185 |
+
for move in move_scores:
|
| 186 |
+
move_index = uci_to_index[str(move)]
|
| 187 |
+
move_scores[move] *= probs[move_index]
|
| 188 |
|
| 189 |
+
# Pick best move
|
| 190 |
+
best_move = max(move_scores, key=move_scores.get)
|
| 191 |
+
return best_move, move_scores
|
| 192 |
|
| 193 |
|
| 194 |
|