diff --git a/__pycache__/board.cpython-312.pyc b/__pycache__/board.cpython-312.pyc index 3496d30..cf4388c 100644 Binary files a/__pycache__/board.cpython-312.pyc and b/__pycache__/board.cpython-312.pyc differ diff --git a/__pycache__/stockfish.cpython-312.pyc b/__pycache__/stockfish.cpython-312.pyc index cb0329d..31ac8db 100644 Binary files a/__pycache__/stockfish.cpython-312.pyc and b/__pycache__/stockfish.cpython-312.pyc differ diff --git a/gui.py b/gui.py index 5423bcc..34561f8 100644 --- a/gui.py +++ b/gui.py @@ -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 diff --git a/main.py b/main.py index 7b110b0..ed01a98 100644 --- a/main.py +++ b/main.py @@ -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 @@ -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() diff --git a/stockfish.py b/stockfish.py index 4684bfb..ae21b6f 100644 --- a/stockfish.py +++ b/stockfish.py @@ -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) @@ -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) @@ -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))