Update selfchess.py
Browse files- selfchess.py +46 -12
selfchess.py
CHANGED
|
@@ -5,6 +5,8 @@ import chess
|
|
| 5 |
import os
|
| 6 |
import chess.engine as eng
|
| 7 |
import torch.multiprocessing as mp
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# CONFIGURATION
|
| 10 |
CONFIG = {
|
|
@@ -117,23 +119,55 @@ def get_evaluation(board):
|
|
| 117 |
else:
|
| 118 |
return -evaluation
|
| 119 |
|
| 120 |
-
def search(board, depth,
|
| 121 |
"""
|
| 122 |
-
|
|
|
|
| 123 |
"""
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
board.push(move)
|
| 130 |
-
|
| 131 |
board.pop()
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
| 137 |
|
| 138 |
|
| 139 |
|
|
|
|
| 5 |
import os
|
| 6 |
import chess.engine as eng
|
| 7 |
import torch.multiprocessing as mp
|
| 8 |
+
import random
|
| 9 |
+
from pathlib import Path
|
| 10 |
|
| 11 |
# CONFIGURATION
|
| 12 |
CONFIG = {
|
|
|
|
| 119 |
else:
|
| 120 |
return -evaluation
|
| 121 |
|
| 122 |
+
def search(board, depth, simulations=100):
|
| 123 |
"""
|
| 124 |
+
Monte Carlo Tree Search with basic negamax evaluation.
|
| 125 |
+
Reads moves from san_moves.txt and picks the best move.
|
| 126 |
"""
|
| 127 |
+
# Load predefined moves from SAN file
|
| 128 |
+
san_file_path = Path("ChessEnv/san_moves.txt")
|
| 129 |
+
if san_file_path.exists():
|
| 130 |
+
with open(san_file_path, "r") as f:
|
| 131 |
+
san_moves = [line.strip() for line in f if line.strip()]
|
| 132 |
+
else:
|
| 133 |
+
san_moves = []
|
| 134 |
+
|
| 135 |
+
def negamax(board, depth, alpha, beta):
|
| 136 |
+
if depth == 0 or board.is_game_over():
|
| 137 |
+
return get_evaluation(board)
|
| 138 |
+
|
| 139 |
+
max_eval = float('-inf')
|
| 140 |
+
for move in board.legal_moves:
|
| 141 |
+
board.push(move)
|
| 142 |
+
eval = -negamax(board, depth - 1, -beta, -alpha)
|
| 143 |
+
board.pop()
|
| 144 |
+
max_eval = max(max_eval, eval)
|
| 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 |
|