-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.py
More file actions
195 lines (166 loc) · 6.27 KB
/
Graph.py
File metadata and controls
195 lines (166 loc) · 6.27 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
import numpy as np
from utils import *
from matplotlib import pyplot as plt
import networkx as nx
from Map import Map
class Node:
def __init__(self, node_id, coord, startIndex=None):
self.node_id = node_id
self.coord = np.asarray(coord)
self.loc = self.coord.mean(axis=0)
self.startIndex = startIndex
self.parent = None
self.children = []
def addChild(self, child):
assert isinstance(child, Node)
child.parent = self
self.children.append(child)
def __str__(self):
s = ""
s += f"<Node: {self.node_id}>"
return s
def __repr__(self):
return self.__str__()
def isStartNode(self):
return True if self.startIndex is not None else False
def get_level(self):
level = 0
p = self.parent
while p:
level += 1
p = p.parent
return level
class Graph:
def __init__(self, edges):
self.Graph = nx.Graph()
self.Graph.add_edges_from(edges)
self.MST = self.getMST()
def draw(self, filename="map"):
plt.figure()
nx.draw(self.Graph, with_labels=True)
plt.savefig(f"Graph_{filename}.png", format="png")
plt.figure()
nx.draw(self.MST, with_labels=True)
plt.savefig(f"Minimum_Spanning_Tree_{filename}.png", format="png")
def getMST(self):
# Obtaining minimum spanning tree from graph
return nx.minimum_spanning_tree(self.Graph)
class Tree:
R = np.array([[0, 1], [-1, 0]])
def __init__(self, mst, map):
assert isinstance(mst, nx.Graph)
assert isinstance(map, Map)
self.map = map
self.graph = mst
self.root = None
self.getTree()
def getEdges(self):
return list(self.graph.edges())
def getEdgeOf(self, node):
return list(self.graph.edges(node))
def getNodes(self):
return list(self.graph.nodes())
def getTree(self):
self.root = self.getStartNode()
self.addChild(self.root)
def addChild(self, node):
for _, child in self.getEdgeOf(node):
if child == node.parent:
continue
node.addChild(child)
self.addChild(child)
def getStartNode(self):
startNode = [node for node in self.getNodes() if node.isStartNode()]
return startNode[0]
def print_tree(self, node):
prefix = f"{node.get_level()} "
spacer = ' ' * node.get_level()*3 + "|____" if node.parent else ""
statement = prefix + spacer + f"{node}"
print(statement)
for child in node.children:
self.print_tree(child)
def printTree(self):
self.print_tree(self.root)
def traversing(self):
route = [None]
path = []
self.routing(self.root, route)
route.append(None)
idx = np.asarray([route[1].startIndex])
for i in range(len(route)-2):
v, idx = self.nearestCoord([route[i], route[i+1], route[i+2]], idx)
path.append(v)
path = np.vstack(path)
return path, route
def routing(self, node, route):
# Generated the list of nodes to be traversed at each step
route.append(node)
for child in node.children:
self.routing(child, route)
route.append(node)
def setDirection(self, idx):
# special case when the previous or next nodes are None and to identify directions for clockwise movements
if idx == 0:
direction = np.array([0, 1])
elif idx == 1:
direction = np.array([1, 0])
elif idx == 3:
direction = np.array([0, -1])
else:
direction = np.array([-1, 0])
return direction
def nearestCoord(self, nodes, idx):
node1, node2, node3 = nodes
if node1 is None:
direction2 = 0.5*(node3.loc - node2.loc)
direction1 = self.setDirection(idx)
elif node3 is None:
direction1 = 0.5*(node2.loc - node1.loc)
direction2 = self.setDirection(idx)
else:
direction1 = 0.5*(node2.loc - node1.loc)
direction2 = 0.5*(node3.loc - node2.loc)
rot = (direction1[0]*direction2[1] - direction1[1]*direction2[0])
d = np.linalg.norm(direction1 + direction2)
indices = []
direction = direction1
if rot < 0: # clockwise movment
while len(idx) > 0:
current_coord = node2.coord[idx]
indices.append(idx)
idx = location(node2.coord, current_coord + direction)
if len(idx) == 0:
direction = direction2
idx = location(node2.coord, current_coord + direction)
elif rot > 0: # anti-clockwise movement
direction = direction2
while len(idx) > 0:
current_coord = node2.coord[idx]
indices.append(idx)
idx = location(node2.coord, current_coord + direction)
else: # rot == 0, straight
if d > 1:
while len(idx) > 0:
current_coord = node2.coord[idx]
indices.append(idx)
idx = location(node2.coord, current_coord + direction)
if len(idx) == 0:
direction = direction2
idx = location(node2.coord, current_coord + direction)
else: # U-turn for dead ends
temp_dir = [self.R.dot(direction1), direction2]
j = False
while len(idx) > 0:
current_coord = node2.coord[idx]
indices.append(idx)
idx = location(node2.coord, current_coord + direction)
if len(idx) == 0:
direction = temp_dir[j]
idx = location(node2.coord, current_coord + direction)
j = not j
direction = direction2
if node3 is not None:
idx2 = location(node3.coord, current_coord + direction)
else:
idx2 = None
return node2.coord[np.hstack(indices), :], idx2