-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMCT.py
More file actions
311 lines (250 loc) · 11 KB
/
MCT.py
File metadata and controls
311 lines (250 loc) · 11 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
############################################
# Project: MCT-TFE
# File: MCT.py
# By: ProgrammingIncluded
# Website: ProgrammingIncluded.com
############################################
from MCTNode import *
from mct_config import *
import time
import math
import operator
import numpy as np
import random
# Try a simpler MCT
class MCTEZ:
def __init__(self, secondsPerMove, trainer, generateValue):
# Number of seconds you can make per move.
self.secondsPerMove = secondsPerMove
# Tracking stats
self.gamesPlayed = 0
self.gamesWon = 0
self.probs = {}
self.expectedDir = {}
self.normExpectedDir = {}
self.expected = 0
# Accumulated max values
self.accum = 0
self.trainer = trainer
self.generateValue = generateValue
# Make a player move
def playerDecision(self, game):
self.curGameState = game
# Keep stats for root node.
self.gamesPlayed = 0
self.gamesWon = 0
self.expected = 0
self.accum = 0
# Keep states for action-state probs
wonDir = {k: 0 for k,v in DIR_KEY.items()}
accumDir = {k: 0 for k,v in DIR_KEY.items()}
# Our time loop
endTime = time.time() + self.secondsPerMove
availDir = self.curGameState.availDir()
while time.time() < endTime:
# Sample once from each game
# We want to first manually go down one level if possible
for dirc, val in availDir.items():
# Copy the game state
tempGame = self.curGameState.copy()
# Force down one path before we do full rollout
tempGame.moveGrid(dirc)
# Generate a grid
tempGame.putNew()
# Perform a full rollout now.
res, val = self.rolloutOnPlayerDecision(tempGame)
# Update accumulator
self.accum += val
accumDir[dirc] += val
# Increase stats if we won
if res == 1:
self.gamesWon += 1
wonDir[dirc] += 1
self.gamesPlayed += 1
# Calculate the probabilities and stats
# TODO: Use these probs for training
self.probs = {k: v / self.gamesPlayed for k, v in wonDir.items()}
# Caculate the expected value for each child node
self.expectedDir = {k: v / self.gamesPlayed for k, v in accumDir.items()}
self.expected = self.accum / self.gamesPlayed
# Calculate normalized expected value
self.normExpectedDir = {k: v / self.expected for k, v in self.expectedDir.items()}
# Pass it to the trainer
stateActionProbabilities,stateValue = self.generateValue(self.curGameState.grid)
legalDirections = list(availDir.keys())
legalMoves = [DIR_KEY[dir] for dir in legalDirections]
legalStateActionProbabilities = [stateActionProbabilities[move] for move in legalMoves]
sumLegalStateActionProbabilities = sum(legalStateActionProbabilities)
targetStateActionProbabilities = {}
for key in DIR_KEY.keys():
if key in legalDirections:
targetStateActionProbabilities[key] = self.normExpectedDir[key]*sumLegalStateActionProbabilities
else:
targetStateActionProbabilities[key] = stateActionProbabilities[DIR_KEY[key]]
self.trainer([(targetStateActionProbabilities, self.curGameState.grid.sum() / self.expected, self.curGameState)])
# self.trainer([(self.probs, self.gamesWon / self.gamesPlayed, self.curGameState)])
# Return largest direction with highest prob
best = max(self.expectedDir.items(), key=operator.itemgetter(1))[0]
# If max is zero then everything is zero, we rather just random guess
if self.expectedDir[best] == 0:
return DIR_VAL[random.randint(0, 3)]
return best
# Just pass in the game state
def adversaryDecision(self, game):
self.curGameState = game.copy()
# Simulate a full roll-out game
# Assume game given is on player decision
# Returns 1 if game won, 0 otherwise, also returns max for the rollout
def rolloutOnPlayerDecision(self, game):
currentState = game.copy()
availDir = currentState.availDir()
won = currentState.isWin()
lost = len(availDir) == 0
while not lost:
stateActionProbabilities,stateValue = self.generateValue(currentState.grid)
legalDirections = list(availDir.keys())
legalMoves = [DIR_KEY[dir] for dir in legalDirections]
legalStateActionProbabilities = [stateActionProbabilities[move] for move in legalMoves]
legalStateActionProbabilities = legalStateActionProbabilities/sum(legalStateActionProbabilities)
dirc = np.random.choice(list(availDir.keys()),p=legalStateActionProbabilities)
# Check to see if direction is proper
currentState.moveGrid(dirc)
currentState.putNew()
availDir = currentState.availDir()
won = currentState.isWin()
lost = len(availDir) == 0
return (1 if won else 0, currentState.grid.sum())
# This monte carlo implementation assumes MCT is retained every move.
class MCTZero:
def __init__(self, game, secondsPerMove, genValFunc, policyUpdateFunc):
# Create a new root
self.root = MCTNode(None, game, -1, True)
self.game = game
self.secondsPerMove = secondsPerMove
self.genValFunc = genValFunc
self.policyUpdateFunc = policyUpdateFunc
self.tracker = []
def adversaryDecision(self, decision):
# Decode option number
toggle = 0 if decision[1] == 2 else 1
boardSize = self.game.board_width ** 2
opt = toggle * boardSize + decision[0][1] + decision[0][0] * self.game.board_width
# check if inside
if opt in self.root.childrenOptions:
self.root = self.root.genChild(opt)
else:
# If not, must be one of the children. Search for it
res = None
for child in self.root.children:
if child.opt == opt:
res = child
self.root = res
# reset the root
self.root.parent = None
self.root.opt = -1
# Run the simulation
def playerDecision(self):
# Wrap everything in a timer
endTime = time.time() + self.secondsPerMove
self.tracker.append(self.root)
while time.time() < endTime or not self.root.allChildrenGenerated():
# Start from the root
curNode = self.root
# Selection
curNode = self.selection(curNode)
# Expansion
genNode = curNode.randGenChild()
# Simulation, if false then that means we hit
# leaf node prematurely.
if genNode:
# Add to tracker
self.tracker.append(genNode)
# Estimate q
genNode.guessProbs, genNode.guessQ = self.genValFunc(genNode.game.grid)
curNode = self.simulation(genNode)
# Backpropagate result
self.backpropagate(curNode)
# Also backpropogate what we learned
actions = []
for t in self.tracker:
# Only backprop for player decisions
if t.isPlayerDecision:
actions.append((t.stateActProbs, t.wins / t.totalGames, t))
# Call function to train NN
self.policyUpdateFunc(actions)
# Reset trackers
self.tracker = []
# Times up! Time to make a decision
# Pick the one with the highest UCB
ucbs = self.childrenUCB(self.root)
act = 'u'
if len(self.root.childrenOptions) != 0:
print("MCTS not enough time")
return act
# Before we leave, update new root
self.root = self.root.children[np.argmax(ucbs)]
seloption = self.root.opt
# Reset
self.root.parent = None
self.opt = -1
# Convert int key into a letter
return DIR_VAL[seloption]
def backpropagate(self, node):
win = 1 if node.game.isWin() else 0
while node is not None:
node.totalGames += 1
node.wins += win
# Probabilities are updated
# Only works on player nodes because of definition of the game.
self.updateProbabilities(node)
node = node.parent
def simulation(self, node):
# Use heavy heuristics
while not node.isLeaf():
node = node.randGenChild()
self.tracker.append(node)
# Estimate q
node.guessProbs, node.guessQ = self.genValFunc(node.game.grid)
return node
# Select each node based off its UCB
def selection(self, curNode):
# while we have the statistics
while curNode.allChildrenGenerated() and not len(curNode.children)==0:
# Select the best children for UCB
childrenUCB = self.childrenUCB(curNode)
arg = np.argmax(childrenUCB)
curNode = curNode.children[arg]
self.tracker.append(curNode)
curNode.guessProbs, curNode.guessQ = self.genValFunc(curNode.game.grid)
return curNode
# Calculate ucb of the children of node
def childrenUCB(self, node):
childrenUCB = []
for child in node.children:
bias = ((child.wins / child.totalGames) + child.guessQ) / 2
ucb = bias + 1.6 * math.log(node.totalGames / child.totalGames)
childrenUCB.append(ucb)
return childrenUCB
# Update the action-state probabilities assigned to each decision for a node
# Only works on a node that is a player since that is the only action-state
# that have different probability distributions
def updateProbabilities(self, node):
# If we are the player, we want to makesure that
# our probabilities update based off our wins
# We don't need to update for non-player since prob is fixed
if node.isPlayerDecision:
# keep track of which probabilities were updated
probAccum = 0
notUpdated = [x for x in range(0, 4)]
for child in node.children:
prob = child.wins / node.totalGames
node.stateActProbs[child.opt] = prob
probAccum += prob
notUpdated.remove(child.opt)
if len(notUpdated) == 0:
return
# Update the rest of the values
# They should take up the remaining probabilities not assigned
prob = (1 - probAccum) / len(notUpdated)
for x in notUpdated:
node.stateActProbs[x] = prob