-
Notifications
You must be signed in to change notification settings - Fork 2
/
aloha.py
67 lines (48 loc) · 1.39 KB
/
aloha.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
import random
import matplotlib.pyplot as plt
TSLOTS = 100000
class classNode:
def __init__(self, ttl):
self.ttl = ttl # slots left until retransmission
def tick(self):
self.ttl = self.ttl - 1
def main():
random.seed()
for window_size in [8, 16, 32]:
Nlist = []
selist = []
print "Window size: {0:2d}".format(window_size)
for N in range(1, 33):
snode = [ classNode(random.randrange(0, window_size)) for _ in range(N) ]
successful_slots = 0
slot_efficiency = 0
for slot in range(TSLOTS):
transmitted_nodes = []
for i in range(N):
if not snode[i].ttl:
transmitted_nodes.append(i)
snode[i].ttl = random.randrange(0, window_size)
else:
snode[i].tick()
if not transmitted_nodes:
pass
if (len(transmitted_nodes) == 1):
successful_slots = successful_slots + 1
if (len(transmitted_nodes) > 2):
for j in transmitted_nodes:
snode[j].ttl = random.randrange(0, window_size)
slot_efficiency = (successful_slots/float(TSLOTS))
print "N = {0:2d}: {1:f}".format(N, slot_efficiency)
Nlist.append(N)
selist.append(slot_efficiency)
plt.plot(Nlist, selist)
print ""
plt.xlabel("# of Nodes")
plt.ylabel("Slot Efficiency")
plt.legend(['W = 8', 'W = 16', 'W = 32'], loc='upper right')
plt.axis([0, 32, 0, 1])
plt.grid(linestyle='--')
plt.show()
return
if __name__ == "__main__":
main()