-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulate.py
More file actions
119 lines (105 loc) · 4.13 KB
/
Copy pathSimulate.py
File metadata and controls
119 lines (105 loc) · 4.13 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
import random
import argparse
import sys
parser = argparse.ArgumentParser()
parser.add_argument('filepath', nargs=1)
parser.add_argument('-n', nargs=1, type=int, dest='nodes', default=20)
parser.add_argument('-i', nargs=1, type=int, dest='runtimes', default=1000)
parser.add_argument('--gossip', dest='gossip', action='store_true')
parser.add_argument('--my-gossip', dest='my_gossip', action='store_true')
parser.add_argument('--my-gossip100', dest='my_gossip100', action='store_true')
parser.add_argument('--my-gossip-split', dest='my_gossip_split', action='store_true')
varlist = parser.parse_args(sys.argv)
def start_algorithm(algorythm, current, times, nodes, runtimes, *args):
reslist = []
iterations = [0]
for i in range(runtimes):
list = [0 for i in range(nodes)]
if len(args) == 0:
algorythm(current, times, list, iterations)
else:
algorythm(current, times, list, iterations, args)
reslist.append(list.count(1) * 100 / nodes)
result = 0
for i in reslist:
result += i
result = result / len(reslist)
return "{0}% with {1} avg iterations({2} total)".format(
str(round(result, 2)),
str(iterations[0] / runtimes),
str(iterations[0]))
def gossip(current, times, list, iterations):
iterations[0] = iterations[0] + 1
if list[current] == 0:
list[current] = 1
for i in range(times):
gossip(random.randrange(0,(len(list))), times, list, iterations)
else:
return 1
def my_gossip(current, times, list, iterations):
iterations[0] = iterations[0] + 1
if list[current] == 0:
list[current] = 1
for i in range(times):
x = random.randrange(0,(len(list)))
while (x == current):
x = random.randrange(0,(len(list)))
my_gossip(x, times, list, iterations)
else:
return 1
def my_gossip_split(current, times, list, iterations):
iterations[0] = iterations[0] + 1
if list[current] == 0:
list[current] = 1
for i in range(times):
first_part = list[:int(len(list)/2)].count(1)
second_part = list[int(len(list)/2):].count(1)
if first_part == int(len(list)/2) and second_part == int(len(list)/2):
return 1
elif first_part >= second_part:
x = random.randrange(int(len(list)/2),len(list))
while (x == current):
x = random.randrange(int(len(list)/2),len(list))
else:
x = random.randrange(0,int(len(list)/2))
while (x == current):
x = random.randrange(0,int(len(list)/2))
my_gossip_split(x, times, list, iterations)
else:
return 1
def my_gossip100(current, times, nodelist, iterations, *args):
iterations[0] = iterations[0] + 1
if len(args) == 0:
visited = []
elif type(args[0]) is list:
visited = args[0]
else:
visited = []
if nodelist[current] == 0:
nodelist[current] = 1
for i in range(times):
x = random.randrange(0,(len(nodelist)))
if len(visited) == len(nodelist):
return 1
while (x == current or x in visited):
x = random.randrange(0,(len(nodelist)))
visited += [x]
my_gossip100(x, times, nodelist, iterations, visited)
else:
return 1
if not (varlist.my_gossip or varlist.my_gossip_split or varlist.my_gossip100):
print(start_algorithm(gossip, 0, 4, varlist.nodes[0], varlist.runtimes[0]))
else:
if varlist.gossip:
print("standart gossip:")
print(start_algorithm(gossip, 0, 4, varlist.nodes[0], varlist.runtimes[0]))
if varlist.my_gossip:
print("my_gossip:")
print(start_algorithm(my_gossip, 0, 4, varlist.nodes[0], varlist.runtimes[0]))
if varlist.my_gossip_split:
print("my_gossip_split:")
print(start_algorithm(my_gossip_split, 0, 4, varlist.nodes[0], varlist.runtimes[0]))
if varlist.my_gossip100:
print("my_gossip100:")
print(start_algorithm(my_gossip100, 0, 4, varlist.nodes[0], varlist.runtimes[0]))
exit