-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchessbot.py
More file actions
26 lines (20 loc) · 921 Bytes
/
Copy pathchessbot.py
File metadata and controls
26 lines (20 loc) · 921 Bytes
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
import chess
from modules.minimax import minimax
import math
import torch
from typing import Dict
class ChessBot:
def __init__(self, max_depth: int, model: torch.nn.Module, transposition_table: Dict | None = None) -> None:
self.max_depth = max_depth
self.transposition_table = transposition_table
self.model = model
self.model.eval()
self.model = self._optimize_model(model)
def search(self, board: chess.Board) -> chess.Move:
is_maximizing = True if board.turn == chess.WHITE else False
_, best_move = minimax(board, self.max_depth, -math.inf, math.inf, is_maximizing, self.model, self.transposition_table)
return best_move
def _optimize_model(self, model: torch.nn.Module) -> torch.jit.ScriptModule:
example_input = torch.zeros(1, 768)
traced_model = torch.jit.trace(model, example_input)
return traced_model