fixed limit for external engines and added arguments to web.py

This commit is contained in:
2024-01-30 23:07:00 +01:00
parent f3d82d6b19
commit e2ddc3868f
3 changed files with 22 additions and 14 deletions

View File

@@ -1,6 +1,5 @@
import random import random
import time import time
import os
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from torch import distributions as dist from torch import distributions as dist
@@ -48,6 +47,12 @@ class Limit:
while (time.perf_counter_ns() - start) / 1e9 < self.time: while (time.perf_counter_ns() - start) / 1e9 < self.time:
func(*args, **kwargs) 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): class Engine(ABC):
board: chess.Board board: chess.Board
@@ -147,7 +152,7 @@ class StockFishEngine(Engine):
self.stockfish = chess.engine.SimpleEngine.popen_uci(path) self.stockfish = chess.engine.SimpleEngine.popen_uci(path)
def play(self, board: chess.Board, limit: Limit) -> chess.engine.PlayResult: 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 @staticmethod
def get_name() -> str: def get_name() -> str:
@@ -160,7 +165,7 @@ class Lc0Engine(Engine):
self.lc0 = chess.engine.SimpleEngine.popen_uci(path) self.lc0 = chess.engine.SimpleEngine.popen_uci(path)
def play(self, board: chess.Board, limit: Limit) -> chess.engine.PlayResult: 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 @staticmethod
def get_name() -> str: def get_name() -> str:

View File

@@ -6,6 +6,7 @@ from aiohttp import web
import chess import chess
from chesspp import engine from chesspp import engine
from chesspp.engine_factory import EngineFactory
from chesspp.stockfish_strategy import StockFishStrategy from chesspp.stockfish_strategy import StockFishStrategy
_DIR = os.path.abspath(os.path.dirname(__file__)) _DIR = os.path.abspath(os.path.dirname(__file__))
@@ -23,12 +24,7 @@ def load_index() -> str:
class Simulate: class Simulate:
""" Run a simulation of two engines""" """ Run a simulation of two engines"""
def __init__(self, engine_white=None, engine_black=None): def __init__(self, engine_white, engine_black):
if engine_white is None:
engine_white = engine.ClassicMctsEngine(chess.WHITE)
if engine_black is None:
engine_black = engine.ClassicMctsEngine(chess.BLACK)
self.white = engine_white self.white = engine_white
self.black = engine_black self.black = engine_black
@@ -44,9 +40,13 @@ class Simulate:
class WebInterface: 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.white = white_engine
self.black = black_engine self.black = black_engine
self.strategy1 = strategy1
self.strategy2 = strategy2
self.stockfish_path = stockfish_path
self.lc0_path = lc0_path
self.limit = limit self.limit = limit
@@ -73,8 +73,9 @@ class WebInterface:
async def turns(): async def turns():
""" Simulates the game and sends the response to the client """ """ Simulates the game and sends the response to the client """
runner = Simulate(self.white(chess.Board(), chess.WHITE, StockFishStrategy()), self.black( white = EngineFactory.create_engine(self.white, self.strategy1, chess.WHITE, self.stockfish_path, self.lc0_path)
chess.Board(), chess.BLACK, StockFishStrategy())).run(self.limit) 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(): def sim():
return next(runner, None) return next(runner, None)

6
web.py
View File

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