From e2ddc3868f5447f918cdc37460174fff330851ba Mon Sep 17 00:00:00 2001 From: luk3k Date: Tue, 30 Jan 2024 23:07:00 +0100 Subject: [PATCH] fixed limit for external engines and added arguments to web.py --- chesspp/engine.py | 11 ++++++++--- chesspp/web.py | 19 ++++++++++--------- web.py | 6 ++++-- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/chesspp/engine.py b/chesspp/engine.py index 0c40a3f..d06d8ee 100644 --- a/chesspp/engine.py +++ b/chesspp/engine.py @@ -1,6 +1,5 @@ import random import time -import os from abc import ABC, abstractmethod from torch import distributions as dist @@ -48,6 +47,12 @@ class Limit: while (time.perf_counter_ns() - start) / 1e9 < self.time: func(*args, **kwargs) + def translate_to_engine_limit(self) -> chess.engine.Limit: + if self.nodes: + return chess.engine.Limit(nodes=self.nodes) + elif self.time: + return chess.engine.Limit(time=self.time) + class Engine(ABC): board: chess.Board @@ -147,7 +152,7 @@ class StockFishEngine(Engine): 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) + return self.stockfish.play(board, limit.translate_to_engine_limit()) @staticmethod def get_name() -> str: @@ -160,7 +165,7 @@ class Lc0Engine(Engine): 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) + return self.lc0.play(board, limit.translate_to_engine_limit()) @staticmethod def get_name() -> str: diff --git a/chesspp/web.py b/chesspp/web.py index 9a51745..e2a8887 100644 --- a/chesspp/web.py +++ b/chesspp/web.py @@ -6,6 +6,7 @@ from aiohttp import web import chess from chesspp import engine +from chesspp.engine_factory import EngineFactory from chesspp.stockfish_strategy import StockFishStrategy _DIR = os.path.abspath(os.path.dirname(__file__)) @@ -23,12 +24,7 @@ def load_index() -> str: class Simulate: """ Run a simulation of two engines""" - def __init__(self, engine_white=None, engine_black=None): - if engine_white is None: - engine_white = engine.ClassicMctsEngine(chess.WHITE) - if engine_black is None: - engine_black = engine.ClassicMctsEngine(chess.BLACK) - + def __init__(self, engine_white, engine_black): self.white = engine_white self.black = engine_black @@ -44,9 +40,13 @@ class Simulate: class WebInterface: - def __init__(self, white_engine: engine.Engine.__class__, black_engine: engine.Engine.__class__, limit: engine.Limit): + def __init__(self, white_engine, black_engine, strategy1, strategy2, stockfish_path, lc0_path, limit: engine.Limit): self.white = white_engine self.black = black_engine + self.strategy1 = strategy1 + self.strategy2 = strategy2 + self.stockfish_path = stockfish_path + self.lc0_path = lc0_path self.limit = limit @@ -73,8 +73,9 @@ class WebInterface: async def turns(): """ Simulates the game and sends the response to the client """ - runner = Simulate(self.white(chess.Board(), chess.WHITE, StockFishStrategy()), self.black( - chess.Board(), chess.BLACK, StockFishStrategy())).run(self.limit) + white = EngineFactory.create_engine(self.white, self.strategy1, chess.WHITE, self.stockfish_path, self.lc0_path) + black = EngineFactory.create_engine(self.black, self.strategy1, chess.BLACK, self.stockfish_path, self.lc0_path) + runner = Simulate(white, black).run(self.limit) def sim(): return next(runner, None) diff --git a/web.py b/web.py index f2edfd9..5c12365 100644 --- a/web.py +++ b/web.py @@ -1,7 +1,9 @@ from chesspp import engine from chesspp import web +from main import read_arguments if __name__ == '__main__': - limit = engine.Limit(time=0.5) - web.WebInterface(engine.BayesMctsEngine, engine.ClassicMctsEngine, limit).run_app() + engine1, engine2, strategy1, strategy2, n_games, time, stockfish_path, lc0_path, n_proc = read_arguments() + limit = engine.Limit(time=6) + web.WebInterface(engine1, engine2, strategy1, strategy2, stockfish_path, lc0_path, limit).run_app()