-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathanimate.py
82 lines (66 loc) · 2.52 KB
/
animate.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
import numpy as np
import networkx as nx
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
def avg_bandwidth(peers):
bws = []
for peer in peers:
for c in peer.connections.values():
bws.append(c.bandwidth)
return sum(bws)/len(bws)
def median_bandwidth(peers):
bws = []
for peer in peers:
for c in peer.connections.values():
bws.append(c.bandwidth)
bws.sort()
return bws[len(bws)/2]
def max_peers(peers):
return max(len(p.connections) for p in peers)
def min_peers(peers):
return min(len(p.connections) for p in peers)
class Visualizer(object):
def __init__(self, env, peers):
self.env = env
self.peers = peers
fig = plt.figure(figsize=(8, 8))
# interval: draws a new frame every *interval* milliseconds
anim = FuncAnimation(fig, self.update, interval=50, blit=False)
plt.show()
def update_simulation(self):
self.env.run(self.env.now + 1)
def update(self, n):
# print 'update simulation'
self.update_simulation()
# print 'update visualization'
# create graph
G = nx.Graph()
for peer in self.peers:
G.add_node(peer, label=peer.name)
for peer in self.peers:
for other, cnx in peer.connections.items():
G.add_edge(peer, other, weight=cnx.bandwidth)
pos = nx.graphviz_layout(G)
#pos = nx.spring_layout(G)
plt.cla()
edges = nx.draw_networkx_edges(G, pos)
nodes = nx.draw_networkx_nodes(G, pos, node_size=20)
#labels = dict((p, p.name) for p in self.peers)
#nx.draw_networkx_nodes(G, pos, labels=labels, font_color='k')
plt.axis('off')
KBit = 1024 / 8
plt.text(0.5, 1.1, "time: %.2f" % self.env.now,
horizontalalignment='left',
transform=plt.gca().transAxes)
plt.text(0.5, 1.07, "avg bandwidth = %d KBit" % (avg_bandwidth(self.peers)/KBit),
horizontalalignment='left',
transform=plt.gca().transAxes)
plt.text(0.5, 1.04, "median bandwidth = %d KBit" % (median_bandwidth(self.peers)/KBit),
horizontalalignment='left',
transform=plt.gca().transAxes)
plt.text(0.5, 1.01, "min/max connections %d/%d" % (min_peers(self.peers), max_peers(self.peers)),
horizontalalignment='left',
transform=plt.gca().transAxes)
#nx.draw_networkx_labels(G, pos, labels)
return nodes,