-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrollout.py
348 lines (270 loc) · 9.06 KB
/
rollout.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
from random import random, choice
import sys
from time import perf_counter
import queue as Queue
import argparse
# import numba
import pickle
from numpy import linspace
import tqdm
import numpy as np
parser = argparse.ArgumentParser(description='Rollout example')
parser.add_argument('--example', action='store_true',help='run with a given tree')
parser.add_argument('--prob', default=0.5, type=float, help='specify the probability with which a node is inactive')
parser.add_argument('--height', default=6, type=int, help='height of the binary tree')
parser.add_argument('--simulation', action='store_true', help='run the simulation over a number of trials')
parser.add_argument('--prob-steps', default=10, type=int)
parser.add_argument('--height-steps', default=5, type=int)
parser.set_defaults(example=False)
parser.set_defaults(simulation=False)
args = parser.parse_args()
class Node:
def __init__(self, data, left=None, right=None, parent=None):
self.data = data
self.left = left
self.right = right
self.parent = parent
def is_root(self):
return bool(self.parent is not None)
def is_leaf(self):
return bool(self.left is None) and bool(self.right is None)
class BinaryTree:
def __init__(self, node):
self.root = node
class SubRes:
def __init__(self,m,n):
self.time = np.zeros((m,n))
self.acc = np.zeros((m,n))
class Result:
def __init__(self,m,n):
self.greedy = SubRes(m,n)
self.dp = SubRes(m,n)
self.rollout = SubRes(m,n)
def inorder(root):
stack = Queue.LifoQueue()
node = root
done = 0
while done == 0:
if node is not None:
stack.put(node)
node = node.left
else:
if not stack.empty():
node = stack.get()
print(node.data)
node = node.right
else:
done = 1
def depth_first(root):
stack = Queue.LifoQueue()
node = root
stack.put(node)
while not stack.empty():
node = stack.get()
print(node.data)
if node.right is not None:
stack.put(node.right)
if node.left is not None:
stack.put(node.left)
def to_list(root):
queue = Queue.Queue()
node = root
queue.put(node)
l = []
while not queue.empty():
node = queue.get()
l.append(node.data)
if node.left is not None:
queue.put(node.left)
if node.right is not None:
queue.put(node.right)
return l
def breadth_first(root):
queue = Queue.Queue()
node = root
queue.put(node)
while not queue.empty():
node = queue.get()
print(node.data)
if node.left is not None:
queue.put(node.left)
if node.right is not None:
queue.put(node.right)
def create_binary_tree(height, prob, load_data=True):
if load_data:
dd = list(reversed([True, True, True, False, False, True, True]))
data = dd.pop()
else:
data = bool(random() > prob)
# data = choice(list(range(100)))
root = Node(data)
queue = Queue.Queue()
queue.put(root)
for i in range(1, height):
count = 0
assert(queue.qsize()==2**(i-1))
while count < 2**(i-1):
node = queue.get()
# data = choice(list(range(100)))
if load_data:
data = dd.pop()
else:
data = bool(random() > prob)
node.left = Node(data, parent=node)
# data = choice(list(range(100)))
if load_data:
data = dd.pop()
else:
data = bool(random() > prob)
node.right = Node(data, parent=node)
queue.put(node.left)
queue.put(node.right)
count += 1
return BinaryTree(root)
def greedy_policy(node):
if node.data is False:
return False
while node is not None:
if not node.is_leaf():
if node.left.data is True:
node = node.left
elif node.right.data is True:
node = node.right
else:
return False
else:
if node.data:
return True
else:
return False
def dp_approach(root, n):
l = to_list(root)
table = [None] * 2**n
ind = n
while ind > 1:
for i in range(2**(ind-1)-1, 2**ind-1):
# print(i)
if 2*i+1 >= 2**n-1:
l[i] = l[i]
else:
# print(i, 2*i+1, 2**n, len(l))
l[i] = l[i] and (l[2*i+1] or l[2*i+2])
ind = ind - 1
res = l[0] and (l[1] or l[2])
return res
def rollout(tree):
node = tree.root
if node.data is False:
return False
while node is not None:
if not node.is_leaf():
left = node.left
right = node.right
if left.data and right.data:
left_path = greedy_policy(left)
right_path = greedy_policy(right)
if left_path or right_path:
return True
else:
node = left # like in greedy policy
elif left.data:
node = left
elif right.data:
node = right
else:
return False
else:
return node.data
def save(obj, filename):
with open(filename, 'wb') as output:
pickle.dump(obj, output, pickle.HIGHEST_PROTOCOL)
def file_open(filename):
with open(filename, 'rb') as output:
ret = pickle.load(output)
return ret
# @numba.jit(nopython=True, parallel=True)
def run_simulation():
print(args)
# result = dict()
# result['greedy'] = dict()
# result['dp'] = dict()
# result['rollout'] = dict()
#
# result['greedy']['time'] = []
# result['greedy']['acc'] = []
#
# result['rollout']['time'] = []
# result['rollout']['acc'] = []
#
# result['dp']['time'] = []
# result['dp']['acc'] = []
prob_steps = args.prob_steps
height_steps = args.height_steps
result = Result(prob_steps, height_steps)
prob_values = linspace(0.1, 0.9, prob_steps)
height_values = linspace(6, 8, height_steps).astype(int)
ref_values = dict()
ref_values['heights'] = height_values
ref_values['probs'] = prob_values
sim_start = perf_counter()
for prob_ind in range(prob_steps):
prob = prob_values[prob_ind]
for height_ind in range(height_steps):
n = height_values[height_ind]
n_lim = max(2 ** (n + 1), 1000)
for _ in tqdm.tqdm(range(n_lim)):
tree = create_binary_tree(n, prob, load_data=False)
greedy_start = perf_counter()
greedy_ans = greedy_policy(tree.root)
greedy_stop = perf_counter()
dp_start = perf_counter()
dp_ans = dp_approach(tree.root, n)
dp_stop = perf_counter()
rollout_start = perf_counter()
rollout_ans = rollout(tree)
rollout_stop = perf_counter()
result.greedy.time[prob_ind][height_ind] += greedy_stop-greedy_start
result.dp.time[prob_ind][height_ind] += dp_stop-dp_start
result.rollout.time[prob_ind][height_ind] += rollout_stop-rollout_start
result.greedy.acc[prob_ind][height_ind] += 1 if greedy_ans == dp_ans else 0
result.dp.acc[prob_ind][height_ind] = 1
result.rollout.acc[prob_ind][height_ind] += 1 if rollout_ans == dp_ans else 0
result.greedy.acc[prob_ind][height_ind]/=n_lim
result.rollout.acc[prob_ind][height_ind]/=n_lim
result.greedy.time[prob_ind][height_ind] /= n_lim
result.rollout.time[prob_ind][height_ind] /= n_lim
result.dp.time[prob_ind][height_ind] /= n_lim
sim_stop = perf_counter()
print(sim_stop - sim_start)
save(result, 'result.pkl')
save(ref_values, 'reference.pkl')
def main():
if not args.simulation:
print(args)
if args.example:
n = 3
else:
n = args.height
prob = args.prob
tree = create_binary_tree(n, prob, load_data=args.example)
if args.example:
print("inorder traversal")
inorder(tree.root)
print("breadth first traversal")
breadth_first(tree.root)
greedy_start = perf_counter()
print("greedy policy:", greedy_policy(tree.root))
greedy_stop = perf_counter()
print("time taken: {}".format(greedy_stop-greedy_start))
dp_start = perf_counter()
print("dp exact:", dp_approach(tree.root, n))
dp_end = perf_counter()
print("time taken: {}".format(dp_end-dp_start))
rollout_start = perf_counter()
print("rollout:", rollout(tree))
rollout_end = perf_counter()
print("time taken: {}".format(rollout_end-rollout_start))
else:
run_simulation()
if __name__=="__main__":
main()