added engine class and simulate_game function

This commit is contained in:
2024-01-27 17:31:02 +01:00
parent 662da27f72
commit 42fbf374c6
5 changed files with 157 additions and 85 deletions

40
main.py
View File

@@ -3,12 +3,37 @@ import chess.engine
from classic_mcts import ClassicMcts
import engine
import eval
import util
def simulate_game(white: engine.Engine, black: engine.Engine) -> chess.pgn.Game:
board = chess.Board()
is_white_playing = True
while not board.is_game_over():
play_result = white.play(board) if is_white_playing else black.play(board)
board.push(play_result.move)
print(board)
print()
is_white_playing = not is_white_playing
game = chess.pgn.Game.from_board(board)
game.headers['White'] = white.get_name()
game.headers['Black'] = black.get_name()
return game
def test_simulate():
white = engine.ClassicMctsEngine(chess.WHITE)
black = engine.ClassicMctsEngine(chess.BLACK)
game = simulate_game(white, black)
print(game)
def test_mcts():
fools_mate = "rnbqkbnr/pppp1ppp/4p3/8/5PP1/8/PPPPP2P/RNBQKBNR b KQkq f3 0 2"
board = chess.Board(fools_mate)
mcts_root = ClassicMcts(board)
mcts_root = ClassicMcts(board, chess.BLACK)
mcts_root.build_tree()
sorted_moves = sorted(mcts_root.children, key=lambda x: x.move.uci())
for c in sorted_moves:
@@ -21,7 +46,7 @@ def test_stockfish():
moves = {}
untried_moves = list(board.legal_moves)
for move in untried_moves:
engine.simulate_game(board, move, 100)
util.simulate_game(board, move, 100)
moves[move] = board
board = chess.Board(fools_mate)
@@ -35,7 +60,7 @@ def test_stockfish_prob():
moves = {}
untried_moves = list(board.legal_moves)
for move in untried_moves:
mean, std = engine.simulate_stockfish_prob(board, move, 10, 4)
mean, std = util.simulate_stockfish_prob(board, move, 10, 4)
moves[move] = (mean, std)
board = chess.Board(fools_mate)
@@ -47,14 +72,15 @@ def test_stockfish_prob():
def analyze_results(moves: dict):
for m, b in moves.items():
manual_score = eval.score_manual(b)
engine_score = eval.score_stockfish(b).white()
engine_score = eval.score_stockfish(b).white().score(mate_score=100_000)
print(f"score for move {m}: manual_score={manual_score}, engine_score={engine_score}")
def main():
test_mcts()
test_stockfish()
test_stockfish_prob()
test_simulate()
# test_mcts()
# test_stockfish()
# test_stockfish_prob()
if __name__ == '__main__':