-
Notifications
You must be signed in to change notification settings - Fork 5
/
overlay.py
927 lines (862 loc) · 38.5 KB
/
overlay.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
# General code that should be globally run and or useful re-usable utility functions
import gym
import numpy as np
import pyvirtualdisplay
import torch
import torch.nn as nn
from tqdm import tqdm
from envs.oderl import envs
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # 'cpu'
torch.set_default_dtype(torch.float32)
def start_virtual_display():
return pyvirtualdisplay.Display(visible=0, size=(1400, 900)).start() # pyright: ignore
def create_oderl_env(env_name, dt=0.05, ts_grid="fixed", noise=0.0, friction=False):
################## environment and dataset ##################
# dt = 0.05 # mean time difference between observations
# noise = 0.0 # observation noise std
# ts_grid = 'fixed' # the distribution for the observation time differences: ['fixed','uniform','exp']
# ENV_CLS = envs.CTCartpole # [CTPendulum, CTCartpole, CTAcrobot]
if env_name == "oderl-pendulum":
ENV_CLS = envs.CTPendulum # [CTPendulum, CTCartpole, CTAcrobot]
elif env_name == "oderl-cartpole":
ENV_CLS = envs.CTCartpole # [CTPendulum, CTCartpole, CTAcrobot]
elif env_name == "oderl-acrobot":
ENV_CLS = envs.CTAcrobot # [CTPendulum, CTCartpole, CTAcrobot]
else:
raise ValueError(f"Unknown environment: {env_name}")
env = ENV_CLS(
dt=dt,
obs_trans=True,
device=device, # pyright: ignore
obs_noise=noise,
ts_grid=ts_grid,
solver="euler",
friction=friction,
)
return env
def create_env(env_name, dt=0.05, ts_grid="fixed", noise=0.0, friction=False):
if "oderl" in env_name:
env = create_oderl_env(env_name, dt=dt, ts_grid=ts_grid, noise=noise, friction=friction)
else:
env = gym.make(env_name)
return env
def setup_logger(file, log_folder="logs", return_path_to_log=False):
import logging
import os
import time
file_name = os.path.basename(os.path.realpath(file)).split(".py")[0]
from pathlib import Path
Path(f"./{log_folder}").mkdir(parents=True, exist_ok=True)
path_run_name = "{}-{}".format(file_name, time.strftime("%Y%m%d-%H%M%S"))
logging.basicConfig(
format="%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s",
handlers=[
logging.FileHandler(f"{log_folder}/{path_run_name}_log.txt"),
logging.StreamHandler(),
],
datefmt="%H:%M:%S",
level=logging.INFO,
)
logger = logging.getLogger()
# pylint: disable-next=logging-fstring-interpolation
logger.info(f"Starting: Log file at: {log_folder}/{path_run_name}_log.txt")
if return_path_to_log:
return logger, f"{log_folder}/{path_run_name}_log.txt"
else:
return logger
def load_replay_buffer(fn):
offline_dataset = np.load(fn, allow_pickle=True).item()
return offline_dataset
def get_val_loss_delay_latent(model, train_env_task, env, delay=0, dt=0.05):
s0, a0, sb, sn, _ = generate_irregular_data_delay_latent(
train_env_task, env, samples_per_dim=5, delay=delay, latent=True
)
s0, a0, sb, sn = s0.to(device), a0.to(device), sb.to(device), sn.to(device)
ts = torch.tensor([0.05]).to(device).view(1, 1).repeat(s0.shape[0], 1)
s0 = s0.double()
sb = sb.double()
a0 = a0.double()
sn = sn.double()
ts = ts.double()
pred_sd = model(
torch.cat(
(
s0.view(s0.shape[0], 1, s0.shape[1]),
sb.view(sb.shape[0], 1, sb.shape[1]),
),
dim=1,
),
a0,
ts,
)
sd = sn - s0
return nn.MSELoss()(pred_sd, sd).item()
def get_val_loss_delay_precomputed(model, s0, a0, sn, ts):
pred_sd = model(s0, a0, ts)
sd = sn - s0
return nn.MSELoss()(pred_sd.squeeze(), sd.squeeze()).item()
def compute_val_data_delay(train_env_task, env, delay, dt=0.05, samples_per_dim=5):
s0, a0, sn, _ = generate_irregular_data_delay(train_env_task, env, samples_per_dim=samples_per_dim, delay=delay)
s0, a0, sn = s0.to(device), a0.to(device), sn.to(device)
ts = torch.tensor([0.05]).to(device).view(1, 1).repeat(s0.shape[0], 1)
if train_env_task == "oderl-cartpole":
from oracle import cartpole_dynamics_dt_delay
sn = cartpole_dynamics_dt_delay(s0, a0, ts, delay=delay)
elif train_env_task == "oderl-pendulum":
from oracle import pendulum_dynamics_dt_delay
sn = pendulum_dynamics_dt_delay(s0, a0, ts, delay=delay)
s0 = s0.double()
a0 = a0.double()
sn = sn.double() # pyright: ignore
ts = ts.double()
return s0, a0, sn, ts
def get_val_loss_delay_time_multi(
model,
train_env_task,
env,
delay,
dt=0.05,
samples_per_dim=5,
encode_obs_time=False,
action_buffer_size=5,
):
s0, a0, sn, _ = generate_irregular_data_delay_time_multi(
train_env_task,
env,
samples_per_dim=samples_per_dim,
delay=delay,
encode_obs_time=encode_obs_time,
action_buffer_size=action_buffer_size,
)
s0, a0, sn = s0.to(device), a0.to(device), sn.to(device)
ts = torch.tensor([0.05]).to(device).view(1, 1).repeat(s0.shape[0], 1)
if train_env_task == "oderl-cartpole":
from oracle import cartpole_dynamics_dt_delay
if encode_obs_time:
sn = cartpole_dynamics_dt_delay(s0, a0[:, :, :1], ts, delay=delay)
else:
sn = cartpole_dynamics_dt_delay(s0, a0, ts, delay=delay)
elif train_env_task == "oderl-pendulum":
from oracle import pendulum_dynamics_dt_delay
if encode_obs_time:
sn = pendulum_dynamics_dt_delay(s0, a0[:, :, :1], ts, delay=delay)
else:
sn = pendulum_dynamics_dt_delay(s0, a0, ts, delay=delay)
s0 = s0.double()
a0 = a0.double()
sn = sn.double() # pyright: ignore
ts = ts.double()
pred_sd = model(s0, a0, ts)
sd = sn - s0
return nn.MSELoss()(pred_sd.squeeze(), sd.squeeze()).item()
def get_val_loss_delay(model, train_env_task, env, delay, dt=0.05, samples_per_dim=5):
s0, a0, sn, _ = generate_irregular_data_delay(train_env_task, env, samples_per_dim=samples_per_dim, delay=delay)
s0, a0, sn = s0.to(device), a0.to(device), sn.to(device)
ts = torch.tensor([0.05]).to(device).view(1, 1).repeat(s0.shape[0], 1)
if train_env_task == "oderl-cartpole":
from oracle import cartpole_dynamics_dt_delay
sn = cartpole_dynamics_dt_delay(s0, a0, ts, delay=delay)
elif train_env_task == "oderl-pendulum":
from oracle import pendulum_dynamics_dt_delay
sn = pendulum_dynamics_dt_delay(s0, a0, ts, delay=delay)
s0 = s0.double()
a0 = a0.double()
sn = sn.double() # pyright: ignore
ts = ts.double()
pred_sd = model(s0, a0, ts)
sd = sn - s0
return nn.MSELoss()(pred_sd, sd).item()
def get_val_loss(model, train_env_task, env):
s0, a0, sn, ts = generate_irregular_data(train_env_task, env, samples_per_dim=5)
s0, a0, sn, ts = s0.to(device), a0.to(device), sn.to(device), ts.to(device)
if train_env_task == "oderl-cartpole":
raise NotImplementedError
# from oracle import cartpole_dynamics
# sn = cartpole_dynamics(s0, a0)
elif train_env_task == "oderl-pendulum":
from oracle import pendulum_dynamics_dt
sn = pendulum_dynamics_dt(s0, a0) # pyright: ignore # pylint: disable=no-value-for-parameter
ts = torch.tensor([0.05]).to(device).view(1, 1).repeat(s0.shape[0], 1)
s0 = s0.double()
a0 = a0.double()
sn = sn.double()
ts = ts.double()
pred_sd = model(s0, a0, ts)
sd = sn - s0
return nn.MSELoss()(pred_sd, sd).item()
def generate_irregular_data_delay_latent(
train_env_task,
env,
delay,
samples_per_dim=None,
mode="grid",
rand=False,
latent=False,
):
if samples_per_dim is None:
if train_env_task == "oderl-pendulum":
samples_per_dim = 33
elif train_env_task == "oderl-cartpole":
samples_per_dim = 20
elif train_env_task == "oderl-acrobot":
samples_per_dim = 15
ACTION_HIGH = env.action_space.high[0]
# ACTION_LOW = env.action_space.low[0]
nu = env.action_space.shape[0]
if train_env_task == "oderl-cartpole":
state_max = torch.tensor([5.0, 20, torch.pi, 30])
state_min = -state_max
device_h = "cpu"
s0_l, a0_l, sb_l, sn_l, ts_l = [], [], [], [], []
# pylint: disable-next=unused-variable
for ti in range(samples_per_dim): # pyright: ignore
if rand:
s0s = (
(torch.rand(samples_per_dim**4, 4, dtype=torch.double, device=device_h) - 0.5) # pyright: ignore
* 2.0
* state_max
)
action_max = torch.tensor([ACTION_HIGH])
actions = (
(torch.rand(samples_per_dim, 1, dtype=torch.double, device=device_h) - 0.5) # pyright: ignore
* 2.0
* action_max
)
actions = actions.view(-1)
else:
x, y, z, k = torch.meshgrid(
torch.linspace(state_min[0], state_max[0], samples_per_dim, device=device_h), # pyright: ignore
torch.linspace(state_min[1], state_max[1], samples_per_dim, device=device_h), # pyright: ignore
torch.linspace(state_min[2], state_max[2], samples_per_dim, device=device_h), # pyright: ignore
torch.linspace(state_min[3], state_max[3], samples_per_dim, device=device_h), # pyright: ignore
indexing="ij",
)
all_t = torch.cat(
(
x.unsqueeze(-1),
y.unsqueeze(-1),
z.unsqueeze(-1),
k.unsqueeze(-1),
),
-1,
)
s0s = all_t.view(-1, 4)
actions = torch.linspace(-ACTION_HIGH, ACTION_HIGH, samples_per_dim, device=device_h)
s0, a0, sb, sn, ts = env.batch_integrate_system_double_time(
s0s, actions, device=device_h
) # Only support 1d actions
ts = ts.view(1).repeat(a0.shape[0]).view(-1, 1)
# pylint: disable-next=expression-not-assigned
s0_l.append(s0), a0_l.append(a0), sb_l.append(sb), sn_l.append(sn), ts_l.append(ts) # pyright: ignore
elif train_env_task == "oderl-pendulum":
state_max = torch.tensor([torch.pi, 5.0])
state_min = -state_max
device_h = "cpu"
s0_l, a0_l, sn_l, ts_l = [], [], [], []
for ti in range(samples_per_dim): # pyright: ignore
if rand:
s0s = (
(torch.rand(samples_per_dim**2, 2, dtype=torch.double, device=device_h) - 0.5) # pyright: ignore
* 2.0
* state_max
)
action_max = torch.tensor([ACTION_HIGH])
actions = (
(torch.rand(samples_per_dim, 1, dtype=torch.double, device=device_h) - 0.5) # pyright: ignore
* 2.0
* action_max
)
actions = actions.view(-1)
else:
x, y = torch.meshgrid(
torch.linspace(state_min[0], state_max[0], samples_per_dim, device=device_h), # pyright: ignore
torch.linspace(state_min[1], state_max[1], samples_per_dim, device=device_h), # pyright: ignore
indexing="ij",
)
all_t = torch.cat((x.unsqueeze(-1), y.unsqueeze(-1)), -1)
s0s = all_t.view(-1, 2)
actions = torch.linspace(-ACTION_HIGH, ACTION_HIGH, samples_per_dim, device=device_h)
s0, a0, sn, ts = env.batch_integrate_system(s0s, actions, device=device_h) # Only support 1d actions
ts = ts.view(1).repeat(a0.shape[0]).view(-1, 1)
# pylint: disable-next=expression-not-assigned
s0_l.append(s0), a0_l.append(a0), sn_l.append(sn), ts_l.append(ts) # pyright: ignore
elif train_env_task == "oderl-acrobot":
state_max = torch.tensor([torch.pi, torch.pi, 5.0, 5.0])
state_min = -state_max
device_h = "cpu"
s0_l, a0_l, sn_l, ts_l = [], [], [], []
for ti in tqdm(range(samples_per_dim)): # pyright: ignore
if rand:
s0s = (
(torch.rand(samples_per_dim**4, 4, dtype=torch.double, device=device_h) - 0.5) # pyright: ignore
* 2.0
* state_max
)
action_max = torch.tensor([ACTION_HIGH, ACTION_HIGH])
actions = (
(torch.rand(samples_per_dim**2, 2, dtype=torch.double, device=device_h) - 0.5) # pyright: ignore
* 2.0
* action_max
)
else:
x, y, z, k = torch.meshgrid(
torch.linspace(state_min[0], state_max[0], samples_per_dim, device=device_h), # pyright: ignore
torch.linspace(state_min[1], state_max[1], samples_per_dim, device=device_h), # pyright: ignore
torch.linspace(state_min[2], state_max[2], samples_per_dim, device=device_h), # pyright: ignore
torch.linspace(state_min[3], state_max[3], samples_per_dim, device=device_h), # pyright: ignore
indexing="ij",
)
all_t = torch.cat(
(
x.unsqueeze(-1),
y.unsqueeze(-1),
z.unsqueeze(-1),
k.unsqueeze(-1),
),
-1,
)
s0s = all_t.view(-1, 4)
if env.action_space.shape[0] == 2:
a1, a2 = torch.meshgrid(
torch.linspace(-ACTION_HIGH, ACTION_HIGH, samples_per_dim, device=device_h),
torch.linspace(-ACTION_HIGH, ACTION_HIGH, samples_per_dim, device=device_h),
indexing="ij",
)
a_t = torch.cat((a1.unsqueeze(-1), a2.unsqueeze(-1)), -1)
actions = a_t.view(-1, 2)
s0, a0, sn, ts = env.batch_integrate_system(
s0s, actions, device=device_h # pyright: ignore
) # Stochastic sampling of time
ts = ts.view(1).repeat(a0.shape[0]).view(-1, 1)
# pylint: disable-next=expression-not-assigned
s0_l.append(s0), a0_l.append(a0), sn_l.append(sn), ts_l.append(ts) # pyright: ignore
s0 = torch.cat(s0_l, dim=0) # pyright: ignore
a0 = torch.cat(a0_l, dim=0) # pyright: ignore
sb = torch.cat(sb_l, dim=0) # pyright: ignore
sn = torch.cat(sn_l, dim=0) # pyright: ignore
ts = torch.cat(ts_l, dim=0) # pyright: ignore
if delay > 0:
a = (
(torch.rand(a0.shape[0], delay, nu, dtype=torch.double, device=device_h) - 0.5) # pyright: ignore
* 2.0
* ACTION_HIGH
)
a0 = torch.cat((a0.view(a0.shape[0], -1, nu), a), dim=1)
if latent:
from oracle import cartpole_dynamics_dt_latent
sn = cartpole_dynamics_dt_latent(sb, s0, a0, ts)
s0 = s0[:, [0, 2, 3]] # Remove x_dot, theta_dot
sb = sb[:, [0, 2, 3]] # Remove x_dot, theta_dot
sn = sn[:, [0, 2, 3]] # Remove x_dot, theta_dot
s0 = s0.double()
sb = sb.double()
a0 = a0.double()
sn = sn.double()
ts = ts.double()
return s0.detach(), a0.detach(), sb.detach(), sn.detach(), ts.detach()
def generate_irregular_data_delay(train_env_task, env, delay, samples_per_dim=None, mode="grid", rand=False):
if samples_per_dim is None:
if train_env_task == "oderl-pendulum":
samples_per_dim = 33
elif train_env_task == "oderl-cartpole":
samples_per_dim = 20
elif train_env_task == "oderl-acrobot":
samples_per_dim = 15
time_multiplier = 10
ACTION_HIGH = env.action_space.high[0]
# ACTION_LOW = env.action_space.low[0]
nu = env.action_space.shape[0]
if train_env_task == "oderl-cartpole":
state_max = torch.tensor([5.0, 20, torch.pi, 30])
state_min = -state_max
device_h = "cpu"
s0_l, a0_l, sn_l, ts_l = [], [], [], []
# pylint: disable-next=unused-variable
for ti in range(int(samples_per_dim * time_multiplier)): # pyright: ignore
if rand:
s0s = (
(torch.rand(samples_per_dim**4, 4, dtype=torch.double, device=device_h) - 0.5) # pyright: ignore
* 2.0
* state_max
)
action_max = torch.tensor([ACTION_HIGH])
actions = (
(torch.rand(samples_per_dim, 1, dtype=torch.double, device=device_h) - 0.5) # pyright: ignore
* 2.0
* action_max
)
actions = actions.view(-1)
else:
x, y, z, k = torch.meshgrid(
torch.linspace(state_min[0], state_max[0], samples_per_dim, device=device_h), # pyright: ignore
torch.linspace(state_min[1], state_max[1], samples_per_dim, device=device_h), # pyright: ignore
torch.linspace(state_min[2], state_max[2], samples_per_dim, device=device_h), # pyright: ignore
torch.linspace(state_min[3], state_max[3], samples_per_dim, device=device_h), # pyright: ignore
indexing="ij",
)
all_t = torch.cat(
(
x.unsqueeze(-1),
y.unsqueeze(-1),
z.unsqueeze(-1),
k.unsqueeze(-1),
),
-1,
)
s0s = all_t.view(-1, 4)
actions = torch.linspace(-ACTION_HIGH, ACTION_HIGH, samples_per_dim, device=device_h)
s0, a0, sn, ts = env.batch_integrate_system(s0s, actions, device=device_h) # Only support 1d actions
ts = ts.view(1).repeat(a0.shape[0]).view(-1, 1)
# pylint: disable-next=expression-not-assigned
s0_l.append(s0), a0_l.append(a0), sn_l.append(sn), ts_l.append(ts) # pyright: ignore
elif train_env_task == "oderl-pendulum":
state_max = torch.tensor([torch.pi, 5.0])
state_min = -state_max
device_h = "cpu"
s0_l, a0_l, sn_l, ts_l = [], [], [], []
for ti in range(samples_per_dim): # pyright: ignore
if rand:
s0s = (
(torch.rand(samples_per_dim**2, 2, dtype=torch.double, device=device_h) - 0.5) # pyright: ignore
* 2.0
* state_max
)
action_max = torch.tensor([ACTION_HIGH])
actions = (
(torch.rand(samples_per_dim, 1, dtype=torch.double, device=device_h) - 0.5) # pyright: ignore
* 2.0
* action_max
)
actions = actions.view(-1)
else:
x, y = torch.meshgrid(
torch.linspace(state_min[0], state_max[0], samples_per_dim, device=device_h), # pyright: ignore
torch.linspace(state_min[1], state_max[1], samples_per_dim, device=device_h), # pyright: ignore
indexing="ij",
)
all_t = torch.cat((x.unsqueeze(-1), y.unsqueeze(-1)), -1)
s0s = all_t.view(-1, 2)
actions = torch.linspace(-ACTION_HIGH, ACTION_HIGH, samples_per_dim, device=device_h)
s0, a0, sn, ts = env.batch_integrate_system(s0s, actions, device=device_h) # Only support 1d actions
ts = ts.view(1).repeat(a0.shape[0]).view(-1, 1)
# pylint: disable-next=expression-not-assigned
s0_l.append(s0), a0_l.append(a0), sn_l.append(sn), ts_l.append(ts) # pyright: ignore
elif train_env_task == "oderl-acrobot":
state_max = torch.tensor([torch.pi, torch.pi, 5.0, 5.0])
state_min = -state_max
device_h = "cpu"
s0_l, a0_l, sn_l, ts_l = [], [], [], []
for ti in tqdm(range(samples_per_dim)): # pyright: ignore
if rand:
s0s = (
(torch.rand(samples_per_dim**4, 4, dtype=torch.double, device=device_h) - 0.5) # pyright: ignore
* 2.0
* state_max
)
action_max = torch.tensor([ACTION_HIGH, ACTION_HIGH])
actions = (
(torch.rand(samples_per_dim**2, 2, dtype=torch.double, device=device_h) - 0.5) # pyright: ignore
* 2.0
* action_max
)
else:
x, y, z, k = torch.meshgrid(
torch.linspace(state_min[0], state_max[0], samples_per_dim, device=device_h), # pyright: ignore
torch.linspace(state_min[1], state_max[1], samples_per_dim, device=device_h), # pyright: ignore
torch.linspace(state_min[2], state_max[2], samples_per_dim, device=device_h), # pyright: ignore
torch.linspace(state_min[3], state_max[3], samples_per_dim, device=device_h), # pyright: ignore
indexing="ij",
)
all_t = torch.cat(
(
x.unsqueeze(-1),
y.unsqueeze(-1),
z.unsqueeze(-1),
k.unsqueeze(-1),
),
-1,
)
s0s = all_t.view(-1, 4)
if env.action_space.shape[0] == 2:
a1, a2 = torch.meshgrid(
torch.linspace(-ACTION_HIGH, ACTION_HIGH, samples_per_dim, device=device_h),
torch.linspace(-ACTION_HIGH, ACTION_HIGH, samples_per_dim, device=device_h),
indexing="ij",
)
a_t = torch.cat((a1.unsqueeze(-1), a2.unsqueeze(-1)), -1)
actions = a_t.view(-1, 2)
s0, a0, sn, ts = env.batch_integrate_system(
s0s, actions, device=device_h # pyright: ignore
) # Stochastic sampling of time
ts = ts.view(1).repeat(a0.shape[0]).view(-1, 1)
# pylint: disable-next=expression-not-assigned
s0_l.append(s0), a0_l.append(a0), sn_l.append(sn), ts_l.append(ts) # pyright: ignore
s0 = torch.cat(s0_l, dim=0) # pyright: ignore
a0 = torch.cat(a0_l, dim=0) # pyright: ignore
sn = torch.cat(sn_l, dim=0) # pyright: ignore
ts = torch.cat(ts_l, dim=0) # pyright: ignore
if delay > 0:
a = (
(torch.rand(a0.shape[0], delay, nu, dtype=torch.double, device=device_h) - 0.5) # pyright: ignore
* 2.0
* ACTION_HIGH
)
# a = torch.zeros_like(a)
a0 = torch.cat((a0.view(a0.shape[0], -1, nu), a), dim=1)
s0 = s0.double()
a0 = a0.double()
sn = sn.double()
ts = ts.double()
return s0.detach(), a0.detach(), sn.detach(), ts.detach()
def compute_state_actions_dim_2(rand, samples_per_dim, device_h, state_max, action_max):
if rand:
s0s = (
(
torch.rand(
samples_per_dim**4,
state_max.shape[0],
dtype=torch.double,
device=device_h,
)
- 0.5
)
* 2.0
* state_max
)
actions = (
(
torch.rand(
samples_per_dim,
action_max.shape[0],
dtype=torch.double,
device=device_h,
)
- 0.5
)
* 2.0
* action_max
)
actions = actions.view(-1)
else:
x, y, z, k = torch.meshgrid(
torch.linspace(-state_max[0], state_max[0], samples_per_dim, device=device_h),
torch.linspace(-state_max[1], state_max[1], samples_per_dim, device=device_h),
torch.linspace(-state_max[2], state_max[2], samples_per_dim, device=device_h),
torch.linspace(-state_max[3], state_max[3], samples_per_dim, device=device_h),
indexing="ij",
)
all_t = torch.cat((x.unsqueeze(-1), y.unsqueeze(-1), z.unsqueeze(-1), k.unsqueeze(-1)), -1)
s0s = all_t.view(-1, 4)
actions = torch.linspace(-action_max[0], action_max[0], samples_per_dim, device=device_h)
return s0s, actions
def compute_state_actions(rand, samples_per_dim, device_h, state_max, action_max):
if rand:
s0s = (
(
torch.rand(
samples_per_dim ** state_max.shape[0],
state_max.shape[0],
dtype=torch.double,
device=device_h,
)
- 0.5
)
* 2.0
* state_max
)
actions = (
(
torch.rand(
samples_per_dim,
action_max.shape[0],
dtype=torch.double,
device=device_h,
)
- 0.5
)
* 2.0
* action_max
)
actions = actions.view(-1, action_max.shape[0])
else:
if state_max.shape[0] == 4:
x, y, z, k = torch.meshgrid(
torch.linspace(-state_max[0], state_max[0], samples_per_dim, device=device_h),
torch.linspace(-state_max[1], state_max[1], samples_per_dim, device=device_h),
torch.linspace(-state_max[2], state_max[2], samples_per_dim, device=device_h),
torch.linspace(-state_max[3], state_max[3], samples_per_dim, device=device_h),
indexing="ij",
)
all_t = torch.cat((x.unsqueeze(-1), y.unsqueeze(-1), z.unsqueeze(-1), k.unsqueeze(-1)), -1)
s0s = all_t.view(-1, 4)
elif state_max.shape[0] == 2:
x, y = torch.meshgrid(
torch.linspace(state_max[0], state_max[0], samples_per_dim, device=device_h),
torch.linspace(state_max[1], state_max[1], samples_per_dim, device=device_h),
indexing="ij",
)
all_t = torch.cat((x.unsqueeze(-1), y.unsqueeze(-1)), -1)
s0s = all_t.view(-1, 2)
if action_max.shape[0] == 1:
actions = torch.linspace(-action_max[0], action_max[0], samples_per_dim, device=device_h).view(-1, 1)
elif action_max.shape[0] == 2:
a1, a2 = torch.meshgrid(
torch.linspace(-action_max[0], action_max[0], samples_per_dim, device=device_h),
torch.linspace(-action_max[1], action_max[1], samples_per_dim, device=device_h),
indexing="ij",
)
a_t = torch.cat((a1.unsqueeze(-1), a2.unsqueeze(-1)), -1)
actions = a_t.view(-1, 2)
return s0s, actions # pyright: ignore
def generate_irregular_data_delay_time_multi(
train_env_task,
env,
delay,
samples_per_dim=None,
mode="grid",
rand=False,
action_buffer_size=5,
encode_obs_time=False,
reuse_state_actions_when_sampling_times=False,
): # , time_multiplier=10): # Delay is number of timesteps dt
if samples_per_dim is None:
if train_env_task == "oderl-pendulum":
samples_per_dim = 33
elif train_env_task == "oderl-cartpole":
samples_per_dim = 20
elif train_env_task == "oderl-acrobot":
samples_per_dim = 15
time_multiplier = 10
ACTION_HIGH = env.action_space.high[0]
# ACTION_LOW = env.action_space.low[0]
nu = env.action_space.shape[0]
s0_l, a0_l, sn_l, ts_l = [], [], [], []
action_max = torch.tensor([ACTION_HIGH] * nu)
device_h = "cpu"
if train_env_task == "oderl-cartpole":
state_max = torch.tensor([5.0, 20, torch.pi, 30]) # state_max = torch.tensor([7.0, 20, torch.pi, 30])
elif train_env_task == "oderl-pendulum":
state_max = torch.tensor([torch.pi, 5.0])
elif train_env_task == "oderl-acrobot":
state_max = torch.tensor([torch.pi, torch.pi, 5.0, 5.0])
if reuse_state_actions_when_sampling_times:
s0s, actions = compute_state_actions(rand, samples_per_dim, device_h, state_max, action_max) # pyright: ignore
# pylint: disable-next=unused-variable
for ti in range(int(samples_per_dim * time_multiplier)): # pyright: ignore
s0, a0, sn, ts = env.batch_integrate_system(s0s, actions, device=device_h) # Only support 1d actions
ts = ts.view(1).repeat(a0.shape[0]).view(-1, 1)
# pylint: disable-next=expression-not-assigned
s0_l.append(s0), a0_l.append(a0), sn_l.append(sn), ts_l.append(ts) # pyright: ignore
else:
for ti in range(int(samples_per_dim * time_multiplier)): # pyright: ignore
s0s, actions = compute_state_actions(
rand, samples_per_dim, device_h, state_max, action_max # pyright: ignore
)
s0, a0, sn, ts = env.batch_integrate_system(s0s, actions, device=device_h) # Only support 1d actions
ts = ts.view(1).repeat(a0.shape[0]).view(-1, 1)
# pylint: disable-next=expression-not-assigned
s0_l.append(s0), a0_l.append(a0), sn_l.append(sn), ts_l.append(ts) # pyright: ignore
s0 = torch.cat(s0_l, dim=0)
a0 = torch.cat(a0_l, dim=0)
sn = torch.cat(sn_l, dim=0)
ts = torch.cat(ts_l, dim=0)
a = (torch.rand(a0.shape[0], action_buffer_size, nu, dtype=torch.double, device=device_h) - 0.5) * 2.0 * ACTION_HIGH
# a = torch.zeros_like(a)
a[:, -(delay + 1)] = a0
a0 = a
if encode_obs_time:
a0 = torch.cat(
(
a0,
torch.flip(torch.arange(action_buffer_size), (0,))
.view(1, action_buffer_size, 1)
.repeat(a0.shape[0], 1, 1),
),
dim=2,
)
s0 = s0.double()
a0 = a0.double()
sn = sn.double()
ts = ts.double()
return s0.detach(), a0.detach(), sn.detach(), ts.detach()
def load_expert_irregular_data_delay_time_multi(
train_env_task,
delay,
encode_obs_time=True,
config=None,
):
from mppi_dataset_collector import mppi_with_model_collect_data
if config is None:
config = dict()
final_data = mppi_with_model_collect_data(
"oracle", # 'nl', 'NN', 'oracle', 'random'
train_env_task,
action_delay=delay,
roll_outs=config.mppi_roll_outs, # pyright: ignore
time_steps=config.mppi_time_steps, # pyright: ignore
lambda_=config.mppi_lambda, # pyright: ignore
sigma=config.mppi_sigma, # pyright: ignore
dt=config.dt, # pyright: ignore
save_video=False,
state_constraint=False,
change_goal=False,
encode_obs_time=encode_obs_time,
model=None,
uniq=None,
log_debug=False,
collect_samples=config.collect_expert_samples, # pyright: ignore
action_buffer_size=config.action_buffer_size, # pyright: ignore
config_in=config,
ts_grid=config.collect_expert_ts_grid, # pyright: ignore
intermediate_run=False,
)
(s0, a0, sn, ts) = final_data
s0 = s0.double()
a0 = a0.double()
sn = sn.double()
ts = ts.double()
return s0.detach(), a0.detach(), sn.detach(), ts.detach()
def generate_irregular_data(train_env_task, env, samples_per_dim=None, mode="grid", rand=False):
if samples_per_dim is None:
if train_env_task == "oderl-pendulum":
samples_per_dim = 33
elif train_env_task == "oderl-cartpole":
samples_per_dim = 20
elif train_env_task == "oderl-acrobot":
samples_per_dim = 15
ACTION_HIGH = env.action_space.high[0]
# ACTION_LOW = env.action_space.low[0]
if train_env_task == "oderl-cartpole":
state_max = torch.tensor([5.0, 20, torch.pi, 30])
state_min = -state_max
device_h = "cpu"
s0_l, a0_l, sn_l, ts_l = [], [], [], []
# pylint: disable-next=unused-variable
for ti in range(samples_per_dim): # pyright: ignore
if rand:
s0s = (
(torch.rand(samples_per_dim**4, 4, dtype=torch.double, device=device_h) - 0.5) # pyright: ignore
* 2.0
* state_max
)
action_max = torch.tensor([-ACTION_HIGH, ACTION_HIGH])
actions = (
(torch.rand(samples_per_dim, 1, dtype=torch.double, device=device_h) - 0.5) # pyright: ignore
* 2.0
* action_max
)
actions = actions.view(-1)
else:
x, y, z, k = torch.meshgrid(
torch.linspace(state_min[0], state_max[0], samples_per_dim, device=device_h), # pyright: ignore
torch.linspace(state_min[1], state_max[1], samples_per_dim, device=device_h), # pyright: ignore
torch.linspace(state_min[2], state_max[2], samples_per_dim, device=device_h), # pyright: ignore
torch.linspace(state_min[3], state_max[3], samples_per_dim, device=device_h), # pyright: ignore
indexing="ij",
)
all_t = torch.cat(
(
x.unsqueeze(-1),
y.unsqueeze(-1),
z.unsqueeze(-1),
k.unsqueeze(-1),
),
-1,
)
s0s = all_t.view(-1, 4)
actions = torch.linspace(-ACTION_HIGH, ACTION_HIGH, samples_per_dim, device=device_h)
s0, a0, sn, ts = env.batch_integrate_system(s0s, actions, device=device_h) # Only support 1d actions
ts = ts.view(1).repeat(a0.shape[0]).view(-1, 1)
# pylint: disable-next=expression-not-assigned
s0_l.append(s0), a0_l.append(a0), sn_l.append(sn), ts_l.append(ts) # pyright: ignore
elif train_env_task == "oderl-pendulum":
state_max = torch.tensor([torch.pi, 5.0])
state_min = -state_max
device_h = "cpu"
s0_l, a0_l, sn_l, ts_l = [], [], [], []
for ti in range(samples_per_dim): # pyright: ignore
if rand:
s0s = (
(torch.rand(samples_per_dim**2, 2, dtype=torch.double, device=device_h) - 0.5) # pyright: ignore
* 2.0
* state_max
)
action_max = torch.tensor([-ACTION_HIGH, ACTION_HIGH])
actions = (
(torch.rand(samples_per_dim, 1, dtype=torch.double, device=device_h) - 0.5) # pyright: ignore
* 2.0
* action_max
)
actions = actions.view(-1)
else:
x, y = torch.meshgrid(
torch.linspace(state_min[0], state_max[0], samples_per_dim, device=device_h), # pyright: ignore
torch.linspace(state_min[1], state_max[1], samples_per_dim, device=device_h), # pyright: ignore
indexing="ij",
)
all_t = torch.cat((x.unsqueeze(-1), y.unsqueeze(-1)), -1)
s0s = all_t.view(-1, 2)
actions = torch.linspace(-ACTION_HIGH, ACTION_HIGH, samples_per_dim, device=device_h)
s0, a0, sn, ts = env.batch_integrate_system(s0s, actions, device=device_h) # Only support 1d actions
ts = ts.view(1).repeat(a0.shape[0]).view(-1, 1)
# pylint: disable-next=expression-not-assigned
s0_l.append(s0), a0_l.append(a0), sn_l.append(sn), ts_l.append(ts) # pyright: ignore
elif train_env_task == "oderl-acrobot":
state_max = torch.tensor([torch.pi, torch.pi, 5.0, 5.0])
state_min = -state_max
device_h = "cpu"
s0_l, a0_l, sn_l, ts_l = [], [], [], []
for ti in tqdm(range(samples_per_dim)): # pyright: ignore
if rand:
s0s = (
(torch.rand(samples_per_dim**4, 4, dtype=torch.double, device=device_h) - 0.5) # pyright: ignore
* 2.0
* state_max
)
action_max = torch.tensor([-ACTION_HIGH, ACTION_HIGH])
actions = (
(torch.rand(samples_per_dim**2, 2, dtype=torch.double, device=device_h) - 0.5) # pyright: ignore
* 2.0
* action_max
)
else:
x, y, z, k = torch.meshgrid(
torch.linspace(state_min[0], state_max[0], samples_per_dim, device=device_h), # pyright: ignore
torch.linspace(state_min[1], state_max[1], samples_per_dim, device=device_h), # pyright: ignore
torch.linspace(state_min[2], state_max[2], samples_per_dim, device=device_h), # pyright: ignore
torch.linspace(state_min[3], state_max[3], samples_per_dim, device=device_h), # pyright: ignore
indexing="ij",
)
all_t = torch.cat(
(
x.unsqueeze(-1),
y.unsqueeze(-1),
z.unsqueeze(-1),
k.unsqueeze(-1),
),
-1,
)
s0s = all_t.view(-1, 4)
if env.action_space.shape[0] == 2:
a1, a2 = torch.meshgrid(
torch.linspace(-ACTION_HIGH, ACTION_HIGH, samples_per_dim, device=device_h),
torch.linspace(-ACTION_HIGH, ACTION_HIGH, samples_per_dim, device=device_h),
indexing="ij",
)
a_t = torch.cat((a1.unsqueeze(-1), a2.unsqueeze(-1)), -1)
actions = a_t.view(-1, 2)
s0, a0, sn, ts = env.batch_integrate_system(
s0s, actions, device=device_h # pyright: ignore
) # Stochastic sampling of time
ts = ts.view(1).repeat(a0.shape[0]).view(-1, 1)
# pylint: disable-next=expression-not-assigned
s0_l.append(s0), a0_l.append(a0), sn_l.append(sn), ts_l.append(ts) # pyright: ignore
s0 = torch.cat(s0_l, dim=0) # pyright: ignore
a0 = torch.cat(a0_l, dim=0) # pyright: ignore
sn = torch.cat(sn_l, dim=0) # pyright: ignore
ts = torch.cat(ts_l, dim=0) # pyright: ignore
s0 = s0.double()
a0 = a0.double()
sn = sn.double()
ts = ts.double()
return s0.detach(), a0.detach(), sn.detach(), ts.detach()