This repository has been archived by the owner on Jan 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDatasetCreator.py
280 lines (215 loc) · 8.88 KB
/
DatasetCreator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# Script that labels different board positions according to Stockfish's evaluation
# function and creates suitable chess board representations for the ANNs
from __future__ import division
import time
import chess
import chess.pgn
import chess.uci
import numpy as np
import os
GAMES_DIRECTORY = '' # Directory where the pgn games are saved
STORING_PATH = '' # Where to store the games evaluated by Stockfish
engine = chess.uci.popen_engine('/usr/games/stockfish') # Be sure to have Stockfish installed
engine.uci()
ImportantSquareSet = chess.SquareSet(
chess.BB_D4 | chess.BB_D5 |
chess.BB_C4 | chess.BB_C5 |
chess.BB_E4 | chess.BB_E5 |
chess.BB_F2 | chess.BB_F7 |
chess.BB_H2 | chess.BB_H7
)
SquareSet = chess.SquareSet(
chess.BB_A1 | chess.BB_A2 | chess.BB_A3 | chess.BB_A4 | chess.BB_A5 |
chess.BB_A6 | chess.BB_A7 | chess.BB_A8 |
chess.BB_B1 | chess.BB_B2 | chess.BB_B3 | chess.BB_B4 | chess.BB_B5 |
chess.BB_B6 | chess.BB_B7 | chess.BB_B8 |
chess.BB_C1 | chess.BB_C2 | chess.BB_C3 | chess.BB_C4 | chess.BB_C5 |
chess.BB_C6 | chess.BB_C7 | chess.BB_C8 |
chess.BB_D1 | chess.BB_D2 | chess.BB_D3 | chess.BB_D4 | chess.BB_D5 |
chess.BB_D6 | chess.BB_D7 | chess.BB_D8 |
chess.BB_A1 | chess.BB_E2 | chess.BB_E3 | chess.BB_E4 | chess.BB_E5 |
chess.BB_E6 | chess.BB_E7 | chess.BB_E8 |
chess.BB_F1 | chess.BB_F2 | chess.BB_F3 | chess.BB_F4 | chess.BB_F5 |
chess.BB_F6 | chess.BB_F7 | chess.BB_F8 |
chess.BB_G1 | chess.BB_G2 | chess.BB_G3 | chess.BB_G4 | chess.BB_G5 |
chess.BB_G6 | chess.BB_G7 | chess.BB_G8 |
chess.BB_H1 | chess.BB_H2 | chess.BB_H3 | chess.BB_H4 | chess.BB_H5 |
chess.BB_H6 | chess.BB_H7 | chess.BB_H8
)
def load_game():
for root, dirs, filenames in os.walk(GAMES_DIRECTORY):
for f in filenames:
if f.endswith('.pgn'):
try:
pgn = open(os.path.join(root, f), 'r')
game = chess.pgn.read_game(pgn)
process_game(game)
os.remove(GAMES_DIRECTORY+str(f))
except:
pass
def splitter(inputStr, black):
inputStr = format(inputStr, "064b")
tmp = [inputStr[i:i+8] for i in range(0, len(inputStr), 8)]
for i in xrange(0, len(tmp)):
tmp2 = list(tmp[i])
tmp2 = [int(x) * black for x in tmp2]
tmp[i] = tmp2
return tmp
def is_checked(board):
if board.is_check() and board.turn is True:
CheckedInfo = [-1] * 64
elif board.is_check() and board.turn is False:
CheckedInfo = [1] * 64
elif not board.is_check():
CheckedInfo = [0] * 64
return CheckedInfo
def MlpBitmaps(board, e, filename):
P_input = splitter(int(board.pieces(chess.PAWN, chess.WHITE)), 1)
R_input = splitter(int(board.pieces(chess.ROOK, chess.WHITE)), 1)
N_input = splitter(int(board.pieces(chess.KNIGHT, chess.WHITE)), 1)
B_input = splitter(int(board.pieces(chess.BISHOP, chess.WHITE)), 1)
Q_input = splitter(int(board.pieces(chess.QUEEN, chess.WHITE)), 1)
K_input = splitter(int(board.pieces(chess.KING, chess.WHITE)), 1)
p_input = splitter(int(board.pieces(chess.PAWN, chess.BLACK)), -1)
r_input = splitter(int(board.pieces(chess.ROOK, chess.BLACK)), -1)
n_input = splitter(int(board.pieces(chess.KNIGHT, chess.BLACK)), -1)
b_input = splitter(int(board.pieces(chess.BISHOP, chess.BLACK)), -1)
q_input = splitter(int(board.pieces(chess.QUEEN, chess.BLACK)), -1)
k_input = splitter(int(board.pieces(chess.KING, chess.BLACK)), -1)
with open(filename, 'a') as thefile:
thefile.write("%s;" % P_input)
thefile.write("%s;" % R_input)
thefile.write("%s;" % N_input)
thefile.write("%s;" % B_input)
thefile.write("%s;" % Q_input)
thefile.write("%s;" % K_input)
thefile.write("%s;" % p_input)
thefile.write("%s;" % r_input)
thefile.write("%s;" % n_input)
thefile.write("%s;" % b_input)
thefile.write("%s;" % q_input)
thefile.write("%s;" % k_input)
thefile.write("%s\n" % e)
def CnnBitmaps(board, e, filename):
P_input = splitter(int(board.pieces(chess.PAWN, chess.WHITE)), 1)
R_input = splitter(int(board.pieces(chess.ROOK, chess.WHITE)), 1)
N_input = splitter(int(board.pieces(chess.KNIGHT, chess.WHITE)), 1)
B_input = splitter(int(board.pieces(chess.BISHOP, chess.WHITE)), 1)
Q_input = splitter(int(board.pieces(chess.QUEEN, chess.WHITE)), 1)
K_input = splitter(int(board.pieces(chess.KING, chess.WHITE)), 1)
p_input = splitter(int(board.pieces(chess.PAWN, chess.BLACK)), -1)
r_input = splitter(int(board.pieces(chess.ROOK, chess.BLACK)), -1)
n_input = splitter(int(board.pieces(chess.KNIGHT, chess.BLACK)), -1)
b_input = splitter(int(board.pieces(chess.BISHOP, chess.BLACK)), -1)
q_input = splitter(int(board.pieces(chess.QUEEN, chess.BLACK)), -1)
k_input = splitter(int(board.pieces(chess.KING, chess.BLACK)), -1)
CheckedInfo = is_checked(board)
SquareAttackers = []
PinnedSquares = []
ImportantAttackers = []
for square in SquareSet:
if board.is_attacked_by(chess.WHITE, square):
SquareAttackers.append(1)
elif board.is_attacked_by(chess.BLACK, square):
SquareAttackers.append(-1)
else:
SquareAttackers.append(0)
if board.is_pinned(chess.WHITE, square):
PinnedSquares.append(1)
elif board.is_attacked_by(chess.BLACK, square):
PinnedSquares.append(-1)
else:
PinnedSquares.append(-1)
attackers_tracker = []
for ImportantSquare in ImportantSquareSet:
WhiteAttackers = board.attackers(chess.WHITE, ImportantSquare)
BlackAttackers = board.attackers(chess.BLACK, ImportantSquare)
if len(WhiteAttackers) > len(BlackAttackers):
ImportantAttackersFeatures = [1] * 64
elif len(WhiteAttackers) < len(BlackAttackers):
ImportantAttackersFeatures = [-1] * 64
else:
ImportantAttackersFeatures = [0] * 64
with open(filename, 'a') as thefile:
thefile.write("%s;" % P_input)
thefile.write("%s;" % R_input)
thefile.write("%s;" % N_input)
thefile.write("%s;" % B_input)
thefile.write("%s;" % Q_input)
thefile.write("%s;" % K_input)
thefile.write("%s;" % p_input)
thefile.write("%s;" % r_input)
thefile.write("%s;" % n_input)
thefile.write("%s;" % b_input)
thefile.write("%s;" % q_input)
thefile.write("%s;" % k_input)
thefile.write("%s;" % CheckedInfo)
thefile.write("%s;" % SquareAttackers)
thefile.write("%s;" % PinnedSquares)
thefile.write("%s;" % ImportantAttackersFeatures)
thefile.write("%s\n" % e)
def GameChecker(board):
P_input = splitter(int(board.pieces(chess.PAWN, chess.WHITE)), 1)
R_input = splitter(int(board.pieces(chess.ROOK, chess.WHITE)), 5)
N_input = splitter(int(board.pieces(chess.KNIGHT, chess.WHITE)), 3)
B_input = splitter(int(board.pieces(chess.BISHOP, chess.WHITE)), 3)
Q_input = splitter(int(board.pieces(chess.QUEEN, chess.WHITE)), 9)
p_input = splitter(int(board.pieces(chess.PAWN, chess.BLACK)), 1)
r_input = splitter(int(board.pieces(chess.ROOK, chess.BLACK)), 5)
n_input = splitter(int(board.pieces(chess.KNIGHT, chess.BLACK)), 3)
b_input = splitter(int(board.pieces(chess.BISHOP, chess.BLACK)), 3)
q_input = splitter(int(board.pieces(chess.QUEEN, chess.BLACK)), 9)
Status = P_input+R_input+N_input+B_input+Q_input+p_input+r_input+n_input+b_input+q_input
TmpStatus = [item for sublist in Status for item in sublist]
return sum(TmpStatus)
def makeDatasets(board, evaluation, moveCnt):
if moveCnt < 40: # Before the 40 plys we consider the moves that have been
# played part of the opening
MlpBitmaps(board,evaluation,STORING_PATH+'MlpFileOpening.txt')
CnnBitmaps(board,evaluation,STORING_PATH+'CnnFileOpening.txt')
else:
cp = GameChecker(board)
if cp <= 12: # If the value of the pieces on the Board is smaller
# than 12 we assume we are in the endgame stage
MlpBitmaps(board,evaluation,STORING_PATH+'MlpFileEnd.txt')
CnnBitmaps(board,evaluation,STORING_PATH+'CnnFileEnd.txt')
elif cp > 12: # Otherwise we are still in the MiddleGame
MlpBitmaps(board,evaluation,STORING_PATH+'MlpFileMiddle.txt')
CnnBitmaps(board,evaluation,STORING_PATH+'CnnFileMiddle.txt')
else:
pass
def process_game(game):
positions = []
evaluations = []
GM_board = chess.Board()
node = game
movetime = 100 #Milliseconds, the lower the more approximate Stockfish's evaluation is
info_handler = chess.uci.InfoHandler()
engine.info_handlers.append(info_handler)
tmp = 0
while not node.is_end():
try:
engine.position(GM_board)
b_m = engine.go(movetime=movetime)
info = info_handler.info["score"][1]
next_node = node.variation(0)
if info[0] is not None and GM_board.turn is True:
stock_evaluation = info[0]/100
new_stock_evaluation = stock_evaluation
GM_move = str(node.board().san(next_node.move))
GM_board.push_san(GM_move)
makeDatasets(GM_board, new_stock_evaluation, tmp)
elif info[0] is not None and GM_board.turn is False:
stock_evaluation = info[0]/100
stock_evaluation = - stock_evaluation # Flip evaluations for Black
GM_move = str(node.board().san(next_node.move))
GM_board.push_san(GM_move)
makeDatasets(GM_board, stock_evaluation, tmp)
node = next_node
tmp = tmp + 1
except:
print('Unknown Position')
pass
if __name__ == '__main__':
print('Job has started')
load_game()