Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified __pycache__/board.cpython-312.pyc
Binary file not shown.
Binary file modified __pycache__/stockfish.cpython-312.pyc
Binary file not shown.
1 change: 0 additions & 1 deletion gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#https://blog.devgenius.io/simple-interactive-chess-gui-in-python-c6d6569f7b6c

engine = ChessEngine("models/movepredictorV2_24.keras", 1, 3)
print("testing")

def highlight_king_square(scrn, outcome, BOARD):
# Find the position of the checkmated king
Expand Down
53 changes: 51 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import matplotlib.pyplot as plt
from pgn_parser import load_pgn_file, get_move_sequence
from board import initialize_board, make_move
from stockfish import evaluate_move_stockfish, close_engine, evaluate_game_stockfish
from engine.chessEngine import ChessEngine
import numpy as np

engine = ChessEngine("models/movepredictorV2_24.keras", 1, 3)

def play_game_pgn(pgn_file):
# Load games from the PGN file
Expand All @@ -16,10 +21,54 @@ def play_game_pgn(pgn_file):
for move in move_sequence:
print(move)
make_move(board, move)
evaluate_move_stockfish(board)
evaluate_game_stockfish(move_sequence)
close_engine()

def play_ai_vs_ai(num_games=100):
all_accuracy_scores = []

for game_index in range(num_games):
print(f"Playing game {game_index + 1}/{num_games}")
board = initialize_board()
move_sequence = []
while not board.is_game_over():
move = engine.predict_best_move(board, True)
move_sequence.append(move.uci())
make_move(board, move.uci())

accuracy_scores = evaluate_game_stockfish(move_sequence)
all_accuracy_scores.append(accuracy_scores)

return all_accuracy_scores

def visualize_accuracy_scores(all_accuracy_scores):
# Find the maximum number of moves in any game
max_moves = max(len(scores) for scores in all_accuracy_scores)

# Initialize a list to hold the sum of centipawn losses for each move number
summed_scores = np.zeros(max_moves)
move_counts = np.zeros(max_moves)

# Sum the centipawn losses for each move number across all games
for game_scores in all_accuracy_scores:
for i, score in enumerate(game_scores):
if score <= 8000: # Filter out scores greater than 8000
capped_score = min(score, 1000) # Cap the score at 1000
summed_scores[i] += capped_score
move_counts[i] += 1

# Calculate the average centipawn loss for each move number
average_scores = summed_scores / move_counts

# Plot the average centipawn loss per move
plt.plot(average_scores)
plt.xlabel('Move Number')
plt.ylabel('Average Centipawn Loss')
plt.title('Average Centipawn Loss per Move (Capped at 1000)')
plt.show()

if __name__ == "__main__":
play_game_pgn("data/lichess_dataset_1.pgn")
num_games = 10
all_accuracy_scores = play_ai_vs_ai(num_games)
visualize_accuracy_scores(all_accuracy_scores)
close_engine()
5 changes: 3 additions & 2 deletions stockfish.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def evaluate_game_stockfish(move_sequence):
board.push(move)

evaluation = engine.analyse(board, chess.engine.Limit(depth=stockfish_depth))
best_move = engine.play(board, chess.engine.Limit(time=0.5))
best_move = engine.play(board, chess.engine.Limit(time=0.2))
# If increase time, it increases accuracy but decreases speed (obviously)

eval_score = evaluation['score'].relative.score(mate_score=10000)
Expand All @@ -45,7 +45,7 @@ def evaluate_game_stockfish(move_sequence):
best_move_score = best_move_evaluation['score'].relative.score(mate_score=10000)
# We compare the centipawn loss between the best move, and the move given to get a delta. Average the delta for average centipawn loss

print(f"Move: {move_str} -> Eval: {eval_score}, Best Move: {best_move_score}")
# print(f"Move: {move_str} -> Eval: {eval_score}, Best Move: {best_move_score}")

accuracy_score = abs(eval_score - best_move_score)
accuracy_scores.append(accuracy_score)
Expand All @@ -57,6 +57,7 @@ def evaluate_game_stockfish(move_sequence):
print(f"Average centipawn loss: {average_accuracy:.2f}")
else:
print("No valid moves to evaluate.")
return accuracy_scores

def generate_move(board: chess.Board):
result = engine.play(board, chess.engine.Limit(time=2.0))
Expand Down