1+ import matplotlib .pyplot as plt
12from pgn_parser import load_pgn_file , get_move_sequence
23from board import initialize_board , make_move
34from 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
510def 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
2470if __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 ()
0 commit comments