Skip to content

Commit dbb51a5

Browse files
Merge pull request #7 from rapidarray1211/stockfish_eval_centipawn_loss
main measures ai v ai over 10 games and evaluates it and outputs a graph
2 parents e4436c7 + bc97745 commit dbb51a5

File tree

5 files changed

+54
-5
lines changed

5 files changed

+54
-5
lines changed

__pycache__/board.cpython-312.pyc

0 Bytes
Binary file not shown.
-84 Bytes
Binary file not shown.

gui.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#https://blog.devgenius.io/simple-interactive-chess-gui-in-python-c6d6569f7b6c
1515

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

1918
def highlight_king_square(scrn, outcome, BOARD):
2019
# Find the position of the checkmated king

main.py

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
import matplotlib.pyplot as plt
12
from pgn_parser import load_pgn_file, get_move_sequence
23
from board import initialize_board, make_move
34
from stockfish import evaluate_move_stockfish, close_engine, evaluate_game_stockfish
5+
from engine.chessEngine import ChessEngine
6+
import numpy as np
7+
8+
engine = ChessEngine("models/movepredictorV2_24.keras", 1, 3)
49

510
def play_game_pgn(pgn_file):
611
# Load games from the PGN file
@@ -16,10 +21,54 @@ def play_game_pgn(pgn_file):
1621
for move in move_sequence:
1722
print(move)
1823
make_move(board, move)
19-
evaluate_move_stockfish(board)
2024
evaluate_game_stockfish(move_sequence)
2125
close_engine()
26+
27+
def play_ai_vs_ai(num_games=100):
28+
all_accuracy_scores = []
29+
30+
for game_index in range(num_games):
31+
print(f"Playing game {game_index + 1}/{num_games}")
32+
board = initialize_board()
33+
move_sequence = []
34+
while not board.is_game_over():
35+
move = engine.predict_best_move(board, True)
36+
move_sequence.append(move.uci())
37+
make_move(board, move.uci())
38+
39+
accuracy_scores = evaluate_game_stockfish(move_sequence)
40+
all_accuracy_scores.append(accuracy_scores)
41+
42+
return all_accuracy_scores
43+
44+
def visualize_accuracy_scores(all_accuracy_scores):
45+
# Find the maximum number of moves in any game
46+
max_moves = max(len(scores) for scores in all_accuracy_scores)
47+
48+
# Initialize a list to hold the sum of centipawn losses for each move number
49+
summed_scores = np.zeros(max_moves)
50+
move_counts = np.zeros(max_moves)
51+
52+
# Sum the centipawn losses for each move number across all games
53+
for game_scores in all_accuracy_scores:
54+
for i, score in enumerate(game_scores):
55+
if score <= 8000: # Filter out scores greater than 8000
56+
capped_score = min(score, 1000) # Cap the score at 1000
57+
summed_scores[i] += capped_score
58+
move_counts[i] += 1
59+
60+
# Calculate the average centipawn loss for each move number
61+
average_scores = summed_scores / move_counts
2262

63+
# Plot the average centipawn loss per move
64+
plt.plot(average_scores)
65+
plt.xlabel('Move Number')
66+
plt.ylabel('Average Centipawn Loss')
67+
plt.title('Average Centipawn Loss per Move (Capped at 1000)')
68+
plt.show()
2369

2470
if __name__ == "__main__":
25-
play_game_pgn("data/lichess_dataset_1.pgn")
71+
num_games = 10
72+
all_accuracy_scores = play_ai_vs_ai(num_games)
73+
visualize_accuracy_scores(all_accuracy_scores)
74+
close_engine()

stockfish.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def evaluate_game_stockfish(move_sequence):
3636
board.push(move)
3737

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

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

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

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

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

0 commit comments

Comments
 (0)