From 15ce6c5316adc4897cec39567b308a768c092a46 Mon Sep 17 00:00:00 2001 From: luk3k Date: Wed, 31 Jan 2024 10:19:49 +0100 Subject: [PATCH] added random_stockfish_strategy.py --- chesspp/engine_factory.py | 8 +++++++ chesspp/random_stockfish_strategy.py | 36 ++++++++++++++++++++++++++++ chesspp/stockfish_strategy.py | 1 - main.py | 11 +++++---- web.py | 2 +- 5 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 chesspp/random_stockfish_strategy.py diff --git a/chesspp/engine_factory.py b/chesspp/engine_factory.py index c257ba5..7112be4 100644 --- a/chesspp/engine_factory.py +++ b/chesspp/engine_factory.py @@ -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) diff --git a/chesspp/random_stockfish_strategy.py b/chesspp/random_stockfish_strategy.py new file mode 100644 index 0000000..f6e0708 --- /dev/null +++ b/chesspp/random_stockfish_strategy.py @@ -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) diff --git a/chesspp/stockfish_strategy.py b/chesspp/stockfish_strategy.py index 0a089f2..dd2fdf2 100644 --- a/chesspp/stockfish_strategy.py +++ b/chesspp/stockfish_strategy.py @@ -1,4 +1,3 @@ -import os import chess from chesspp.i_strategy import IStrategy from chesspp.eval import score_stockfish diff --git a/main.py b/main.py index 28f135e..ef8d05c 100644 --- a/main.py +++ b/main.py @@ -44,7 +44,7 @@ def test_bayes_mcts(): t1 = time.time_ns() mcts.sample(1) t2 = time.time_ns() - print ((t2 - t1)/1e6) + print((t2 - t1) / 1e6) mcts.print() for move, score in mcts.get_moves().items(): print("move (mcts):", move, " with score:", score) @@ -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(): diff --git a/web.py b/web.py index 5c12365..0355157 100644 --- a/web.py +++ b/web.py @@ -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()