|
|
@@ -34,12 +34,38 @@ class LpaStar:
|
|
|
self.g[(i, j)] = float("inf")
|
|
|
|
|
|
self.rhs[self.xI] = 0
|
|
|
- self.U.put(self.xI, [self.h(self.xI), 0])
|
|
|
+ self.U.put(self.xI, self.CalculateKey(self.xI))
|
|
|
|
|
|
def searching(self):
|
|
|
self.computePath()
|
|
|
- path = self.extract_path()
|
|
|
- return path
|
|
|
+ path = [self.extract_path()]
|
|
|
+
|
|
|
+ obs_change = set()
|
|
|
+ for j in range(14, 15):
|
|
|
+ self.obs.add((30, j))
|
|
|
+ obs_change.add((30, j))
|
|
|
+ for s in obs_change:
|
|
|
+ self.rhs[s] = float("inf")
|
|
|
+ self.g[s] = float("inf")
|
|
|
+ for x in self.get_neighbor(s):
|
|
|
+ self.UpdateVertex(x)
|
|
|
+ # for x in obs_change:
|
|
|
+ # self.obs.remove(x)
|
|
|
+ # for x in obs_change:
|
|
|
+ # self.UpdateVertex(x)
|
|
|
+ print(self.g[(29, 15)])
|
|
|
+ print(self.g[(29, 14)])
|
|
|
+ print(self.g[(29, 13)])
|
|
|
+ print(self.g[(30, 13)])
|
|
|
+ print(self.g[(31, 13)])
|
|
|
+ print(self.g[(32, 13)])
|
|
|
+ print(self.g[(33, 13)])
|
|
|
+ print(self.g[(34, 13)])
|
|
|
+
|
|
|
+ self.computePath()
|
|
|
+ path.append(self.extract_path_test())
|
|
|
+
|
|
|
+ return path, obs_change
|
|
|
|
|
|
def computePath(self):
|
|
|
while self.U.top_key() < self.CalculateKey(self.xG) \
|
|
|
@@ -47,13 +73,12 @@ class LpaStar:
|
|
|
s = self.U.get()
|
|
|
if self.g[s] > self.rhs[s]:
|
|
|
self.g[s] = self.rhs[s]
|
|
|
- for x in self.get_neighbor(s):
|
|
|
- self.UpdateVertex(x)
|
|
|
else:
|
|
|
self.g[s] = float("inf")
|
|
|
self.UpdateVertex(s)
|
|
|
- for x in self.get_neighbor(s):
|
|
|
- self.UpdateVertex(x)
|
|
|
+ for x in self.get_neighbor(s):
|
|
|
+ self.UpdateVertex(x)
|
|
|
+ # return self.extract_path()
|
|
|
|
|
|
def extract_path(self):
|
|
|
path = []
|
|
|
@@ -68,6 +93,18 @@ class LpaStar:
|
|
|
return list(reversed(path))
|
|
|
path.append(s)
|
|
|
|
|
|
+ def extract_path_test(self):
|
|
|
+ path = []
|
|
|
+ s = self.xG
|
|
|
+
|
|
|
+ for k in range(30):
|
|
|
+ g_list = {}
|
|
|
+ for x in self.get_neighbor(s):
|
|
|
+ g_list[x] = self.g[x]
|
|
|
+ s = min(g_list, key=g_list.get)
|
|
|
+ path.append(s)
|
|
|
+ return list(reversed(path))
|
|
|
+
|
|
|
def get_neighbor(self, s):
|
|
|
nei_list = set()
|
|
|
for u in self.u_set:
|
|
|
@@ -85,7 +122,7 @@ class LpaStar:
|
|
|
if u != self.xI:
|
|
|
u_min = float("inf")
|
|
|
for x in self.get_neighbor(u):
|
|
|
- u_min = min(u_min, self.g[x] + 1)
|
|
|
+ u_min = min(u_min, self.g[x] + self.get_cost(u, x))
|
|
|
self.rhs[u] = u_min
|
|
|
self.U.check_remove(u)
|
|
|
if self.g[u] != self.rhs[u]:
|
|
|
@@ -102,32 +139,45 @@ class LpaStar:
|
|
|
else:
|
|
|
print("Please choose right heuristic type!")
|
|
|
|
|
|
- @staticmethod
|
|
|
- def get_cost(x, u):
|
|
|
+ def get_cost(self, s_start, s_end):
|
|
|
"""
|
|
|
Calculate cost for this motion
|
|
|
|
|
|
- :param x: current node
|
|
|
- :param u: current input
|
|
|
+ :param s_start:
|
|
|
+ :param s_end:
|
|
|
:return: cost for this motion
|
|
|
:note: cost function could be more complicate!
|
|
|
"""
|
|
|
|
|
|
- return 1
|
|
|
+ if s_start not in self.obs:
|
|
|
+ if s_end not in self.obs:
|
|
|
+ return 1
|
|
|
+ else:
|
|
|
+ return float("inf")
|
|
|
+ return float("inf")
|
|
|
|
|
|
|
|
|
def main():
|
|
|
x_start = (5, 5)
|
|
|
x_goal = (45, 25)
|
|
|
|
|
|
- lpastar = LpaStar(x_start, x_goal, "manhattan")
|
|
|
+ lpastar = LpaStar(x_start, x_goal, "euclidean")
|
|
|
plot = plotting.Plotting(x_start, x_goal)
|
|
|
|
|
|
- path = lpastar.searching()
|
|
|
- plot.plot_grid("test")
|
|
|
- px = [x[0] for x in path]
|
|
|
- py = [x[1] for x in path]
|
|
|
- plt.plot(px, py, color='red', marker='o')
|
|
|
+ path, obs = lpastar.searching()
|
|
|
+
|
|
|
+ plot.plot_grid("Lifelong Planning A*")
|
|
|
+ p = path[0]
|
|
|
+ px = [x[0] for x in p]
|
|
|
+ py = [x[1] for x in p]
|
|
|
+ plt.plot(px, py, marker='o')
|
|
|
+ plt.pause(0.5)
|
|
|
+
|
|
|
+ p = path[1]
|
|
|
+ px = [x[0] for x in p]
|
|
|
+ py = [x[1] for x in p]
|
|
|
+ plt.plot(px, py, marker='o')
|
|
|
+ plt.pause(0.01)
|
|
|
plt.show()
|
|
|
|
|
|
|