added random_stockfish_strategy.py
This commit is contained in:
@@ -4,6 +4,7 @@ from chesspp.engine import *
|
|||||||
from chesspp.lc0_strategy import Lc0Strategy
|
from chesspp.lc0_strategy import Lc0Strategy
|
||||||
from chesspp.random_strategy import RandomStrategy
|
from chesspp.random_strategy import RandomStrategy
|
||||||
from chesspp.stockfish_strategy import StockFishStrategy
|
from chesspp.stockfish_strategy import StockFishStrategy
|
||||||
|
from chesspp.random_stockfish_strategy import RandomStockfishStrategy
|
||||||
from chesspp.i_strategy import IStrategy
|
from chesspp.i_strategy import IStrategy
|
||||||
import chess
|
import chess
|
||||||
|
|
||||||
@@ -20,6 +21,7 @@ class StrategyEnum(Enum):
|
|||||||
Stockfish = 0
|
Stockfish = 0
|
||||||
Lc0 = 1
|
Lc0 = 1
|
||||||
Random = 2
|
Random = 2
|
||||||
|
RandomStockfish = 3
|
||||||
|
|
||||||
|
|
||||||
class EngineFactory:
|
class EngineFactory:
|
||||||
@@ -33,6 +35,8 @@ class EngineFactory:
|
|||||||
strategy = EngineFactory._get_lc0_strategy(lc0_path, rollout_depth)
|
strategy = EngineFactory._get_lc0_strategy(lc0_path, rollout_depth)
|
||||||
case StrategyEnum.Random:
|
case StrategyEnum.Random:
|
||||||
strategy = EngineFactory._get_random_strategy(rollout_depth)
|
strategy = EngineFactory._get_random_strategy(rollout_depth)
|
||||||
|
case StrategyEnum.RandomStockfish:
|
||||||
|
strategy = EngineFactory._get_random_stockfish_strategy(stockfish_path, rollout_depth)
|
||||||
|
|
||||||
match engine_name:
|
match engine_name:
|
||||||
case EngineEnum.ClassicMcts:
|
case EngineEnum.ClassicMcts:
|
||||||
@@ -71,6 +75,10 @@ class EngineFactory:
|
|||||||
def _get_stockfish_strategy(engine_path: str, rollout_depth: int) -> IStrategy:
|
def _get_stockfish_strategy(engine_path: str, rollout_depth: int) -> IStrategy:
|
||||||
return StockFishStrategy(engine_path, rollout_depth)
|
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
|
@staticmethod
|
||||||
def _get_lc0_strategy(engine_path: str, rollout_depth: int) -> IStrategy:
|
def _get_lc0_strategy(engine_path: str, rollout_depth: int) -> IStrategy:
|
||||||
return Lc0Strategy(engine_path, rollout_depth)
|
return Lc0Strategy(engine_path, rollout_depth)
|
||||||
|
|||||||
36
chesspp/random_stockfish_strategy.py
Normal file
36
chesspp/random_stockfish_strategy.py
Normal 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)
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
import os
|
|
||||||
import chess
|
import chess
|
||||||
from chesspp.i_strategy import IStrategy
|
from chesspp.i_strategy import IStrategy
|
||||||
from chesspp.eval import score_stockfish
|
from chesspp.eval import score_stockfish
|
||||||
|
|||||||
9
main.py
9
main.py
@@ -109,7 +109,8 @@ def read_arguments():
|
|||||||
|
|
||||||
engines = {"ClassicMCTS": EngineEnum.ClassicMcts, "BayesianMCTS": EngineEnum.BayesianMcts,
|
engines = {"ClassicMCTS": EngineEnum.ClassicMcts, "BayesianMCTS": EngineEnum.BayesianMcts,
|
||||||
"Random": EngineEnum.Random, "Stockfish": EngineEnum.Stockfish, "Lc0": EngineEnum.Lc0}
|
"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':
|
if os.name == 'nt':
|
||||||
stockfish_default = "stockfish/stockfish-windows-x86-64-avx2"
|
stockfish_default = "stockfish/stockfish-windows-x86-64-avx2"
|
||||||
@@ -140,8 +141,10 @@ def read_arguments():
|
|||||||
strategy1 = strategies[args.strategy1]
|
strategy1 = strategies[args.strategy1]
|
||||||
strategy2 = strategies[args.strategy2]
|
strategy2 = strategies[args.strategy2]
|
||||||
|
|
||||||
print(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,
|
||||||
return engine1, engine2, strategy1, strategy2, int(args.n), float(args.time), args.stockfish_path, args.lc0_path, int(args.proc)
|
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():
|
def main():
|
||||||
|
|||||||
2
web.py
2
web.py
@@ -5,5 +5,5 @@ from main import read_arguments
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
engine1, engine2, strategy1, strategy2, n_games, time, stockfish_path, lc0_path, n_proc = read_arguments()
|
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()
|
web.WebInterface(engine1, engine2, strategy1, strategy2, stockfish_path, lc0_path, limit).run_app()
|
||||||
|
|||||||
Reference in New Issue
Block a user