-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_script.py
254 lines (220 loc) · 7.31 KB
/
test_script.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
"""
Example script built on top of this framework. Included for testing purposes.
"""
import argparse
import copy
import pexpect
import multiprocessing
import numpy
import random
import time
from common import *
NUM_STATES = 50
NUM_ACTIONS = 5
STATES = list(range(NUM_STATES))
ACTIONS = list(range(NUM_ACTIONS))
REWARDS = {s: round(random.uniform(-1, 1), 5) for s in STATES}
MODEL = {(s, a): [[]] for s in STATES for a in ACTIONS}
for s in STATES:
for a in ACTIONS:
s_a = MODEL[(s, a)][0]
next_s = random.choice(STATES)
r = REWARDS[next_s]
s_a.append([next_s, r])
for _ in range(len(ACTIONS) - 1):
next_s = random.choice(STATES)
r = random.choice([None, REWARDS[next_s]])
s_r = (next_s, r)
if r is not None and s_r not in s_a:
s_a.append(s_r)
probs = [float(random.randint(1, 100)) for _ in s_a]
total_prob = sum(probs)
for i in range(len(probs)):
probs[i] = probs[i] / total_prob
MODEL[(s, a)].append(probs)
GAMMA = 0.9
MAX_ITER = 10
def num():
return int(pexpect.run('curl \
http://metadata/computeMetadata/v1beta1/instance/attributes/num'))
def update_model(model):
global MODEL
MODEL = model
def base(s):
return random.choice(ACTIONS)
base = {s: base(s) for s in STATES}
def simulate(s, a):
transitions, probs = MODEL[(s, a)]
choice = random.uniform(0, 1)
i = 0
while choice > probs[i]:
choice -= probs[i]
i += 1
return transitions[i]
def rollout(s, a, policy, len_traj):
next_s, r = simulate(s, a)
emp_Q = r
s = next_s
for t in range(len_traj):
next_s, r = simulate(s, policy[s])
emp_Q += (GAMMA ** t) * r
s = next_s
return emp_Q
def rollout_map(args):
return args[0], args[1], rollout(*args)
def val_iter():
V = {s: 0 for s in STATES}
epsilon = 1e-300
while True:
delta = 0
for s in STATES:
val = V[s]
possible_values = []
for a in ACTIONS:
val_a = 0
transitions, probs = MODEL[(s, a)]
for (next_s, r), p in zip(transitions, probs):
val_a += p * (r + GAMMA * V[next_s])
possible_values.append(val_a)
V[s] = max(possible_values)
delta = max(delta, abs(val - V[s]))
if delta < epsilon:
break
def optimal_policy(s):
values = {}
for a in ACTIONS:
val_a = 0
transitions, probs = MODEL[(s, a)]
for (next_s, r), p in zip(transitions, probs):
val_a += p * (r + GAMMA * V[next_s])
values[a] = val_a
return max(values, key=lambda x: values[x])
return optimal_policy
def evaluate_approx(policy):
V = [0 for s in STATES]
epsilon = 1e-200
while True:
delta = 0
old_V = V[:]
for s in STATES:
val = V[s]
V[s] = 0
transitions, probs = MODEL[(s, policy[s])]
for (next_s, r), p in zip(transitions, probs):
V[s] += p * (r + GAMMA * old_V[next_s])
delta = max(delta, abs(val - V[s]))
if delta < epsilon:
return V
def evaluate_exact(policy):
prob_mat = []
for s in STATES:
prob_s = [0 for _ in STATES]
transitions, probs = MODEL[(s, policy[s])]
for (next_s, r), p in zip(transitions, probs):
prob_s[next_s] = p
prob_mat.append(prob_s)
P = numpy.matrix(prob_mat)
R = numpy.matrix([REWARDS[s] for s in STATES]).T
return list(float(mat) for mat in (numpy.identity(NUM_STATES) - GAMMA * P).I * R)
def pol_iter(eval_func=evaluate_exact):
policy = copy.copy(base)
while True:
V = eval_func(policy)
diff_a = 0
for s in STATES:
old_a = policy[s]
values = {}
for a in ACTIONS:
val_a = 0
transitions, probs = MODEL[(s, a)]
for (next_s, r), p in zip(transitions, probs):
val_a += p * (r + GAMMA * V[next_s])
values[a] = val_a
policy[s] = max(values, key=lambda x: values[x])
if old_a != policy[s]:
diff_a += 1
if diff_a > 0:
break
return policy
def approx_optimal(policy, optimal_perf, i):
tolerance = 1
num_diff = 0
for s in STATES:
if policy[s] != optimal[s]:
num_diff += 1
if num_diff > tolerance:
print('policy #{} is not good enough'.format(i))
return False
print('policy #{} is good enough'.format(i))
print('num diff: {}'.format(num_diff))
return True
def learn(training_set, last_policy):
cache = copy.deepcopy(training_set)
def new_policy(s):
if s in cache:
return cache[s]
return last_policy[s]
new_policy = {s: new_policy(s) for s in STATES}
num_diff, num_nopt = 0, 0
for s in STATES:
if new_policy[s] != last_policy[s]:
num_diff += 1
if new_policy[s] != optimal[s]:
num_nopt += 1
wf(colorize('{}, {}\t'.format(num_diff, num_nopt), 'yellow'))
return new_policy
def rcpi(num_traj, len_traj, par=False):
policy = base
i = 1
training_set = copy.copy(base)
while policy != optimal and i <= MAX_ITER:
start = time.time()
rollout_args = [(s, a, policy, len_traj) \
for _ in range(num_traj) for a in ACTIONS for s in STATES]
if par:
total_emp_Q = parallel.parallel_map(rollout_map, rollout_args)
else:
pool = multiprocessing.Pool()
total_emp_Q = pool.map(rollout_map, rollout_args)
pool.close()
pool.join()
for s in STATES:
emp_Qs = {}
total_emp_Qs = [e[1:] for e in total_emp_Q if e[0] == s]
for a in ACTIONS:
emp_Qs[a] = sum(e[1] for e in total_emp_Qs if e[0] == a) / num_traj
best_a = max(emp_Qs, key=lambda x: emp_Qs[x])
training_set[s] = best_a
policy = learn(training_set, policy)
print('running time of iter #{}:\t{}'.format(i, time.time() - start))
i += 1
return policy
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--parallel', action='store_true')
parser.add_argument('-c', '--cluster', type=str, \
help='The cluster that will be used for this script')
parser.add_argument('-s', '--slave', action='store_true')
parser.add_argument('-v', '--value', action='store_true')
parser.add_argument('-r', '--rollouts', type=int)
args = parser.parse_args()
val_iter_pol = val_iter()
opt_val = {s: val_iter_pol(s) for s in STATES}
opt_app = pol_iter(eval_func=evaluate_approx)
if args.value:
optimal = opt_val
else:
optimal = pol_iter()
num_traj = args.rollouts or 100
if args.parallel:
if not args.slave:
import parallel
parallel.claim_cluster(args.cluster)
parallel.apply_on_all_insts(update_model, (MODEL,))
best = rcpi(num_traj, 100, par=True)
else:
import slave
print('starting slave loop')
slave.run_slave_loop()
else:
best = rcpi(num_traj, 100)