Move files into chesspp module

This commit is contained in:
2024-01-27 21:08:37 +01:00
parent 17e5bebd88
commit 69aa9ce2d9
10 changed files with 9 additions and 9 deletions

0
chesspp/__init__.py Normal file
View File

109
chesspp/classic_mcts.py Normal file
View File

@@ -0,0 +1,109 @@
import chess
import random
import eval
import util
import numpy as np
class ClassicMcts:
def __init__(self, board: chess.Board, color: chess.Color, parent=None, move: chess.Move | None = None,
random_state: int | None = None):
self.random = random.Random(random_state)
self.board = board
self.color = color
self.parent = parent
self.move = move
self.children = []
self.visits = 0
self.legal_moves = list(board.legal_moves)
self.untried_actions = self.legal_moves
self.score = 0
def _expand(self) -> 'ClassicMcts':
"""
Expands the node, i.e., choose an action and apply it to the board
:return:
"""
move = self.random.choice(self.untried_actions)
self.untried_actions.remove(move)
next_board = self.board.copy()
next_board.push(move)
child_node = ClassicMcts(next_board, color=self.color, parent=self, move=move)
self.children.append(child_node)
return child_node
def _rollout(self, rollout_depth: int = 20) -> int:
"""
Rolls out the node by simulating a game for a given depth.
Sometimes this step is called 'simulation' or 'playout'.
:return: the score of the rolled out game
"""
copied_board = self.board.copy()
steps = 1
for i in range(rollout_depth):
if copied_board.is_game_over():
break
m = util.pick_move(copied_board)
copied_board.push(m)
steps += 1
return eval.score_manual(copied_board) // steps
def _backpropagate(self, score: float) -> None:
"""
Backpropagates the results of the rollout
:param score:
:return:
"""
self.visits += 1
# TODO: maybe use score + num of moves together (a win in 1 move is better than a win in 20 moves)
self.score += score
if self.parent:
self.parent._backpropagate(score)
def is_fully_expanded(self) -> bool:
return len(self.untried_actions) == 0
def _best_child(self) -> 'ClassicMcts':
"""
Picks the best child according to our policy
:return: the best child
"""
# NOTE: maybe clamp the score between [-1, +1] instead of [-inf, +inf]
choices_weights = [(c.score / c.visits) + np.sqrt(((2 * np.log(self.visits)) / c.visits))
for c in self.children]
best_child_index = np.argmax(choices_weights) if self.color == chess.WHITE else np.argmin(choices_weights)
return self.children[best_child_index]
def _select_leaf(self) -> 'ClassicMcts':
"""
Selects a leaf node.
If the node is not expanded is will be expanded.
:return: Leaf node
"""
current_node = self
while not current_node.board.is_game_over():
if not current_node.is_fully_expanded():
return current_node._expand()
else:
current_node = current_node._best_child()
return current_node
def build_tree(self, samples: int = 1000) -> 'ClassicMcts':
"""
Runs the MCTS with the given number of samples
:param samples: number of simulations
:return: best node containing the best move
"""
for i in range(samples):
# selection & expansion
# rollout
# backpropagate score
node = self._select_leaf()
score = node._rollout()
node._backpropagate(score)
return self._best_child()

60
chesspp/engine.py Normal file
View File

@@ -0,0 +1,60 @@
from abc import ABC, abstractmethod
import chess
import chess.engine
from classic_mcts import ClassicMcts
import random
class Engine(ABC):
color: chess.Color
"""The side the engine plays (``chess.WHITE`` or ``chess.BLACK``)."""
def __init__(self, color: chess.Color):
self.color = color
@abstractmethod
def play(self, board: chess.Board) -> chess.engine.PlayResult:
"""
Return the next action the engine chooses based on the given board
:param board: the chess board
:return: the engine's PlayResult
"""
pass
@staticmethod
@abstractmethod
def get_name() -> str:
"""
Return the engine's name
:return: the engine's name
"""
pass
class ClassicMctsEngine(Engine):
def __init__(self, color: chess.Color):
super().__init__(color)
@staticmethod
def get_name() -> str:
return "ClassicMctsEngine"
def play(self, board: chess.Board) -> chess.engine.PlayResult:
mcts_root = ClassicMcts(board, self.color)
mcts_root.build_tree()
best_move = max(mcts_root.children, key=lambda x: x.score).move if board.turn == chess.WHITE else (
min(mcts_root.children, key=lambda x: x.score).move)
return chess.engine.PlayResult(move=best_move, ponder=None)
class RandomEngine(Engine):
def __init__(self, color: chess.Color):
super().__init__(color)
@staticmethod
def get_name() -> str:
return "Random"
def play(self, board: chess.Board) -> chess.engine.PlayResult:
move = random.choice(list(board.legal_moves))
return chess.engine.PlayResult(move=move, ponder=None)

