-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontinuousUtil.py
More file actions
284 lines (213 loc) · 8.99 KB
/
continuousUtil.py
File metadata and controls
284 lines (213 loc) · 8.99 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
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
import numpy as np
import matplotlib.pyplot as plt
import copy
import networkx as nx
import networkx.algorithms.isomorphism as iso
from sklearn.neighbors import KDTree
import json
import csv
import time
import os
import sys
sys.setrecursionlimit(600000)
class ContinuousTask:
def __init__(self, tid, rid, start, goal, time) -> None:
self.taskID = tid
self.robotID = rid
self.startPos = np.array(start)
self.goalPos = np.array(goal)
self.time = time
def __repr__(self) -> str:
asd = "\n"
for i in self.__dir__():
if not i.startswith('__'):
asd+=i
asd+=": "
asd+=str(getattr(self,i))
asd+=", "
return asd
class ContinuousExecutionGraph:
def __init__(self, positions = None) -> None:
assert positions is not None
self.graph = nx.DiGraph()
self.taskList = dict()
self.robotList = []
self.THRESH = 1e8
for i in range(positions.shape[1]):
for r in range(positions.shape[0]):
for r_ in range(r+1, positions.shape[0]):
self.THRESH = min(self.THRESH, self.getDistance(positions[r,i], positions[r_,i]))
def checkCollision(self, a, b):
return self.getDistance(a,b)<self.THRESH
def getDistance(self, a,b):
a = np.array(a)
b = np.array(b)
if(np.array_equal([-2,-2], a) or np.array_equal([-2,-2], b)):
return 1e8
return np.linalg.norm(a-b, 2)
def fileWrite(self, path):
# print()
nx.write_adjlist(self.graph,path+"/"+(type(self)).__name__+"_Graph.txt")
with open(path+"/"+(type(self)).__name__+"_TaskList.txt", "w") as f:
for i in self.taskList:
f.write(self.taskList[i].__repr__())
class OriginalADG(ContinuousExecutionGraph):
def __init__(self, allPositions) -> None:
super().__init__(allPositions)
## Create Graph Skeleton and type1 dependencies
numRobots = allPositions.shape[0]
tId = 1
for rid in range(numRobots):
prevTask = None
for i, task in enumerate(allPositions[rid, :-1]):
t = ContinuousTask(tId, rid, task, allPositions[rid,i+1], i)
# print(t)
self.taskList[tId] = t
tId+=1
self.graph.add_node(t.taskID)
if(prevTask is None):
self.robotList.append(t)
else:
self.graph.add_edge(prevTask.taskID, t.taskID)
prevTask = t
## Create type2 dependencies
for rid in range(numRobots):
firstTid = self.robotList[rid].taskID
for taskID in range(firstTid, firstTid+allPositions.shape[1]-1):
task = self.taskList[taskID]
if(task.startPos[0]==-2 and task.startPos[1]==-2):
break
for rid_ in range(numRobots):
if(rid != rid_):
# print(rid, rid_)
firstTid_ = self.robotList[rid_].taskID
for taskID_ in range(firstTid_, firstTid_+allPositions.shape[1]-1):
task_ = self.taskList[taskID_]
# print(task.startPos, task_.goalPos)
if self.checkCollision(task.startPos, task_.goalPos) and task.time<=task_.time:
self.graph.add_edge(task.taskID, task_.taskID)
break
class SAGE(ContinuousExecutionGraph):
def __init__(self, allPositions=None) -> None:
super().__init__(allPositions)
numRobots = allPositions.shape[0]
tId = 1
positions = []
for rid in range(numRobots):
prevTask = None
for i, task in enumerate(allPositions[rid,:-1]):
t = ContinuousTask(tId, rid, task, allPositions[rid,i+1], i)
self.taskList[tId] = t
tId+=1
self.graph.add_node(t.taskID)
positions.append(t.goalPos)
if(prevTask is None):
self.robotList.append(t)
else:
self.graph.add_edge(prevTask.taskID, t.taskID)
prevTask = t
tree = KDTree(positions)
taskQueue = [i.taskID for i in self.robotList]
while len(taskQueue)!=0:
tID = taskQueue.pop(0)
t = self.taskList[tID]
if t.startPos[0]==-2 and t.startPos[1]==-2:
continue
if(tID+1) in self.taskList and self.taskList[tID+1].time!=0:
taskQueue.append(tID+1)
possibeDependencies = tree.query_radius([t.startPos], r=self.THRESH)[0]
possibeDependencies = sorted(possibeDependencies)
dependentRobots = [t.robotID]
for tID__ in possibeDependencies:
tID_ = tID__+1
t_ = self.taskList[tID_]
if(t_.robotID not in dependentRobots and t.time<=t_.time):
if(self.checkCollision(t.startPos, t_.goalPos)):
self.graph.add_edge(tID, tID_)
dependentRobots.append(t_.robotID)
class MAGE(SAGE):
def __init__(self, allPositions=None, filename="temp.dat") -> None:
super().__init__(allPositions)
if((len(self.taskList)+2)>50000):
assert filename is not None
dp = np.memmap(filename, dtype='bool', mode='w+', shape=(len(self.taskList)+2, len(self.taskList)+2))
else:
dp = np.zeros((len(self.taskList)+2, len(self.taskList)+2), dtype=bool)
for t in self.robotList:
self.reduceGraph(t.taskID, dp)
def reduceGraph(self, root, dp):
if(dp[root][root])==0:
dp[root][root]=1
children = [i[1] for i in self.graph.out_edges(root)]
if(root+1) in children:
dp[root] = np.logical_or(self.reduceGraph(root+1, dp), dp[root])
children.remove(root+1)
children = sorted(children, key=lambda x: self.taskList[x].time)
while(len(children)!=0):
if(dp[root][children[0]]==1):
self.graph.remove_edge(root, children[0])
else:
dp[root] = np.logical_or(self.reduceGraph(children[0], dp), dp[root])
children.pop(0)
return dp[root]
class Multi_KDTree_SAGE(ContinuousExecutionGraph):
def __init__(self, allPositions=None) -> None:
super().__init__(allPositions)
numRobots = allPositions.shape[0]
tId = 1
trees = []
positions = []
for rid in range(numRobots):
prevTask = None
for i, task in enumerate(allPositions[rid,:-1]):
t = ContinuousTask(tId, rid, task, allPositions[rid,i+1], i)
self.taskList[tId] = t
tId+=1
self.graph.add_node(t.taskID)
positions.append(t.goalPos)
if(prevTask is None):
self.robotList.append(t)
else:
self.graph.add_edge(prevTask.taskID, t.taskID)
prevTask = t
trees.append(KDTree(positions,leaf_size=4))
positions = []
taskQueue = [i.taskID for i in self.robotList]
while len(taskQueue)!=0:
tID = taskQueue.pop(0)
t = self.taskList[tID]
if t.startPos[0]==-2 and t.startPos[1]==-2:
continue
if(tID+1) in self.taskList and self.taskList[tID+1].time!=0:
taskQueue.append(tID+1)
for rID_ in range(numRobots):
if rID_==t.robotID:
continue
possibeDependencies = trees[rID_].query_radius([t.startPos], r=self.THRESH)[0]
possibeDependencies = sorted(possibeDependencies)
for i in possibeDependencies:
if i>=t.time:
tID_ = self.robotList[rID_].taskID+i
self.graph.add_edge(tID, tID_)
break
#Helper Functions
def testTime(method, allPos, fname="temp.dat"):
start = time.time()
if(method is MAGE):
exGraph = method(allPos, fname)
else:
exGraph = method(allPos)
end = time.time()
return len(exGraph.graph.edges)-len(allPos[0])*(len(exGraph.robotList)-1), end-start
def jsonToNpy(data, NUM_AGENTS):
temp = []
for i in range(NUM_AGENTS):
temp_ = []
for j in range(len(data['agent'+str(i)])):
temp_.append(data['agent'+str(i)][j]["position"])
temp.append(temp_)
positions = np.full((len(temp), max([len(i) for i in temp]),2), -2.0)
for idx, _ in np.ndenumerate(positions):
if(idx[1]<len(temp[idx[0]])):
positions[idx] = temp[idx[0]][idx[1]][idx[2]]
return positions