-
Notifications
You must be signed in to change notification settings - Fork 5
/
mctslib.py
296 lines (237 loc) · 6.59 KB
/
mctslib.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
import sys
import os
import math
import random
class Node:
global_node_id = 0
def __init__(self, action_size, lamb):
self._action_size = action_size
self._lambda = lamb
self._lact = None
self._act = []
self._base = 0
self._N = [0 for k in range(action_size)]
self._P = [0 for k in range(action_size)]
self._Q = [0 for k in range(action_size)]
self._parent = None
self._child = [None for k in range(action_size)]
self._id = Node.global_node_id
Node.global_node_id += 1
def get_action_size(self):
return self._action_size
def get_last_action(self):
return self._lact
def get_best(self):
bid, bvl= -1, -100000000.0
for k in range(self._action_size):
#curvl = (self._P[k] + self._Q[k]) / (self._N[k] + 1.0)
curvl = 0.0
if self._N[k] != 0:
curvl = self._Q[k] / self._N[k]
if curvl > bvl:
bid = k
bvl = curvl
return bid
def get_action_score(self, action):
#return (self._P[action] + self._Q[action]) / (self._N[action] + 1.0)
if self._N[action] == 0:
return 0.0
return self._Q[action] / self._N[action]
def get_max(self):
bid, bvl, SN = -1, -100000000.0, self._action_size
for k in range(self._action_size):
SN += self._N[k]
for k in range(self._action_size):
curvl = (self._P[k] + self._Q[k]) / (self._N[k] + 1.0)
curvl += self._lambda * math.sqrt(math.log(SN) / (self._N[k] + 1.0))
if curvl > bvl:
bid = k
bvl = curvl
return bid
def add_prior(self, prior):
for k in range(self._action_size):
self._P[k] += prior[k]
def set_prior(self, prior):
for k in range(self._action_size):
self._P[k] = prior[k]
def add_parent(self, parent):
self._parent = parent
def add_child(self, child, action):
self._child[action] = child
def get_parent(self):
return self._parent
def get_child(self, action):
return self._child[action]
def set_base(self, base):
self._base = base
def get_base(self):
return self._base
def get_action_seq(self):
return self._act
def print_info(self):
print '--------------------'
if self._parent == None:
nid = 'None'
else:
nid = self._parent._id
print 'Parent:', nid
print 'This:', self._id
string = ''
for k in range(self._action_size):
if self._child[k] == None:
nid = 'None'
else:
nid = str(self._child[k]._id)
string += nid + ' '
print 'Child:', string
print 'Last Act:', self._lact
string = ''
for a in self._act:
string += str(a) + ' '
print 'Act Seq:', string
string = ''
for k in range(self._action_size):
string += str(self._N[k]) + ' '
print 'N:', string
string = ''
for k in range(self._action_size):
string += str(self._Q[k]) + ' '
print 'Q:', string
string = ''
for k in range(self._action_size):
string += str(self._P[k]) + ' '
print 'P:', string
SN = self._action_size
for k in range(self._action_size):
SN += self._N[k]
S = []
for k in range(self._action_size):
curvl = (self._P[k] + self._Q[k]) / (self._N[k] + 1.0)
curvl += self._lambda * math.sqrt(math.log(SN) / (self._N[k] + 1.0))
S.append(curvl)
string = ''
for k in range(self._action_size):
string += str(S[k]) + ' '
print 'SC:', string
print '--------------------'
def traverse(root):
if root == -1:
return []
node_list = []
que = [root]
while que != []:
node = que[0]
del que[0]
node_list.append(node)
for k in range(node._action_size):
if node._child[k] != None:
que.append(node._child[k])
return node_list
class Tree:
def __init__(self):
self._action_size = 0
self._lambda = 1
self._root = None
self._nodes = []
def set_lambda(self, lamb):
self._lambda = lamb
def get_lambda(self):
return self._lambda
def set_action_size(self, action_size):
self._nodes = []
self._action_size = action_size
self._root = Node(action_size, self._lambda)
self._nodes = [self._root]
def get_action_size(self):
return self._action_size
def get_root(self):
return self._root
def get_size(self):
return len(self._nodes)
def clear(self):
self._nodes = []
self._root = Node(self._action_size, self._lambda)
self._nodes.append(self._root)
def select(self):
curnode = self._root
bestid = -1
while True:
bestid = curnode.get_max()
if bestid == -1:
return curnode
if curnode._child[bestid] == None:
return curnode
else:
curnode = curnode._child[bestid]
def expand(self, node, action):
child = Node(self._action_size, self._lambda)
child._lact = action
child._act = [a for a in node._act]
child._act.append(action)
child.add_parent(node)
node.add_child(child, action)
self._nodes.append(child)
return child
def update(self, node, action, value):
curnode = node
curaction = action
while curnode != None:
curnode._Q[curaction] += value - curnode._base
curnode._N[curaction] += 1
curaction = curnode._lact
curnode = curnode._parent
def update_with_penalty(self, node, action, value, penalty):
current_node = node
current_action = action
next_action = None
sample_list = []
while current_node != None:
next_node = current_node._child[current_action]
if current_node is node:
gain = value - current_node._base - penalty
current_node._Q[current_action] += gain
current_node._N[current_action] += 1
current_pair = [current_node._act, current_action]
next_pair = [None, None]
sample_list.append([current_pair, next_pair, gain])
else:
short_gain = value - current_node._base - penalty
long_gain = next_node._Q[next_action] / next_node._N[next_action]
gain = short_gain + long_gain
current_node._Q[current_action] += gain
current_node._N[current_action] += 1
current_pair = [current_node._act, current_action]
next_pair = [next_node._act, next_action]
sample_list.append([current_pair, next_pair, short_gain])
value = current_node._base
next_action = current_action
current_action = current_node._lact
current_node = current_node._parent
return sample_list
def derive(self, action):
if self._root._child[action] == None:
return -1
self._root = self._root._child[action]
self._nodes = traverse(self._root)
for k in range(len(self._nodes)):
if self._nodes[k] is self._root:
self._nodes[k]._lact = -1
self._nodes[k]._act = []
self._nodes[k]._parent = None
else:
del self._nodes[k]._act[0]
return 1
def search(self, node):
for k in range(len(self._nodes)):
if self._nodes[k] is node:
return 1
return 0
def search_id(self, node_id):
for k in range(len(self._nodes)):
if self._nodes[k]._id is node_id:
return 1
return 0
def print_info(self):
for k in range(len(self._nodes)):
self._nodes[k].print_info()
print '\n'