183
chesspp/eval.py Normal file
View File

@@ -0,0 +1,183 @@
import chess
import chess.engine
import sys
# Eval constants for scoring chess boards
# Evaluation metric inspired by Tomasz Michniewski: https://www.chessprogramming.org/Simplified_Evaluation_Function
PIECE_VALUES = {
chess.PAWN: 100,
chess.KNIGHT: 320,
chess.BISHOP: 330,
chess.ROOK: 500,
chess.QUEEN: 900,
chess.KING: 20000
}
pawn_eval = [
0, 0, 0, 0, 0, 0, 0, 0,
5, 10, 10, -20, -20, 10, 10, 5,
5, -5, -10, 0, 0, -10, -5, 5,
0, 0, 0, 20, 20, 0, 0, 0,
5, 5, 10, 25, 25, 10, 5, 5,
10, 10, 20, 30, 30, 20, 10, 10,
50, 50, 50, 50, 50, 50, 50, 50,
0, 0, 0, 0, 0, 0, 0, 0
]
knight_eval = [
-50, -40, -30, -30, -30, -30, -40, -50,
-40, -20, 0, 0, 0, 0, -20, -40,
-30, 0, 10, 15, 15, 10, 0, -30,
-30, 5, 15, 20, 20, 15, 5, -30,
-30, 0, 15, 20, 20, 15, 0, -30,
-30, 5, 10, 15, 15, 10, 5, -30,
-40, -20, 0, 5, 5, 0, -20, -40,
-50, -40, -30, -30, -30, -30, -40, -50
]
bishop_eval = [
-20, -10, -10, -10, -10, -10, -10, -20,
-10, 5, 0, 0, 0, 0, 5, -10,
-10, 10, 10, 10, 10, 10, 10, -10,
-10, 0, 10, 10, 10, 10, 0, -10,
-10, 5, 5, 10, 10, 5, 5, -10,
-10, 0, 5, 10, 10, 5, 0, -10,
-10, 0, 0, 0, 0, 0, 0, -10,
-20, -10, -10, -10, -10, -10, -10, -20
]
rook_eval = [
0, 0, 0, 5, 5, 0, 0, 0,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
5, 10, 10, 10, 10, 10, 10, 5,
0, 0, 0, 0, 0, 0, 0, 0
]
queen_eval = [
-20, -10, -10, -5, -5, -10, -10, -20,
-10, 0, 0, 0, 0, 0, 0, -10,
-10, 0, 5, 5, 5, 5, 0, -10,
-5, 0, 5, 5, 5, 5, 0, -5,
0, 0, 5, 5, 5, 5, 0, -5,
-10, 5, 5, 5, 5, 5, 0, -10,
-10, 0, 5, 0, 0, 0, 0, -10,
-20, -10, -10, -5, -5, -10, -10, -20
]
king_eval = [
20, 30, 10, 0, 0, 10, 30, 20,
20, 20, 0, 0, 0, 0, 20, 20,
-10, -20, -20, -20, -20, -20, -20, -10,
20, -30, -30, -40, -40, -30, -30, -20,
-30, -40, -40, -50, -50, -40, -40, -30,
-30, -40, -40, -50, -50, -40, -40, -30,
-30, -40, -40, -50, -50, -40, -40, -30,
-30, -40, -40, -50, -50, -40, -40, -30
]
king_endgame_eval = [
50, -30, -30, -30, -30, -30, -30, -50,
-30, -30, 0, 0, 0, 0, -30, -30,
-30, -10, 20, 30, 30, 20, -10, -30,
-30, -10, 30, 40, 40, 30, -10, -30,
-30, -10, 30, 40, 40, 30, -10, -30,
-30, -10, 20, 30, 30, 20, -10, -30,
-30, -20, -10, 0, 0, -10, -20, -30,
-50, -40, -30, -20, -20, -30, -40, -50
]
PIECE_TABLES = {
chess.WHITE: {
chess.PAWN: pawn_eval,
chess.KNIGHT: knight_eval,
chess.BISHOP: bishop_eval,
chess.ROOK: rook_eval,
chess.QUEEN: queen_eval,
chess.KING: king_eval,
'end_game_king': king_endgame_eval
},
chess.BLACK: {
chess.PAWN: list(reversed(pawn_eval)),
chess.KNIGHT: list(reversed(knight_eval)),
chess.BISHOP: list(reversed(bishop_eval)),
chess.ROOK: list(reversed(rook_eval)),
chess.QUEEN: list(reversed(queen_eval)),
chess.KING: list(reversed(king_eval)),
'end_game_king': list(reversed(king_endgame_eval))
}
}
def check_endgame(board: chess.Board) -> bool:
"""
Endgame according to Tomasz Michniewski:
1. Both sides have no queens or
2. Every side which has a queen has additionally no other pieces or one minorpiece maximum.
"""
queens_white = 0
minors_white = 0
queens_black = 0
minors_black = 0
for s in chess.SQUARES:
piece = board.piece_at(s)
if piece is None:
continue
if piece.piece_type == chess.QUEEN:
if piece.color == chess.WHITE:
queens_white += 1
else:
queens_black += 1
if piece.piece_type == chess.BISHOP or piece.piece_type == chess.KNIGHT:
if piece.color == chess.WHITE:
minors_white += 1
else:
minors_black += 1
return (queens_black == 0 and queens_white == 0) or ((queens_black >= 1 >= minors_black) or (
queens_white >= 1 >= minors_white))
def score_manual(board: chess.Board) -> int:
"""
Calculate the score of the given board regarding the given color
:param board: the chess board
:return: score metric
"""
outcome = board.outcome()
if outcome is not None:
if outcome.termination == chess.Termination.CHECKMATE:
return sys.maxsize if outcome.winner == chess.WHITE else -sys.maxsize
else: # draw
return 0
score = 0
for s in chess.SQUARES:
piece = board.piece_at(s)
if piece is None:
continue
if piece.color == chess.WHITE:
if piece.piece_type == chess.KING and check_endgame(board):
score += PIECE_VALUES[piece.piece_type] * PIECE_TABLES[chess.WHITE]['end_game_king'][s]
else:
score += PIECE_VALUES[piece.piece_type] * PIECE_TABLES[chess.WHITE][piece.piece_type][s]
else:
if piece.piece_type == chess.KING and check_endgame(board):
score -= PIECE_VALUES[piece.piece_type] * PIECE_TABLES[chess.BLACK]['end_game_king'][s]
else:
score -= PIECE_VALUES[piece.piece_type] * PIECE_TABLES[chess.BLACK][piece.piece_type][s]
return score
def score_stockfish(board: chess.Board) -> chess.engine.PovScore:
"""
Calculate the score of the given board using stockfish
:param board:
:return:
"""
engine = chess.engine.SimpleEngine.popen_uci("./stockfish/stockfish-ubuntu-x86-64-avx2")
info = engine.analyse(board, chess.engine.Limit(depth=0))
engine.quit()
return info["score"]

