-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathbit_star.py
368 lines (299 loc) · 13.7 KB
/
bit_star.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
import numpy as np
import math
import yaml
import heapq
import time
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
from shapely.geometry import Point, LineString, Polygon
from descartes import PolygonPatch
from shapely import affinity
import itertools
from time import time
from environment.timer import Timer
INF = float("inf")
class BITStar:
def __init__(self, environment, maxIter=5, plot_flag=False, batch_size=200, T=1000, sampling=None, timer=None):
if timer is None:
self.timer = Timer()
else:
self.timer = timer
self.env = environment
start, goal, bounds = tuple(environment.init_state), tuple(environment.goal_state), environment.bound
self.start = start
self.goal = goal
self.bounds = bounds
self.bounds = np.array(self.bounds).reshape((2, -1)).T
self.ranges = self.bounds[:, 1] - self.bounds[:, 0]
self.dimension = environment.config_dim
# This is the tree
self.vertices = []
self.edges = dict() # key = point,value = parent
self.g_scores = dict()
self.samples = []
self.vertex_queue = []
self.edge_queue = []
self.old_vertices = set()
self.maxIter = maxIter
self.r = INF
self.batch_size = batch_size
self.T, self.T_max = 0, T
self.eta = 1.1 # tunable parameter
self.obj_radius = 1
self.resolution = 3
# the parameters for informed sampling
self.c_min = self.distance(self.start, self.goal)
self.center_point = None
self.C = None
# whether plot the middle planning process
self.plot_planning_process = plot_flag
if sampling is None:
self.sampling = self.informed_sample
else:
self.sampling = sampling
self.n_collision_points = 0
self.n_free_points = 2
def setup_planning(self):
# add goal to the samples
self.samples.append(self.goal)
self.g_scores[self.goal] = INF
# add start to the tree
self.vertices.append(self.start)
self.g_scores[self.start] = 0
# Computing the sampling space
self.informed_sample_init()
radius_constant = self.radius_init()
return radius_constant
def radius_init(self):
from scipy import special
# Hypersphere radius calculation
n = self.dimension
unit_ball_volume = np.pi ** (n / 2.0) / special.gamma(n / 2.0 + 1)
volume = np.abs(np.prod(self.ranges)) * self.n_free_points / (self.n_collision_points + self.n_free_points)
gamma = (1.0 + 1.0 / n) * volume / unit_ball_volume
radius_constant = 2 * self.eta * (gamma ** (1.0 / n))
return radius_constant
def informed_sample_init(self):
self.center_point = np.array([(self.start[i] + self.goal[i]) / 2.0 for i in range(self.dimension)])
a_1 = (np.array(self.goal) - np.array(self.start)) / self.c_min
id1_t = np.array([1.0] * self.dimension)
M = np.dot(a_1.reshape((-1, 1)), id1_t.reshape((1, -1)))
U, S, Vh = np.linalg.svd(M, 1, 1)
self.C = np.dot(np.dot(U, np.diag([1] * (self.dimension - 1) + [np.linalg.det(U) * np.linalg.det(np.transpose(Vh))])), Vh)
def sample_unit_ball(self):
u = np.random.normal(0, 1, self.dimension) # an array of d normally distributed random variables
norm = np.sum(u ** 2) ** (0.5)
r = np.random.random() ** (1.0 / self.dimension)
x = r * u / norm
return x
def informed_sample(self, c_best, sample_num, vertices):
if c_best < float('inf'):
c_b = math.sqrt(c_best ** 2 - self.c_min ** 2) / 2.0
r = [c_best / 2.0] + [c_b] * (self.dimension - 1)
L = np.diag(r)
sample_array = []
cur_num = 0
while cur_num < sample_num:
if c_best < float('inf'):
x_ball = self.sample_unit_ball()
random_point = tuple(np.dot(np.dot(self.C, L), x_ball) + self.center_point)
else:
random_point = self.get_random_point()
if self.is_point_free(random_point):
sample_array.append(random_point)
cur_num += 1
return sample_array
def get_random_point(self):
point = self.bounds[:, 0] + np.random.random(self.dimension) * self.ranges
return tuple(point)
def is_point_free(self, point):
result = self.env._state_fp(np.array(point))
if result:
self.n_free_points += 1
else:
self.n_collision_points += 1
return result
def is_edge_free(self, edge):
result = self.env._edge_fp(np.array(edge[0]), np.array(edge[1]))
# self.T += self.env.k
return result
def get_g_score(self, point):
# gT(x)
if point == self.start:
return 0
if point not in self.edges:
return INF
else:
return self.g_scores.get(point)
def get_f_score(self, point):
# f^(x)
return self.heuristic_cost(self.start, point) + self.heuristic_cost(point, self.goal)
def actual_edge_cost(self, point1, point2):
# c(x1,x2)
if not self.is_edge_free([point1, point2]):
return INF
return self.distance(point1, point2)
def heuristic_cost(self, point1, point2):
# Euler distance as the heuristic distance
return self.distance(point1, point2)
def distance(self, point1, point2):
return np.linalg.norm(np.array(point1) - np.array(point2))
def get_edge_value(self, edge):
# sort value for edge
return self.get_g_score(edge[0]) + self.heuristic_cost(edge[0], edge[1]) + self.heuristic_cost(edge[1],
self.goal)
def get_point_value(self, point):
# sort value for point
return self.get_g_score(point) + self.heuristic_cost(point, self.goal)
def bestVertexQueueValue(self):
if not self.vertex_queue:
return INF
else:
return self.vertex_queue[0][0]
def bestEdgeQueueValue(self):
if not self.edge_queue:
return INF
else:
return self.edge_queue[0][0]
def prune_edge(self, c_best):
edge_array = list(self.edges.items())
for point, parent in edge_array:
if self.get_f_score(point) > c_best or self.get_f_score(parent) > c_best:
self.edges.pop(point)
def prune(self, c_best):
self.samples = [point for point in self.samples if self.get_f_score(point) < c_best]
self.prune_edge(c_best)
vertices_temp = []
for point in self.vertices:
if self.get_f_score(point) <= c_best:
if self.get_g_score(point) == INF:
self.samples.append(point)
else:
vertices_temp.append(point)
self.vertices = vertices_temp
def expand_vertex(self, point):
self.timer.start()
# get the nearest value in vertex for every one in samples where difference is less than the radius
neigbors_sample = []
for sample in self.samples:
if self.distance(point, sample) <= self.r:
neigbors_sample.append(sample)
self.timer.finish(Timer.NN)
self.timer.start()
# add an edge to the edge queue is the path might improve the solution
for neighbor in neigbors_sample:
estimated_f_score = self.heuristic_cost(self.start, point) + \
self.heuristic_cost(point, neighbor) + self.heuristic_cost(neighbor, self.goal)
if estimated_f_score < self.g_scores[self.goal]:
heapq.heappush(self.edge_queue, (self.get_edge_value((point, neighbor)), (point, neighbor)))
# add the vertex to the edge queue
if point not in self.old_vertices:
neigbors_vertex = []
for ver in self.vertices:
if self.distance(point, ver) <= self.r:
neigbors_vertex.append(ver)
for neighbor in neigbors_vertex:
if neighbor not in self.edges or point != self.edges.get(neighbor):
estimated_f_score = self.heuristic_cost(self.start, point) + \
self.heuristic_cost(point, neighbor) + self.heuristic_cost(neighbor, self.goal)
if estimated_f_score < self.g_scores[self.goal]:
estimated_g_score = self.get_g_score(point) + self.heuristic_cost(point, neighbor)
if estimated_g_score < self.get_g_score(neighbor):
heapq.heappush(self.edge_queue, (self.get_edge_value((point, neighbor)), (point, neighbor)))
self.timer.finish(Timer.EXPAND)
def get_best_path(self):
path = []
if self.g_scores[self.goal] != INF:
path.append(self.goal)
point = self.goal
while point != self.start:
point = self.edges[point]
path.append(point)
path.reverse()
return path
def path_length_calculate(self, path):
path_length = 0
for i in range(len(path) - 1):
path_length += self.distance(path[i], path[i + 1])
return path_length
def plan(self, pathLengthLimit, refine_time_budget=None, time_budget=None):
collision_checks = self.env.collision_check_count
if time_budget is None:
time_budget = INF
if refine_time_budget is None:
refine_time_budget = 10
self.setup_planning()
init_time = time()
while self.T < self.T_max and (time() - init_time < time_budget):
if not self.vertex_queue and not self.edge_queue:
c_best = self.g_scores[self.goal]
self.prune(c_best)
self.samples.extend(self.sampling(c_best, self.batch_size, self.vertices))
self.T += self.batch_size
self.timer.start()
self.old_vertices = set(self.vertices)
self.vertex_queue = [(self.get_point_value(point), point) for point in self.vertices]
heapq.heapify(self.vertex_queue) # change to op priority queue
q = len(self.vertices) + len(self.samples)
self.r = self.radius_init() * ((math.log(q) / q) ** (1.0 / self.dimension))
self.timer.finish(Timer.HEAP)
try:
while self.bestVertexQueueValue() <= self.bestEdgeQueueValue():
self.timer.start()
_, point = heapq.heappop(self.vertex_queue)
self.timer.finish(Timer.HEAP)
self.expand_vertex(point)
except Exception as e:
if (not self.edge_queue) and (not self.vertex_queue):
continue
else:
raise e
best_edge_value, bestEdge = heapq.heappop(self.edge_queue)
# Check if this can improve the current solution
if best_edge_value < self.g_scores[self.goal]:
actual_cost_of_edge = self.actual_edge_cost(bestEdge[0], bestEdge[1])
self.timer.start()
actual_f_edge = self.heuristic_cost(self.start, bestEdge[0]) + actual_cost_of_edge + self.heuristic_cost(bestEdge[1], self.goal)
if actual_f_edge < self.g_scores[self.goal]:
actual_g_score_of_point = self.get_g_score(bestEdge[0]) + actual_cost_of_edge
if actual_g_score_of_point < self.get_g_score(bestEdge[1]):
self.g_scores[bestEdge[1]] = actual_g_score_of_point
self.edges[bestEdge[1]] = bestEdge[0]
if bestEdge[1] not in self.vertices:
self.samples.remove(bestEdge[1])
self.vertices.append(bestEdge[1])
heapq.heappush(self.vertex_queue, (self.get_point_value(bestEdge[1]), bestEdge[1]))
self.edge_queue = [item for item in self.edge_queue if item[1][1] != bestEdge[1] or \
self.get_g_score(item[1][0]) + self.heuristic_cost(item[1][0], item[1][
1]) < self.get_g_score(item[1][0])]
heapq.heapify(
self.edge_queue) # Rebuild the priority queue because it will be destroyed after the element is removed
self.timer.finish(Timer.HEAP)
else:
self.vertex_queue = []
self.edge_queue = []
if self.g_scores[self.goal] < pathLengthLimit and (time() - init_time > refine_time_budget):
break
return self.samples, self.edges, self.env.collision_check_count - collision_checks, \
self.g_scores[self.goal], self.T, time() - init_time
if __name__ == '__main__':
from utils.plot import plot_edges
from config import set_random_seed
from environment import MazeEnv
from tqdm import tqdm
solutions = []
environment = MazeEnv(dim=2)
def sample_empty_points(env):
while True:
point = np.random.uniform(-1, 1, 2)
if env._state_fp(point):
return point
for _ in tqdm(range(3000)):
pb = environment.init_new_problem()
set_random_seed(1234)
cur_time = time.time()
BIT = BITStar(environment)
nodes, edges, collision, success, n_samples = BIT.plan(INF)
solutions.append((nodes, edges, collision, success, n_samples))
plot_edges(set(nodes)|set(edges.keys()), edges, environment.get_problem())
print('hello')