From bc97745a5164d2d289f4e5c9554b8da14b0a93b9 Mon Sep 17 00:00:00 2001 From: rapidarray1211 Date: Thu, 28 Nov 2024 15:22:16 -0500 Subject: [PATCH] main measures ai v ai over 10 games and evaluates it and outputs a graph --- __pycache__/board.cpython-312.pyc | Bin 994 -> 994 bytes __pycache__/stockfish.cpython-312.pyc | Bin 4234 -> 4150 bytes gui.py | 1 - main.py | 53 +++++++++++++++++++++++++- stockfish.py | 5 ++- 5 files changed, 54 insertions(+), 5 deletions(-) diff --git a/__pycache__/board.cpython-312.pyc b/__pycache__/board.cpython-312.pyc index 3496d306f4a6e8e2a13a0d0afece4407905cf770..cf4388ce0300ace67451c3b843675cf41dbd5b63 100644 GIT binary patch delta 19 ZcmaFF{)nCHG%qg~0}$BeY~;GZ3;;Gu1q%QG delta 19 ZcmaFF{)nCHG%qg~0}$+u*vNH*82~uv1$O`d diff --git a/__pycache__/stockfish.cpython-312.pyc b/__pycache__/stockfish.cpython-312.pyc index cb0329d7a7929ae4f86a9c52bb97c5b982a9a83b..31ac8dbfdf6846958541d3c81c671cf095972afc 100644 GIT binary patch delta 319 zcmeBD+@`>LnwOW00SMSmdZgQJo8cfItd+4a;nXxlF4WA$&%L35-2nldU;TwGnDK zYIssOXR{!y%Hu{dgKP3^PAR<_reFqT21ABo#&RY`hDZiRAOwk3Fh??!Giq}C6>$RX zNS`$m1Wr!A$7h3l%&E#}0e;#+Kq1qG>jDMf-nC6l?ipE7oD{>aV6$il^-q&iue zH%tA41p|-t1s19AmJD3BFwU=HUZDDeEN)Eh0?fw+Sly*I&*hb6WVD;Sn{N-J^W+Nt QFRY4;(jVL=7YaxM0PO=zI{*Lx delta 453 zcmdm{(51+GnwOW00SKIO+|v~{@?K}<0&$sv`18HV?^vfWMo(^Li)WRoVayVqT*xlG z`4`(kM!OXD8kX4%DI7H%K$5eDYc|7Nw$+Rva~K#H8A_BPECz-ejvB5ipn)*CTJ9RA zEY-;uITRKBN)%zLQn)~-&1HhAVq}=W*b_HdnA5Z#p@zGLCxvG=3$m&_ZX`2!5xP?t zKyWn^#C%4EN(N;HLxy6;awbNGNTB;bA{ES$4CRcPe11ioKxOF=r4Q^^GTve-$;?eH z;s%Oj3QRuDDWt^an_re{rBKDCplhe#T9%jtWb^7MIHeYsC_tnp-{HI_R3r%0a*H`J zsrVLKVnIP_UdrSnTu&KiY+l99#i+o>ASgD2ZAQh6tQA}vtTyQE$UWhI!8_)HSnL&n zxF?D#lXviBNnGc&xyWhrtC$yP(m@tCF?VI=W5C#Uo6VRV|T!vBR; Nfzj}T%Vb#rNdWyvX@dX& 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))