-
Notifications
You must be signed in to change notification settings - Fork 0
/
gridGraph.py
363 lines (303 loc) · 12.7 KB
/
gridGraph.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
from fractions import Fraction as frac
import random
import itertools
import bisect
import pickle
CLOCKWISE = 0
COUNTERCLOCKWISE = 1
def getDiamondCenters(n, m):
G = {}
deltas = [[0, 2], [2, 0], [0, -2], [-2, 0]]
for x in range(1, 2*n+1, 2):
for y in range(1, 2*m+1, 2):
for dx, dy in deltas:
if 1 <= x+dx <= 2*n and 1 <= y+dy <= 2*m:
G.setdefault((x, y), []).append((x+dx, y+dy))
G.setdefault((x+dx, y+dy), []).append((x, y))
for k in G:
G[k] = list(set(G[k]))
return G
def getDiamondGraph(n, m):
centers = getDiamondCenters(n, m)
G = {}
associatedCircles = {}
cycle = [[0, 1], [1, 0], [0, -1], [-1, 0]]
for x, y in centers.keys():
direction = CLOCKWISE
if ((x-1)/2) % 2 == ((y-1)/2) % 2:
aux = cycle # CW oriented
else:
aux = cycle[::-1] # CCW oriented
direction = COUNTERCLOCKWISE
for i in range(4):
dux, duy = aux[i]
dvx, dvy = aux[(i+1) % 4]
G.setdefault((x+dux, y+duy), []).append((x+dvx, y+dvy))
associatedCircles[((x+dux, y+duy), (x+dvx, y+dvy))] = [(x, y), direction]
return G, associatedCircles
def dfs(G, v, pathLength=4):
paths = []
def DFS(u, currentPath):
for w in G[u]:
currentPath.append(w)
if len(currentPath) < pathLength:
DFS(w, currentPath)
else:
paths.append([v]+currentPath[:])
currentPath.pop()
DFS(v, [])
return paths
def getPathProbability(G, path):
p = frac(1, 1)
for i in range(len(path)-1):
p *= frac(1, len(G[path[i]]))
return p
def getDronesGridGraph(n, m):
'''Returns a triple G, w, paths, where G is the graph obtained in the 2pi model,
w is a dictionary mapping each edge in G to its probability, and paths id a dictionary
mapping each edge in G to the path in the circles model'''
diamondGraph, associatedCircle = getDiamondGraph(n, m)
G = {}
w = {}
paths = {}
circles = set(tuple(x) for x in associatedCircle.values())
# sync, edge = None, None
for center, orientation in circles:
sync, u = None, None
if (center[1]+1) % 4 == 2: # Sinchronization point below
sync = (center[0], center[1]-1)
if orientation == COUNTERCLOCKWISE:
u = (sync[0]+1, sync[1]+1)
else: # orientation == CLOCKWISE
u = (sync[0]-1, sync[1]+1)
else: # Synchronization point above
sync = (center[0], center[1]+1)
if orientation == COUNTERCLOCKWISE:
u = ((sync[0]-1, sync[1]-1))
else: # orientation == CLOCKWISE
u = ((sync[0]+1, sync[1]-1))
syncPointpaths = dfs(diamondGraph, u, 4)
syncPointProb = frac(0, 1)
for path in syncPointpaths:
G.setdefault((sync, u), []).append((path[-2], path[-1]))
p = getPathProbability(diamondGraph, path)
syncPointProb += p
w[((sync, u), (path[-2], path[-1]))] = p
paths[((sync, u), (path[-2], path[-1]))] = [sync]+path
# assert float(syncPointProb) == 1.0
return G, w, paths
def randomWalk(n, m, k, osteps):
steps = osteps
diamondGraph, circles = getDiamondGraph(n, m)
G, w, paths = getDronesGridGraph(n, m)
edges = [(v, u) for v in diamondGraph for u in diamondGraph[v]]
# print("edges", len(edges))
assert len(edges) == n*m*4
timeSinceLastCover = {(u, v): 0 for (u, v) in edges}
maxUncoveredTime = {(u, v): -1 for (u, v) in edges}
minUncoveredTime = {(u, v): float('inf') for (u, v) in edges}
avgUncoveredTime = {(u, v): 0 for (u, v) in edges}
totalUncoveredTime = {(u, v): 0 for (u, v) in edges}
timesVisited = {(u, v): 0 for (u, v) in edges}
timesCommunicated = {i: 0 for i in range(k)}
timeSinceLastCom = {i: 0 for i in range(k)}
maxTimeSinceLastCom = {i: -1 for i in range(k)}
minTimeSinceLastCom = {i: float('inf') for i in range(k)}
avgTimeSinceLastCom = {i: 0 for i in range(k)}
totalUncomTime = {i: 0 for i in range(k)}
currentPositions = random.sample(G.keys(), k=k)
while steps:
newPositions = [None for i in range(k)]
pathsTaken = [None for i in range(k)]
for i in range(k):
u = currentPositions[i]
# Python 3.5
choices = list(G[u])
weights = [w[(u, x)] for x in choices]
cumdist = list(itertools.accumulate(weights))
x = random.random() * cumdist[-1]
v = choices[bisect.bisect(cumdist, x)]
# Python 3.7:
# weights = [w[(u, x)] for x in G[u]]
# v = random.choices(G[u], weights=weights)[0]
newPositions[i] = v
pathsTaken[i] = paths[(u, v)]
traversedEdges = set()
# Edge cover
for path in pathsTaken:
for i in range(len(path)-2):
e = (path[i], path[i+1])
traversedEdges.add(e)
for e in edges:
if e not in traversedEdges:
timeSinceLastCover[e] += 1
else:
timesVisited[e] += 1
aux = timesVisited[e]
totalUncoveredTime[e] += timeSinceLastCover[e]
maxUncoveredTime[e] = max(maxUncoveredTime[e], timeSinceLastCover[e])
minUncoveredTime[e] = min(minUncoveredTime[e], timeSinceLastCover[e])
avgUncoveredTime[e] = avgUncoveredTime[e]*((aux-1)/aux)+timeSinceLastCover[e]/aux
timeSinceLastCover[e] = 0
# Communication
communicatingDrones = set()
for i in range(k):
for j in range(i+1, k):
if currentPositions[i] == currentPositions[j]:
if i not in communicatingDrones:
timesCommunicated[i] += 1
aux = timesCommunicated[i]
avgTimeSinceLastCom[i] = avgTimeSinceLastCom[i]*((aux-1)/aux)+timeSinceLastCom[i]/aux
if j not in communicatingDrones:
timesCommunicated[j] += 1
aux = timesCommunicated[j]
avgTimeSinceLastCom[j] = avgTimeSinceLastCom[j]*((aux-1)/aux)+timeSinceLastCom[j]/aux
communicatingDrones.add(i)
communicatingDrones.add(j)
totalUncomTime[i] += timeSinceLastCom[i]
totalUncomTime[j] += timeSinceLastCom[j]
maxTimeSinceLastCom[i] = max(maxTimeSinceLastCom[i], timeSinceLastCom[i])
minTimeSinceLastCom[i] = min(minTimeSinceLastCom[i], timeSinceLastCom[i])
maxTimeSinceLastCom[j] = max(maxTimeSinceLastCom[j], timeSinceLastCom[j])
minTimeSinceLastCom[j] = min(minTimeSinceLastCom[j], timeSinceLastCom[j])
timeSinceLastCom[i] = 0
timeSinceLastCom[j] = 0
for drone in set(range(k)).difference(communicatingDrones):
timeSinceLastCom[drone] += 1
currentPositions = newPositions
steps -= 1
for e in totalUncoveredTime:
totalUncoveredTime[e] += timeSinceLastCover[e]
nEdges = len(edges)
# print("Uncovered edges:", sum(1 for v in timesVisited.values() if v == 0))
averageUncovered = sum(totalUncoveredTime.values())/nEdges
# print("Average total time for uncovered edges:", averageUncovered)
averageMaxUncovered = sum(maxUncoveredTime.values())/nEdges
# print("Average max time for uncovered edges:", averageMaxUncovered)
averageMinUncovered = sum(minUncoveredTime.values())/nEdges
# print("Average min time for uncovered edges:", averageMinUncovered)
averageAverageUncovered = sum(avgUncoveredTime.values())/nEdges
# print("Average average time for uncovered edges:", averageAverageUncovered)
# print("Proportion of time:", averageUncovered/osteps)
# print("\nIsolated drones:", sum(1 for v in timesCommunicated.values() if v == 0))
averageUncom = sum(totalUncomTime.values())/k
# print("Average total time for isolated drones:", averageUncom)
averageMaxUncom = sum(maxTimeSinceLastCom.values())/k
# print("Average max time for isolated drones:", averageMaxUncom)
averageMinUncom = sum(minTimeSinceLastCom.values())/k
# print("Average min time for isolated drones:", averageMinUncom)
averageAverageUncom = sum(avgTimeSinceLastCom.values())/k
# print("Average average time for isolated drones:", averageAverageUncom)
# print("Proportion of time:", averageUncom/osteps)
# return (totalUncoveredTime, maxUncoveredTime, minUncoveredTime, avgUncoveredTime), (totalUncomTime, maxTimeSinceLastCom, minTimeSinceLastCom, avgTimeSinceLastCom), (timesVisited, timesCommunicated)
return (averageUncovered, averageMaxUncovered, averageMinUncovered, averageAverageUncovered), (averageUncom, averageMaxUncom, averageMinUncom, averageAverageUncom)
def getCols(d):
cols = {}
maxVal = max(d.values())
minVal = min(d.values())
for k, v in d.items():
col = -int((v-minVal)/(maxVal-minVal)*255)+255
col = hex(col).split('x')[-1]
while len(col) < 2:
col = "0"+col
cols[k] = "#ff"+col+col
return cols
def simulate(n, m, steps):
res = []
total = n*m
delta = int(n*m/100)
for k in range(1, total+1, delta):
res.append(randomWalk(n, m, k, steps))
return res
def simulateK(n, m, repetitions, steps):
totalSim = []
for i in range(repetitions):
print("repetition", i+1)
res = []
total = n*m
delta = int(n*m/100)
for k in range(1, total+1, delta):
print("Random Walk with", k, "drones")
res.append(randomWalk(n, m, k, steps))
totalSim.append(res)
avg = []
# return totalSim
for d in range(len(totalSim[0])):
auxCover = zip(*[res[d][0] for res in totalSim])
# print("auxCover", list(auxCover))
# print("zip", list(zip(*auxCover)))
avgCover = tuple(sum(x) / len(x) for x in auxCover)
auxCom = zip(*[res[d][1] for res in totalSim])
avgCom = tuple(sum(x) / len(x) for x in auxCom)
avg.append((avgCover, avgCom))
return totalSim, avg
def saveData(data, filename):
f = open(filename, "wb")
pickle.dump(data, f)
f.close()
print("Saved.")
def loadData(filename):
f = open(filename, "rb")
res = pickle.load(f)
f.close()
print("Loaded.")
return res
def broadcast(n, m, k):
diamondGraph, circles = getDiamondGraph(n, m)
G, w, paths = getDronesGridGraph(n, m)
edges = [(v, u) for v in diamondGraph for u in diamondGraph[v]]
# print("edges", len(edges))
assert len(edges) == n*m*4
drones = list(range(k))
source = random.choice(drones)
dronesCom = set()
dronesCom.add(source)
currentPositions = random.sample(G.keys(), k=k)
# print("Initial positions", currentPositions)
steps = 0
while len(dronesCom) < k:
# print("iteration", steps, "drones that know", len(dronesCom))
newPositions = [None for i in range(k)]
pathsTaken = [None for i in range(k)]
for i in range(k):
u = currentPositions[i]
# Python 3.5
choices = list(G[u])
weights = [w[(u, x)] for x in choices]
cumdist = list(itertools.accumulate(weights))
x = random.random() * cumdist[-1]
v = choices[bisect.bisect(cumdist, x)]
# Python 3.7:
# weights = [w[(u, x)] for x in G[u]]
# v = random.choices(G[u], weights=weights)[0]
newPositions[i] = v
pathsTaken[i] = paths[(u, v)]
edgePositions = {(v, u): [[], [], [], [], []] for v in diamondGraph for u in diamondGraph[v]}
for i in range(k):
path = pathsTaken[i]
# print("len path", len(path))
# print(path)
for j in range(len(path)-1):
e = (path[j], path[j+1])
edgePositions[e][j].append(i)
for i in range(5):
for e in edgePositions:
# print('e', e, edgePositions[e])
s = set(edgePositions[e][i])
if len(s.intersection(dronesCom)) > 0:
# print("meeting")
dronesCom.update(s)
currentPositions = newPositions
steps += 1
return steps
def simBroadcast(n, m, k):
avg = []
for d in range(2, 101):
print("Simulating with", d, "drones")
broadcastD = 0
for i in range(k):
print(" Simulation", i)
broadcastD += broadcast(n, m, d)
broadcastD /= k
avg.append(broadcastD)
return avg