added engine factory and adapted command line parsing, added lc0_strategy.py, added stockfish and lc0 standalone engines
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import random
|
||||
import time
|
||||
import os
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
from torch import distributions as dist
|
||||
@@ -15,13 +16,13 @@ from typing import Dict
|
||||
class Limit:
|
||||
""" Class to determine when to stop searching for moves """
|
||||
|
||||
time: float|None
|
||||
time: float | None
|
||||
""" Search for `time` seconds """
|
||||
|
||||
nodes: int|None
|
||||
nodes: int | None
|
||||
""" Search for a limited number of `nodes`"""
|
||||
|
||||
def __init__(self, time: float|None = None, nodes: int|None = None):
|
||||
def __init__(self, time: float | None = None, nodes: int | None = None):
|
||||
self.time = time
|
||||
self.nodes = nodes
|
||||
|
||||
@@ -44,7 +45,7 @@ class Limit:
|
||||
|
||||
def _run_time(self, func, *args, **kwargs):
|
||||
start = time.perf_counter_ns()
|
||||
while (time.perf_counter_ns()-start)/1e9 < self.time:
|
||||
while (time.perf_counter_ns() - start) / 1e9 < self.time:
|
||||
func(*args, **kwargs)
|
||||
|
||||
|
||||
@@ -56,7 +57,7 @@ class Engine(ABC):
|
||||
strategy: IStrategy
|
||||
"""The strategy used to pick moves when simulating games."""
|
||||
|
||||
def __init__(self, board: chess.Board, color: chess.Color, strategy: IStrategy):
|
||||
def __init__(self, board: chess.Board, color: chess.Color, strategy: IStrategy | None):
|
||||
self.board = board
|
||||
self.color = color
|
||||
self.strategy = strategy
|
||||
@@ -138,3 +139,29 @@ class RandomEngine(Engine):
|
||||
def play(self, board: chess.Board, limit: Limit) -> chess.engine.PlayResult:
|
||||
move = random.choice(list(board.legal_moves))
|
||||
return chess.engine.PlayResult(move=move, ponder=None)
|
||||
|
||||
|
||||
class StockFishEngine(Engine):
|
||||
def __init__(self, board: chess.Board, color: chess, path="../stockfish/stockfish-ubuntu-x86-64-avx2"):
|
||||
super().__init__(board, color, None)
|
||||
self.stockfish = chess.engine.SimpleEngine.popen_uci(path)
|
||||
|
||||
def play(self, board: chess.Board, limit: Limit) -> chess.engine.PlayResult:
|
||||
return self.stockfish.play(board, limit)
|
||||
|
||||
@staticmethod
|
||||
def get_name() -> str:
|
||||
return "Stockfish"
|
||||
|
||||
|
||||
class Lc0Engine(Engine):
|
||||
def __init__(self, board: chess.Board, color: chess, path="../lc0/lc0"):
|
||||
super().__init__(board, color, None)
|
||||
self.lc0 = chess.engine.SimpleEngine.popen_uci(path)
|
||||
|
||||
def play(self, board: chess.Board, limit: Limit) -> chess.engine.PlayResult:
|
||||
return self.lc0.play(board, limit)
|
||||
|
||||
@staticmethod
|
||||
def get_name() -> str:
|
||||
return "Lc0"
|
||||
|
||||
Reference in New Issue
Block a user