-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.py
290 lines (236 loc) · 6.65 KB
/
graph.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
from shape import *
import matplotlib.pyplot as plt
class Graph():
def __init__(self):
self.map = None
self.dest = None
self.start = None
self.path = None
def set_graph(self, tmp_map):
self.map = tmp_map
def set_dest(self,dest):
self.dest = dest
def set_start(self,start):
self.start = start
def locate_node(self,pos):
for key,node in self.map.items():
inside,loc = p_inside_rect(pos,node.shape)
if inside == True:
return key
return None
def reset_nodes(self):
for key, node in self.map.items():
node.visited = False
node.prev = None
def dijkstras(self):
print("inside dijkstras")
self.reset_nodes()
cost = dict((el,float("inf")) for el in self.map.keys())
queue = [self.start]
cost[self.start] = 0
visited = [self.start]
while len(queue) > 0:
node = queue.pop(0)
visited.append(node)
nodes = self.map[node].neighbors_pos()
for n in nodes:
if cost[n] > cost[node] + length(node,n):
self.map[n].prev = node
cost[n] = min(cost[n],cost[node]+length(node,n))
if n not in visited:
queue.append(n)
#print(cost)
#print(self.map[self.dest].prev)
node = self.dest
path = []
while node != None:
path.append(node)
node = self.map[node].prev
return path[::-1]
def distance_to_dest(self,pos):
return length(self.dest,pos)
def a_star(self):
print("inside a_star")
self.reset_nodes()
#G value is the cost form start to current node
costG = dict((el,float("inf")) for el in self.map.keys())
#H estimated cost from current to goal
costH = dict((el,float("inf")) for el in self.map.keys())
#F = G + H
costF = dict((el,float("inf")) for el in self.map.keys())
costG[self.start] = 0
costH[self.start] = self.distance_to_dest(self.start)
costF[self.start] = costG[self.start] + costH[self.start]
min_node = self.start
visited = []
print("costG:")
print(costG)
print('costH:')
print(costH)
while(min_node):
node = min_node
min_node = None
visited.append(node)
#update G cost and H cost around neighbors
nodes = self.map[node].neighbors_pos()
print("a_star neighbors: "+ str(nodes))
for n in nodes:
dd = self.distance_to_dest(n)
#destination is reached
if(dd == 0):
print("break inside a_star")
self.map[n].prev = node
break;
costH[n] = dd
dist_to_node = costG[node] + length(node,n)
if(costG[n] > dist_to_node):
costG[n] = dist_to_node
self.map[n].prev = node
#update F
costF[n] = costG[n] + costH[n]
#determine the next min
candidates = sort_eliminate_key(costF,visited)
print("a_star: candidate: " + str(candidates))
if(len(candidates) > 0):
min_node = candidates[0]
print("a_star: explored "+str(node))
print("a_star: Fcost: ")
print(costF)
print("a_star: next: " + str(min_node))
node = self.dest
path = []
while node != None:
path.append(node)
node = self.map[node].prev
return path[::-1]
# a wonderfully clear path
def tree_extend(self,branch):
if self.map == None:
init = branch[0]
self.map = dict()
self.map[init] = Node(init[0], init[1])
print("tree initialized: " + str(self.map.keys()))
#start branch with a known position
p = branch.pop(0)
assert(p in self.map.keys())
#extend branch
while len(branch) > 0:
node = branch.pop(0)
if node not in self.map.keys():
self.map[node] = Node(node[0],node[1])
self.map[node].prev = p
p = node
#print tree structure
def print_tree(self):
for node in self.map.keys():
prev = self.map[node].prev
if prev:
print(str(node) + "--" + str(prev))
else:
print(str(node) + "-- None")
#closest distance based on euclean distance
def closest_point(self,pos):
closest_point = self.map.keys()[0]
distance = (closest_point[0]-pos[0])**2 + (closest_point[1]-pos[1])**2
for k in self.map.keys():
dist = (k[0] - pos[0])**2 + (k[1] - pos[1])**2
if (dist < distance):
closest_point = k
distance = dist
return closest_point
def traceback(self):
assert(self.start != None)
assert(self.dest != None)
node = self.dest
if(node not in self.map.keys()):
print("failed to traceback")
return []
path = []
while node != None:
path.append(node)
node = self.map[node].prev
self.path = path[::-1]
return self.path
def graph_print(self):
points = self.map.keys()
x = []
y = []
edgex = []
edgey = []
for (i,j) in self.map.keys():
plt.plot([i],[j],'ro')
if(self.map[(i,j)].prev):
(prev_i,prev_j) = self.map[(i,j)].prev
plt.plot([prev_i,i],[prev_j,j],'k')
if(self.path):
prev = self.path[0]
for(i,j) in self.path:
plt.plot([i,prev[0]],[j,prev[1]],color="y",linewidth=2)
prev = (i,j)
plt.title('Path Graph')
plt.show()
def sort_eliminate_key(d,visited):
l = {}
for i in d.items():
if i[1] < float("inf") and i[0] not in visited:
l[i[0]] = i[1]
return sorted(l,key=lambda k:l[k])
class Node():
def __init__(self,x,y):
self.x = x
self.y = y
self.neighbors = dict(up=None,down=None,left=None,right=None)
#q1=None,q2=None,q3=None,q4=None)
self.shape = None
self.visited = False
self.prev = None
def __eq__(self, other):
if isinstance(other, Node):
return self.x == other.x and self.y == other.y
#print function
def __repr__(self):
s = "center (x,y): ("+str(self.x)+" "+str(self.y) + ")\n" + "visited: "+str(self.visited)+"\n"
s += "-----neighbor information------\n"
s += "empty neighbors: " + str(self.empty_neighbors()) + "\n"
for n in self.neighbors.keys():
if self.neighbors[n] is not None:
s+= n +"'s empty neighbors: " + str(self.neighbors[n].empty_neighbors()) + "\n"
s +="\n------------------------------\n"
return s
def get_pos(self):
return (self.x,self.y)
#node should be a position tupple
def set_prev(self,node):
self.prev = node
def set_shape(self,shape):
self.shape = shape
def get_dim(self):
if self.shape is None:
print("need shape for this node")
return
return (self.shape.get_width(),self.shape.get_height())
#input: rp: string, relative position | n: neighboring node
def assign_neighbor(self,rp,n):
assert(n != None)
assert(rp in self.neighbors.keys())
self.neighbors[rp] = n
#return all neighbors unexplored
def empty_neighbors(self):
unexplored = []
for k in self.neighbors.keys():
if self.neighbors[k] is None:
unexplored.append(k)
return unexplored
#return all neighbors that are explored
def neighbors(self):
explored = []
for k in self.neighbors.keys():
if self.neighbors[k]:
explored.append((k,self.neighbor(k)))
return explored
def neighbors_pos(self):
explored = []
for k,node in self.neighbors.items():
if node:
explored.append(node.get_pos())
return explored