forked from smlbansal/deepreach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataio.py
232 lines (176 loc) · 8.48 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
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
from torchvision.transforms import Resize, Compose, ToTensor, Normalize
import utils
import pickle
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)
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()
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}