added engine factory and adapted command line parsing, added lc0_strategy.py, added stockfish and lc0 standalone engines

This commit is contained in:
2024-01-30 19:44:09 +01:00
parent 50cd4cde9b
commit b2ce73f06b
9 changed files with 214 additions and 56 deletions

39
chesspp/lc0_strategy.py Normal file
View File

@@ -0,0 +1,39 @@
import chess
import chess.engine
import os
from chesspp.i_strategy import IStrategy
from chesspp.eval import score_lc0
_DIR = os.path.abspath(os.path.dirname(__file__))
class Lc0Strategy(IStrategy):
def __init__(self, path="../lc0/lc0", rollout_depth: int = 4,
limit: chess.engine.Limit = chess.engine.Limit(depth=4)):
super().__init__(rollout_depth)
self._lc0 = None
self.path = path
self.limit = limit
def __del__(self):
if self._lc0 is not None:
self._lc0.quit()
@property
def lc0(self) -> chess.engine.SimpleEngine:
if self._lc0 is None:
self._lc0 = self.lc0 = chess.engine.SimpleEngine.popen_uci(self.path)
return self._lc0
@lc0.setter
def lc0(self, value):
self._lc0 = value
def pick_next_move(self, board: chess.Board) -> chess.Move | None:
return self.lc0.play(board, self.limit).move
def analyze_board(self, board: chess.Board) -> int:
score = score_lc0(board, self.lc0)
print("lc0 score", score)
return score