-
Notifications
You must be signed in to change notification settings - Fork 3
/
plotting_rollouts.py
418 lines (389 loc) · 19.3 KB
/
plotting_rollouts.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import os
import numpy as np
import pandas as pd
import seaborn as sb
from matplotlib import pyplot as plt
from utils import RMS, log_multivariate_normal_likelihood, reshape_pt1, \
reshape_dim1
sb.set_style('whitegrid')
# Some useful plotting functions to run open-loop rollouts (trajectory of GP
# predictions given a true and a control trajectory)
# Run open-loop rollouts of GP model
def model_rollout(dyn_GP, folder, init_state, control_traj, true_mean,
rollout_length=100, title=None, verbose=False, save=False,
only_prior=False):
rollout_length = int(np.min([rollout_length, len(true_mean) - 1]))
predicted_mean = np.zeros((rollout_length + 1, init_state.shape[1]))
predicted_lowconf = np.zeros((rollout_length + 1, init_state.shape[1]))
predicted_uppconf = np.zeros((rollout_length + 1, init_state.shape[1]))
predicted_var = np.zeros((rollout_length + 1, 1))
predicted_mean[0] = init_state
predicted_lowconf[0] = init_state
predicted_uppconf[0] = init_state
predicted_var[0] = np.zeros((1, 1))
time = np.arange(rollout_length + 1)
for t in range(rollout_length):
control = control_traj[t]
if 'Michelangelo' in dyn_GP.system:
# True and predicted trajectory over time (random start, random
# control) with Euler to get xt+1 from GP xt, ut->phit
mean_next, varnext, next_lowconf, next_uppconf = \
dyn_GP.predict_euler_Michelangelo(predicted_mean[t], control,
only_prior=only_prior)
elif ('justvelocity' in dyn_GP.system) and not dyn_GP.continuous_model:
# True and predicted trajectory over time (random start, random
# control) with Euler to get xt+1 from GP xt, ut->xn_t+1
mean_next, varnext, next_lowconf, next_uppconf = \
dyn_GP.predict_euler_discrete_justvelocity(
predicted_mean[t], control, only_prior=only_prior)
elif ('justvelocity' in dyn_GP.system) and dyn_GP.continuous_model:
# True and predicted trajectory over time (random start, random
# control) with Euler to get xt+1 from GP xt, ut->xdot_t
mean_next, varnext, next_lowconf, next_uppconf = \
dyn_GP.predict_euler_continuous_justvelocity(
predicted_mean[t], control, only_prior=only_prior)
else:
# True and predicted trajectory over time (random start, random
# control)
mean_next, varnext, next_lowconf, next_uppconf = dyn_GP.predict(
predicted_mean[t], control, only_prior=only_prior)
predicted_mean[t + 1] = mean_next
predicted_lowconf[t + 1] = next_lowconf
predicted_uppconf[t + 1] = next_uppconf
predicted_var[t + 1] = varnext
if save:
for i in range(predicted_mean.shape[1]):
if title:
name = title + 'rollout_model_predictions' + str(i) + '.pdf'
else:
name = 'Rollout_model_predictions' + str(i) + '.pdf'
plt.plot(time, true_mean[:, i], 'g', label='True trajectory')
plt.plot(time, predicted_mean[:, i], 'b',
label='Predicted trajectory',
alpha=0.7)
plt.fill_between(time,
predicted_lowconf[:, i],
predicted_uppconf[:, i],
facecolor='blue', alpha=0.2)
if not dyn_GP.ground_truth_approx:
plt.title(
'Roll out of predicted and true trajectory over time, '
'random start, random control')
else:
if title and ('Test' in title):
plt.title(
'Roll out of predicted and true trajectory over time '
'over testing data')
elif title and ('Val' in title):
plt.title(
'Roll out of predicted and true trajectory over time '
'over validation data')
else:
plt.title('Roll out of predicted and true trajectory over '
'time, random start, data control')
plt.legend()
plt.xlabel('Time steps')
plt.ylabel('State')
plt.savefig(os.path.join(folder, name), bbox_inches='tight')
if verbose:
plt.show()
plt.close('all')
for i in range(predicted_mean.shape[1] - 1):
if title:
name = title + 'rollout_phase_portrait' + str(i) + '.pdf'
else:
name = 'Rollout_phase_portrait' + str(i) + '.pdf'
plt.plot(true_mean[:, i], true_mean[:, i + 1], 'g',
label='True trajectory')
plt.plot(predicted_mean[:, i], predicted_mean[:, i + 1], 'b',
label='Predicted trajectory', alpha=0.7)
plt.fill_between(predicted_mean[:, i],
predicted_lowconf[:, i + 1],
predicted_uppconf[:, i + 1],
facecolor='blue', alpha=0.2)
if not dyn_GP.ground_truth_approx:
plt.title(
'Roll out of predicted and true phase portrait, random '
'start, random control')
else:
if title and ('Test' in title):
plt.title('Roll out of predicted and true phase portrait '
'over testing data')
elif title and ('Val' in title):
plt.title('Roll out of predicted and true phase portrait '
'over validation data')
else:
plt.title('Roll out of predicted and true phase portrait, '
'random start, data control')
plt.legend()
plt.xlabel('x_' + str(i))
plt.ylabel('x_' + str(i + 1))
plt.savefig(os.path.join(folder, name), bbox_inches='tight')
if verbose:
plt.show()
plt.close('all')
RMSE = RMS(predicted_mean - true_mean)
log_likelihood = log_multivariate_normal_likelihood(true_mean[1:, :],
predicted_mean[1:, :],
predicted_var[1:, :])
return init_state, control_traj, true_mean, predicted_mean, predicted_var, \
predicted_lowconf, predicted_uppconf, RMSE, log_likelihood
# Save the results of rollouts
def save_rollout_variables(results_folder, nb_rollouts, rollout_list, step,
results=False, ground_truth_approx=False,
plots=True, title=None):
if title:
folder = os.path.join(results_folder, title + '_' + str(step))
else:
folder = os.path.join(results_folder, 'Rollouts_' + str(step))
os.makedirs(folder, exist_ok=True)
for i in range(nb_rollouts):
rollout_folder = os.path.join(folder, 'Rollout_' + str(i))
if results:
filename = 'Predicted_mean_traj.csv'
file = pd.DataFrame(reshape_pt1(np.array(rollout_list)[i, 3]))
file.to_csv(os.path.join(rollout_folder, filename),
header=False)
filename = 'Predicted_var_traj.csv'
file = pd.DataFrame(reshape_pt1(np.array(rollout_list)[i, 4]))
file.to_csv(os.path.join(rollout_folder, filename),
header=False)
filename = 'Predicted_lowconf_traj.csv'
file = pd.DataFrame(reshape_pt1(np.array(rollout_list)[i, 5]))
file.to_csv(os.path.join(rollout_folder, filename),
header=False)
filename = 'Predicted_uppconf_traj.csv'
file = pd.DataFrame(reshape_pt1(np.array(rollout_list)[i, 6]))
file.to_csv(os.path.join(rollout_folder, filename),
header=False)
filename = 'RMSE.csv'
file = pd.DataFrame(reshape_pt1(np.array(rollout_list)[i, 7]))
file.to_csv(os.path.join(rollout_folder, filename),
header=False)
filename = 'SRMSE.csv'
file = pd.DataFrame(reshape_pt1(np.array(rollout_list)[i, 8]))
file.to_csv(os.path.join(rollout_folder, filename),
header=False)
filename = 'Log_likelihood.csv'
file = pd.DataFrame(reshape_pt1(np.array(rollout_list)[i, 9]))
file.to_csv(os.path.join(rollout_folder, filename),
header=False)
filename = 'Standardized_log_likelihood.csv'
file = pd.DataFrame(reshape_pt1(np.array(rollout_list)[i, 10]))
file.to_csv(os.path.join(rollout_folder, filename),
header=False)
true_mean = reshape_dim1(np.array(rollout_list)[i, 2])
predicted_mean = reshape_dim1(np.array(rollout_list)[i, 3])
predicted_lowconf = reshape_dim1(np.array(rollout_list)[i, 5])
predicted_uppconf = reshape_dim1(np.array(rollout_list)[i, 6])
time = np.arange(0, len(true_mean))
if plots:
for k in range(predicted_mean.shape[1]):
name = 'Rollout_model_predictions' + str(k) + '.pdf'
plt.plot(time, true_mean[:, k], 'g',
label='True trajectory')
plt.plot(time, predicted_mean[:, k], 'b',
label='Predicted trajectory', alpha=0.7)
plt.fill_between(time,
predicted_lowconf[:, k],
predicted_uppconf[:, k],
facecolor='blue', alpha=0.2)
if not ground_truth_approx:
plt.title('Roll out of predicted and true trajectory '
'over time, random start, random control')
else:
plt.title('Roll out of predicted and true trajectory '
'over time, random start, data control')
plt.legend()
plt.xlabel('Time steps')
plt.ylabel('State')
plt.savefig(os.path.join(rollout_folder, name),
bbox_inches='tight')
plt.close('all')
for k in range(predicted_mean.shape[1] - 1):
name = 'Rollout_phase_portrait' + str(k) + '.pdf'
plt.plot(true_mean[:, k], true_mean[:, k + 1], 'g',
label='True trajectory')
plt.plot(predicted_mean[:, k], predicted_mean[:, k + 1],
'b', label='Predicted trajectory', alpha=0.7)
plt.fill_between(predicted_mean[:, k],
predicted_lowconf[:, k + 1],
predicted_uppconf[:, k + 1],
facecolor='blue', alpha=0.2)
if not ground_truth_approx:
plt.title('Roll out of predicted and true phase '
'portrait over time, random start, '
'random control')
else:
plt.title('Roll out of predicted and true phase '
'portrait over time, random start, '
'data control')
plt.legend()
plt.xlabel('x_' + str(k))
plt.ylabel('x_' + str(k + 1))
plt.savefig(os.path.join(rollout_folder, name),
bbox_inches='tight')
plt.close('all')
else:
os.makedirs(rollout_folder, exist_ok=True)
filename = 'Init_state.csv'
file = pd.DataFrame(np.array(rollout_list)[i, 0])
file.to_csv(os.path.join(rollout_folder, filename),
header=False)
filename = 'Control_traj.csv'
file = pd.DataFrame(np.array(rollout_list)[i, 1])
file.to_csv(os.path.join(rollout_folder, filename),
header=False)
filename = 'True_traj.csv'
file = pd.DataFrame(np.array(rollout_list)[i, 2])
file.to_csv(os.path.join(rollout_folder, filename),
header=False)
# Plot quantities about rollouts over time
def plot_rollout_data(dyn_GP, folder):
name = 'Rollout_RMSE'
RMSE_df = pd.DataFrame(dyn_GP.rollout_RMSE)
RMSE_df.to_csv(os.path.join(folder, name + '.csv'), header=False)
plt.plot(dyn_GP.rollout_RMSE[:, 0], dyn_GP.rollout_RMSE[:, 1],
'c', label='RMSE')
plt.title(
'Rollout RMSE over time, over ' + str(dyn_GP.nb_rollouts) + ' rollouts')
plt.xlabel('Number of samples')
plt.ylabel('RMSE over rollouts')
plt.legend()
plt.savefig(os.path.join(folder, name + '.pdf'), bbox_inches='tight')
plt.close('all')
name = 'Rollout_SRMSE'
SRMSE_df = pd.DataFrame(dyn_GP.rollout_SRMSE)
SRMSE_df.to_csv(os.path.join(folder, name + '.csv'), header=False)
plt.plot(dyn_GP.rollout_SRMSE[:, 0], dyn_GP.rollout_SRMSE[:, 1],
'c', label='SRMSE')
plt.title('Rollout SRMSE over time, over ' + str(dyn_GP.nb_rollouts) +
' rollouts')
plt.xlabel('Number of samples')
plt.ylabel('SRMSE over rollouts')
plt.legend()
plt.savefig(os.path.join(folder, name + '.pdf'), bbox_inches='tight')
plt.close('all')
name = 'Rollout_log_AL'
log_AL_df = pd.DataFrame(dyn_GP.rollout_log_AL)
log_AL_df.to_csv(os.path.join(folder, name + '.csv'), header=False)
plt.plot(dyn_GP.rollout_log_AL[:, 0], dyn_GP.rollout_log_AL[:, 1],
'c', label='Average log likelihood')
plt.title('Rollout average log likelihood over time, over ' + str(
dyn_GP.nb_rollouts) + ' rollouts')
plt.xlabel('Number of samples')
plt.ylabel('Average log likelihood over rollouts')
plt.legend()
plt.savefig(os.path.join(folder, name + '.pdf'), bbox_inches='tight')
plt.close('all')
name = 'Rollout_stand_log_AL'
stand_log_AL_df = pd.DataFrame(dyn_GP.rollout_stand_log_AL)
stand_log_AL_df.to_csv(os.path.join(folder, name + '.csv'), header=False)
plt.plot(dyn_GP.rollout_stand_log_AL[:, 0],
dyn_GP.rollout_stand_log_AL[:, 1],
'c', label='Average log likelihood')
plt.title('Rollout average log likelihood over time, over ' + str(
dyn_GP.nb_rollouts) + ' rollouts')
plt.xlabel('Number of samples')
plt.ylabel('Average log likelihood over rollouts')
plt.legend()
plt.savefig(os.path.join(folder, name + '.pdf'), bbox_inches='tight')
plt.close('all')
# Plot quantities about test rollouts over time
def plot_test_rollout_data(dyn_GP, folder):
name = 'Test_rollout_RMSE'
RMSE_df = pd.DataFrame(dyn_GP.test_rollout_RMSE)
RMSE_df.to_csv(os.path.join(folder, name + '.csv'), header=False)
plt.plot(dyn_GP.test_rollout_RMSE[:, 0], dyn_GP.test_rollout_RMSE[:, 1],
'c', label='RMSE')
plt.title('Rollout RMSE over time, over testing data')
plt.xlabel('Number of samples')
plt.ylabel('RMSE')
plt.legend()
plt.savefig(os.path.join(folder, name + '.pdf'), bbox_inches='tight')
plt.close('all')
name = 'Test_rollout_SRMSE'
SRMSE_df = pd.DataFrame(dyn_GP.test_rollout_SRMSE)
SRMSE_df.to_csv(os.path.join(folder, name + '.csv'), header=False)
plt.plot(dyn_GP.test_rollout_SRMSE[:, 0], dyn_GP.test_rollout_SRMSE[:, 1],
'c', label='SRMSE')
plt.title('Rollout SRMSE over time, over testing data')
plt.xlabel('Number of samples')
plt.ylabel('SRMSE')
plt.legend()
plt.savefig(os.path.join(folder, name + '.pdf'), bbox_inches='tight')
plt.close('all')
name = 'Test_rollout_log_AL'
log_AL_df = pd.DataFrame(dyn_GP.test_rollout_log_AL)
log_AL_df.to_csv(os.path.join(folder, name + '.csv'), header=False)
plt.plot(dyn_GP.test_rollout_log_AL[:, 0], dyn_GP.test_rollout_log_AL[:, 1],
'c', label='Average log likelihood')
plt.title('Rollout average log likelihood over time, over testing data')
plt.xlabel('Number of samples')
plt.ylabel('Average log likelihood')
plt.legend()
plt.savefig(os.path.join(folder, name + '.pdf'),
bbox_inches='tight')
plt.close('all')
name = 'Test_rollout_stand_log_AL'
stand_log_AL_df = pd.DataFrame(dyn_GP.test_rollout_stand_log_AL)
stand_log_AL_df.to_csv(os.path.join(folder, name + '.csv'), header=False)
plt.plot(dyn_GP.test_rollout_stand_log_AL[:, 0],
dyn_GP.test_rollout_stand_log_AL[:, 1],
'c', label='Average log likelihood')
plt.title('Rollout average log likelihood over time, over testing data')
plt.xlabel('Number of samples')
plt.ylabel('Average log likelihood')
plt.legend()
plt.savefig(os.path.join(folder, name + '.pdf'),
bbox_inches='tight')
plt.close('all')
# Plot quantities about validation rollouts over time
def plot_val_rollout_data(dyn_GP, folder):
name = 'Val_rollout_RMSE'
RMSE_df = pd.DataFrame(dyn_GP.val_rollout_RMSE)
RMSE_df.to_csv(os.path.join(folder, name + '.csv'), header=False)
plt.plot(dyn_GP.val_rollout_RMSE[:, 0], dyn_GP.val_rollout_RMSE[:, 1],
'c', label='RMSE')
plt.title('Rollout RMSE over time, over validation data')
plt.xlabel('Number of samples')
plt.ylabel('RMSE')
plt.legend()
plt.savefig(os.path.join(folder, name + '.pdf'), bbox_inches='tight')
plt.close('all')
name = 'Val_rollout_SRMSE'
SRMSE_df = pd.DataFrame(dyn_GP.val_rollout_SRMSE)
SRMSE_df.to_csv(os.path.join(folder, name + '.csv'), header=False)
plt.plot(dyn_GP.val_rollout_SRMSE[:, 0], dyn_GP.val_rollout_SRMSE[:, 1],
'c', label='SRMSE')
plt.title('Rollout SRMSE over time, over validation data')
plt.xlabel('Number of samples')
plt.ylabel('SRMSE')
plt.legend()
plt.savefig(os.path.join(folder, name + '.pdf'), bbox_inches='tight')
plt.close('all')
name = 'Val_rollout_log_AL'
log_AL_df = pd.DataFrame(dyn_GP.val_rollout_log_AL)
log_AL_df.to_csv(os.path.join(folder, name + '.csv'), header=False)
plt.plot(dyn_GP.val_rollout_log_AL[:, 0], dyn_GP.val_rollout_log_AL[:, 1],
'c', label='Average log likelihood')
plt.title('Rollout average log likelihood over time, over validation data')
plt.xlabel('Number of samples')
plt.ylabel('Average log likelihood')
plt.legend()
plt.savefig(os.path.join(folder, name + '.pdf'),
bbox_inches='tight')
plt.close('all')
name = 'Val_rollout_stand_log_AL'
stand_log_AL_df = pd.DataFrame(dyn_GP.val_rollout_stand_log_AL)
stand_log_AL_df.to_csv(os.path.join(folder, name + '.csv'), header=False)
plt.plot(dyn_GP.val_rollout_stand_log_AL[:, 0],
dyn_GP.val_rollout_stand_log_AL[:, 1],
'c', label='Average log likelihood')
plt.title('Rollout average log likelihood over time, over validation data')
plt.xlabel('Number of samples')
plt.ylabel('Average log likelihood')
plt.legend()
plt.savefig(os.path.join(folder, name + '.pdf'),
bbox_inches='tight')
plt.close('all')