51
chesspp/i_mcts.py Normal file
View File

@@ -0,0 +1,51 @@
import chess
from abc import ABC, abstractmethod
from i_strategy import IStrategy
from typing import Dict
class IMcts(ABC):
def __init__(self, board: chess.Board, strategy: IStrategy):
self.board = board
@abstractmethod
def sample(self, runs: int = 1000) -> None:
"""
Run the MCTS simulation
:param runs: number of runs
:return:
"""
pass
@abstractmethod
def apply_move(self, move: chess.Move) -> None:
"""
Apply the move to the chess board
:param move: move to apply
:return:
"""
pass
@abstractmethod
def get_children(self) -> list['IMcts']:
"""
Return the immediate children of the root node
:return: list of immediate children of mcts root
"""
pass
@abstractmethod
def get_moves(self) -> Dict[chess.Move, int]:
"""
Return all legal moves from this node with respective scores
:return: dictionary with moves as key and scores as values
"""
pass
"""
TODO: add score class:
how many moves until the end of the game?
score ranges?
perspective of white/black
"""

8
chesspp/i_strategy.py Normal file
View File

@@ -0,0 +1,8 @@
from abc import ABC, abstractmethod
# TODO extend class
class IStrategy(ABC):
@abstractmethod
def pick_next_move(self, ):
pass

20
chesspp/lichess-engine.py Normal file
View File

