diff --git a/chesspp/engine_factory.py b/chesspp/engine_factory.py index bc9a74c..df31818 100644 --- a/chesspp/engine_factory.py +++ b/chesspp/engine_factory.py @@ -57,20 +57,20 @@ class EngineFactory: return EngineFactory.lc0_engine(color, lc0_path) @staticmethod - def stockfish_engine(color: chess.Color, engine_path: str, stockfish_elo: int, board: chess.Board | None = chess.Board()) -> Engine: - return StockFishEngine(board, color, stockfish_elo, engine_path) + def stockfish_engine(color: chess.Color, engine_path: str, stockfish_elo: int) -> Engine: + return StockFishEngine(chess.Board(), color, stockfish_elo, engine_path) @staticmethod - def lc0_engine(color: chess.Color, engine_path: str, board: chess.Board | None = chess.Board()) -> Engine: - return Lc0Engine(board, color, engine_path) + def lc0_engine(color: chess.Color, engine_path: str) -> Engine: + return Lc0Engine(chess.Board(), color, engine_path) @staticmethod - def bayesian_mcts(color: chess.Color, strategy: IStrategy, board: chess.Board | None = chess.Board()) -> Engine: - return BayesMctsEngine(board, color, strategy) + def bayesian_mcts(color: chess.Color, strategy: IStrategy) -> Engine: + return BayesMctsEngine(chess.Board(), color, strategy) @staticmethod - def classic_mcts(color: chess.Color, strategy: IStrategy, board: chess.Board | None = chess.Board()) -> Engine: - return ClassicMctsEngine(board, color, strategy) + def classic_mcts(color: chess.Color, strategy: IStrategy) -> Engine: + return ClassicMctsEngine(chess.Board(), color, strategy) @staticmethod def _get_random_strategy(rollout_depth: int) -> IStrategy: @@ -91,3 +91,4 @@ class EngineFactory: @staticmethod def _get_pesto_strategy(rollout_depth: int) -> IStrategy: return PestoStrategy(rollout_depth) + diff --git a/chesspp/mcts/baysian_mcts_node.py b/chesspp/mcts/baysian_mcts_node.py index a94a38a..16c5fe3 100644 --- a/chesspp/mcts/baysian_mcts_node.py +++ b/chesspp/mcts/baysian_mcts_node.py @@ -97,8 +97,8 @@ class BayesianMctsNode(IMctsNode): copied_board.push(m) steps += 1 - steps = max(1, steps) - score = int(self.strategy.analyze_board(copied_board) / (math.log2(steps) + 1)) + steps = max(2, steps) + score = int(self.strategy.analyze_board(copied_board) / math.log2(steps)) self.result = score return score diff --git a/chesspp/mcts/classic_mcts.py b/chesspp/mcts/classic_mcts.py index ae2586b..02fe053 100644 --- a/chesspp/mcts/classic_mcts.py +++ b/chesspp/mcts/classic_mcts.py @@ -1,3 +1,5 @@ +import math + import chess import random import numpy as np @@ -7,7 +9,7 @@ from chesspp.i_strategy import IStrategy class ClassicMcts: def __init__(self, board: chess.Board, color: chess.Color, strategy: IStrategy, parent=None, move: chess.Move | None = None, - random_state: int | None = None): + random_state: int | None = None, depth: int = 0): self.random = random.Random(random_state) self.board = board self.color = color @@ -19,6 +21,7 @@ class ClassicMcts: self.legal_moves = list(board.legal_moves) self.untried_actions = self.legal_moves self.score = 0 + self.depth = depth def _expand(self) -> 'ClassicMcts': """ @@ -29,7 +32,7 @@ class ClassicMcts: self.untried_actions.remove(move) next_board = self.board.copy() next_board.push(move) - child_node = ClassicMcts(next_board, color=not self.color, strategy=self.strategy, parent=self, move=move) + child_node = ClassicMcts(next_board, color=not self.color, strategy=self.strategy, parent=self, move=move, depth=self.depth+1) self.children.append(child_node) return child_node @@ -40,7 +43,7 @@ class ClassicMcts: :return: the score of the rolled out game """ copied_board = self.board.copy() - steps = 1 + steps = self.depth for i in range(rollout_depth): if copied_board.is_game_over(): break @@ -49,7 +52,8 @@ class ClassicMcts: copied_board.push(m) steps += 1 - return self.strategy.analyze_board(copied_board) // steps + steps = max(2, steps) + return int(self.strategy.analyze_board(copied_board) / math.log2(steps)) def _backpropagate(self, score: float) -> None: """