HaileyStorm
commited on
Commit
•
b3c0ce4
1
Parent(s):
38285b4
Update chess-gpt-eval/main.py
Browse files-Instead of the play_games max_games parameter controlling the number of games played, it now controls the number of games *saved* -- an important distinction because...
-We now track the transcript of all played games (reset for each new player_ones) and record/save only unique games. So, play_games will continue playing new games until the desired number of games have been saved.
-Additionally, when random_opening is True, we check for game over after each added move, (stop adding moves and) discard the game (/ start over adding moves) if the random moves cause the game to end.
- chess-gpt-eval/main.py +36 -24
chess-gpt-eval/main.py
CHANGED
@@ -348,7 +348,7 @@ def add_random_moves(
|
|
348 |
for i in range(num_moves * 2): # Full moves to half moves
|
349 |
legal_moves = list(board.legal_moves)
|
350 |
if not legal_moves:
|
351 |
-
|
352 |
|
353 |
move = board.san(random.choice(legal_moves))
|
354 |
board.push(board.parse_san(move))
|
@@ -358,6 +358,9 @@ def add_random_moves(
|
|
358 |
else:
|
359 |
game_state += f" {move}"
|
360 |
|
|
|
|
|
|
|
361 |
game_state = game_state.strip()
|
362 |
return game_state, board, num_moves
|
363 |
|
@@ -452,8 +455,10 @@ def play_games(
|
|
452 |
random_opening: bool = False,
|
453 |
random_opening_moves: int = 20,
|
454 |
):
|
455 |
-
|
456 |
-
|
|
|
|
|
457 |
|
458 |
with open("gpt_inputs/prompt.txt", "r") as f:
|
459 |
game_state = f.read()
|
@@ -462,7 +467,10 @@ def play_games(
|
|
462 |
if book_opening:
|
463 |
game_state, board, opening_moves = random_book_opening(game_state, board)
|
464 |
elif random_opening:
|
465 |
-
|
|
|
|
|
|
|
466 |
else:
|
467 |
opening_moves = 0
|
468 |
player_one_illegal_moves = 0
|
@@ -545,26 +553,30 @@ def play_games(
|
|
545 |
print(f"Result: {board.result()}")
|
546 |
print(board)
|
547 |
print()
|
548 |
-
|
549 |
-
|
550 |
-
|
551 |
-
|
552 |
-
|
553 |
-
|
554 |
-
|
555 |
-
|
556 |
-
|
557 |
-
|
558 |
-
|
559 |
-
|
560 |
-
|
561 |
-
|
562 |
-
|
563 |
-
|
564 |
-
|
565 |
-
|
566 |
-
|
567 |
-
|
|
|
|
|
|
|
|
|
568 |
if isinstance(player_one, StockfishPlayer):
|
569 |
player_one.close()
|
570 |
if isinstance(player_two, StockfishPlayer):
|
|
|
348 |
for i in range(num_moves * 2): # Full moves to half moves
|
349 |
legal_moves = list(board.legal_moves)
|
350 |
if not legal_moves:
|
351 |
+
return None, None, 0 # Game over, discard the game
|
352 |
|
353 |
move = board.san(random.choice(legal_moves))
|
354 |
board.push(board.parse_san(move))
|
|
|
358 |
else:
|
359 |
game_state += f" {move}"
|
360 |
|
361 |
+
if board.is_game_over():
|
362 |
+
return None, None, 0 # Game over, discard the game
|
363 |
+
|
364 |
game_state = game_state.strip()
|
365 |
return game_state, board, num_moves
|
366 |
|
|
|
455 |
random_opening: bool = False,
|
456 |
random_opening_moves: int = 20,
|
457 |
):
|
458 |
+
unique_games = set()
|
459 |
+
games_saved = 0
|
460 |
+
while games_saved < max_games:
|
461 |
+
print(f"\nGame {games_saved} of {max_games}\n")
|
462 |
|
463 |
with open("gpt_inputs/prompt.txt", "r") as f:
|
464 |
game_state = f.read()
|
|
|
467 |
if book_opening:
|
468 |
game_state, board, opening_moves = random_book_opening(game_state, board)
|
469 |
elif random_opening:
|
470 |
+
while True:
|
471 |
+
game_state, board, opening_moves = add_random_moves(game_state, board, random_opening_moves)
|
472 |
+
if game_state is not None:
|
473 |
+
break
|
474 |
else:
|
475 |
opening_moves = 0
|
476 |
player_one_illegal_moves = 0
|
|
|
553 |
print(f"Result: {board.result()}")
|
554 |
print(board)
|
555 |
print()
|
556 |
+
if game_transcript not in unique_games:
|
557 |
+
unique_games.add(game_transcript)
|
558 |
+
record_results(
|
559 |
+
board,
|
560 |
+
player_one,
|
561 |
+
player_two,
|
562 |
+
game_state,
|
563 |
+
player_one_illegal_moves,
|
564 |
+
player_one_illegal_attempts,
|
565 |
+
player_two_illegal_moves,
|
566 |
+
player_one_legal_moves,
|
567 |
+
player_two_legal_moves,
|
568 |
+
total_time,
|
569 |
+
player_one_resignation,
|
570 |
+
player_two_resignation,
|
571 |
+
player_one_failed_to_find_legal_move,
|
572 |
+
player_two_failed_to_find_legal_move,
|
573 |
+
total_moves,
|
574 |
+
illegal_moves,
|
575 |
+
opening_moves,
|
576 |
+
illegal_move_numbers
|
577 |
+
)
|
578 |
+
else:
|
579 |
+
print("Duplicate game; not saved.")
|
580 |
if isinstance(player_one, StockfishPlayer):
|
581 |
player_one.close()
|
582 |
if isinstance(player_two, StockfishPlayer):
|