-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulate.py
99 lines (81 loc) · 3.52 KB
/
simulate.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
import random
from router import router
from threading import Thread
def get_initial_weight():
return random.randint(1, 100)
def routers_input_weights(pick_neighbor_prob, UDP_port, TCP_port, network_size, dont_change_weight_prob, max_update_rounds):
total_weight = 0
for router in range(1, network_size + 1):
router_UDP_port = UDP_port + router - 1
router_TCP_port = TCP_port + router - 1
neighbor = (router % network_size) + 1
neighbor_UDP_port = UDP_port + neighbor - 1
neighbor_TCP_port = TCP_port + neighbor - 1
input_file_name = 'input_router_' + str(router)+'.txt'
weights_file_name = 'weights_router_' + str(router)+'.txt'
input_file = open(input_file_name, 'w')
input_file.write(str(router_UDP_port)+'\n')
input_file.write(str(router_TCP_port)+'\n')
input_file.write(str(network_size)+'\n')
input_file.write(str(neighbor)+'\n')
input_file.write('127.0.0.1\n')
input_file.write(str(neighbor_UDP_port)+'\n')
input_file.write(str(neighbor_TCP_port)+'\n')
weight = get_initial_weight()
total_weight = total_weight + weight
input_file.write(str(weight)+'\n')
neighbors_count = 1
for new_neighbor in range(1, network_size + 1):
if new_neighbor == router or new_neighbor == neighbor:
continue
prob = random.random()
if prob < pick_neighbor_prob:
neighbors_count = neighbors_count + 1
new_neighbor_UDP_port = UDP_port + new_neighbor - 1
new_neighbor_TCP_port = TCP_port + new_neighbor - 1
input_file.write(str(new_neighbor)+'\n')
input_file.write('127.0.0.1\n')
input_file.write(str(new_neighbor_UDP_port)+'\n')
input_file.write(str(new_neighbor_TCP_port)+'\n')
weight = get_initial_weight()
total_weight = total_weight + weight
input_file.write(str(weight)+'\n')
input_file.close()
weights_file = open(weights_file_name,'w')
for i in range(0, max_update_rounds):
for j in range(0, neighbors_count):
prob = random.random()
if prob < dont_change_weight_prob:
weight_prob = random.randint(1, 100)
weights_file.write(str(weight_prob) + '\n')
else:
weights_file.write('None\n')
weights_file.close()
for router in range(1, network_size + 1):
file_name = 'input_router_' + str(router)+'.txt'
file = open(file_name, 'a')
file.write('*\n')
file.write(str(total_weight) + '\n')
file.close()
# Constructs and simulates a network.
def main():
random.seed(53689)
pick_neighbor_prob = 0.1
UDP_port_start = 31000
TCP_port_start = 41000
dont_change_weight_prob = 0.3
max_update_rounds = 100
try:
network_size = int(input('Enter network size \n'))
except:
print ('ERROR--network size')
routers_input_weights(pick_neighbor_prob, UDP_port_start, TCP_port_start, network_size, dont_change_weight_prob, max_update_rounds)
threads = []
for i in range(1, network_size+1):
threads.append(Thread(target=router, args=(i,)))
for thread in threads:
thread.start()
for thread in threads:
thread.join()
if __name__ == '__main__':
main()