added random_stockfish_strategy.py

This commit is contained in:
2024-01-31 10:19:49 +01:00
parent 82ec4971a6
commit 15ce6c5316
5 changed files with 52 additions and 6 deletions

View File

@@ -4,6 +4,7 @@ from chesspp.engine import *
from chesspp.lc0_strategy import Lc0Strategy
from chesspp.random_strategy import RandomStrategy
from chesspp.stockfish_strategy import StockFishStrategy
from chesspp.random_stockfish_strategy import RandomStockfishStrategy
from chesspp.i_strategy import IStrategy
import chess
@@ -20,6 +21,7 @@ class StrategyEnum(Enum):
Stockfish = 0
Lc0 = 1
Random = 2
RandomStockfish = 3
class EngineFactory:
@@ -33,6 +35,8 @@ class EngineFactory:
strategy = EngineFactory._get_lc0_strategy(lc0_path, rollout_depth)
case StrategyEnum.Random:
strategy = EngineFactory._get_random_strategy(rollout_depth)
case StrategyEnum.RandomStockfish:
strategy = EngineFactory._get_random_stockfish_strategy(stockfish_path, rollout_depth)
match engine_name:
case EngineEnum.ClassicMcts:
@@ -71,6 +75,10 @@ class EngineFactory:
def _get_stockfish_strategy(engine_path: str, rollout_depth: int) -> IStrategy:
return StockFishStrategy(engine_path, rollout_depth)
@staticmethod
def _get_random_stockfish_strategy(engine_path: str, rollout_depth: int) -> IStrategy:
return RandomStockfishStrategy(rollout_depth, engine_path)
@staticmethod
def _get_lc0_strategy(engine_path: str, rollout_depth: int) -> IStrategy:
return Lc0Strategy(engine_path, rollout_depth)

View File

@@ -0,0 +1,36 @@
import random
import chess
import chess.engine
from chesspp.i_strategy import IStrategy
from chesspp.eval import score_stockfish
class RandomStockfishStrategy(IStrategy):
def __init__(self, rollout_depth: int, path="../stockfish/stockfish-windows-x86-64-avx2",
random_seed: random.Random = random.Random()) -> None:
super().__init__(rollout_depth)
self._stockfish = None
self.path = path
self.random_seed = random_seed
def __del__(self):
if self._stockfish is not None:
self._stockfish.quit()
@property
def stockfish(self) -> chess.engine.SimpleEngine:
if self._stockfish is None:
self._stockfish = self.stockfish = chess.engine.SimpleEngine.popen_uci(self.path)
return self._stockfish
@stockfish.setter
def stockfish(self, stockfish):
self._stockfish = stockfish
def pick_next_move(self, board: chess.Board) -> chess.Move:
return self.random_seed.choice(list(board.legal_moves))
def analyze_board(self, board: chess.Board) -> int:
return score_stockfish(board, self.stockfish)

View File

@@ -1,4 +1,3 @@
import os
import chess
from chesspp.i_strategy import IStrategy
from chesspp.eval import score_stockfish

View File

@@ -109,7 +109,8 @@ def read_arguments():
engines = {"ClassicMCTS": EngineEnum.ClassicMcts, "BayesianMCTS": EngineEnum.BayesianMcts,
"Random": EngineEnum.Random, "Stockfish": EngineEnum.Stockfish, "Lc0": EngineEnum.Lc0}
strategies = {"Random": StrategyEnum.Random, "Stockfish": StrategyEnum.Stockfish, "Lc0": StrategyEnum.Lc0}
strategies = {"Random": StrategyEnum.Random, "Stockfish": StrategyEnum.Stockfish, "Lc0": StrategyEnum.Lc0,
"RandomStockfish": StrategyEnum.RandomStockfish}
if os.name == 'nt':
stockfish_default = "stockfish/stockfish-windows-x86-64-avx2"
@@ -140,8 +141,10 @@ def read_arguments():
strategy1 = strategies[args.strategy1]
strategy2 = strategies[args.strategy2]
print(engine1, engine2, strategy1, strategy2, int(args.n), float(args.time), args.stockfish_path, args.lc0_path, int(args.proc))
return engine1, engine2, strategy1, strategy2, int(args.n), float(args.time), args.stockfish_path, args.lc0_path, int(args.proc)
print(engine1, engine2, strategy1, strategy2, int(args.n), float(args.time), args.stockfish_path, args.lc0_path,
int(args.proc))
return engine1, engine2, strategy1, strategy2, int(args.n), float(
args.time), args.stockfish_path, args.lc0_path, int(args.proc)
def main():

2
web.py
View File

@@ -5,5 +5,5 @@ from main import read_arguments
if __name__ == '__main__':
engine1, engine2, strategy1, strategy2, n_games, time, stockfish_path, lc0_path, n_proc = read_arguments()
limit = engine.Limit(time=6)
limit = engine.Limit(time=0.5)
web.WebInterface(engine1, engine2, strategy1, strategy2, stockfish_path, lc0_path, limit).run_app()