-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbase.py
More file actions
440 lines (377 loc) · 15.6 KB
/
base.py
File metadata and controls
440 lines (377 loc) · 15.6 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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
from random import Random
import matplotlib.pyplot as plt
import matplotlib.lines as mlines
import matplotlib.animation as animation
import numpy as np
from help import *
class Car:
def __init__(self, index, seed, source_pos, targets=None):
assert 0 <= index
self.index = index
self.when = 0 if index == 0 else -1
self.rand = Random(f"{seed}+{self.index}")
if self.index == 0:
pos = source_pos
else:
while True:
x_pos = self.rand.uniform(0, X_MAX) # !
y_pos = self.rand.uniform(0, Y_MAX)
if x_pos != source_pos[0] or y_pos != source_pos[1]:
pos = (x_pos, y_pos)
break
self.courses = [pos]
self.targets = [pos]
if targets is not None: self.targets.extend(targets)
self.target_idx = 1
def set_target(self):
assert False, "not implemented"
def move(self):
assert False, "not implemented"
def get_pos(self):
cx = self.courses[-1][0]
cy = self.courses[-1][1]
return cx, cy
def get_target(self):
self.set_target()
tx = self.targets[self.target_idx][0]
ty = self.targets[self.target_idx][1]
return tx, ty
def get_prev_target(self):
px = self.targets[self.target_idx - 1][0]
py = self.targets[self.target_idx - 1][1]
return px, py
def truncate(self):
self.set_target()
self.courses = self.courses[-1:]
self.targets = self.targets[-2:]
self.target_idx = 1
class SynCar(Car):
def move(self):
step = 1
while step > 0:
# if the car sees a repeated target
# it won't move but stays at the current pos
px, py = self.get_prev_target()
tx, ty = self.get_target()
if tx == px and ty == py:
self.courses.append((tx, ty))
return
cx, cy = self.get_pos()
dist = get_dist(cx, cy, tx, ty)
if step >= dist:
self.courses.append((tx, ty))
self.target_idx += 1
step -= dist
else:
dx = (tx - cx) * step / dist
dy = (ty - cy) * step / dist
cx = cx + dx
cy = cy + dy
self.courses.append((cx, cy))
return
class SynMGCar(Car):
def __init__(self, index, seed, source_pos, targets=None, type=1):
super().__init__(index, seed, source_pos, targets)
if self.index == 0:
pos = source_pos
else:
while True:
if type == 1:
x_pos = self.rand.choice([i for i in range(0, X_MAX + 1)])
y_pos = self.rand.choice([i for i in range(0, Y_MAX + 1)])
else:
x_pos = self.rand.choice([i for i in range(0, X_MAX)])
y_pos = self.rand.choice([i for i in range(0, Y_MAX)])
if x_pos != source_pos[0] or y_pos != source_pos[1]:
pos = (x_pos, y_pos)
break
self.courses = [pos]
self.targets = [pos]
if targets is not None: self.targets.extend(targets)
def move(self):
px, py = self.get_prev_target()
tx, ty = self.get_target()
if px != tx or py != ty:
self.target_idx += 1
self.courses.append((tx, ty))
class Simulation:
def __init__(self):
self.cars = []
self.num_of_broadcasters = []
self.neighbor_percentage = []
def cars_move(self):
[car.move() for car in self.cars]
def propagate(self, rd):
assert False, "not implemented"
def calculate_num_of_broadcasters(self):
num = 0
for car in self.cars:
if car.when >= 0:
num += 1
self.num_of_broadcasters.append(num)
def calculate_neighbor_percentage(self):
assert False, "not implemented"
def simulate(self):
for _ in range(PRE_RUN_COUNT):
for car in self.cars[1:]:
car.move()
for car in self.cars[1:]:
car.truncate()
self.calculate_num_of_broadcasters()
# self.calculate_neighbor_percentage() ###
rd = 1
while self.num_of_broadcasters[-1] != NUM_OF_CARS:
self.cars_move()
self.propagate(rd)
self.calculate_num_of_broadcasters()
# self.calculate_neighbor_percentage() ###
if not EXCEED_MOVES and rd == NUM_OF_MOVES:
break
rd += 1
def summary(self):
courses = []
targets = []
for car in self.cars:
courses.append(car.courses)
targets.append(car.targets)
return courses, targets, self.num_of_broadcasters, self.neighbor_percentage
class SynSimulation(Simulation):
def propagate(self, rd):
broadcaster_pos_list = [car.get_pos() for car in self.cars if car.when >= 0]
for car in self.cars:
if car.when == -1:
for pos in broadcaster_pos_list:
dist = get_dist(*pos, *car.get_pos())
if dist <= 1:
car.when = rd
break
def calculate_neighbor_percentage(self):
rates = []
for car in self.cars:
num_of_nbrs = -1
for c in self.cars:
if get_dist(*c.get_pos(), *car.get_pos()) <= 1:
num_of_nbrs += 1
rate = num_of_nbrs / NUM_OF_CARS
rates.append(rate)
self.neighbor_percentage.append((sum(rates) / NUM_OF_CARS))
class TorSynSimulation(Simulation):
def propagate(self, rd):
broadcaster_pos_list = [car.get_pos() for car in self.cars if car.when >= 0]
mod_pos_list = list(map(lambda p: (p[0] % X_MAX, p[1] % Y_MAX), broadcaster_pos_list))
for car in self.cars:
if car.when == -1:
car_x, car_y = car.get_pos()
car_x, car_y = car_x % X_MAX, car_y % Y_MAX
for pos in mod_pos_list:
dist = get_euclidean_dist(*pos, car_x, car_y)
if dist <= 1:
car.when = rd
break
def calculate_neighbor_percentage(self):
original_positions = [car.get_pos() for car in self.cars]
mod_positions = list(map(lambda pos: (pos[0] % X_MAX, pos[1] % Y_MAX), original_positions))
rates = []
for pos1 in mod_positions:
num_of_nbrs = -1 # minus itself
for pos2 in mod_positions:
if get_euclidean_dist(*pos1, *pos2) <= 1:
num_of_nbrs += 1
rate = num_of_nbrs / NUM_OF_CARS
rates.append(rate)
self.neighbor_percentage.append((sum(rates) / NUM_OF_CARS))
class GUI:
def show(self):
plt.show()
plt.clf()
plt.close()
def save(self, name):
plt.tight_layout()
plt.savefig(f"{name}.png")
plt.clf()
plt.close()
class GUIFinalPos(GUI):
def __init__(self, sim: Simulation, mod, solo):
self.sim = sim
self.mod = mod
self.solo = solo
if solo:
self.fig = plt.figure(figsize=fig_size)
else:
self.fig = plt.figure(figsize=(fig_size[0] * 2, fig_size[1]))
self.ax1 = self.fig.add_subplot(121, xlim=[0, X_MAX], ylim=[0, Y_MAX])
self.ax1.set_xticks(np.arange(0, X_MAX + 1, 5))
self.ax1.set_yticks(np.arange(0, Y_MAX + 1, 5))
def draw(self):
# draw all final positions:
for car in self.sim.cars:
fx, fy = car.courses[-1]
if self.mod:
self.ax1.plot(fx % X_MAX, fy % X_MAX, "go", markersize=2)
else:
self.ax1.plot(fx, fy, "go", markersize=2)
# for RD, to validate target positions
# for car in self.sim.cars:
# self.ax1.plot(*unzip(car.targets[1:], False), "ro", markersize=2)
source_courses = self.sim.cars[0].courses
self.ax1.plot(*unzip(source_courses, self.mod), "bo", markersize=2)
source_targets = self.sim.cars[0].targets
self.ax1.plot(*unzip(source_targets, self.mod), "ro", markersize=4)
self.ax1.set_xlabel("x axis", fontdict={"size": 12})
self.ax1.set_ylabel("y axis", fontdict={"size": 12})
self.ax1.set_title("final positions and the source's trace")
blue_line = mlines.Line2D([], [], color='blue', marker='o', markersize=8, label="source's trace")
red_line = mlines.Line2D([], [], color='red', marker='o', markersize=8, label="source's targets")
green_line = mlines.Line2D([], [], color='green', marker='o', markersize=8, label="peers' final positions")
self.ax1.legend(handles=[blue_line, red_line, green_line], loc='upper left')
self.ax1.grid(True)
class GUIHeatMap(GUIFinalPos):
def __init__(self, sim: Simulation, mod, solo):
super().__init__(sim, mod, solo)
if not solo:
self.ax3 = self.fig.add_subplot(122)
else:
self.ax3 = self.fig.add_subplot(111)
self.ax3.set_xticks(np.arange(0, X_MAX + 1, 5))
self.ax3.set_yticks(np.arange(0, Y_MAX + 1, 5))
def draw(self):
if not self.solo:
super().draw()
hot_map = [[0 for _ in range(X_MAX + 1)] for _ in range(Y_MAX + 1)]
for car in self.sim.cars[1:]:
for target in car.courses:
if self.mod:
int_target_x = int(target[0]) % X_MAX
int_target_y = int(target[1]) % Y_MAX
else:
int_target_x = int(target[0])
int_target_y = int(target[1])
hot_map[int_target_y][int_target_x] += 1
# hot_map = list(reversed(hot_map))
im = self.ax3.imshow(hot_map)
cbar = self.fig.colorbar(im, ax=self.ax3)
cbar.ax.set_ylabel("frequencies", rotation=-90, va="bottom", fontsize=20)
self.ax3.set_title("the heat map of all cars' paths", fontsize=20)
class GUINumBro(GUIFinalPos):
def __init__(self, sim: Simulation, mod, solo):
super().__init__(sim, mod, solo)
x_max = NUM_OF_MOVES if len(self.sim.num_of_broadcasters) <= NUM_OF_MOVES else len(self.sim.num_of_broadcasters)
if not solo:
self.ax3 = self.fig.add_subplot(122, xlim=[x_max - 500, x_max], ylim=[0, NUM_OF_CARS])
else:
self.ax3 = self.fig.add_subplot(111, xlim=[x_max - 500, x_max], ylim=[0, NUM_OF_CARS])
def draw(self):
if not self.solo:
super().draw()
xs = [i for i in range(len(self.sim.num_of_broadcasters))]
self.ax3.plot(xs, self.sim.num_of_broadcasters, marker='o', markersize=3)
self.ax3.set_xlabel("rounds", fontdict={"size": 12})
self.ax3.set_ylabel("# of msg receivers", fontdict={"size": 12})
self.ax3.set_title("rounds vs. # of msg receivers")
self.ax3.grid(True)
class GUINumNei(GUIFinalPos):
def __init__(self, sim: Simulation, mod, solo):
super().__init__(sim, mod, solo)
if not solo:
self.ax2 = self.fig.add_subplot(122, xlim=[0, NUM_OF_MOVES], ylim=[0, 0.002])
else:
self.ax2 = self.fig.add_subplot(111, xlim=[0, NUM_OF_MOVES], ylim=[0, 0.002])
def draw(self):
if not self.solo:
super().draw()
xs = [i for i in range(len(self.sim.neighbor_percentage))]
self.ax2.plot(xs, self.sim.neighbor_percentage, marker='o')
self.ax2.grid(True)
class GUISnapshot(GUI):
def __init__(self, sim: Simulation, count=6, interval=10):
assert count in [6, 12, 150]
assert interval > 0
self.sim = sim
self.axs = []
self.interval = interval
if count == 6:
self.fig = plt.figure(figsize=(fig_size[0] * 4, fig_size[1] * 2))
for i in range(6):
axi = self.fig.add_subplot(2, 3, i + 1, xlim=[0, X_MAX], ylim=[0, Y_MAX])
self.axs.append(axi)
elif count == 12:
self.fig = plt.figure(figsize=(fig_size[0] * 3, fig_size[1] * 4))
for i in range(12):
axi = self.fig.add_subplot(4, 3, i + 1, xlim=[0, X_MAX], ylim=[0, Y_MAX])
self.axs.append(axi)
else:
self.fig = plt.figure(figsize=(fig_size[0] * 10, fig_size[1] * 15))
for i in range(150):
axi = self.fig.add_subplot(15, 10, i + 1, xlim=[0, X_MAX], ylim=[0, Y_MAX])
self.axs.append(axi)
for axi in self.axs:
axi.set_xticks(np.arange(0, X_MAX + 1, 5))
axi.set_yticks(np.arange(0, Y_MAX + 1, 5))
def draw(self):
for i, axi in enumerate(self.axs):
target_length = i * self.interval
axi.set_title(f"at round {i * self.interval}")
source_acc_length = 0
for j, pos in enumerate(self.sim.cars[0].courses[1:]):
source_acc_length += get_dist(*pos, *self.sim.cars[0].courses[j])
if source_acc_length >= target_length:
source_courses = self.sim.cars[0].courses[:j + 1]
break
else:
raise Exception
xys = list(zip(*source_courses))
xs = list(map(lambda x: x % X_MAX, list(xys[0])))
ys = list(map(lambda y: y % Y_MAX, list(xys[1])))
self.axs[i].plot(xs, ys, "bo", markersize=2)
self.axs[i].grid(True)
for car in self.sim.cars:
acc_length = 0
for j, pos in enumerate(car.courses[1:]):
acc_length += get_dist(*pos, *car.courses[j])
if acc_length >= target_length:
x = pos[0] % X_MAX
y = pos[1] % Y_MAX
if car.when <= i * self.interval:
self.axs[i].plot(x, y, "go", markersize=4)
else:
self.axs[i].plot(x, y, "ro", markersize=4)
break
class GUISnapshot2(GUI):
def __init__(self, sim: Simulation, rd=1):
self.sim = sim
self.i = rd
self.fig = plt.figure(figsize=fig_size)
self.axi = self.fig.add_subplot(1, 1, 1, xlim=[0, X_MAX], ylim=[0, Y_MAX])
self.axi.set_xticks(np.arange(0, X_MAX + 1, 5))
self.axi.set_yticks(np.arange(0, Y_MAX + 1, 5))
def draw(self):
i = self.i
target_length = i
self.axi.set_title(f"at round {i}")
source_acc_length = 0
for j, pos in enumerate(self.sim.cars[0].courses[1:]):
source_acc_length += get_dist(*pos, *self.sim.cars[0].courses[j])
if source_acc_length >= target_length:
source_courses = self.sim.cars[0].courses[:j + 1]
break
else:
raise Exception
xys = list(zip(*source_courses))
xs = list(map(lambda x: x % X_MAX, list(xys[0])))
ys = list(map(lambda y: y % Y_MAX, list(xys[1])))
self.axi.plot(xs, ys, "bo", markersize=4)
self.axi.grid(True)
for car in self.sim.cars:
acc_length = 0
for j, pos in enumerate(car.courses[1:]):
acc_length += get_dist(*pos, *car.courses[j])
if acc_length >= target_length:
x = pos[0] % X_MAX
y = pos[1] % Y_MAX
if car.when <= i:
self.axi.plot(x, y, "go", markersize=8)
else:
self.axi.plot(x, y, "ro", markersize=8)
break
if __name__ == '__main__':
pass