-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexpt_utils.py
303 lines (283 loc) · 11 KB
/
expt_utils.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
import numpy as np
import matplotlib.pyplot as plt
from scipy import linalg
from scipy.stats import linregress
import pdb
from utils import *
from datetime import datetime
import importlib
import pickle
import os
from tqdm import trange
from copy import deepcopy
def dump_script(
dirname, script_file, dest=None, timestamp=None, file_list=None):
import glob, os, shutil, sys
from datetime import datetime
if dest is None:
if timestamp is None:
timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
dest = os.path.join(
dirname, 'script_{}'.format(timestamp))
os.mkdir(dest)
print('copying files to {}'.format(dest))
if file_list is None:
file_list = glob.glob("*.py")
for file in file_list:
print('copying {}'.format(file))
shutil.copy2(file, dest)
print('copying {}'.format(script_file))
shutil.copy2(script_file, dest)
with open(os.path.join(dest, "command.txt"), "w") as f:
f.write(" ".join(sys.argv) + "\n")
def save_output(output, timestamp, dir_name=None):
if dir_name == None:
dir_name = '.'
f = open('{}/output_{}.pkl'.format(
dir_name, timestamp), 'wb')
pickle.dump(output,f)
f.close()
def algos_vs_var_metrics(
prob_dict, algos_dict, prob_function, T,
results_dir = None, script_file=None, xlims = None, ylims = None, load = None, log_y = False
):
list_num = [isinstance(prob_dict[k], list) for k in prob_dict]
if sum(list_num) > 1:
raise Exception("More than one variables!")
variable = np.array(list(prob_dict.keys()))[list_num][0]
N_trials = 1
for algo in algos_dict:
algo_N_trials = algos_dict[algo][2]
if algo_N_trials < 1:
raise ValueError
N_trials = max(algo_N_trials, N_trials)
timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
results_dir = 'results' if results_dir is None else results_dir
expt_name = '{}_vs_{}'.format(
'-'.join(algos_dict.keys()), timestamp)
dir_name = os.path.join(results_dir, expt_name)
os.makedirs(dir_name, exist_ok=True)
dump_script(dir_name, script_file, timestamp=timestamp, file_list=[])
end_list = {algo:[] for algo in algos_dict}
reg_list_total = []
if load == None:
for t_var in prob_dict[variable]:
reg_list = {algo:[] for algo in algos_dict}
_end_list = {algo:[] for algo in algos_dict}
t_prob_dict = deepcopy(prob_dict)
t_prob_dict[variable] = t_var
for trial_idx in trange(N_trials):
prob = prob_function(**t_prob_dict)
# print(prob.x)
for algo, algo_setup in algos_dict.items():
algo_func, algo_params, algo_N_trials, _, _ = algo_setup
# print(algo_func)
if not trial_idx < algo_N_trials:
continue
solver = algo_func(prob, **algo_params)
reg, _ = solver.run(T)
# print(prob.rewards)
reg_list[algo].append(reg)
_end_list[algo].append(reg[-1]) # ultimate regret
for algo in algos_dict:
algo_label = algos_dict[algo][3]
algo_style = algos_dict[algo][4]
plt.plot(range(T), np.mean(reg_list[algo], axis = 0),
algo_style, label=algo_label)
if log_y:
plt.yscale('log')
if xlims is not None:
plt.xlim(xlims)
if ylims is not None:
plt.ylim(ylims)
plt.legend()
plot_file_name = expt_name
# plt.title(plt_titles[metric])
plt.xlabel("T")
plt.ylabel("Regrets")
# plt.show()
plt.savefig(os.path.join(dir_name, plot_file_name + "_" + str(t_var) +'_labeled.png'))
plt.savefig(os.path.join(dir_name, plot_file_name + "_" + str(t_var) +'_labeled.pdf'))
plt.close()
for algo in algos_dict:
end_list[algo].append(_end_list[algo])
reg_list_total.append(reg_list)
else:
f = open(load, 'rb')
reg_list_total = pickle.load(f)
f.close()
# pdb.set_trace()
for t_var in range(len(prob_dict[variable])):
for algo, algo_setup in algos_dict.items():
tmp = [reg_list_total[t_var][algo][trial_idx][T-1] for trial_idx in trange(N_trials)]
end_list[algo].append(tmp)
for t_var in range(len(prob_dict[variable])):
for algo in algos_dict:
algo_label = algos_dict[algo][3]
algo_style = algos_dict[algo][4]
y = np.mean(reg_list_total[t_var][algo], axis = 0)[:T]
ci = np.std(reg_list_total[t_var][algo], axis = 0)[:T]
plt.plot(range(T), y,
"-" + algo_style, label=algo_label)
plt.fill_between(range(T), (y-ci), (y+ci), color = algo_style, alpha=.1)
if log_y:
plt.yscale('log')
if xlims is not None:
plt.xlim(xlims)
if ylims is not None:
plt.ylim(ylims)
plt.legend()
plot_file_name = expt_name
plt.xlabel("T")
plt.ylabel("Regrets")
plt.savefig(os.path.join(dir_name, plot_file_name + "_" + str(t_var) +'_labeled.png'))
plt.savefig(os.path.join(dir_name, plot_file_name + "_" + str(t_var) +'_labeled.pdf'))
plt.close()
save_output(reg_list_total, timestamp, dir_name)
# plot summary
# pdb.set_trace()
# a = 1
for algo in algos_dict:
algo_label = algos_dict[algo][3]
algo_style = algos_dict[algo][4]
# pdb.set_trace()
y = np.mean(end_list[algo], axis = 1)
ci = np.std(end_list[algo], axis = 1)
plt.plot(prob_dict[variable], y,
"-s" + algo_style, label=algo_label)
plt.fill_between(prob_dict[variable], (y-ci), (y+ci), color = algo_style, alpha=.1)
if log_y:
plt.yscale('log')
if xlims is not None:
plt.xlim(xlims)
if ylims is not None:
plt.ylim(ylims)
plt.legend()
plot_file_name = expt_name
# plt.title(plt_titles[metric])
plt.xlabel(variable)
plt.ylabel("%d-step Regrets"%(T))
# plt.show()
plt.savefig(os.path.join(dir_name, plot_file_name +'_summarized_labeled.png'))
plt.savefig(os.path.join(dir_name, plot_file_name +'_summarized_labeled.pdf'))
plt.close()
def algos_metrics(
prob_dict, algos_dict, prob_function, T,
results_dir = None, script_file=None, xlims = None, ylims = None, load = None, log_y = False
):
N_trials = 1
for algo in algos_dict:
algo_N_trials = algos_dict[algo][2]
if algo_N_trials < 1:
raise ValueError
N_trials = max(algo_N_trials, N_trials)
timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
results_dir = 'results' if results_dir is None else results_dir
expt_name = '{}_vs_{}'.format(
'-'.join(algos_dict.keys()), timestamp)
dir_name = os.path.join(results_dir, expt_name)
os.makedirs(dir_name, exist_ok=True)
dump_script(dir_name, script_file, timestamp=timestamp, file_list=[])
reg_list = {algo:[] for algo in algos_dict}
if load == None:
for trial_idx in trange(N_trials):
prob = prob_function(**prob_dict)
# print(prob.x)
for algo, algo_setup in algos_dict.items():
algo_func, algo_params, algo_N_trials, _, _ = algo_setup
# print(algo_func)
if not trial_idx < algo_N_trials:
continue
solver = algo_func(prob, **algo_params)
reg, _ = solver.run(T)
# print(prob.rewards)
reg_list[algo].append(reg)
else:
f = open(load, 'rb')
reg_list = pickle.load(f)
f.close()
save_output(reg_list, timestamp, dir_name)
for algo in algos_dict:
algo_label = algos_dict[algo][3]
algo_style = algos_dict[algo][4]
y = np.mean(reg_list[algo], axis = 0)[:T]
ci = np.std(reg_list[algo], axis = 0)[:T]
plt.plot(range(T), y,
algo_style, label=algo_label)
plt.fill_between(range(T), (y-ci), (y+ci), color = algo_style, alpha=.1)
if log_y:
plt.yscale('log')
if xlims is not None:
plt.xlim(xlims)
if ylims is not None:
plt.ylim(ylims)
plt.legend()
plot_file_name = expt_name
# plt.title(plt_titles[metric])
plt.xlabel("T")
plt.ylabel("Regrets")
# plt.show()
plt.savefig(os.path.join(dir_name, plot_file_name+'_labeled.png'))
plt.savefig(os.path.join(dir_name, plot_file_name+'_labeled.pdf'))
plt.close()
def algos_real_data(
prob, algos_dict, prob_function, T,
results_dir = None, script_file=None, xlims = None, ylims = None, load = None, log_y = False
):
N_trials = 1
for algo in algos_dict:
algo_N_trials = algos_dict[algo][2]
if algo_N_trials < 1:
raise ValueError
N_trials = max(algo_N_trials, N_trials)
timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
results_dir = 'results' if results_dir is None else results_dir
expt_name = '{}_vs_{}'.format(
'-'.join(algos_dict.keys()), timestamp)
dir_name = os.path.join(results_dir, expt_name)
os.makedirs(dir_name, exist_ok=True)
dump_script(dir_name, script_file, timestamp=timestamp, file_list=[])
reg_list = {algo:[] for algo in algos_dict}
if load == None:
for trial_idx in trange(N_trials):
t_prob = deepcopy(prob)
# print(prob.x)
for algo, algo_setup in algos_dict.items():
algo_func, algo_params, algo_N_trials, _, _ = algo_setup
# print(algo_func)
if not trial_idx < algo_N_trials:
continue
solver = algo_func(t_prob, **algo_params)
reg, _ = solver.run(T)
# print(prob.rewards)
reg_list[algo].append(reg)
else:
f = open(load, 'rb')
reg_list = pickle.load(f)
f.close()
save_output(reg_list, timestamp, dir_name)
# output, timestamp, dir_name=None
for algo in algos_dict:
algo_label = algos_dict[algo][3]
algo_style = algos_dict[algo][4]
y = np.mean(reg_list[algo], axis = 0)[:T]
ci = np.std(reg_list[algo], axis = 0)[:T]
plt.plot(range(T), y,
"-"+algo_style, label=algo_label)
plt.fill_between(range(T), (y-ci), (y+ci), color = algo_style, alpha=.1)
if log_y:
plt.yscale('log')
if xlims is not None:
plt.xlim(xlims)
if ylims is not None:
plt.ylim(ylims)
plt.legend()
plot_file_name = expt_name
# plt.title(plt_titles[metric])
plt.xlabel("T")
plt.ylabel("Regrets")
# plt.show()
plt.savefig(os.path.join(dir_name, plot_file_name+'_labeled.png'))
plt.savefig(os.path.join(dir_name, plot_file_name+'_labeled.pdf'))
plt.close()
# generate numerical summary