|
@@ -3,12 +3,13 @@ import matplotlib.pyplot as plt
|
|
|
|
|
|
|
|
import os
|
|
import os
|
|
|
import sys
|
|
import sys
|
|
|
|
|
+from collections import defaultdict
|
|
|
|
|
|
|
|
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../../Search-based Planning/")
|
|
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../../Search-based Planning/")
|
|
|
from Search_3D.env3D import env
|
|
from Search_3D.env3D import env
|
|
|
from Search_3D import Astar3D
|
|
from Search_3D import Astar3D
|
|
|
from Search_3D.utils3D import StateSpace, getDist, getNearest, getRay, isinbound, isinball, isCollide, children, cost, initcost
|
|
from Search_3D.utils3D import StateSpace, getDist, getNearest, getRay, isinbound, isinball, isCollide, children, cost, initcost
|
|
|
-import pyrr
|
|
|
|
|
|
|
+from Search_3D.plot_util3D import visualization
|
|
|
|
|
|
|
|
|
|
|
|
|
class D_star(object):
|
|
class D_star(object):
|
|
@@ -21,16 +22,19 @@ class D_star(object):
|
|
|
self.env = env(resolution = resolution)
|
|
self.env = env(resolution = resolution)
|
|
|
self.X = StateSpace(self.env)
|
|
self.X = StateSpace(self.env)
|
|
|
self.x0, self.xt = getNearest(self.X, self.env.start), getNearest(self.X, self.env.goal)
|
|
self.x0, self.xt = getNearest(self.X, self.env.start), getNearest(self.X, self.env.goal)
|
|
|
- self.b = {} # back pointers every state has one except xt.
|
|
|
|
|
|
|
+ self.b = defaultdict(lambda: defaultdict(dict))# back pointers every state has one except xt.
|
|
|
self.OPEN = {} # OPEN list, here use a hashmap implementation. hash is point, key is value
|
|
self.OPEN = {} # OPEN list, here use a hashmap implementation. hash is point, key is value
|
|
|
self.h = self.initH() # estimate from a point to the end point
|
|
self.h = self.initH() # estimate from a point to the end point
|
|
|
self.tag = self.initTag() # set all states to new
|
|
self.tag = self.initTag() # set all states to new
|
|
|
-
|
|
|
|
|
|
|
+ self.V = set()# vertice in closed
|
|
|
# initialize cost set
|
|
# initialize cost set
|
|
|
- self.c = initcost(self)
|
|
|
|
|
|
|
+ # self.c = initcost(self)
|
|
|
|
|
+ # for visualization
|
|
|
|
|
+ self.ind = 0
|
|
|
|
|
+ self.Path = []
|
|
|
|
|
+ self.done = False
|
|
|
|
|
|
|
|
- # put G (ending state) into the OPEN list
|
|
|
|
|
- self.OPEN[self.xt] = 0
|
|
|
|
|
|
|
+
|
|
|
|
|
|
|
|
def initH(self):
|
|
def initH(self):
|
|
|
# h set, all initialzed h vals are 0 for all states.
|
|
# h set, all initialzed h vals are 0 for all states.
|
|
@@ -53,7 +57,7 @@ class D_star(object):
|
|
|
# -1 if it does not exist
|
|
# -1 if it does not exist
|
|
|
if self.OPEN:
|
|
if self.OPEN:
|
|
|
minv = np.inf
|
|
minv = np.inf
|
|
|
- for k,v in enumerate(self.OPEN):
|
|
|
|
|
|
|
+ for v,k in enumerate(self.OPEN):
|
|
|
if v < minv: minv = v
|
|
if v < minv: minv = v
|
|
|
return minv
|
|
return minv
|
|
|
return -1
|
|
return -1
|
|
@@ -64,7 +68,7 @@ class D_star(object):
|
|
|
# it also removes this min value form the OPEN set.
|
|
# it also removes this min value form the OPEN set.
|
|
|
if self.OPEN:
|
|
if self.OPEN:
|
|
|
minv = np.inf
|
|
minv = np.inf
|
|
|
- for k,v in enumerate(self.OPEN):
|
|
|
|
|
|
|
+ for v,k in enumerate(self.OPEN):
|
|
|
if v < minv: mink, minv = k, v
|
|
if v < minv: mink, minv = k, v
|
|
|
return mink, self.OPEN.pop(mink)
|
|
return mink, self.OPEN.pop(mink)
|
|
|
return None, -1
|
|
return None, -1
|
|
@@ -84,15 +88,16 @@ class D_star(object):
|
|
|
def process_state(self):
|
|
def process_state(self):
|
|
|
x, kold = self.min_state()
|
|
x, kold = self.min_state()
|
|
|
self.tag[x] = 'Closed'
|
|
self.tag[x] = 'Closed'
|
|
|
|
|
+ self.V.add(x)
|
|
|
if x == None: return -1
|
|
if x == None: return -1
|
|
|
if kold < self.h[x]: # raised states
|
|
if kold < self.h[x]: # raised states
|
|
|
for y in children(self,x):
|
|
for y in children(self,x):
|
|
|
- a = self.h[y] + self.c[y][x]
|
|
|
|
|
|
|
+ a = self.h[y] + cost(self,y,x)
|
|
|
if self.h[y] <= kold and self.h[x] > a:
|
|
if self.h[y] <= kold and self.h[x] > a:
|
|
|
self.b[x], self.h[x] = y , a
|
|
self.b[x], self.h[x] = y , a
|
|
|
elif kold == self.h[x]:# lower
|
|
elif kold == self.h[x]:# lower
|
|
|
for y in children(self,x):
|
|
for y in children(self,x):
|
|
|
- bb = self.h[x] + self.c[x][y]
|
|
|
|
|
|
|
+ bb = self.h[x] + cost(self,x,y)
|
|
|
if self.tag[y] == 'New' or \
|
|
if self.tag[y] == 'New' or \
|
|
|
(self.b[y] == x and self.h[y] != bb) or \
|
|
(self.b[y] == x and self.h[y] != bb) or \
|
|
|
(self.b[y] != x and self.h[y] > bb):
|
|
(self.b[y] != x and self.h[y] > bb):
|
|
@@ -100,7 +105,7 @@ class D_star(object):
|
|
|
self.insert(y, bb)
|
|
self.insert(y, bb)
|
|
|
else:
|
|
else:
|
|
|
for y in children(self,x):
|
|
for y in children(self,x):
|
|
|
- bb = self.h[x] + self.c[x][y]
|
|
|
|
|
|
|
+ bb = self.h[x] + cost(self,x,y)
|
|
|
if self.tag[y] == 'New' or \
|
|
if self.tag[y] == 'New' or \
|
|
|
(self.b[y] == x and self.h[y] != bb):
|
|
(self.b[y] == x and self.h[y] != bb):
|
|
|
self.b[y] = x
|
|
self.b[y] = x
|
|
@@ -115,14 +120,51 @@ class D_star(object):
|
|
|
return self.get_kmin()
|
|
return self.get_kmin()
|
|
|
|
|
|
|
|
def modify_cost(self,x,y,cval):
|
|
def modify_cost(self,x,y,cval):
|
|
|
- self.c[x][y] = cval # set the new cost to the cval
|
|
|
|
|
- if self.tag[x] == 'Closed': self.insert(x,self.h[x])
|
|
|
|
|
- return self.get_kmin()
|
|
|
|
|
|
|
+ # TODO: implement own function
|
|
|
|
|
+ # self.c[x][y] = cval
|
|
|
|
|
+ # if self.tag[x] == 'Closed': self.insert(x,self.h[x])
|
|
|
|
|
+ # return self.get_kmin()
|
|
|
|
|
+ pass
|
|
|
|
|
+
|
|
|
|
|
+ def path(self):
|
|
|
|
|
+ path = []
|
|
|
|
|
+ x = self.x0
|
|
|
|
|
+ start = self.xt
|
|
|
|
|
+ while x != start:
|
|
|
|
|
+ path.append([np.array(x), np.array(self.b[x])])
|
|
|
|
|
+ x = self.b[x]
|
|
|
|
|
+ return path
|
|
|
|
|
|
|
|
def run(self):
|
|
def run(self):
|
|
|
- # TODO: implementation of changing obstable in process
|
|
|
|
|
- pass
|
|
|
|
|
|
|
+ # put G (ending state) into the OPEN list
|
|
|
|
|
+ self.OPEN[self.xt] = 0
|
|
|
|
|
+ # first run
|
|
|
|
|
+ while True:
|
|
|
|
|
+ #TODO: self.x0 =
|
|
|
|
|
+ self.process_state()
|
|
|
|
|
+ visualization(self)
|
|
|
|
|
+ if self.tag[self.x0] == "Closed":
|
|
|
|
|
+ break
|
|
|
|
|
+ self.ind += 1
|
|
|
|
|
+ self.Path = self.path()
|
|
|
|
|
+ self.done = True
|
|
|
|
|
+ visualization(self)
|
|
|
|
|
+ # plt.show()
|
|
|
|
|
+ # when the environemnt changes over time
|
|
|
|
|
+ s = tuple(self.env.start)
|
|
|
|
|
+ while s != self.xt:
|
|
|
|
|
+ if s == tuple(self.env.start):
|
|
|
|
|
+ s = self.b[self.x0]
|
|
|
|
|
+ else:
|
|
|
|
|
+ s = self.b[s]
|
|
|
|
|
+ self.process_state()
|
|
|
|
|
+ self.env.move_block(a=[0,0,0.1],s=0.5,mode='translation')
|
|
|
|
|
+ self.Path = self.path()
|
|
|
|
|
+ visualization(self)
|
|
|
|
|
+ self.ind += 1
|
|
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
if __name__ == '__main__':
|
|
|
- D = D_star(1)
|
|
|
|
|
|
|
+ D = D_star(1)
|
|
|
|
|
+ D.run()
|