fixed limit for external engines and added arguments to web.py
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user