-
Notifications
You must be signed in to change notification settings - Fork 9
/
uct_mcstsagent.py
269 lines (226 loc) · 8.94 KB
/
uct_mcstsagent.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
from math import sqrt, log
from copy import deepcopy
from queue import Queue
from random import choice
from time import time as clock
from meta import GameMeta, MCTSMeta
from gamestate import GameState
class Node:
"""
Node for the MCTS. Stores the move applied to reach this node from its parent,
stats for the associated game position, children, parent and outcome
(outcome==none unless the position ends the game).
Args:
move:
parent:
N (int): times this position was visited
Q (int): average reward (wins-losses) from this position
Q_RAVE (int): times this move has been critical in a rollout
N_RAVE (int): times this move has appeared in a rollout
children (dict): dictionary of successive nodes
outcome (int): If node is a leaf, then outcome indicates
the winner, else None
"""
def __init__(self, move: tuple = None, parent: object = None):
"""
Initialize a new node with optional move and parent and initially empty
children list and rollout statistics and unspecified outcome.
"""
self.move = move
self.parent = parent
self.N = 0 # times this position was visited
self.Q = 0 # average reward (wins-losses) from this position
self.Q_RAVE = 0 # times this move has been critical in a rollout
self.N_RAVE = 0 # times this move has appeared in a rollout
self.children = {}
self.outcome = GameMeta.PLAYERS['none']
def add_children(self, children: dict) -> None:
"""
Add a list of nodes to the children of this node.
"""
for child in children:
self.children[child.move] = child
@property
def value(self, explore: float = MCTSMeta.EXPLORATION):
"""
Calculate the UCT value of this node relative to its parent, the parameter
"explore" specifies how much the value should favor nodes that have
yet to be thoroughly explored versus nodes that seem to have a high win
rate.
Currently explore is set to 0.5.
"""
# if the node is not visited, set the value as infinity. Nodes with no visits are on priority
# (lambda: print("a"), lambda: print("b"))[test==true]()
if self.N == 0:
return 0 if explore == 0 else GameMeta.INF
else:
return self.Q / self.N + explore * sqrt(2 * log(self.parent.N) / self.N) # exploitation + exploration
class UctMctsAgent:
"""
Basic no frills implementation of an agent that preforms MCTS for hex.
Attributes:
root_state (GameState): Game simulator that helps us to understand the game situation
root (Node): Root of the tree search
run_time (int): time per each run
node_count (int): the whole nodes in tree
num_rollouts (int): The number of rollouts for each search
EXPLORATION (int): specifies how much the value should favor
nodes that have yet to be thoroughly explored versus nodes
that seem to have a high win rate.
"""
def __init__(self, state=GameState(8)):
self.root_state = deepcopy(state)
self.root = Node()
self.run_time = 0
self.node_count = 0
self.num_rollouts = 0
def search(self, time_budget: int) -> None:
"""
Search and update the search tree for a
specified amount of time in seconds.
"""
start_time = clock()
num_rollouts = 0
# do until we exceed our time budget
while clock() - start_time < time_budget:
node, state = self.select_node()
turn = state.turn()
outcome = self.roll_out(state)
self.backup(node, turn, outcome)
num_rollouts += 1
run_time = clock() - start_time
node_count = self.tree_size()
self.run_time = run_time
self.node_count = node_count
self.num_rollouts = num_rollouts
def select_node(self) -> tuple:
"""
Select a node in the tree to preform a single simulation from.
"""
node = self.root
state = deepcopy(self.root_state)
# stop if we find reach a leaf node
while len(node.children) != 0:
# descend to the maximum value node, break ties at random
children = node.children.values()
max_value = max(children, key=lambda n: n.value).value
max_nodes = [n for n in node.children.values()
if n.value == max_value]
node = choice(max_nodes)
state.play(node.move)
# if some child node has not been explored select it before expanding
# other children
if node.N == 0:
return node, state
# if we reach a leaf node generate its children and return one of them
# if the node is terminal, just return the terminal node
if self.expand(node, state):
node = choice(list(node.children.values()))
state.play(node.move)
return node, state
@staticmethod
def expand(parent: Node, state: GameState) -> bool:
"""
Generate the children of the passed "parent" node based on the available
moves in the passed gamestate and add them to the tree.
Returns:
bool: returns false If node is leaf (the game has ended).
"""
children = []
if state.winner != GameMeta.PLAYERS['none']:
# game is over at this node so nothing to expand
return False
for move in state.moves():
children.append(Node(move, parent))
parent.add_children(children)
return True
@staticmethod
def roll_out(state: GameState) -> int:
"""
Simulate an entirely random game from the passed state and return the winning
player.
Args:
state: game state
Returns:
int: winner of the game
"""
moves = state.moves() # Get a list of all possible moves in current state of the game
while state.winner == GameMeta.PLAYERS['none']:
move = choice(moves)
state.play(move)
moves.remove(move)
return state.winner
@staticmethod
def backup(node: Node, turn: int, outcome: int) -> None:
"""
Update the node statistics on the path from the passed node to root to reflect
the outcome of a randomly simulated playout.
Args:
node:
turn: winner turn
outcome: outcome of the rollout
Returns:
object:
"""
# Careful: The reward is calculated for player who just played
# at the node and not the next player to play
reward = 0 if outcome == turn else 1
while node is not None:
node.N += 1
node.Q += reward
node = node.parent
reward = 0 if reward == 1 else 1
def best_move(self) -> tuple:
"""
Return the best move according to the current tree.
Returns:
best move in terms of the most simulations number unless the game is over
"""
if self.root_state.winner != GameMeta.PLAYERS['none']:
return GameMeta.GAME_OVER
# choose the move of the most simulated node breaking ties randomly
max_value = max(self.root.children.values(), key=lambda n: n.N).N
max_nodes = [n for n in self.root.children.values() if n.N == max_value]
bestchild = choice(max_nodes)
return bestchild.move
def move(self, move: tuple) -> None:
"""
Make the passed move and update the tree appropriately. It is
designed to let the player choose an action manually (which might
not be the best action).
Args:
move:
"""
if move in self.root.children:
child = self.root.children[move]
child.parent = None
self.root = child
self.root_state.play(child.move)
return
# if for whatever reason the move is not in the children of
# the root just throw out the tree and start over
self.root_state.play(move)
self.root = Node()
def set_gamestate(self, state: GameState) -> None:
"""
Set the root_state of the tree to the passed gamestate, this clears all
the information stored in the tree since none of it applies to the new
state.
"""
self.root_state = deepcopy(state)
self.root = Node()
def statistics(self) -> tuple:
return self.num_rollouts, self.node_count, self.run_time
def tree_size(self) -> int:
"""
Count nodes in tree by BFS.
"""
Q = Queue()
count = 0
Q.put(self.root)
while not Q.empty():
node = Q.get()
count += 1
for child in node.children.values():
Q.put(child)
return count