@@ -0,0 +1,20 @@
from lichess_bot.lib.engine_wrapper import MinimalEngine, MOVE
import chess.engine
import engine
class ProbStockfish(MinimalEngine):
def search(self, board: chess.Board, time_limit: chess.engine.Limit, ponder: bool, draw_offered: bool,
root_moves: MOVE) -> chess.engine.PlayResult:
moves = {}
untried_moves = list(board.legal_moves)
for move in untried_moves:
mean, std = engine.simulate_game(board, move, 10)
moves[move] = (mean, std)
return self.get_best_move(moves)
def get_best_move(self, moves: dict) -> chess.engine.PlayResult:
best_avg = max(moves.items(), key=lambda m: m[1][0])
next_move = best_avg[0]
return chess.engine.PlayResult(next_move, None)

72
chesspp/simulation.py Normal file
View File

@@ -0,0 +1,72 @@
import multiprocessing as mp
import random
import chess
import chess.pgn
from typing import Tuple, List
from enum import Enum
from dataclasses import dataclass
from engine import Engine
class Winner(Enum):
Engine_A = 0
Engine_B = 1
Draw = 2
@dataclass
class EvaluationResult:
winner: Winner
game: chess.pgn.Game
def simulate_game(white: Engine, black: 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)
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
class Evaluation:
def __init__(self, engine_a: Engine.__class__, engine_b: Engine.__class__):
self.engine_a = engine_a
self.engine_b = engine_b
def run(self, n_games=100) -> List[EvaluationResult]:
with mp.Pool(mp.cpu_count()) as pool:
args = [(self.engine_a, self.engine_b) for i in range(n_games)]
return pool.map(Evaluation._test_simulate, args)
@staticmethod
def _test_simulate(arg: Tuple[Engine.__class__, Engine.__class__]) -> EvaluationResult:
engine_a, engine_b = arg
flip_engines = bool(random.getrandbits(1))
if flip_engines:
black, white = engine_a(chess.BLACK), engine_b(chess.WHITE)
else:
white, black = engine_a(chess.WHITE), engine_b(chess.BLACK)
game = simulate_game(white, black)
winner = game.end().board().outcome().winner
result = Winner.Draw
match (winner, flip_engines):
case (chess.WHITE, True):
result = Winner.Engine_B
case (chess.BLACK, True):
result = Winner.Engine_A
case (chess.WHITE, False):
result = Winner.Engine_A
case (chess.BLACK, False):
result = Winner.Engine_B
return EvaluationResult(result, game)

79
chesspp/util.py Normal file
View File

@@ -0,0 +1,79 @@
import chess
import chess.engine
from stockfish import Stockfish
import numpy as np
import random
def pick_move(board: chess.Board) -> chess.Move | None:
"""
Pick a random move
:param board: chess board
:return: a valid move or None if no valid move available
"""
if len(list(board.legal_moves)) == 0:
return None
return random.choice(list(board.legal_moves))
def simulate_game(board: chess.Board, move: chess.Move, depth: int):
"""
Simulate a game starting with the given move
:param board: chess board
:param move: chosen move
:param depth: number of moves that should be simulated after playing the chosen move
:return: the score for the simulated game
"""
engine = chess.engine.SimpleEngine.popen_uci("./stockfish/stockfish-ubuntu-x86-64-avx2")
board.push(move)
for i in range(depth):
if board.is_game_over():
engine.quit()
return
r = engine.play(board, chess.engine.Limit(depth=2))
board.push(r.move)
engine.quit()
def simulate_stockfish_prob(board: chess.Board, move: chess.Move, games: int = 10, depth: int = 10) -> (float, float):
"""
Simulate a game using
:param board: chess board
:param move: chosen move
:param games: number of games that should be simulated after playing the move
:param depth: simulation depth per game
:return:
"""
board.push(move)
copied_board = board.copy()
scores = []
stockfish = Stockfish("./stockfish/stockfish-ubuntu-x86-64-avx2", depth=2, parameters={"Threads": 8, "Hash": 2048})
stockfish.set_elo_rating(1200)
stockfish.set_fen_position(board.fen())
def reset_game():
nonlocal scores, copied_board, board
score = eval.score_stockfish(copied_board).white().score(mate_score=100_000)
scores.append(score)
copied_board = board.copy()
stockfish.set_fen_position(board.fen())
for _ in range(games):
for d in range(depth):
if copied_board.is_game_over() or d == depth - 1:
reset_game()
break
if d == depth - 1:
reset_game()
top_moves = stockfish.get_top_moves(3)
chosen_move = random.choice(top_moves)['Move']
stockfish.make_moves_from_current_position([chosen_move])
copied_board.push(chess.Move.from_uci(chosen_move))
print(scores)
# TODO: return distribution here?
return np.array(scores).mean(), np.array(scores).std()