This repository has been archived by the owner on Jun 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
plot_against_baseline.py
331 lines (212 loc) · 7.64 KB
/
plot_against_baseline.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
# %%
import pandas as pd
import os
import os.path as osp
import matplotlib.pyplot as plt
import numpy as np
import copy
import csv
from main import get_cmd_args, get_log_dir
from utils.env_utils import domain_to_epoch
plt.rcParams['font.size'] = '12'
def get_one_domain_one_run_res(domain, seed, hyper_params):
args = get_cmd_args()
args.base_log_dir = RLKIT_BASE_LOG_DIR
args.domain = domain
args.seed = seed
for k, v in hyper_params.items():
setattr(args, k, v)
res_path = get_log_dir(args)
csv_path = osp.join(
res_path, 'progress.csv'
)
values = []
with open(csv_path, 'r') as csv_file:
reader = csv.reader(csv_file)
col_names = next(reader)
# Assume that the index of epoch is the last one
# Not sure why the csv file is missing one col header
# epoch_col_idx = col_names.index('Epoch')
epoch_col_idx = -1
val_col_idx = col_names.index('remote_evaluation/Average Returns')
for row in reader:
# If this equals Epoch, it means the header
# was written to the csv file again
# and we reset everything
if row[epoch_col_idx] == 'Epoch':
values = []
continue
epoch = int(row[epoch_col_idx])
val = float(row[val_col_idx])
# We need to check if the row contains the values
# of the correct epoch
# because after reloading from checkpoint,
# we are writing the result to the same csv file
if epoch == len(values):
values.append(val)
else:
# print(
# f'Reloaded row found at epoch {len(values), epoch} found for', domain, seed, hyper_params)
pass
# Reshape the return value
# to accomodate downstream api
values = np.array(values)
values = np.expand_dims(values, axis=-1)
return values
def get_one_domain_all_run_res(domain, run_idxes, hyper_params):
results = []
for idx in run_idxes:
res = get_one_domain_one_run_res(domain, idx, hyper_params)
results.append(res)
min_rows = min([len(col) for col in results])
results = [col[0:min_rows] for col in results]
results = np.hstack(results)
return results
def smooth_results(results, smoothing_window=100):
smoothed = np.zeros((results.shape[0], results.shape[1]))
for idx in range(len(smoothed)):
if idx == 0:
smoothed[idx] = results[idx]
continue
start_idx = max(0, idx - smoothing_window)
smoothed[idx] = np.mean(results[start_idx:idx], axis=0)
return smoothed
def plot(values, label, color=[0, 0, 1, 1]):
mean = np.mean(values, axis=1)
std = np.std(values, axis=1)
x_vals = np.arange(len(mean))
blur = copy.deepcopy(color)
blur[-1] = 0.1
plt.plot(x_vals, mean, label=label, color=color)
plt.fill_between(x_vals, mean - std, mean + std, color=blur)
plt.legend()
# DOMAINS = ['humanoid', 'halfcheetah', 'hopper', 'ant', 'walker2d']
DOMAINS = ['humanoid']
RLKIT_BASE_LOG_DIR_BASELINE = RLKIT_BASE_LOG_DIR_ALGO = './data'
RUN_IDXES = list([i for i in range(5) if i is not 3])
NUM_RUN = len(RUN_IDXES)
SAC_ONE_RETRAINING_PARAMS = dict(
delta=0.0,
beta_UB=0.0,
num_expl_steps_per_train_loop=1000,
num_trains_per_train_loop=1000
)
SAVE_FIG = True
print('SAVE_FIG', SAVE_FIG)
FORMAL_FIG = True
print('FORMAL_FIG', FORMAL_FIG)
def sac_get_one_domain_one_run_res(path, domain, seed):
csv_path = osp.join(
path, domain, f'seed_{seed}', 'progress.csv'
)
result = pd.read_csv(csv_path, usecols=[
'remote_evaluation/Average Returns'])
return result.values
def sac_plot_one_retraining_step(domain):
args = get_cmd_args()
set_attr_with_dict(args, SAC_ONE_RETRAINING_PARAMS)
results = get_one_domain_all_run_res(
domain, RUN_IDXES, SAC_ONE_RETRAINING_PARAMS)
results = smooth_results(results)
if FORMAL_FIG:
label = 'Soft Actor Critic'
else:
label = 'SAC'
plot(results, label=label)
def sac_plot(domain, num_trains_per_train_loop):
if num_trains_per_train_loop == 1000:
sac_plot_one_retraining_step(domain)
elif num_trains_per_train_loop == 4000:
sac_plot_four_retraining_step(domain)
else:
exit('Unrecognized environment setting')
def get_plot_title(args):
if FORMAL_FIG:
title = args.env
else:
title = '\n'.join([
args.env,
f'num_run: {NUM_RUN}', '---'
f'beta_UB: {args.beta_UB}',
f'delta: {args.delta}',
f'train/env step ratio: {int(args.num_trains_per_train_loop / args.num_expl_steps_per_train_loop)}'
])
return title
all_hyper_params_dict = [
dict(
delta=23.53,
beta_UB=4.66,
num_expl_steps_per_train_loop=1000,
num_trains_per_train_loop=1000
),
]
def set_attr_with_dict(target, source_dict):
for k, v in source_dict.items():
setattr(target, k, v)
return target
def get_tick_space(domain):
if domain == 'Hopper':
return 200
if domain == 'humanoid':
return 1000
return 500
for hyper_params in all_hyper_params_dict:
for domain in DOMAINS:
plt.clf()
"""
Set up
"""
# We need to do this so that jupyter notebook
# works with argparse
import sys
sys.argv = ['']
del sys
args = get_cmd_args()
set_attr_with_dict(args, hyper_params)
args.env = f'{domain}-v2'
relative_log_dir = get_log_dir(
args, should_include_base_log_dir=False, should_include_seed=False, should_include_domain=False)
graph_base_path = osp.join(
RLKIT_BASE_LOG_DIR_ALGO, 'plot', relative_log_dir)
os.makedirs(graph_base_path, exist_ok=True)
"""
Obtain Result
"""
RLKIT_BASE_LOG_DIR = RLKIT_BASE_LOG_DIR_ALGO
results = get_one_domain_all_run_res(domain, RUN_IDXES, hyper_params)
results = smooth_results(results)
if domain == 'humanoid' and FORMAL_FIG:
mean = np.mean(results, axis=1)
x_vals = np.arange(len(mean))
# This is the index where OAC has
# the same performance as SAC with 10 million steps
# Plus 200 so that we are not overstating our claim
magic_idx = np.argmax(mean > 8000) + 300
plt.plot(8000 * np.ones(magic_idx), linestyle='--',
color=[0, 0, 1, 1], linewidth=3, label='Soft Actor Critic 10 million steps performance')
plt.vlines(x=magic_idx,
ymin=0, ymax=8000, linestyle='--',
color=[0, 0, 1, 1],)
"""
Plot result
"""
plot(results, label='Optimistic Actor Critic',
color=[1.0, 0.0, 0.0, 1.0])
RLKIT_BASE_LOG_DIR = RLKIT_BASE_LOG_DIR_BASELINE
sac_plot(domain, args.num_trains_per_train_loop)
plt.title(get_plot_title(args))
plt.ylabel('Average Episode Return')
xticks = np.arange(0, domain_to_epoch(
domain) + 1, get_tick_space(domain))
plt.xticks(xticks, xticks / 1000.0)
plt.xlabel('Number of environment steps in millions')
plt.legend()
if not SAVE_FIG:
plt.show()
else:
fig_path = osp.join(
graph_base_path, f'{args.env}_formal_fig_{FORMAL_FIG}.png')
plt.savefig(fig_path, bbox_inches='tight')
print(f'Saved fig at {fig_path}')
print('Finish plotting for: ', hyper_params)
# %%