-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD_star.py
More file actions
215 lines (186 loc) · 7.29 KB
/
D_star.py
File metadata and controls
215 lines (186 loc) · 7.29 KB
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
# 0: New, 1:Open, 2: Close
# Tuples (Y, X) since grid[rows][cols]
class Block(object):
def __init__(self, name):
self.obstacle = 0
self.tag = 0
self.name = name
self.H = None
self.K = None
self.B = None
class DStar(object):
def __init__(self, g, s, w, l, o):
self.goal = g
self.start = s
self.obstacles = o
self.open = []
self.cmax = 100000
self.grid = []
self.robot = s
self.width = w
self.length = l
self.path = []
def initialize(self):
self.grid = [[Block((y, x)) for x in range(self.width)] for y in range(self.length)]
self.grid[self.goal[0]][self.goal[1]].H = 0
self.grid[self.goal[0]][self.goal[1]].K = 0
for entry in self.obstacles:
self.grid[entry[0]][entry[1]].obstacle = 1
self.grid[self.goal[0]][self.goal[1]].tag = 1
self.open.append(self.grid[self.goal[0]][self.goal[1]])
def sortList(self):
for num in range(len(self.open)-1, 0, -1):
for i in range(num):
if self.open[i].K > self.open[i+1].K:
temp = self.open[i]
self.open[i] = self.open[i+1]
self.open[i+1] = temp
def neighbor(self, current, new):
n = []
newN = []
# Up 1
if (current[0]-1) >= 0:
n.append((current[0]-1, current[1]))
if self.grid[current[0]-1][current[1]].tag == 0:
newN.append((current[0]-1, current[1]))
# Up 1 right
if (current[1]+1) < self.width:
n.append((current[0]-1, current[1]+1))
if self.grid[current[0]-1][current[1]+1].tag == 0:
newN.append((current[0]-1, current[1]+1))
# Up 1 left
if (current[1]-1) >= 0:
n.append((current[0]-1, current[1]-1))
if self.grid[current[0]-1][current[1]-1].tag == 0:
newN.append((current[0]-1, current[1]-1))
# Down 1
if (current[0]+1) < self.length:
n.append((current[0]+1, current[1]))
if self.grid[current[0]+1][current[1]].tag == 0:
newN.append((current[0]+1, current[1]))
# Down 1 right
if (current[1]+1) < self.width:
n.append((current[0]+1, current[1]+1))
if self.grid[current[0]+1][current[1]+1].tag == 0:
newN.append((current[0]+1, current[1]+1))
# Down 1 left
if (current[1]-1) >= 0:
n.append((current[0]+1, current[1]-1))
if self.grid[current[0]+1][current[1]-1].tag == 0:
newN.append((current[0]+1, current[1]-1))
# Left
if (current[1]-1) >= 0:
n.append((current[0], current[1]-1))
if self.grid[current[0]][current[1]-1].tag == 0:
newN.append((current[0], current[1]-1))
# Right
if (current[1]+1) < self.width:
n.append((current[0], current[1]+1))
if self.grid[current[0]][current[1]+1].tag == 0:
newN.append((current[0], current[1]+1))
if new == True:
return newN
return n
def c(self, X ,Y):
if Y.obstacle == 1:
return self.cmax
elif X.name[0] == Y.name[0] or X.name[1] == Y.name[1]:
return 1
else:
return 1.4
def insert(self, point, hnew):
hnew = min(self.cmax, hnew)
if point.tag == 0:
point.K = hnew
elif point.tag == 1:
point.K = min(hnew, point.K)
else:
point.K = min(hnew, point.H)
point.H = hnew
point.tag = 1
self.open.append(point)
self.sortList()
return point
def backpointerList(self):
cur = self.grid[self.robot[0]][self.robot[1]]
while cur.name != self.goal:
self.path.append(cur.name)
print(cur.name)
cur = self.grid[cur.B[0]][cur.B[1]]
if len(self.path) == 0:
print("No Possible Path")
self.path.append(cur.name)
return self.path
def processState(self, new):
cur = self.open.pop(0)
kold = cur.K
self.grid[cur.name[0]][cur.name[1]].tag = 2
n = self.neighbor(cur.name, new)
if kold < cur.H:
for entry in n:
Y = self.grid[entry[0]][entry[1]]
if Y.H <= kold and cur.H >=Y.H + self.c(Y, cur):
self.grid[cur.name[0]][cur.name[1]].B = Y.name
self.grid[cur.name[0]][cur.name[1]].H = Y.H + self.c(Y, cur)
elif kold == cur.H:
for entry in n:
Y = self.grid[entry[0]][entry[1]]
if (Y.tag == 0 or (Y.B == cur.name and Y.H != cur.H + self.c(cur, Y)) or
(Y.B!= cur.name and Y.H > cur.H + self.c(cur, Y))):
Y.B = cur.name
self.grid[entry[0]][entry[1]] = self.insert(Y, cur.H + self.c(cur, Y))
else:
for entry in n:
Y = self.grid[entry[0]][entry[1]]
if Y.tag == 0 or (Y.B == cur.name and Y.H != cur.H + self.c(cur, Y)):
Y.B = cur.name
self.grid[entry[0]][entry[1]] = self.insert(Y, cur.H + self.c(cur, Y))
else:
if Y.B!= cur.name and Y.H > cur.H + self.c(cur, Y):
self.grid[cur.name[0]][cur.name[1]] = self.insert(cur, cur.H)
else:
if (Y.B!= cur.name and cur.H > Y.H + self.c(Y, cur) and Y.tag == 2
and Y.H > kold):
self.grid[entry[0]][entry[1]] = self.insert(Y, Y.H)
if (len(self.open) == 0):
return -1
return self.open[0].K
def initPlan(self):
self.initialize()
while 1:
kmin = self.processState(True)
if kmin == -1 or self.grid[self.start[0]][self.start[1]].tag == 2:
break
return self.backpointerList()
def modifyCost(self, X):
X = self.grid[X[0]][X[1]]
if X.tag == 2 and X.obstacle == 1:
self.grid[X.name[0]][X.name[1]] = self.insert(X, self.cmax)
elif X.tag == 2:
self.grid[X.name[0]][X.name[1]] = self.insert(X, X.H)
def prepareRepair(self):
n = self.neighbor(self.robot, False)
toEdit = []
for elem in n:
if self.grid[elem[0]][elem[1]].obstacle == 0 and (elem in self.obstacles):
self.grid[elem[0]][elem[1]].obstacle = 1
toEdit.append(elem)
toEdit.extend(self.neighbor(elem, False))
for e in toEdit:
self.modifyCost(e)
def repairReplan(self):
while 1:
kmin = self.processState(False)
if kmin == -1 or self.grid[self.start[0]][self.start[1]].tag == 2:
break
return self.backpointerList()
def run2(self):
index = 0
while self.robot != self.goal:
self.prepareRepair()
path = self.repairReplan()
if len(path) == 0:
return -1
index += 1
self.robot = path[index]
return path