-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim3d.py
83 lines (66 loc) · 2.22 KB
/
sim3d.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
import csv
import sim
import multiprocessing
import operator
from multiprocessing import Pool
filename = 'sim3d.csv'
def worker3d(users, events, rounds):
''' make rounds of simulation of users '''
print "{0} users with {1} events started".format(users, events)
paths = []
for _ in range(rounds):
log, attacker_log = sim.simulate(users, messages=events)
path = sim.match_paths(log, attacker_log)
paths.append(path)
# build mean and confidence interval
mean, conf = sim.mean_confidence_interval(paths)
print "{0} users with {1} events finished".format(users, events)
return users, events, mean
def log_result(result):
# export data to csv
with open(filename, 'a') as f:
writer = csv.writer(f)
writer.writerow(list(result))
f.close()
def sort_csv(file):
f = open(file)
data = csv.reader(f)
sortedlist = sorted(data, key=lambda x: (int(x[0]), int(x[1])))
f.close()
# now write the sorted result into same file
with open(file, "w") as f:
file_writer = csv.writer(f)
for row in sortedlist:
file_writer.writerow(row)
f.close()
def main(max_users=2500, max_events=120, step_user=50, step_events=10, rounds=50):
# change users and events
# only complete paths
users = range(0, max_users+1, step_user)
events = range(0, max_events+1, step_events)
users[0] = 1
events[0] = 1
# make all cases
cases = [(u, e) for u in reversed(users) for e in reversed(events)]
# see if already computed
try:
with open(filename, 'r') as f:
reader = csv.reader(f)
for row in reader:
u = int(row[0])
e = int(row[1])
if (u, e) in cases:
cases.remove((u, e))
print("skip ({0}, {1})".format(u, e))
f.close()
except (OSError, IOError):
pass
# start jobs for processes
processes = min(len(cases), multiprocessing.cpu_count()-1)
pool = Pool(processes)
results = [pool.apply_async(worker3d, args=(u, e, rounds), callback=log_result) for (u, e) in cases]
pool.close()
pool.join()
sort_csv(filename)
if __name__ == "__main__":
main()