-
Notifications
You must be signed in to change notification settings - Fork 2
/
map_generation.py
executable file
·318 lines (264 loc) · 10.3 KB
/
map_generation.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
#!/usr/bin/env python
'''
This file contains the procedure for map generation
Author: Manish Saroya
Contact: saroyam@oregonstate.edu
DARPA SubT Challenge
'''
import matplotlib.pyplot as plt
import numpy as np
import heapq
import random
class PriorityQueue:
def __init__(self):
self.elements = []
def empty(self):
return len(self.elements) == 0
def put(self, item, priority):
heapq.heappush(self.elements, (priority, item))
def get(self):
return heapq.heappop(self.elements)[1]
def heuristic(a, b):
(x1, y1) = a
(x2, y2) = b
return abs(x1 - x2) + abs(y1 - y2)
def a_star_search(grid, start, goal):
frontier = PriorityQueue()
frontier.put(start, 0)
came_from = {}
cost_so_far = {}
came_from[start] = None
cost_so_far[start] = 0
while not frontier.empty():
current = frontier.get()
if current == goal:
break
for d in dirs_motion:
x, y = d(current[0], current[1])
# check for bounds
if 0 <= x < len(grid) and 0 <= y < len(grid[0]):
next = (x,y)
# making all travel as cost 1
new_cost = cost_so_far[current] + 1
if next not in cost_so_far or new_cost < cost_so_far[next]:
cost_so_far[next] = new_cost
priority = new_cost + heuristic(goal, next)
frontier.put(next, priority)
came_from[next] = current
return came_from, cost_so_far
def getPath(grid, start, goal):
start = tuple(start)
goal = tuple(goal)
came_from_, cost_so_far_ = a_star_search(grid, start, goal)
pointer = goal
path = []
path.append(pointer)
while pointer != start:
path.append(came_from_[pointer])
pointer = came_from_[pointer]
return path
# create random points of interests.
def createPOI(numPoints, dimension):
pts = []
while len(pts) < numPoints:
point = [np.random.randint(0, dimension[0]), np.random.randint(0, dimension[1])]
if point not in pts:
pts.append(point)
return pts
def connectGrid(pts, grid):
for i in range(len(pts)):
for j in range(i+1, len(pts)):
path = getPath(np.zeros((len(grid), len(grid[0]))), pts[i], pts[j])
#print("astarpath",path)
for k in path:
grid[k[0], k[1]] = 1
def sparseConnectGrid(pts, grid, near_entrance_point):
tree = []
tree.append(near_entrance_point)
#forbidden_points = {tuple(k): [] for k in pts}
for i in pts:
nearestPoints = nearestNeighbor(i, tree) #, forbidden_points[tuple(i)])
#forbidden_points[tuple(nearestPoint)].append(i)
for nearestPoint in nearestPoints:
if nearestPoint != i:
path = getPath(np.zeros((len(grid), len(grid[0]))), i, nearestPoint)
tree.append(i)
for k in path:
grid[k[0], k[1]] = 1
def nearestNeighbor(center, pts): #, forbidden):
distance = []
for i in pts:
#if i != center: #and (i not in forbidden):
distance.append(manhattanDist(i, center))
#else:
# distance.append(1000000)
nearestPoints = []
#nearestPoints.append(pts[np.argmin(distance)])
distance = np.array(distance)
#print(distance)
indices = distance.argsort()[:2]
#print indices
nearestPoints.append(pts[indices[0]])
if random.uniform(0,1) > 0.8 and len(indices)>=2:
nearestPoints.append(pts[indices[1]])
return nearestPoints
def manhattanDist(p1,p2):
return abs(p1[0] - p2[0]) + abs(p1[1] - p2[1])
def connectEntrance(grid, entrance, pts):
distance = []
for i in pts:
distance.append(manhattanDist(i, entrance))
nearestPoint = pts[np.argmin(distance)]
#print(nearestPoint)
if entrance != nearestPoint:
path = getPath(np.zeros((len(grid), len(grid[0]))), entrance, nearestPoint)
for i in path:
grid[i[0], i[1]] = 1
return nearestPoint
dirs_motion = [
lambda x, y: (x-1, y), # up
lambda x, y: (x+1, y), # down
lambda x, y: (x, y - 1), # left
lambda x, y: (x, y + 1), # right
]
def getTiles(gridDimension, numPOI):
#board = np.zeros((gridDimension[0],gridDimension[1]))
path_viz = np.zeros((gridDimension[0], gridDimension[1]))
points = createPOI(numPOI, gridDimension)
#print("points", points)
#connectGrid(points, path_viz)
#sparseConnectGrid(points, path_viz)
entrance_point = [0, int(gridDimension[1]/2)]
# Connecting Entrance to the nearest point of interest
near_entrance_point = connectEntrance(path_viz,entrance_point,points)
sparseConnectGrid(points, path_viz, near_entrance_point)
tiles = np.zeros((gridDimension[0], gridDimension[1]))
for x in range(len(path_viz)):
for y in range(len(path_viz[0])):
# get all the possible direction values.
dir_vector = []
for d in dirs_motion:
nx, ny = d(x, y)
if 0 <= nx < len(path_viz) and 0 <= ny < len(path_viz[0]):
dir_vector.append(path_viz[nx, ny])
else:
dir_vector.append(0)
# Connect with the entrance
if entrance_point[0] == x and entrance_point[1] == y:
#print("equating entrance", entrance_point, x, y)
dir_vector[0] = 1
# check whether the current point needs a tile.
if path_viz[x,y] == 1:
if dir_vector[0] == 1 \
and dir_vector[1] == 1 \
and dir_vector[2] == 1 \
and dir_vector[3] == 1:
if [x,y] not in points:
tiles[x,y] = 111
else:
tiles[x,y] = 10 # 10 is the code for Plus connection.
elif dir_vector[0] == 1 \
and dir_vector[1] == 1 \
and dir_vector[2] == 1 \
and dir_vector[3] == 0:
tiles[x,y] = 21 # 10 is the code for Plus connection.
elif dir_vector[0] == 1 \
and dir_vector[1] == 1 \
and dir_vector[2] == 0 \
and dir_vector[3] == 1:
tiles[x,y] = 22 # 10 is the code for Plus connection.
elif dir_vector[0] == 1 \
and dir_vector[1] == 0 \
and dir_vector[2] == 1 \
and dir_vector[3] == 1:
tiles[x,y] = 23 # 10 is the code for Plus connection.
elif dir_vector[0] == 0 \
and dir_vector[1] == 1 \
and dir_vector[2] == 1 \
and dir_vector[3] == 1:
tiles[x,y] = 24 # 10 is the code for Plus connection.
elif sum(dir_vector) == 1:
#print("sum", sum(dir_vector))
if dir_vector[0] == 1:
tiles[x,y] = 31 # 10 is the code for Plus connection.
elif dir_vector[1] == 1:
tiles[x,y] = 32
elif dir_vector[2] == 1:
tiles[x,y] = 33
elif dir_vector[3] == 1:
tiles[x,y] = 34
elif dir_vector[0] == 1 \
and dir_vector[1] == 1 \
and dir_vector[2] == 0 \
and dir_vector[3] == 0:
tiles[x,y] = 11 # 11 is the code for straight connection along x axis.
elif dir_vector[0] == 0 \
and dir_vector[1] == 0 \
and dir_vector[2] == 1 \
and dir_vector[3] == 1:
tiles[x,y] = 12 # 12 is the code for straight connection along y axis, make yaw pi/2.
elif dir_vector[0] == 1 \
and dir_vector[1] == 0 \
and dir_vector[2] == 1 \
and dir_vector[3] == 0:
tiles[x,y] = 13 # 13 is the code for turn with yaw 0.
elif dir_vector[0] == 1 \
and dir_vector[1] == 0 \
and dir_vector[2] == 0 \
and dir_vector[3] == 1:
tiles[x,y] = 14 # 14 is the code for turn with yaw -pi/2.
elif dir_vector[0] == 0 \
and dir_vector[1] == 1 \
and dir_vector[2] == 1 \
and dir_vector[3] == 0:
tiles[x,y] = 15 # 15 is the code for turn with yaw pi/2.
elif dir_vector[0] == 0 \
and dir_vector[1] == 1 \
and dir_vector[2] == 0 \
and dir_vector[3] == 1:
tiles[x,y] = 16 # 16 is the code for turn with yaw pi.
#print(path_viz)
#print(tiles)
#plt.imshow(path_viz)
#plt.ylabel('x')
#plt.xlabel('y')
#plt.show()
return tiles, path_viz
#t = getTiles(gridDimension,numPOI)
#print(t)
## Data generation. Ideally this should be in a different file
import sys
import pickle
GRID_SIZE = 32
gridDimension = [GRID_SIZE, GRID_SIZE]
numPOI = 18
trainRatio = 0.8
totalData = 50000
validRatio = 0.1
testRatio = 0.1
def generate(ratio,totalData,tpe):
print("Generating",tpe,"data...")
dat = []
for i in range(int(ratio * totalData)):
m, n = getTiles(gridDimension,numPOI)
dat.append(np.float32(n))
#print("type",n.dtype)
#test = np.logical_or.reduce((m==31,m==32,m==33,m==34))
#data["training_labels"].append(test.astype(int))
print(
'\r[Generating Data {} of {}]'.format(
i,
int(ratio * totalData),
),
end=''
)
print('')
return dat
data = {}
data["train"] = generate(trainRatio,totalData,"training")
data["validation"] = generate(validRatio,totalData,"validation")
data["test"] = generate(testRatio,totalData,"testing")
with open('ground_truth_dataset_{}.pickle'.format(GRID_SIZE), 'wb') as handle:
pickle.dump(data, handle)
#with open('synthetic_dataset.pickle', 'rb') as handle:
# b = pickle.load(handle)