forked from hongzimao/deeprm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slow_down_cdf.py
executable file
·209 lines (153 loc) · 6.23 KB
/
slow_down_cdf.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
import numpy as np
import cPickle
import matplotlib.pyplot as plt
import environment
import parameters
import pg_network
import other_agents
def discount(x, gamma):
"""
Given vector x, computes a vector y such that
y[i] = x[i] + gamma * x[i+1] + gamma^2 x[i+2] + ...
"""
out = np.zeros(len(x))
out[-1] = x[-1]
for i in reversed(xrange(len(x)-1)):
out[i] = x[i] + gamma*out[i+1]
assert x.ndim >= 1
# More efficient version:
# scipy.signal.lfilter([1],[1,-gamma],x[::-1], axis=0)[::-1]
return out
def categorical_sample(prob_n):
"""
Sample from categorical distribution,
specified by a vector of class probabilities
"""
prob_n = np.asarray(prob_n)
csprob_n = np.cumsum(prob_n)
return (csprob_n > np.random.rand()).argmax()
def get_traj(test_type, pa, env, episode_max_length, pg_resume=None, render=False):
"""
Run agent-environment loop for one whole episode (trajectory)
Return dictionary of results
"""
if test_type == 'PG': # load trained parameters
pg_learner = pg_network.PGLearner(pa)
net_handle = open(pg_resume, 'rb')
net_params = cPickle.load(net_handle)
pg_learner.set_net_params(net_params)
env.reset()
rews = []
ob = env.observe()
for _ in xrange(episode_max_length):
if test_type == 'PG':
a = pg_learner.choose_action(ob)
elif test_type == 'Tetris':
a = other_agents.get_packer_action(env.machine, env.job_slot)
elif test_type == 'SJF':
a = other_agents.get_sjf_action(env.machine, env.job_slot)
elif test_type == 'Random':
a = other_agents.get_random_action(env.job_slot)
ob, rew, done, info = env.step(a, repeat=True)
rews.append(rew)
if done: break
if render: env.render()
# env.render()
return np.array(rews), info
def launch(pa, pg_resume=None, render=False, plot=False, repre='image', end='no_new_job'):
# ---- Parameters ----
test_types = ['Tetris', 'SJF', 'Random']
if pg_resume is not None:
test_types = ['PG'] + test_types
env = environment.Env(pa, render, repre=repre, end=end)
all_discount_rews = {}
jobs_slow_down = {}
work_complete = {}
work_remain = {}
job_len_remain = {}
num_job_remain = {}
job_remain_delay = {}
for test_type in test_types:
all_discount_rews[test_type] = []
jobs_slow_down[test_type] = []
work_complete[test_type] = []
work_remain[test_type] = []
job_len_remain[test_type] = []
num_job_remain[test_type] = []
job_remain_delay[test_type] = []
for seq_idx in xrange(pa.num_ex):
print('\n\n')
print("=============== " + str(seq_idx) + " ===============")
for test_type in test_types:
rews, info = get_traj(test_type, pa, env, pa.episode_max_length, pg_resume)
print "---------- " + test_type + " -----------"
print "total discount reward : \t %s" % (discount(rews, pa.discount)[0])
all_discount_rews[test_type].append(
discount(rews, pa.discount)[0]
)
# ------------------------
# ---- per job stat ----
# ------------------------
enter_time = np.array([info.record[i].enter_time for i in xrange(len(info.record))])
finish_time = np.array([info.record[i].finish_time for i in xrange(len(info.record))])
job_len = np.array([info.record[i].len for i in xrange(len(info.record))])
job_total_size = np.array([np.sum(info.record[i].res_vec) for i in xrange(len(info.record))])
finished_idx = (finish_time >= 0)
unfinished_idx = (finish_time < 0)
jobs_slow_down[test_type].append(
(finish_time[finished_idx] - enter_time[finished_idx]) / job_len[finished_idx]
)
work_complete[test_type].append(
np.sum(job_len[finished_idx] * job_total_size[finished_idx])
)
work_remain[test_type].append(
np.sum(job_len[unfinished_idx] * job_total_size[unfinished_idx])
)
job_len_remain[test_type].append(
np.sum(job_len[unfinished_idx])
)
num_job_remain[test_type].append(
len(job_len[unfinished_idx])
)
job_remain_delay[test_type].append(
np.sum(pa.episode_max_length - enter_time[unfinished_idx])
)
env.seq_no = (env.seq_no + 1) % env.pa.num_ex
# -- matplotlib colormap no overlap --
if plot:
num_colors = len(test_types)
cm = plt.get_cmap('gist_rainbow')
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_color_cycle([cm(1. * i / num_colors) for i in range(num_colors)])
for test_type in test_types:
slow_down_cdf = np.sort(np.concatenate(jobs_slow_down[test_type]))
slow_down_yvals = np.arange(len(slow_down_cdf))/float(len(slow_down_cdf))
ax.plot(slow_down_cdf, slow_down_yvals, linewidth=2, label=test_type)
plt.legend(loc=4)
plt.xlabel("job slowdown", fontsize=20)
plt.ylabel("CDF", fontsize=20)
# plt.show()
plt.savefig(pg_resume + "_slowdown_fig" + ".pdf")
return all_discount_rews, jobs_slow_down
def main():
pa = parameters.Parameters()
pa.simu_len = 200 # 5000 # 1000
pa.num_ex = 10 # 100
pa.num_nw = 10
pa.num_seq_per_batch = 20
# pa.max_nw_size = 5
# pa.job_len = 5
pa.new_job_rate = 0.3
pa.discount = 1
pa.episode_max_length = 20000 # 2000
pa.compute_dependent_parameters()
render = False
plot = True # plot slowdown cdf
pg_resume = None
pg_resume = 'data/pg_re_discount_1_rate_0.3_simu_len_200_num_seq_per_batch_20_ex_10_nw_10_1450.pkl'
# pg_resume = 'data/pg_re_1000_discount_1_5990.pkl'
pa.unseen = True
launch(pa, pg_resume, render, plot, repre='image', end='all_done')
if __name__ == '__main__':
main()