HaileyStorm commited on
Commit
67472fe
1 Parent(s): 601d60d

Added random move opening option

Browse files
Files changed (1) hide show
  1. chess-gpt-eval/main.py +33 -6
chess-gpt-eval/main.py CHANGED
@@ -313,7 +313,7 @@ def get_player_titles_and_time(
313
 
314
 
315
  used_openings = []
316
- def initialize_game_with_opening(
317
  game_state: str, board: chess.Board
318
  ) -> Tuple[str, chess.Board]:
319
  global used_openings
@@ -342,6 +342,26 @@ def initialize_game_with_opening(
342
  return game_state.rstrip(), board, len(tokens) // 2
343
 
344
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
345
  # Return is (move_san, move_uci, attempts, is_resignation, is_illegal_move)
346
  def get_legal_move(
347
  player: Player,
@@ -428,7 +448,9 @@ def play_game(
428
  player_one: Player,
429
  player_two: Player,
430
  max_games: int = 10,
431
- random_opening_seed: bool = False,
 
 
432
  ):
433
  for z in range(max_games):
434
  print(f"\nGame {z} of {max_games}\n")
@@ -437,8 +459,10 @@ def play_game(
437
  game_state = f.read()
438
  board = chess.Board()
439
 
440
- if random_opening_seed:
441
- game_state, board, opening_moves = initialize_game_with_opening(game_state, board)
 
 
442
  else:
443
  opening_moves = 0
444
  player_one_illegal_moves = 0
@@ -557,10 +581,13 @@ recording_file = "logs/determine.csv" # default recording file. Because we are u
557
  player_ones = ["50M/ckpt_9120050b.pt"]
558
  player_two_recording_name = "lc0_sweep" #"stockfish_sweep"
559
  move_num_in_gamestate = False
 
 
 
560
  if __name__ == "__main__":
561
  for nanogpt_player in player_ones:
562
  for i in range(1): #range(11):
563
- num_games = 522 #265 #265 instead of 250 for duplicates (for lc0, stockfish doesn't need it)
564
  # player_one = GPTPlayer(model="gpt-3.5-turbo-instruct")
565
  # player_one = LocalLlamaPlayer(model_name="meta-llama/Llama-2-7b-hf")
566
  # player_one = LocalLoraLlamaPlayer("meta-llama/Llama-2-7b-hf", "/workspace/axolotl/lora2-out")
@@ -580,6 +607,6 @@ if __name__ == "__main__":
580
  #print(f"\n\nSTARTING GAMES AGAINST STOCKFISH LEVEL {i}\n\n")
581
  print(f"\n\nSTARTING GAMES AGAINST LC0 LEVEL {i}\n\n")
582
 
583
- play_game(player_one, player_two, num_games, random_opening_seed=True)
584
 
585
  print("\n\n\n********\nDONE!\n********\n\n\n")
 
313
 
314
 
315
  used_openings = []
316
+ def random_book_opening(
317
  game_state: str, board: chess.Board
318
  ) -> Tuple[str, chess.Board]:
319
  global used_openings
 
342
  return game_state.rstrip(), board, len(tokens) // 2
343
 
344
 
345
+ def add_random_moves(
346
+ game_state: str, board: chess.Board, num_moves: int = 20
347
+ ) -> Tuple[str, chess.Board, int]:
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
+ break
352
+
353
+ move = board.san(random.choice(legal_moves))
354
+ board.push(board.parse_san(move))
355
+
356
+ if board.turn == chess.BLACK:
357
+ game_state += f" {i//2 + 1}.{move}" if move_num_in_gamestate else f" .{move}"
358
+ else:
359
+ game_state += f" {move}"
360
+
361
+ game_state = game_state.strip()
362
+ return game_state, board, num_moves
363
+
364
+
365
  # Return is (move_san, move_uci, attempts, is_resignation, is_illegal_move)
366
  def get_legal_move(
367
  player: Player,
 
448
  player_one: Player,
449
  player_two: Player,
450
  max_games: int = 10,
451
+ book_opening: bool = False,
452
+ random_opening: bool = False,
453
+ random_opening_moves: int = 20,
454
  ):
455
  for z in range(max_games):
456
  print(f"\nGame {z} of {max_games}\n")
 
459
  game_state = f.read()
460
  board = chess.Board()
461
 
462
+ if book_opening:
463
+ game_state, board, opening_moves = random_book_opening(game_state, board)
464
+ elif random_opening:
465
+ game_state, board, opening_moves = add_random_moves(game_state, board, random_opening_moves)
466
  else:
467
  opening_moves = 0
468
  player_one_illegal_moves = 0
 
581
  player_ones = ["50M/ckpt_9120050b.pt"]
582
  player_two_recording_name = "lc0_sweep" #"stockfish_sweep"
583
  move_num_in_gamestate = False
584
+ book_opening = True
585
+ random_opening = False
586
+ random_opening_moves = 20
587
  if __name__ == "__main__":
588
  for nanogpt_player in player_ones:
589
  for i in range(1): #range(11):
590
+ num_games = 540
591
  # player_one = GPTPlayer(model="gpt-3.5-turbo-instruct")
592
  # player_one = LocalLlamaPlayer(model_name="meta-llama/Llama-2-7b-hf")
593
  # player_one = LocalLoraLlamaPlayer("meta-llama/Llama-2-7b-hf", "/workspace/axolotl/lora2-out")
 
607
  #print(f"\n\nSTARTING GAMES AGAINST STOCKFISH LEVEL {i}\n\n")
608
  print(f"\n\nSTARTING GAMES AGAINST LC0 LEVEL {i}\n\n")
609
 
610
+ play_game(player_one, player_two, num_games, book_opening=book_opening, random_opening=random_opening,random_opening_moves=random_opening_moves)
611
 
612
  print("\n\n\n********\nDONE!\n********\n\n\n")