lapp0 commited on
Commit
0272a42
·
verified ·
1 Parent(s): 2e0a1b0

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +99 -0
README.md CHANGED
@@ -23,3 +23,102 @@ configs:
23
  - split: train
24
  path: data/train-*
25
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  - split: train
24
  path: data/train-*
25
  ---
26
+
27
+ # 100M Random Chess Boards
28
+
29
+ 100M randomly sampled chess boards. 100M games were played, with each move being randomly determined until completion (termination). Then a single random board state within the game is sampled.
30
+
31
+ - **fen**: The FEN representation of the randomly sampled board
32
+ - **n_moves**: The number of moves into the game the sampled FEN is.
33
+ - **term_n_moves**: The total number of moves in the random-walk game from which the board was sampled.
34
+ - **term_reason**: How the random-walk game ended. [Codes](https://python-chess.readthedocs.io/en/latest/core.html#chess.Termination)
35
+ - **term_white_wins**: Whether white won the game. `null` indicates a tie. (No games are incomplete, but board states are from an intermediate position in the game)
36
+
37
+ # Code
38
+
39
+ ```python3
40
+ from tqdm import tqdm
41
+ import collections
42
+ import chess
43
+ import random
44
+ from datasets import Dataset
45
+
46
+ import os
47
+ from multiprocessing import Process, Queue
48
+
49
+
50
+ def random_fen():
51
+ """
52
+ Play random game to completion
53
+ backtrack random uniform number of moves
54
+ """
55
+ board = chess.Board()
56
+ num_moves = 0
57
+ while True:
58
+ board.push(random.choice(list(board.legal_moves)))
59
+ num_moves += 1
60
+ if board.is_game_over():
61
+ outcome = board.outcome()
62
+ revert_moves = random.randint(0, num_moves)
63
+ for _ in range(revert_moves):
64
+ board.pop()
65
+ fen_num_moves = num_moves - revert_moves
66
+ termination_reason = outcome.termination.value
67
+ white_is_winner = outcome.winner
68
+ return board.fen(), fen_num_moves, num_moves, termination_reason, white_is_winner
69
+
70
+
71
+ def batched_random_fen(queue, batch_size=100):
72
+ while True:
73
+ fens = []
74
+ while len(fens) < batch_size:
75
+ fens.append(random_fen())
76
+ queue.put(fens)
77
+
78
+
79
+ def generate_fens_parallel(N):
80
+ all_fens = collections.defaultdict(
81
+ lambda: {"n_moves": [], "term_n_moves": [], "term_reason": [], "term_white_wins": []}
82
+ )
83
+ queue = Queue()
84
+ num_workers = os.cpu_count() // 2
85
+
86
+ # Start worker processes in advance
87
+ workers = []
88
+ for _ in range(num_workers):
89
+ worker = Process(target=batched_random_fen, args=(queue,))
90
+ worker.daemon = True # Ensures the process terminates with the main process
91
+ workers.append(worker)
92
+ worker.start()
93
+
94
+ with tqdm(total=N, desc="FENs Captured") as pbar:
95
+ for i in range(1_000_000_000_000):
96
+ for fen, board_num_moves, final_state_num_moves, final_result, final_is_white_winner in queue.get():
97
+ all_fens[fen]["n_moves"].append(board_num_moves)
98
+ all_fens[fen]["term_n_moves"].append(final_state_num_moves)
99
+ all_fens[fen]["term_reason"].append(final_result)
100
+ all_fens[fen]["term_white_wins"].append(final_is_white_winner)
101
+
102
+ if i % 100 == 0:
103
+ pbar.n = len(all_fens)
104
+ pbar.last_print_n = len(all_fens)
105
+ pbar.refresh()
106
+
107
+ if len(all_fens) >= N:
108
+ break
109
+
110
+ # Terminate workers
111
+ for worker in workers:
112
+ worker.terminate()
113
+
114
+ # flatten
115
+ return [{"fen": k, **v} for k, v in all_fens.items()]
116
+
117
+
118
+ if __name__ == "__main__":
119
+ board_dicts = list(generate_fens_parallel(100_000_000))
120
+ ds = Dataset.from_dict({key: [dic[key] for dic in board_dicts] for key in board_dicts[0]})
121
+ ds.save_to_disk("random_uniform_chess_boards.hf")
122
+ # ds = datasets.load_from_disk("random_uniform_chess_boards.hf")
123
+ # ds.push_to_hub(...)
124
+ ```