-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathencoder.py
More file actions
402 lines (303 loc) · 12.4 KB
/
encoder.py
File metadata and controls
402 lines (303 loc) · 12.4 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import chess
import numpy as np
import torch
cuda = False
def parseResult( result ):
"""
Map the result string to an int in {-1, 0, 1}
for black won, draw, and white won respectively.
Args:
result (string) string representation of the winner of a game
Returns:
(int) integer representing the winner
"""
if result == "1-0":
return 1
elif result == "1/2-1/2":
return 0
elif result == "0-1":
return -1
else:
raise Exception( "Unexpected result string {}. Exiting".format( result ) )
def encodePosition( board ):
"""
Encodes a chess position as a vector. The first 12 planes represent
the different pieces. The next 4 represent castling rights.
Args:
board (chess.Board) the position to be encoded.
Returns:
planes (numpy.array (16,8,8) float32) the array encoding this position
"""
planes = np.zeros( (16, 8, 8), dtype=np.float32 )
#white pawns
wPawns = board.pieces( chess.PAWN, chess.WHITE )
wPawns = [ (chess.square_rank( sq ), chess.square_file( sq ) ) for sq in wPawns ]
for r, f in wPawns:
planes[ 0, r, f ] = 1.
#black pawns
bPawns = board.pieces( chess.PAWN, chess.BLACK )
bPawns = [ (chess.square_rank( sq ), chess.square_file( sq ) ) for sq in bPawns ]
for r, f in bPawns:
planes[ 1, r, f ] = 1.
#white rooks
wRooks = board.pieces( chess.ROOK, chess.WHITE )
wRooks = [ (chess.square_rank( sq ), chess.square_file( sq ) ) for sq in wRooks ]
for r, f in wRooks:
planes[ 2, r, f ] = 1.
#black rooks
bRooks = board.pieces( chess.ROOK, chess.BLACK )
bRooks = [ (chess.square_rank( sq ), chess.square_file( sq ) ) for sq in bRooks ]
for r, f in bRooks:
planes[ 3, r, f ] = 1.
#white bishops
wBishops = board.pieces( chess.BISHOP, chess.WHITE )
wBishops = [ (chess.square_rank( sq ), chess.square_file( sq ) ) for sq in wBishops ]
for r, f in wBishops:
planes[ 4, r, f ] = 1.
#black bishops
bBishops = board.pieces( chess.BISHOP, chess.BLACK )
bBishops = [ (chess.square_rank( sq ), chess.square_file( sq ) ) for sq in bBishops ]
for r, f in bBishops:
planes[ 5, r, f ] = 1.
#white knights
wKnights = board.pieces( chess.KNIGHT, chess.WHITE )
wKnights = [ (chess.square_rank( sq ), chess.square_file( sq ) ) for sq in wKnights ]
for r, f in wKnights:
planes[ 6, r, f ] = 1.
#black knights
bKnights = board.pieces( chess.KNIGHT, chess.BLACK )
bKnights = [ (chess.square_rank( sq ), chess.square_file( sq ) ) for sq in bKnights ]
for r, f in bKnights:
planes[ 7, r, f ] = 1.
#white queens
wQueens = board.pieces( chess.QUEEN, chess.WHITE )
wQueens = [ (chess.square_rank( sq ), chess.square_file( sq ) ) for sq in wQueens ]
for r, f in wQueens:
planes[ 8, r, f ] = 1.
#black queens
bQueens = board.pieces( chess.QUEEN, chess.BLACK )
bQueens = [ (chess.square_rank( sq ), chess.square_file( sq ) ) for sq in bQueens ]
for r, f in bQueens:
planes[ 9, r, f ] = 1.
#white kings
wKings = board.pieces( chess.KING, chess.WHITE )
wKings = [ (chess.square_rank( sq ), chess.square_file( sq ) ) for sq in wKings ]
for r, f in wKings:
planes[ 10, r, f ] = 1.
#black kings
bKings = board.pieces( chess.KING, chess.BLACK )
bKings = [ (chess.square_rank( sq ), chess.square_file( sq ) ) for sq in bKings ]
for r, f in bKings:
planes[ 11, r, f ] = 1.
#white can kingside castle
if board.has_kingside_castling_rights( chess.WHITE ):
planes[ 12, :, : ] = 1.
#black can kingside castle
if board.has_kingside_castling_rights( chess.BLACK ):
planes[ 13, :, : ] = 1.
#white can queenside castle
if board.has_queenside_castling_rights( chess.WHITE ):
planes[ 14, :, : ] = 1.
#black can queenside castle
if board.has_queenside_castling_rights( chess.BLACK ):
planes[ 15, :, : ] = 1.
return planes
def moveToIdx( move ):
"""
Maps a legal move to an index in (72, 8, 8)
Each of the 72 planes represents a different direction
and distance: rook and bishop directions with distance (64 planes)
and 8 horse directions.
The location in the plane specifies the start square.
Args:
move (chess.Move) the move to be encoded.
Returns:
directionAndDistancePlane (int) the plane the move maps to
from_rank (int) the moves starting rank
from_file (int) the moves starting file
"""
from_rank = chess.square_rank( move.from_square )
from_file = chess.square_file( move.from_square )
to_rank = chess.square_rank( move.to_square )
to_file = chess.square_file( move.to_square )
if from_rank == to_rank and from_file < to_file:
directionPlane = 0
distance = to_file - from_file
directionAndDistancePlane = directionPlane + distance
elif from_rank == to_rank and from_file > to_file:
directionPlane = 8
distance = from_file - to_file
directionAndDistancePlane = directionPlane + distance
elif from_file == to_file and from_rank < to_rank:
directionPlane = 16
distance = to_rank - from_rank
directionAndDistancePlane = directionPlane + distance
elif from_file == to_file and from_rank > to_rank:
directionPlane = 24
distance = from_rank - to_rank
directionAndDistancePlane = directionPlane + distance
elif to_file - from_file == to_rank - from_rank and to_file - from_file > 0:
directionPlane = 32
distance = to_rank - from_rank
directionAndDistancePlane = directionPlane + distance
elif to_file - from_file == to_rank - from_rank and to_file - from_file < 0:
directionPlane = 40
distance = from_rank - to_rank
directionAndDistancePlane = directionPlane + distance
elif to_file - from_file == -(to_rank - from_rank) and to_file - from_file > 0:
directionPlane = 48
distance = to_file - from_file
directionAndDistancePlane = directionPlane + distance
elif to_file - from_file == -(to_rank - from_rank) and to_file - from_file < 0:
directionPlane = 56
distance = from_file - to_file
directionAndDistancePlane = directionPlane + distance
elif to_file - from_file == 1 and to_rank - from_rank == 2:
directionAndDistancePlane = 64
elif to_file - from_file == 2 and to_rank - from_rank == 1:
directionAndDistancePlane = 65
elif to_file - from_file == 2 and to_rank - from_rank == -1:
directionAndDistancePlane = 66
elif to_file - from_file == 1 and to_rank - from_rank == -2:
directionAndDistancePlane = 67
elif to_file - from_file == -1 and to_rank - from_rank == 2:
directionAndDistancePlane = 68
elif to_file - from_file == -2 and to_rank - from_rank == 1:
directionAndDistancePlane = 69
elif to_file - from_file == -2 and to_rank - from_rank == -1:
directionAndDistancePlane = 70
elif to_file - from_file == -1 and to_rank - from_rank == -2:
directionAndDistancePlane = 71
return directionAndDistancePlane, from_rank, from_file
def getLegalMoveMask( board ):
"""
Returns a mask encoding the legal moves.
Args:
board (chess.Board) the chess position.
Returns:
mask (numpy.array (72, 8, 8) int32) the legal move mask
"""
mask = np.zeros( (72, 8, 8), dtype=np.int32 )
for move in board.legal_moves:
planeIdx, rankIdx, fileIdx = moveToIdx( move )
mask[ planeIdx, rankIdx, fileIdx ] = 1
return mask
def mirrorMove( move ):
"""
Mirrors a move vertically.
Args:
move (chess.Move) the move to be flipped
Returns:
(chess.Move) the mirrored move
"""
from_square = move.from_square
to_square = move.to_square
new_from_square = chess.square_mirror( from_square )
new_to_square = chess.square_mirror( to_square )
return chess.Move( new_from_square, new_to_square )
def encodeTrainingPoint( board, move, winner ):
"""
Encodes a position, move, and winner as vectors.
Args:
board (chess.Board) the chess position.
move (chess.Move) the target move from this position
winner (int) the winner of the game. -1 means black won,
0 means draw, 1 means white won.
Returns:
positionPlanes (numpy.array shape=(16,8,8) dtype=float32) the encoded position
moveIdx (int) index of the encoded target move
winner (float) the winner of the game
mask (numpy.array (72, 8, 8) int32) the legal move mask
"""
#Flip everything if black's turn
if not board.turn:
board = board.mirror()
winner *= -1
move = mirrorMove( move )
positionPlanes = encodePosition( board )
planeIdx, rankIdx, fileIdx = moveToIdx( move )
moveIdx = planeIdx * 64 + rankIdx * 8 + fileIdx
mask = getLegalMoveMask( board )
return positionPlanes, moveIdx, float( winner ), mask
def encodePositionForInference( board ):
"""
Encodes a position as a vector.
Args:
board (chess.Board) the chess position.
Returns:
positionPlanes (numpy.array shape=(16,8,8) dtype=float32) the encoded position
mask (numpy.array (72, 8, 8) int32) the legal move mask
"""
#Flip if black's turn
if not board.turn:
board = board.mirror()
positionPlanes = encodePosition( board )
mask = getLegalMoveMask( board )
return positionPlanes, mask
def decodePolicyOutput( board, policy ):
"""
Decode the policy output from the neural network.
Args:
board (chess.Board) the board
policy (numpy.array) the policy output
"""
move_probabilities = np.zeros( 200, dtype=np.float32 )
num_moves = 0
for idx, move in enumerate( board.legal_moves ):
if not board.turn:
move = mirrorMove( move )
planeIdx, rankIdx, fileIdx = moveToIdx( move )
moveIdx = planeIdx * 64 + rankIdx * 8 + fileIdx
move_probabilities[ idx ] = policy[ moveIdx ]
num_moves += 1
return move_probabilities[ :num_moves ]
def callNeuralNetwork( board, neuralNetwork ):
"""
Call the neural network on the given position,
get the outputs.
Args:
board (chess.Board) the chess board
neuralNetwork (torch.nn.Module) the neural network
Returns:
value (float) the value of this position
move_probabilities (numpy.array (num_moves) float) the move probabilities
"""
position, mask = encodePositionForInference( board )
position = torch.from_numpy( position )[ None, ... ]
mask = torch.from_numpy( mask )[ None, ... ]
if cuda:
position = position.cuda()
mask = mask.cuda()
value, policy = neuralNetwork( position, policyMask=mask )
value = value.cpu().numpy()[ 0, 0 ]
policy = policy.cpu().numpy()[ 0 ]
move_probabilities = decodePolicyOutput( board, policy )
return value, move_probabilities
def callNeuralNetworkBatched( boards, neuralNetwork ):
"""
Run neural network on each board given. Return outputs.
Args:
boards (list of chess.Board) the input positions
neuralNetwork (torch.nn.Module) the neural network
Returns:
value (numpy.array (num_inputs) float) the value output for each input position
move_probabilities (numpy.array (num_inputs, 200) float) the move probabilities for each position
"""
num_inputs = len( boards )
inputs = torch.zeros( (num_inputs, 16, 8, 8), dtype=torch.float32 )
masks = torch.zeros( (num_inputs, 72, 8, 8), dtype=torch.float32 )
for i in range( num_inputs ):
position, mask = encodePositionForInference( boards[ i ] )
inputs[ i ] = torch.from_numpy( position )
masks[ i ] = torch.from_numpy( mask )
if cuda:
inputs = inputs.cuda()
masks = masks.cuda()
value, policy = neuralNetwork( inputs, policyMask=masks )
move_probabilities = np.zeros( ( num_inputs, 200 ), dtype=np.float32 )
value = value.cpu().numpy().reshape( (num_inputs) )
policy = policy.cpu().numpy()
for i in range( num_inputs ):
move_probabilities_tmp = decodePolicyOutput( boards[ i ], policy[ i ] )
move_probabilities[ i, : move_probabilities_tmp.shape[0] ] = move_probabilities_tmp
return value, move_probabilities