|
@@ -8,25 +8,26 @@ 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
|
|
|
from Search_3D.plot_util3D import visualization
|
|
from Search_3D.plot_util3D import visualization
|
|
|
|
|
|
|
|
|
|
|
|
|
class D_star(object):
|
|
class D_star(object):
|
|
|
- def __init__(self,resolution = 1):
|
|
|
|
|
|
|
+ def __init__(self, resolution=1):
|
|
|
self.Alldirec = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 1, 0], [1, 0, 1], [0, 1, 1], [1, 1, 1],
|
|
self.Alldirec = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 1, 0], [1, 0, 1], [0, 1, 1], [1, 1, 1],
|
|
|
[-1, 0, 0], [0, -1, 0], [0, 0, -1], [-1, -1, 0], [-1, 0, -1], [0, -1, -1],
|
|
[-1, 0, 0], [0, -1, 0], [0, 0, -1], [-1, -1, 0], [-1, 0, -1], [0, -1, -1],
|
|
|
[-1, -1, -1],
|
|
[-1, -1, -1],
|
|
|
[1, -1, 0], [-1, 1, 0], [1, 0, -1], [-1, 0, 1], [0, 1, -1], [0, -1, 1],
|
|
[1, -1, 0], [-1, 1, 0], [1, 0, -1], [-1, 0, 1], [0, 1, -1], [0, -1, 1],
|
|
|
[1, -1, -1], [-1, 1, -1], [-1, -1, 1], [1, 1, -1], [1, -1, 1], [-1, 1, 1]])
|
|
[1, -1, -1], [-1, 1, -1], [-1, -1, 1], [1, 1, -1], [1, -1, 1], [-1, 1, 1]])
|
|
|
- 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 = 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.h = self.initH() # estimate from a point to the end point
|
|
|
|
|
- self.tag = self.initTag() # set all states to new
|
|
|
|
|
- self.V = set()# vertice in closed
|
|
|
|
|
|
|
+ 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.h = self.initH() # estimate from a point to the end point
|
|
|
|
|
+ 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
|
|
# for visualization
|
|
@@ -34,8 +35,6 @@ class D_star(object):
|
|
|
self.Path = []
|
|
self.Path = []
|
|
|
self.done = False
|
|
self.done = False
|
|
|
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
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.
|
|
|
h = {}
|
|
h = {}
|
|
@@ -47,7 +46,7 @@ class D_star(object):
|
|
|
# tag , New point (never been in the OPEN list)
|
|
# tag , New point (never been in the OPEN list)
|
|
|
# Open point ( currently in OPEN )
|
|
# Open point ( currently in OPEN )
|
|
|
# Closed (currently in CLOSED)
|
|
# Closed (currently in CLOSED)
|
|
|
- t = {}
|
|
|
|
|
|
|
+ t = {}
|
|
|
for xi in self.X:
|
|
for xi in self.X:
|
|
|
t[xi] = 'New'
|
|
t[xi] = 'New'
|
|
|
return t
|
|
return t
|
|
@@ -57,7 +56,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 v,k 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
|
|
@@ -68,46 +67,46 @@ 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 v,k 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
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
def insert(self, x, h_new):
|
|
def insert(self, x, h_new):
|
|
|
# inserting a key and value into OPEN list (x, kx)
|
|
# inserting a key and value into OPEN list (x, kx)
|
|
|
# depending on following situations
|
|
# depending on following situations
|
|
|
if self.tag[x] == 'New':
|
|
if self.tag[x] == 'New':
|
|
|
kx = h_new
|
|
kx = h_new
|
|
|
if self.tag[x] == 'Open':
|
|
if self.tag[x] == 'Open':
|
|
|
- kx = min(self.OPEN[x],h_new)
|
|
|
|
|
|
|
+ kx = min(self.OPEN[x], h_new)
|
|
|
if self.tag[x] == 'Closed':
|
|
if self.tag[x] == 'Closed':
|
|
|
kx = min(self.h[x], h_new)
|
|
kx = min(self.h[x], h_new)
|
|
|
self.OPEN[x] = kx
|
|
self.OPEN[x] = kx
|
|
|
- self.h[x],self.tag[x] = h_new, 'Open'
|
|
|
|
|
-
|
|
|
|
|
|
|
+ self.h[x], self.tag[x] = h_new, 'Open'
|
|
|
|
|
+
|
|
|
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)
|
|
self.V.add(x)
|
|
|
if x == None: return -1
|
|
if x == None: return -1
|
|
|
- if kold < self.h[x]: # raised states
|
|
|
|
|
- for y in children(self,x):
|
|
|
|
|
- a = self.h[y] + cost(self,y,x)
|
|
|
|
|
|
|
+ if kold < self.h[x]: # raised states
|
|
|
|
|
+ for y in children(self, 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
|
|
|
|
|
- elif kold == self.h[x]:# lower
|
|
|
|
|
- for y in children(self,x):
|
|
|
|
|
- bb = self.h[x] + cost(self,x,y)
|
|
|
|
|
|
|
+ self.b[x], self.h[x] = y, a
|
|
|
|
|
+ elif kold == self.h[x]: # lower
|
|
|
|
|
+ for y in children(self, x):
|
|
|
|
|
+ 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):
|
|
|
|
|
|
|
+ (self.b[y] == x and self.h[y] != bb) or \
|
|
|
|
|
+ (self.b[y] != x and self.h[y] > bb):
|
|
|
self.b[y] = x
|
|
self.b[y] = x
|
|
|
self.insert(y, bb)
|
|
self.insert(y, bb)
|
|
|
- else:
|
|
|
|
|
- for y in children(self,x):
|
|
|
|
|
- bb = self.h[x] + cost(self,x,y)
|
|
|
|
|
|
|
+ else:
|
|
|
|
|
+ for y in children(self, x):
|
|
|
|
|
+ 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
|
|
|
self.insert(y, bb)
|
|
self.insert(y, bb)
|
|
|
else:
|
|
else:
|
|
@@ -115,11 +114,11 @@ class D_star(object):
|
|
|
self.insert(x, self.h[x])
|
|
self.insert(x, self.h[x])
|
|
|
else:
|
|
else:
|
|
|
if self.b[y] != x and self.h[y] > bb and \
|
|
if self.b[y] != x and self.h[y] > bb and \
|
|
|
- self.tag[y] == 'Closed' and self.h[y] == kold:
|
|
|
|
|
|
|
+ self.tag[y] == 'Closed' and self.h[y] == kold:
|
|
|
self.insert(y, self.h[y])
|
|
self.insert(y, self.h[y])
|
|
|
return self.get_kmin()
|
|
return self.get_kmin()
|
|
|
|
|
|
|
|
- def modify_cost(self,x,y,cval):
|
|
|
|
|
|
|
+ def modify_cost(self, x, y, cval):
|
|
|
# TODO: implement own function
|
|
# TODO: implement own function
|
|
|
# self.c[x][y] = cval
|
|
# self.c[x][y] = cval
|
|
|
# if self.tag[x] == 'Closed': self.insert(x,self.h[x])
|
|
# if self.tag[x] == 'Closed': self.insert(x,self.h[x])
|
|
@@ -131,11 +130,12 @@ class D_star(object):
|
|
|
kmin = self.process_state()
|
|
kmin = self.process_state()
|
|
|
if kmin >= self.h[x]: break
|
|
if kmin >= self.h[x]: break
|
|
|
|
|
|
|
|
- def path(self, goal = None):
|
|
|
|
|
|
|
+ def path(self, goal=None):
|
|
|
path = []
|
|
path = []
|
|
|
if not goal:
|
|
if not goal:
|
|
|
x = self.x0
|
|
x = self.x0
|
|
|
- else: x = goal
|
|
|
|
|
|
|
+ else:
|
|
|
|
|
+ x = goal
|
|
|
start = self.xt
|
|
start = self.xt
|
|
|
while x != start:
|
|
while x != start:
|
|
|
path.append([np.array(x), np.array(self.b[x])])
|
|
path.append([np.array(x), np.array(self.b[x])])
|
|
@@ -147,7 +147,7 @@ class D_star(object):
|
|
|
self.OPEN[self.xt] = 0
|
|
self.OPEN[self.xt] = 0
|
|
|
# first run
|
|
# first run
|
|
|
while True:
|
|
while True:
|
|
|
- #TODO: self.x0 =
|
|
|
|
|
|
|
+ # TODO: self.x0 =
|
|
|
self.process_state()
|
|
self.process_state()
|
|
|
visualization(self)
|
|
visualization(self)
|
|
|
if self.tag[self.x0] == "Closed":
|
|
if self.tag[self.x0] == "Closed":
|
|
@@ -162,16 +162,15 @@ class D_star(object):
|
|
|
while s != self.xt:
|
|
while s != self.xt:
|
|
|
if s == tuple(self.env.start):
|
|
if s == tuple(self.env.start):
|
|
|
s = self.b[self.x0]
|
|
s = self.b[self.x0]
|
|
|
- else:
|
|
|
|
|
|
|
+ else:
|
|
|
s = self.b[s]
|
|
s = self.b[s]
|
|
|
- self.modify(s)
|
|
|
|
|
- self.env.move_block(a=[0,0,-0.1],s=0.5,block_to_move=1,mode='translation')
|
|
|
|
|
|
|
+ # self.modify(s)
|
|
|
|
|
+ self.env.move_block(a=[0, 0, -0.1], s=0.5, block_to_move=1, mode='translation')
|
|
|
self.Path = self.path(s)
|
|
self.Path = self.path(s)
|
|
|
visualization(self)
|
|
visualization(self)
|
|
|
self.ind += 1
|
|
self.ind += 1
|
|
|
-
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
if __name__ == '__main__':
|
|
if __name__ == '__main__':
|
|
|
D = D_star(1)
|
|
D = D_star(1)
|
|
|
- D.run()
|
|
|
|
|
|
|
+ D.run()
|