-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMCTS.py
472 lines (324 loc) · 11.4 KB
/
MCTS.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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
import encoder
import math
from threading import Thread
from atomic import AtomicLong
import time
def calcUCT( edge, N_p ):
"""
Calculate the UCT formula.
Args:
edge (Edge) the edge which the UCT formula is for
N_p (float) the parents visit count
Returns:
(float) the calculated value
"""
Q = edge.getQ()
N_c = edge.getN()
P = edge.getP()
#This is a quick fix
#when getting nans from nn
#if math.isnan( P ):
# P = 0.1
C = 1.5
UCT = Q + P * C * math.sqrt( N_p ) / ( 1 + N_c )
assert not math.isnan( UCT ), 'Q {} N_c {} P {}'.format( Q, N_c, P )
return UCT
class Node:
"""
A node in the search tree.
Nodes store their visit count (N), the sum of the
win probabilities in the subtree from the point
of view of this node (sum_Q), and a list of
edges
"""
def __init__( self, board, new_Q, move_probabilities ):
"""
Args:
board (chess.Board) the chess board
new_Q (float) the probability of winning according to neural network
move_probabilities (numpy.array (200) float) probability distribution across move list
"""
self.N = 1.
self.sum_Q = new_Q
self.edges = []
for idx, move in enumerate( board.legal_moves ):
edge = Edge( move, move_probabilities[ idx ] )
self.edges.append( edge )
def getN( self ):
"""
Returns:
(float) the number of rollouts performed
"""
return self.N
def getQ( self ):
"""
Returns:
(float) the number of rollouts performed
"""
return self.sum_Q / self.N
def UCTSelect( self ):
"""
Get the edge that maximizes the UCT formula, or none
if this node is terminal.
Returns:
max_edge (Edge) the edge maximizing the UCT formula.
"""
max_uct = -1000.
max_edge = None
for edge in self.edges:
uct = calcUCT( edge, self.N )
if max_uct < uct:
max_uct = uct
max_edge = edge
assert not ( max_edge == None and not self.isTerminal() )
return max_edge
def maxNSelect( self ):
"""
Returns:
max_edge (Edge) the edge with maximum N.
"""
max_N = -1
max_edge = None
for edge in self.edges:
N = edge.getN()
if max_N < N:
max_N = N
max_edge = edge
return max_edge
def getStatisticsString( self ):
"""
Get a string containing the current search statistics.
Returns:
string (string) a string describing all the moves from this node
"""
string = '|{: ^10}|{: ^10}|{: ^10}|{: ^10}|{: ^10}|\n'.format(
'move', 'P', 'N', 'Q', 'UCT' )
edges = self.edges.copy()
edges.sort( key=lambda edge: edge.getN() )
edges.reverse()
for edge in edges:
move = edge.getMove()
P = edge.getP()
N = edge.getN()
Q = edge.getQ()
UCT = calcUCT( edge, self.N )
string += '|{: ^10}|{:10.4f}|{:10.4f}|{:10.4f}|{:10.4f}|\n'.format(
str( move ), P, N, Q, UCT )
return string
def isTerminal( self ):
"""
Checks if this node is terminal.'
"""
return len( self.edges ) == 0
class Edge:
"""
An edge in the search tree.
Each edge stores a move, a move probability,
virtual losses and a child.
"""
def __init__( self, move, move_probability ):
"""
Args:
move (chess.Move) the move this edge represents
move_probability (float) this move's probability from the neural network
"""
self.move = move
self.P = move_probability
self.child = None
#self.virtualLosses = AtomicLong( 0 )
self.virtualLosses = 0.
def has_child( self ):
"""
Returns:
(bool) whether this edge has a child
"""
return self.child != None
def getN( self ):
"""
Returns:
(int) the child's N
"""
if self.has_child():
return self.child.N + self.virtualLosses
else:
return 0. + self.virtualLosses
def getQ( self ):
"""
Returns:
(int) the child's Q
"""
if self.has_child():
return 1. - ( ( self.child.sum_Q + self.virtualLosses ) / ( self.child.N + self.virtualLosses ) )
else:
return 0.
def getP( self ):
"""
Returns:
(int) this move's probability (P)
"""
return self.P
def expand( self, board, new_Q, move_probabilities ):
"""
Create the child node with the given board position. Return
True if we are expanding an unexpanded node, and otherwise false.
Args:
board (chess.Board) the chess position
new_Q (float) the probability of winning according to the neural network
move_probabilities (numpy.array (200) float) the move probabilities according to the neural network
Returns:
(bool) whether we are expanding an unexpanded node
"""
if self.child == None:
self.child = Node( board, new_Q, move_probabilities )
return True
else:
return False
def getChild( self ):
"""
Returns:
(Node) this edge's child node
"""
return self.child
def getMove( self ):
"""
Returns:
(chess.Move) this edge's move
"""
return self.move
def addVirtualLoss( self ):
"""
When doing multiple rollouts in parallel,
we can discourage threads from taking
the same path by adding fake losses
to visited nodes.
"""
self.virtualLosses += 1
def clearVirtualLoss( self ):
#self.virtualLosses = AtomicLong( 0 )
self.virtualLosses = 0.
class Root( Node ):
def __init__( self, board, neuralNetwork ):
"""
Create the root of the search tree.
Args:
board (chess.Board) the chess position
neuralNetwork (torch.nn.Module) the neural network
"""
value, move_probabilities = encoder.callNeuralNetwork( board, neuralNetwork )
Q = value / 2. + 0.5
super().__init__( board, Q, move_probabilities )
self.same_paths = 0
def selectTask( self, board, node_path, edge_path ):
"""
Do the selection stage of MCTS.
Args/Returns:
board (chess.Board) the root position on input,
on return, either the positon of the selected unexpanded node,
or the last node visited, if that is terminal
node_path (list of Node) ordered list of nodes traversed
edge_path (list of Edge) ordered list of edges traversed
"""
cNode = self
while True:
node_path.append( cNode )
cEdge = cNode.UCTSelect()
edge_path.append( cEdge )
if cEdge == None:
#cNode is terminal. Return with board set to the same position as cNode
#and edge_path[ -1 ] = None
assert cNode.isTerminal()
break
cEdge.addVirtualLoss()
board.push( cEdge.getMove() )
if not cEdge.has_child():
#cEdge has not been expanded. Return with board set to the same
#position as the unexpanded Node
break
cNode = cEdge.getChild()
def rollout( self, board, neuralNetwork ):
"""
Each rollout traverses the tree until
it reaches an un-expanded node or a terminal node.
Unexpanded nodes are expanded and their
win probability propagated.
Terminal nodes have their win probability
propagated as well.
Args:
board (chess.Board) the chess position
neuralNetwork (torch.nn.Module) the neural network
"""
node_path = []
edge_path = []
self.selectTask( board, node_path, edge_path )
edge = edge_path[ -1 ]
if edge != None:
value, move_probabilities = encoder.callNeuralNetwork( board, neuralNetwork )
new_Q = value / 2. + 0.5
edge.expand( board, new_Q, move_probabilities )
new_Q = 1. - new_Q
else:
winner = encoder.parseResult( board.result() )
if not board.turn:
winner *= -1
new_Q = float( winner ) / 2. + 0.5
last_node_idx = len( node_path ) - 1
for i in range( last_node_idx, -1, -1 ):
node = node_path[ i ]
node.N += 1
if ( last_node_idx - i ) % 2 == 0:
node.sum_Q += new_Q
else:
node.sum_Q += 1. - new_Q
for edge in edge_paths[ i ]:
if edge != None:
edge.clearVirtualLoss()
def parallelRollouts( self, board, neuralNetwork, num_parallel_rollouts ):
"""
Same as rollout, except done in parallel.
Args:
board (chess.Board) the chess position
neuralNetwork (torch.nn.Module) the neural network
num_parallel_rollouts (int) the number of rollouts done in parallel
"""
boards = []
node_paths = []
edge_paths = []
threads = []
for i in range( num_parallel_rollouts ):
boards.append( board.copy() )
node_paths.append( [] )
edge_paths.append( [] )
threads.append( Thread( target=self.selectTask,
args=( boards[ i ], node_paths[ i ], edge_paths[ i ] ) ) )
threads[ i ].start()
time.sleep( 0.0001 )
for i in range( num_parallel_rollouts ):
threads[ i ].join()
values, move_probabilities = encoder.callNeuralNetworkBatched( boards, neuralNetwork )
for i in range( num_parallel_rollouts ):
edge = edge_paths[ i ][ -1 ]
board = boards[ i ]
value = values[ i ]
if edge != None:
new_Q = value / 2. + 0.5
isunexpanded = edge.expand( board, new_Q,
move_probabilities[ i ] )
if not isunexpanded:
self.same_paths += 1
new_Q = 1. - new_Q
else:
winner = encoder.parseResult( board.result() )
if not board.turn:
winner *= -1
new_Q = float( winner ) / 2. + 0.5
last_node_idx = len( node_paths[ i ] ) - 1
for r in range( last_node_idx, -1, -1 ):
node = node_paths[ i ][ r ]
node.N += 1.
if ( last_node_idx - r ) % 2 == 0:
node.sum_Q += new_Q
else:
node.sum_Q += 1. - new_Q
for edge in edge_paths[ i ]:
if edge != None:
edge.clearVirtualLoss()