-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataio.py
1237 lines (958 loc) · 52.1 KB
/
dataio.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
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import csv
import glob
import math
import os
import matplotlib.colors as colors
import numpy as np
import scipy.io as spio
import torch
from torch.utils.data import Dataset
import diff_operators
from torchvision.transforms import Resize, Compose, ToTensor, Normalize
import utils
import pickle
from scipy.stats import truncnorm
def get_mgrid(sidelen, dim=2):
'''Generates a flattened grid of (x,y,...) coordinates in a range of -1 to 1.'''
if isinstance(sidelen, int):
sidelen = dim * (sidelen,)
if dim == 2:
pixel_coords = np.stack(np.mgrid[:sidelen[0], :sidelen[1]], axis=-1)[None, ...].astype(np.float32)
pixel_coords[0, :, :, 0] = pixel_coords[0, :, :, 0] / (sidelen[0] - 1)
pixel_coords[0, :, :, 1] = pixel_coords[0, :, :, 1] / (sidelen[1] - 1)
elif dim == 3:
pixel_coords = np.stack(np.mgrid[:sidelen[0], :sidelen[1], :sidelen[2]], axis=-1)[None, ...].astype(np.float32)
pixel_coords[..., 0] = pixel_coords[..., 0] / max(sidelen[0] - 1, 1)
pixel_coords[..., 1] = pixel_coords[..., 1] / (sidelen[1] - 1)
pixel_coords[..., 2] = pixel_coords[..., 2] / (sidelen[2] - 1)
elif dim == 4:
pixel_coords = np.stack(np.mgrid[:sidelen[0], :sidelen[1], :sidelen[2], :sidelen[3]], axis=-1)[None, ...].astype(np.float32)
pixel_coords[..., 0] = pixel_coords[..., 0] / (sidelen[0] - 1)
pixel_coords[..., 1] = pixel_coords[..., 1] / (sidelen[1] - 1)
pixel_coords[..., 2] = pixel_coords[..., 2] / (sidelen[2] - 1)
pixel_coords[..., 3] = pixel_coords[..., 3] / (sidelen[3] - 1)
else:
raise NotImplementedError('Not implemented for dim=%d' % dim)
pixel_coords -= 0.5
pixel_coords *= 2.
pixel_coords = torch.Tensor(pixel_coords).view(-1, dim)
return pixel_coords
def to_uint8(x):
return (255. * x).astype(np.uint8)
def to_numpy(x):
return x.detach().cpu().numpy()
def gaussian(x, mu=[0, 0], sigma=1e-4, d=2):
x = x.numpy()
if isinstance(mu, torch.Tensor):
mu = mu.numpy()
q = -0.5 * ((x - mu) ** 2).sum(1)
return torch.from_numpy(1 / np.sqrt(sigma ** d * (2 * np.pi) ** d) * np.exp(q / sigma)).float()
def angle_normalize(x):
return (((x + math.pi) % (2 * math.pi)) - math.pi)
class ReachabilityMultiVehicleCollisionSourceNE(Dataset):
def __init__(self, numpoints,
collisionR=0.25, velocity=0.6, omega_max=1.1,
pretrain=False, tMin=0.0, tMax=0.5, counter_start=0, counter_end=100e3,
numEvaders=1, pretrain_iters=2000, angle_alpha=1.0, time_alpha=1.0,
num_src_samples=1000):
super().__init__()
torch.manual_seed(0)
self.pretrain = pretrain
self.numpoints = numpoints
self.velocity = velocity
self.omega_max = omega_max
self.collisionR = collisionR
self.alpha_angle = angle_alpha * math.pi
self.alpha_time = time_alpha
self.numEvaders = numEvaders
self.num_states_per_vehicle = 3
self.num_states = self.num_states_per_vehicle * (numEvaders + 1)
self.num_pos_states = 2 * (numEvaders + 1)
# The state sequence will be as follows
# [x-y position of vehicle 1, x-y position of vehicle 2, ...., x-y position of vehicle N, heading of vehicle 1, heading of vehicle 2, ...., heading of vehicle N]
self.tMin = tMin
self.tMax = tMax
self.N_src_samples = num_src_samples
self.pretrain_counter = 0
self.counter = counter_start
self.pretrain_iters = pretrain_iters
self.full_count = counter_end
def __len__(self):
return 1
def __getitem__(self, idx):
start_time = 0. # time to apply initial conditions
# uniformly sample domain and include coordinates where source is non-zero
coords = torch.zeros(self.numpoints, self.num_states).uniform_(-1, 1)
if self.pretrain:
# only sample in time around the initial condition
# time = torch.zeros(self.numpoints, 1).uniform_(start_time - 0.001, start_time + 0.001)
time = torch.ones(self.numpoints, 1) * start_time
coords = torch.cat((time, coords), dim=1)
else:
# slowly grow time values from start time
# this currently assumes start_time = tMin and max time value is tMax
time = self.tMin + torch.zeros(self.numpoints, 1).uniform_(0, (self.tMax-self.tMin) * (self.counter / self.full_count))
coords = torch.cat((time, coords), dim=1)
# make sure we always have training samples at the initial time
coords[-self.N_src_samples:, 0] = start_time
# set up the initial value function
# Collision cost between the pursuer and the evaders
boundary_values = torch.norm(coords[:, 1:3] - coords[:, 3:5], dim=1, keepdim=True) - self.collisionR
for i in range(1, self.numEvaders):
boundary_values_current = torch.norm(coords[:, 1:3] - coords[:, 2*(i+1)+1:2*(i+1)+3], dim=1, keepdim=True) - self.collisionR
boundary_values = torch.min(boundary_values, boundary_values_current)
# Collision cost between the evaders themselves
for i in range(self.numEvaders):
for j in range(i+1, self.numEvaders):
evader1_coords_index = 1 + (i+1)*2
evader2_coords_index = 1 + (j+1)*2
boundary_values_current = torch.norm(coords[:, evader1_coords_index:evader1_coords_index+2] - coords[:, evader2_coords_index:evader2_coords_index+2], dim=1, keepdim=True) - self.collisionR
boundary_values = torch.min(boundary_values, boundary_values_current)
# normalize the value function
norm_to = 0.02
mean = 0.25
var = 0.5
boundary_values = (boundary_values - mean)*norm_to/var
if self.pretrain:
dirichlet_mask = torch.ones(coords.shape[0], 1) > 0
else:
# only enforce initial conditions around start_time
dirichlet_mask = (coords[:, 0, None] == start_time)
if self.pretrain:
self.pretrain_counter += 1
elif self.counter < self.full_count:
self.counter += 1
if self.pretrain and self.pretrain_counter == self.pretrain_iters:
self.pretrain = False
return {'coords': coords}, {'source_boundary_values': boundary_values, 'dirichlet_mask': dirichlet_mask}
class ReachabilityAir3DSource(Dataset):
def __init__(self, numpoints,
collisionR=0.25, velocity=0.6, omega_max=1.1,
pretrain=False, tMin=0.0, tMax=0.5, counter_start=0, counter_end=100e3,
pretrain_iters=2000, angle_alpha=1.0, num_src_samples=1000, seed=0):
super().__init__()
torch.manual_seed(0)
self.pretrain = pretrain
self.numpoints = numpoints
self.velocity = velocity
self.omega_max = omega_max
self.collisionR = collisionR
self.alpha_angle = angle_alpha * math.pi
self.num_states = 3
self.tMax = tMax
self.tMin = tMin
self.N_src_samples = num_src_samples
self.pretrain_counter = 0
self.counter = counter_start
self.pretrain_iters = pretrain_iters
self.full_count = counter_end
# Set the seed
torch.manual_seed(seed)
def __len__(self):
return 1
def __getitem__(self, idx):
start_time = 0. # time to apply initial conditions
# uniformly sample domain and include coordinates where source is non-zero
coords = torch.zeros(self.numpoints, self.num_states).uniform_(-1, 1)
if self.pretrain:
# only sample in time around the initial condition
time = torch.ones(self.numpoints, 1) * start_time
coords = torch.cat((time, coords), dim=1)
else:
# slowly grow time values from start time
# this currently assumes start_time = 0 and max time value is tMax
time = self.tMin + torch.zeros(self.numpoints, 1).uniform_(0, (self.tMax-self.tMin) * (self.counter / self.full_count))
coords = torch.cat((time, coords), dim=1)
# make sure we always have training samples at the initial time
coords[-self.N_src_samples:, 0] = start_time
# set up the initial value function
boundary_values = torch.norm(coords[:, 1:3], dim=1, keepdim=True) - self.collisionR
# normalize the value function
norm_to = 0.02
mean = 0.25
var = 0.5
boundary_values = (boundary_values - mean)*norm_to/var
if self.pretrain:
dirichlet_mask = torch.ones(coords.shape[0], 1) > 0
else:
# only enforce initial conditions around start_time
dirichlet_mask = (coords[:, 0, None] == start_time)
if self.pretrain:
self.pretrain_counter += 1
elif self.counter < self.full_count:
self.counter += 1
if self.pretrain and self.pretrain_counter == self.pretrain_iters:
self.pretrain = False
return {'coords': coords}, {'source_boundary_values': boundary_values, 'dirichlet_mask': dirichlet_mask}
class ReachabilityHumanForward(Dataset):
def __init__(self, numpoints,
collisionR=0.1, velocity=1.0, omega_max=1.1,
pretrain=False, tMin=0.0, tMax=0.5, counter_start=0, counter_end=100e3,
pretrain_iters=2000, angle_alpha=1.0, num_src_samples=1000, beta1 = 0.1, beta2 = 10, seed=0):
super().__init__()
torch.manual_seed(0)
self.pretrain = pretrain
self.numpoints = numpoints
self.velocity = velocity
self.collisionR = collisionR
self.alpha_angle = angle_alpha * math.pi
self.num_states = 5 #states are x,y, startx, starty, p
self.tMax = tMax
self.tMin = tMin
self.N_src_samples = num_src_samples
self.pretrain_counter = 0
self.counter = counter_start
self.pretrain_iters = pretrain_iters
self.full_count = counter_end
self.goal = torch.tensor([1.0, -1.0])
self.beta1 = beta1
self.beta2 = beta2
# Set the seed
torch.manual_seed(seed)
def __len__(self):
return 1
def __getitem__(self, idx):
start_time = 0. # time to apply initial conditions
# uniformly sample domain and include coordinates where source is non-zero
coords = torch.zeros(self.numpoints, self.num_states).uniform_(-1, 1)
if self.pretrain:
# only sample in time around the initial condition
time = torch.ones(self.numpoints, 1) * start_time
coords = torch.cat((time, coords), dim=1)
else:
# slowly grow time values from start time
# this currently assumes start_time = 0 and max time value is tMax
time = self.tMin + torch.zeros(self.numpoints, 1).uniform_(0, (self.tMax-self.tMin) * (self.counter / self.full_count))
coords = torch.cat((time, coords), dim=1)
# make sure we always have training samples at the initial time
coords[-self.N_src_samples:, 0] = start_time
# set up the initial value function
boundary_values = torch.norm(coords[:, 1:3]-coords[:,3:5], dim=1, keepdim=True) - self.collisionR
# normalize the value function
norm_to = 0.02
mean = 0.25
var = 0.5
boundary_values = (boundary_values - mean)*norm_to/var
if self.pretrain:
dirichlet_mask = torch.ones(coords.shape[0], 1) > 0
else:
# only enforce initial conditions around start_time
dirichlet_mask = (coords[:, 0, None] == start_time)
if self.pretrain:
self.pretrain_counter += 1
elif self.counter < self.full_count:
self.counter += 1
if self.pretrain and self.pretrain_counter == self.pretrain_iters:
self.pretrain = False
return {'coords': coords}, {'source_boundary_values': boundary_values, 'dirichlet_mask': dirichlet_mask}
class HumanRobotPE(Dataset):
def __init__(self, numpoints,
collisionR=0.1, velocity=1.0, omega_max=1.1,
pretrain=False, tMin=0.0, tMax=0.5, counter_start=0, counter_end=100e3,
pretrain_iters=2000, angle_alpha=1.0, num_src_samples=1000, beta1 = 0.1, beta2 = 10, periodic_boundary = False, seed=0):
super().__init__()
torch.manual_seed(0)
self.pretrain = pretrain
self.periodic_boundary = periodic_boundary
self.numpoints = numpoints
self.velocity = velocity
self.collisionR = collisionR
self.alpha_angle = 1.2 * math.pi
self.omega_max = omega_max
self.num_states = 6 #states are x_r, y_r, theta_r, x_h, y_h, p
self.tMax = tMax
self.tMin = tMin
self.N_src_samples = num_src_samples
self.N_boundary_pts = self.N_src_samples//2
self.pretrain_counter = 0
self.counter = counter_start
self.pretrain_iters = pretrain_iters
self.full_count = counter_end
self.goal = torch.tensor([0.0, 0.0])
self.beta1 = beta1
self.beta2 = beta2
# Set the seed
torch.manual_seed(seed)
def __len__(self):
return 1
def __getitem__(self, idx):
start_time = 0. # time to apply initial conditions
angle_index = 3 #index of angle state
# uniformly sample domain and include coordinates where source is non-zero
coords = torch.zeros(self.numpoints, self.num_states).uniform_(-1, 1)
if self.pretrain:
# only sample in time around the initial condition
time = torch.ones(self.numpoints, 1) * start_time
coords = torch.cat((time, coords), dim=1)
else:
# slowly grow time values from start time
# this currently assumes start_time = 0 and max time value is tMax
time = self.tMin + torch.zeros(self.numpoints, 1).uniform_(0, (self.tMax-self.tMin) * (self.counter / self.full_count))
coords = torch.cat((time, coords), dim=1)
# make sure we always have training samples at the initial time
coords[-self.N_src_samples:, 0] = start_time
# Sample some points to impose the boundary coditions
if self.periodic_boundary:
# import ipdb; ipdb.set_trace()
coords_angle = torch.zeros(self.N_boundary_pts, 1).uniform_(math.pi-0.001, self.alpha_angle) # Sample near the right boundary
coords_angle[0:self.N_boundary_pts//2] = -1.0 * coords_angle[0:self.N_boundary_pts//2] # Assign half of the points to the left boundary
coords_angle_periodic = angle_normalize(coords_angle)
coords_angle_concatenated = torch.cat((coords_angle, coords_angle_periodic), dim=0)
coords_angle_concatenated_normalized = (coords_angle_concatenated)/self.alpha_angle
coords[:self.N_boundary_pts] = coords[self.N_boundary_pts:2*self.N_boundary_pts]
coords[:2*self.N_boundary_pts, angle_index] = coords_angle_concatenated_normalized[..., 0]
# set up the initial value function
boundary_values = torch.norm(coords[:, 1:3]-coords[:,4:6], dim=1, keepdim=True) - self.collisionR
# normalize the value function
norm_to = 0.02
mean = 0.25
var = 0.5
boundary_values = (boundary_values - mean)*norm_to/var
if self.pretrain:
dirichlet_mask = torch.ones(coords.shape[0], 1) > 0
else:
# only enforce initial conditions around start_time
dirichlet_mask = (coords[:, 0, None] == start_time)
if self.pretrain:
self.pretrain_counter += 1
elif self.counter < self.full_count:
self.counter += 1
if self.pretrain and self.pretrain_counter == self.pretrain_iters:
self.pretrain = False
return {'coords': coords}, {'source_boundary_values': boundary_values, 'dirichlet_mask': dirichlet_mask}
class ReachabilityHumanForwardParam(Dataset):
def __init__(self, numpoints,
collisionR=0.1, velocity=1.0, omega_max=1.1,
pretrain=False, tMin=0.0, tMax=0.5, counter_start=0, counter_end=100e3,
pretrain_iters=2000, angle_alpha=1.0, num_src_samples=1000, periodic_boundary=False, diffModel=False, seed=0):
super().__init__()
torch.manual_seed(0)
self.pretrain = pretrain
self.periodic_boundary = periodic_boundary
self.diffModel = diffModel
self.numpoints = numpoints
self.velocity = velocity
self.collisionR = collisionR
self.alpha_angle = angle_alpha * math.pi
self.num_states = 14 #states are x,y, startx, starty, umin1, umax1, umin2, umax2, umin3, umax3, umin4, umax4, umin5, umax5
self.tMax = tMax
self.tMin = tMin
self.N_src_samples = num_src_samples
self.N_boundary_pts = self.N_src_samples//2
self.pretrain_counter = 0
self.counter = counter_start
self.pretrain_iters = pretrain_iters
self.full_count = counter_end
self.norm_to = 0.02
self.mean = 0.25
self.var = 0.5
# Set the seed
torch.manual_seed(seed)
def __len__(self):
return 1
def compute_IC(self, state_coords):
state_coords_unnormalized = state_coords * 1.0
state_coords_unnormalized[..., 0:2] = state_coords_unnormalized[..., 0:2] - state_coords_unnormalized[..., 2:4]
boundary_values = torch.norm(state_coords_unnormalized[..., 0:2], dim=-1, keepdim=True) - self.collisionR
return boundary_values
def __getitem__(self, idx):
start_time = 0. # time to apply initial conditions
# uniformly sample domain and include coordinates where source is non-zero
coords = torch.zeros(self.numpoints, self.num_states).uniform_(-1, 1)
if self.pretrain:
# only sample in time around the initial condition
time = torch.ones(self.numpoints, 1) * start_time
coords = torch.cat((time, coords), dim=1)
else:
# slowly grow time values from start time
# this currently assumes start_time = 0 and max time value is tMax
time = self.tMin + torch.zeros(self.numpoints, 1).uniform_(0, (self.tMax-self.tMin) * (self.counter / self.full_count))
coords = torch.cat((time, coords), dim=1)
# make sure we always have training samples at the initial time
coords[-self.N_src_samples:, 0] = start_time
# set up the initial value function
# Compute the initial value function
if self.diffModel:
coords_var = torch.tensor(coords.clone(), requires_grad=True)
boundary_values = self.compute_IC(coords_var[..., 1:])
boundary_values = (boundary_values - self.mean)*self.norm_to/self.var
# Compute the gradients of the value function
lx_grads = diff_operators.gradient(boundary_values, coords_var)[..., 1:]
else:
boundary_values = self.compute_IC(coords[..., 1:])
boundary_values = (boundary_values - self.mean)*self.norm_to/self.var
if self.pretrain:
dirichlet_mask = torch.ones(coords.shape[0], 1) > 0
else:
# only enforce initial conditions around start_time
dirichlet_mask = (coords[:, 0, None] == start_time)
if self.pretrain:
self.pretrain_counter += 1
elif self.counter < self.full_count:
self.counter += 1
if self.pretrain and self.pretrain_counter == self.pretrain_iters:
self.pretrain = False
if self.diffModel:
return {'coords': coords}, {'source_boundary_values': boundary_values, 'dirichlet_mask': dirichlet_mask, 'lx_grads': lx_grads}
else:
return {'coords': coords}, {'source_boundary_values': boundary_values, 'dirichlet_mask': dirichlet_mask}
class ReachabilityDubins4DForwardParam6set(Dataset):
def __init__(self, numpoints,
collisionR=0.1, velocity=1.0, omega_max=1.1,
pretrain=False, tMin=0.0, tMax=0.5, counter_start=0, counter_end=100e3,
pretrain_iters=2000, angle_alpha=1.0, num_src_samples=1000, periodic_boundary=False, diffModel=False, seed=0):
super().__init__()
torch.manual_seed(0)
self.pretrain = pretrain
self.periodic_boundary = periodic_boundary
self.diffModel = diffModel
self.numpoints = numpoints
self.num_states = 30 #states are x,y, theta, v, startx, starty, amin1, amax1, omegamin1, omegamax1, amin2, amax2, omegamin2, omegamax2
self.angle_index = 3
self.tMax = tMax
self.tMin = tMin
self.collisionR = collisionR
# Define state alphas and betas so that all coordinates are from [-1, 1].
# The conversion rule is state' = (state - beta)/alpha. state' is in [-1, 1].
self.alpha = {}
self.beta = {}
self.alpha['x'] = 10.0
self.alpha['y'] = 10.0
self.alpha['th'] = 1.2*math.pi
self.alpha['v'] = 30.0
self.alpha['a'] = 20.0
self.alpha['o'] = 3.*math.pi
# self.alpha['time'] = 2.0/self.tMax
#self.alpha['time'] = 3.0/self.tMax
self.alpha['time'] = 1.0
self.beta['x'] = 0.0
self.beta['y'] = 0.0
self.beta['th'] = 0.0
self.beta['v'] = 25.0
self.beta['a'] = 0.0
self.beta['o'] = 0.0
# Scale for output normalization
self.norm_to = 0.02
self.mean = 0.25
self.var = 0.5
# State bounds
self.vMin = 0.001
self.vMax = 6.50
#self.thMin = -0.3*math.pi + 0.001
#self.thMax = 0.3*math.pi - 0.001
self.N_src_samples = num_src_samples
self.N_boundary_pts = self.N_src_samples//2
self.pretrain_counter = 0
self.counter = counter_start
self.pretrain_iters = pretrain_iters
self.full_count = counter_end
# Set the seed
torch.manual_seed(seed)
def time_control(self, time, set1, set2, set3, set4, set5, set6):
time_control = (1 - 5*time)*set1 + (5*time)*set2
time_control = torch.where(time > 0.2, (1 - 5*(time- 0.2))*set2 + (5*(time-0.2))*set3, time_control)
time_control = torch.where(time > 0.4, (1 - 5*(time- 0.4))*set3 + (5*(time-0.4))*set4, time_control)
time_control = torch.where(time > 0.6, (1 - 5*(time- 0.6))*set4 + (5*(time-0.6))*set5, time_control)
time_control = torch.where(time > 0.8, (1 - 5*(time- 0.8))*set5 + (5*(time-0.8))*set6, time_control)
time_control = torch.where(time > 1.0, set6, time_control)
return time_control
def compute_overall_ham(self, x, dudx, return_components=False):
alpha = self.alpha
beta = self.beta
# Scale the costates appropriately.
dudx[..., 0] = dudx[..., 0] / alpha['x']
dudx[..., 1] = dudx[..., 1] / alpha['y']
dudx[..., 2] = dudx[..., 2] / alpha['th']
dudx[..., 3] = dudx[..., 3] / alpha['v']
# Scale the states appropriately.
x_u = x * 1.0
#x_u[..., 0] = x_u[..., 0] * alpha['time']
x_u[..., 1] = x_u[..., 1] * alpha['x'] + beta['x']
x_u[..., 2] = x_u[..., 2] * alpha['y'] + beta['y']
x_u[..., 3] = x_u[..., 3] * alpha['th'] + beta['th']
x_u[..., 4] = x_u[..., 4] * alpha['v'] + beta['v']
x_u[..., 5] = x_u[..., 5] * alpha['x'] + beta['x']
x_u[..., 6] = x_u[..., 6] * alpha['y'] + beta['y']
x_u[..., 7] = x_u[..., 7] * alpha['a'] + beta['a'] #amin
x_u[..., 8] = x_u[..., 8] * alpha['a'] + beta['a'] #amax
x_u[..., 9] = x_u[..., 9] * alpha['o'] + beta['o'] # omin
x_u[..., 10] = x_u[..., 10] * alpha['o'] + beta['o'] # omax
x_u[..., 11] = x_u[..., 11] * alpha['a'] + beta['a'] #set 2
x_u[..., 12] = x_u[..., 12] * alpha['a'] + beta['a']
x_u[..., 13] = x_u[..., 13] * alpha['o'] + beta['o']
x_u[..., 14] = x_u[..., 14] * alpha['o'] + beta['o']
x_u[..., 15] = x_u[..., 15] * alpha['a'] + beta['a'] #set3
x_u[..., 16] = x_u[..., 16] * alpha['a'] + beta['a']
x_u[..., 17] = x_u[..., 17] * alpha['o'] + beta['o']
x_u[..., 18] = x_u[..., 18] * alpha['o'] + beta['o']
x_u[..., 19] = x_u[..., 19] * alpha['a'] + beta['a'] # set 4
x_u[..., 20] = x_u[..., 20] * alpha['a'] + beta['a']
x_u[..., 21] = x_u[..., 21] * alpha['o'] + beta['o']
x_u[..., 22] = x_u[..., 23] * alpha['o'] + beta['o']
x_u[..., 23] = x_u[..., 23] * alpha['a'] + beta['a'] #set5
x_u[..., 24] = x_u[..., 24] * alpha['a'] + beta['a']
x_u[..., 25] = x_u[..., 25] * alpha['o'] + beta['o']
x_u[..., 26] = x_u[..., 26] * alpha['o'] + beta['o']
x_u[..., 27] = x_u[..., 27] * alpha['a'] + beta['a'] #set 6
x_u[..., 28] = x_u[..., 28] * alpha['a'] + beta['a']
x_u[..., 29] = x_u[..., 29] * alpha['o'] + beta['o']
x_u[..., 30] = x_u[..., 30] * alpha['o'] + beta['o']
#amin = self.time_control(x_u[...,0], x_u[...,7],x_u[...,11],x_u[...,15],x_u[...,19],x_u[...,23],x_u[...,27])
#amax = self.time_control(x_u[...,0], x_u[...,8],x_u[...,12],x_u[...,16],x_u[...,20],x_u[...,24],x_u[...,28])
#omin = -self.time_control(x_u[...,0], x_u[...,9],x_u[...,13],x_u[...,17],x_u[...,21],x_u[...,25],x_u[...,29])
#omax = -self.time_control(x_u[...,0], x_u[...,10],x_u[...,14],x_u[...,18],x_u[...,22],x_u[...,26],x_u[...,30])
amin = (x_u[..., 7] + x_u[...,11] + x_u[...,15] + x_u[...,19] + x_u[...,23] + x_u[...,27]) / 6
amax = (x_u[..., 8] + x_u[...,12] + x_u[...,16] + x_u[...,20] + x_u[...,24] + x_u[...,28]) / 6
omin = -(x_u[..., 9] + x_u[...,13] + x_u[...,17] + x_u[...,21] + x_u[...,25] + x_u[...,29]) / 6
omax = -(x_u[...,10] + x_u[...,14] + x_u[...,18] + x_u[...,22] + x_u[...,26] + x_u[...,30]) / 6
zero_tensor = torch.Tensor([0]).cuda()
#amin = torch.where((x_u[..., 4] <= self.vMin), zero_tensor, amin)
#amax = torch.where((x_u[..., 4] >= self.vMax), zero_tensor, amax)
o_opt = torch.where(dudx[...,2]>0, omin, omax)
a_opt = torch.where(dudx[...,3]>0, amax, amin)
# xdot = v cos theta
# ydot = v sin theta
# thetadot = o_opt
# vdot = a_opt
# negative dynamics since it is a FRS, want to minimize
ham = -dudx[...,0]*x_u[...,4]*(torch.cos(x_u[..., 3])) - dudx[...,1]*x_u[...,4]*(torch.sin(x_u[...,3]))
ham_o = -dudx[...,2]*o_opt
ham_a = -dudx[...,3]*a_opt
ham = ham + ham_a + ham_o
return ham
def __len__(self):
return 1
def compute_lx(self, state_coords_unnormalized):
# Compute the target boundary condition given the unnormalized state coordinates.
# Vehicle 1
goal_tensor_R1 = state_coords_unnormalized[:, 4:6]
dist_R1 = torch.norm(state_coords_unnormalized[:, 0:2] - goal_tensor_R1, dim=1, keepdim=True) - self.collisionR
return dist_R1
def compute_IC(self, state_coords):
state_coords_unnormalized = state_coords * 1.0
state_coords_unnormalized[:, 0] = state_coords_unnormalized[:, 0] * self.alpha['x'] + self.beta['x']
state_coords_unnormalized[:, 1] = state_coords_unnormalized[:, 1] * self.alpha['y'] + self.beta['y']
state_coords_unnormalized[:, 4] = state_coords_unnormalized[:, 4] * self.alpha['x'] + self.beta['x'] # start x
state_coords_unnormalized[:, 5] = state_coords_unnormalized[:, 5] * self.alpha['y'] + self.beta['y'] # start y
lx = self.compute_lx(state_coords_unnormalized)
return lx
def __getitem__(self, idx):
start_time = 0. # time to apply initial conditions
# uniformly sample domain and include coordinates where source is non-zero
coords = torch.zeros(self.numpoints, self.num_states).uniform_(-1, 1)
if self.pretrain:
# only sample in time around the initial condition
time = torch.ones(self.numpoints, 1) * start_time
coords = torch.cat((time, coords), dim=1)
else:
# slowly grow time values from start time
time = self.tMin + torch.zeros(self.numpoints, 1).uniform_(0, (self.tMax-self.tMin) * (self.counter / self.full_count))
coords = torch.cat((time, coords), dim=1)
# make sure we always have training samples at the initial time
coords[-self.N_src_samples:, 0] = start_time
# Sample some points to impose the boundary coditions
if self.periodic_boundary:
# import ipdb; ipdb.set_trace()
coords_angle = torch.zeros(self.N_boundary_pts, 1).uniform_(math.pi-0.001, self.alpha['th'] + self.beta['th']) # Sample near the right boundary
coords_angle[0:self.N_boundary_pts//2] = -1.0 * coords_angle[0:self.N_boundary_pts//2] # Assign half of the points to the left boundary
coords_angle_periodic = angle_normalize(coords_angle)
coords_angle_concatenated = torch.cat((coords_angle, coords_angle_periodic), dim=0)
coords_angle_concatenated_normalized = (coords_angle_concatenated - self.beta['th'])/self.alpha['th']
coords[:self.N_boundary_pts] = coords[self.N_boundary_pts:2*self.N_boundary_pts]
coords[:2*self.N_boundary_pts, self.angle_index] = coords_angle_concatenated_normalized[..., 0]
coords[..., 6:] = coords[..., 6:] * 0.0 # set controls to be constant
# Compute the initial value function
if self.diffModel:
coords_var = torch.tensor(coords.clone(), requires_grad=True)
boundary_values = self.compute_IC(coords_var[:, 1:])
boundary_values = (boundary_values - self.mean)*self.norm_to/self.var
lx_grads = diff_operators.gradient(boundary_values, coords_var)[..., 1:5] # only care about gradient wrt state inputs
else:
# lx, gx, boundary_values = self.compute_IC(coords[:, 1:])
boundary_values = self.compute_IC(coords[:, 1:])
boundary_values = (boundary_values - self.mean)*self.norm_to/self.var
if self.pretrain:
dirichlet_mask = torch.ones(coords.shape[0], 1) > 0
else:
# only enforce initial conditions around start_time
dirichlet_mask = (coords[:, 0, None] == start_time)
if self.pretrain:
self.pretrain_counter += 1
elif self.counter < self.full_count:
self.counter += 1
if self.pretrain and self.pretrain_counter == self.pretrain_iters:
self.pretrain = False
if self.diffModel:
return {'coords': coords}, {'source_boundary_values': boundary_values, 'dirichlet_mask': dirichlet_mask, 'lx_grads': lx_grads}
else:
return {'coords': coords}, {'source_boundary_values': boundary_values, 'dirichlet_mask': dirichlet_mask}
class ReachabilityDubins4DForwardParam2SetScaled(Dataset):
def __init__(self, numpoints, collisionR, tMin=0.0, tMax=1.0, alphaT=1.0,
pretrain=False, counter_start=0, counter_end=100e3, pretrain_iters=2000,
num_src_samples=1000, periodic_boundary=False, diffModel=False):
super().__init__()
torch.manual_seed(0)
self.pretrain = pretrain
self.periodic_boundary = periodic_boundary
self.diffModel = diffModel
self.sample_inside_target = True
self.numpoints = numpoints
# Dynamics parameters
self.num_states = 13
# TIme parameters
self.tMax = tMax
self.tMin = tMin
# Normalization for states and time
self.alpha = {}
self.beta = {}
self.alpha['x'] = 3.0
self.alpha['y'] = 3.0
self.alpha['th'] = 1.1*math.pi
self.alpha['v'] = 5.5
self.alpha['a'] = 10.0
self.alpha['o'] = 3.0
self.alpha['time'] = 3.0
#self.beta['x'] = 0.5
self.beta['x'] = 1.0
self.beta['y'] = 0.0
self.beta['th'] = 0.0
self.beta['v'] = 5.5
self.beta['a'] = 0.0
self.beta['o'] = 0.0
# Normalization for the value function
self.norm_to = 0.02
self.mean = 5.4
self.var = 5.55
# Collision radius
self.collisionR = collisionR
self.vhR = 0.5
self.N_src_samples = num_src_samples
self.N_boundary_pts = self.N_src_samples//2
self.N_inside_target_samples = num_src_samples*5
self.pretrain_counter = 0
self.counter = counter_start
self.pretrain_iters = pretrain_iters
self.full_count = counter_end
self.zeroTensor = torch.tensor(0.).cuda()
def __len__(self):
return 1
def time_control(self, time, set1, set2):
time_control = (1.0 - time)*set1 + (time)*set2
#time_control = torch.where(time > 1.0, set2, time_control)
return time_control
def sample_inside_target_set(self):
# Sample coordinates that are inside the target set.
target_coords = torch.zeros(self.N_inside_target_samples, 5).uniform_(-1, 1)
# XY position
normalized_x_extent = (1.5*2*self.collisionR)/ self.alpha['x']
normalized_y_extent = (1.5*2*self.collisionR)/ self.alpha['y']
normalized_x_shift = -self.beta['x']/ self.alpha['x']
normalized_y_shift = -self.beta['y']/ self.alpha['y']
# sets the range from [-1, 1] to [-extent, +extent], then centers around origin
target_coords[..., 0] = normalized_x_extent * target_coords[:, 0] + normalized_x_shift
target_coords[..., 1] = normalized_y_extent * target_coords[:, 1] + normalized_y_shift
# Theta position
normalized_th_extent = (1.5* 2.0 *self.vhR - self.beta['th'])/ self.alpha['th']
target_coords[..., 2] = normalized_th_extent * target_coords[:, 2]
# V position
unnormalizedV = target_coords[..., 4] * self.alpha['v'] + self.beta['v'] # Unnormalized starting speed
speed_deviation = target_coords[..., 3] * 1.5 * 2.0 * self.vhR # Speed deviation around the starting speed
target_coords[..., 3] = unnormalizedV + speed_deviation # Starting V around the target set
target_coords[..., 3] = (target_coords[..., 3] - self.beta['v'])/ self.alpha['v'] # Normalize coordinates
target_coords[..., 3] = torch.clip(target_coords[..., 3], -1.0, 1.0) # Clip to the valid grid range
return target_coords
def compute_IC(self, state_coords):
state_coords_unnormalized = state_coords * 1.0
state_coords_unnormalized[..., 0] = state_coords_unnormalized[..., 0] * self.alpha['x'] + self.beta['x']
state_coords_unnormalized[..., 1] = state_coords_unnormalized[..., 1] * self.alpha['y'] + self.beta['y']
state_coords_unnormalized[..., 2] = state_coords_unnormalized[..., 2] * self.alpha['th'] + self.beta['th']
state_coords_unnormalized[..., 3] = state_coords_unnormalized[..., 3] * self.alpha['v'] + self.beta['v']
state_coords_unnormalized[..., 4] = state_coords_unnormalized[..., 4] * self.alpha['v'] + self.beta['v']
# positional states from start
boundary_values1 = torch.norm(state_coords_unnormalized[..., 0:2], dim=-1, keepdim=True) - self.collisionR
# velocity from start
state_coords_unnormalized_thv = state_coords_unnormalized[..., 2:4] * 1.0
state_coords_unnormalized_thv[..., 1] = state_coords_unnormalized_thv[..., 1] - state_coords_unnormalized[..., 4]
boundary_values2 = torch.norm(state_coords_unnormalized_thv, dim=-1, keepdim=True) - self.vhR
#boundary_values1 = torch.norm(state_coords_unnormalized[..., 0:2] - state_coords_unnormalized[..., 4:6], dim=-1, keepdim=True) - self.collisionR
# theta and vel from start
#boundary_values2 = torch.norm(state_coords_unnormalized[..., 2:4] - state_coords_unnormalized[..., 6:8], dim=-1, keepdim=True) - 0.2*self.collisionR
boundary_values = torch.max(boundary_values1, boundary_values2)
return boundary_values
def compute_overall_ham(self, x, dudx):
alpha = self.alpha
beta = self.beta
# Scale the costates appropriately.
dudx[..., 0] = dudx[..., 0] / alpha['x']
dudx[..., 1] = dudx[..., 1] / alpha['y']
dudx[..., 2] = dudx[..., 2] / alpha['th']
dudx[..., 3] = dudx[..., 3] / alpha['v']
# Scale the states appropriately.
x_u = x * 1.0
x_u[..., 1] = x_u[..., 1] * alpha['x'] + beta['x']
x_u[..., 2] = x_u[..., 2] * alpha['y'] + beta['y']
x_u[..., 3] = x_u[..., 3] * alpha['th'] + beta['th']
x_u[..., 4] = x_u[..., 4] * alpha['v'] + beta['v']
x_u[..., 5] = x_u[..., 4] * alpha['v'] + beta['v'] # start v
x_u[..., 6] = x_u[..., 6] * alpha['a'] + beta['a']
x_u[..., 7] = x_u[..., 7] * alpha['a'] + beta['a']
x_u[..., 8] = x_u[..., 8] * alpha['o'] + beta['o']
x_u[..., 9] = x_u[..., 9] * alpha['o'] + beta['o']
x_u[..., 10] = x_u[..., 10] * alpha['a'] + beta['a']
x_u[..., 11] = x_u[..., 11] * alpha['a'] + beta['a']
x_u[..., 12] = x_u[..., 12] * alpha['o'] + beta['o']
x_u[..., 13] = x_u[..., 13] * alpha['o'] + beta['o']
amin = self.time_control(x_u[...,0], x_u[...,6], x_u[...,10])
amax = self.time_control(x_u[...,0], x_u[...,7], x_u[...,11])
omin = self.time_control(x_u[...,0], x_u[...,8], x_u[...,12])
omax = self.time_control(x_u[...,0], x_u[...,9], x_u[...,13])
# Negative dynamics since it is a FRS; controls want to minimize
# xdot = -v cos theta
# ydot = -v sin theta
# thetadot = -o
# vdot = -a
# Optimal control
o_opt = torch.where(-dudx[...,2] > 0, omin, omax)
a_opt = torch.where(-dudx[...,3] > 0, amin, amax)
amin = torch.where(torch.logical_and(x_u[...,4] < 0, amin<0), self.zeroTensor, amin )
amax = torch.where(torch.logical_and(x_u[...,4] < 0, amax<0), self.zeroTensor, amax )
# Hamiltonian
ham = -dudx[...,0]*x_u[...,4]*(torch.cos(x_u[..., 3])) - dudx[...,1]*x_u[...,4]*(torch.sin(x_u[...,3]))
ham_o = -dudx[...,2]*o_opt
ham_a = -dudx[...,3]*a_opt
ham = ham + ham_a + ham_o
return ham
def __getitem__(self, idx):
start_time = 0. # time to apply initial conditions
angle_index = 3 # Index of the angle state
# uniformly sample domain and include coordinates where source is non-zero
coords = torch.zeros(self.numpoints, self.num_states).uniform_(-1, 1)
# Make sure that the oMax and aMax are greater than oMin and aMin respectively.
# Shifting the sampling to [aMin, 1] and [oMin, 1] for aMax and oMax respectively
coords[..., 6] = (0.5 * (1 - coords[..., 5]) * coords[..., 6]) + (0.5 * (1 + coords[..., 5]))
coords[..., 10] = (0.5 * (1 - coords[..., 9]) * coords[..., 10]) + (0.5 * (1 + coords[..., 9]))
coords[..., 8] = (0.5 * (1 - coords[..., 7]) * coords[..., 8]) + (0.5 * (1 + coords[..., 7]))
coords[..., 12] = (0.5 * (1 - coords[..., 11]) * coords[..., 12]) + (0.5 * (1 + coords[..., 11]))
if self.pretrain:
# only sample in time around the initial condition
time = torch.ones(self.numpoints, 1) * start_time
coords = torch.cat((time, coords), dim=1)
else:
# slowly grow time values from start time
# this currently assumes start_time = 0 and max time value is tMax
time = self.tMin + torch.zeros(self.numpoints, 1).uniform_(0, (self.tMax-self.tMin) * (self.counter / self.full_count))
coords = torch.cat((time, coords), dim=1)
# make sure we always have training samples at the initial time
coords[-self.N_src_samples:, 0] = start_time
# Sample some points to impose the boundary coditions
if self.periodic_boundary:
# import ipdb; ipdb.set_trace()
coords_angle = torch.zeros(self.N_boundary_pts, 1).uniform_(math.pi-0.001, self.alpha['th'] + self.beta['th']) # Sample near the right boundary
coords_angle[0:self.N_boundary_pts//2] = -1.0 * coords_angle[0:self.N_boundary_pts//2] # Assign half of the points to the left boundary
coords_angle_periodic = angle_normalize(coords_angle)
coords_angle_concatenated = torch.cat((coords_angle, coords_angle_periodic), dim=0)
coords_angle_concatenated_normalized = (coords_angle_concatenated - self.beta['th'])/self.alpha['th']
coords[:self.N_boundary_pts] = coords[self.N_boundary_pts:2*self.N_boundary_pts]
coords[:2*self.N_boundary_pts, angle_index] = coords_angle_concatenated_normalized[..., 0]
# Add some samples that are inside the target set
if self.sample_inside_target:
target_coords = coords[:self.N_inside_target_samples] * 1.0
target_coords[..., 1:6] = self.sample_inside_target_set()
#target_coords[-self.N_inside_target_samples//6:, 0] = start_time # Ensuring that some of the target set samples are the initial time
coords = torch.cat((coords, target_coords), dim=0)
# Compute the initial value function
if self.diffModel:
coords_var = torch.tensor(coords.clone(), requires_grad=True)
boundary_values = self.compute_IC(coords_var[..., 1:])
# Normalize the value function
#print('Min and max value before normalization are %0.4f and %0.4f' %(min(boundary_values), max(boundary_values)))
boundary_values = (boundary_values - self.mean)*self.norm_to/self.var
#print('Min and max value after normalization are %0.4f and %0.4f' %(min(boundary_values), max(boundary_values)))
# Compute the gradients of the value function
lx_grads = diff_operators.gradient(boundary_values, coords_var)[..., 1:5]
else:
boundary_values = self.compute_IC(coords[..., 1:])