forked from zonghan-zhang/SIM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulation.py
116 lines (83 loc) · 3.37 KB
/
simulation.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import ndlib
import ndlib.models.epidemics as ep
import ndlib.models.ModelConfig as mc
import pandas as pd
from util import combinations, subcombs, substract
import random
def simulationIC(r, g, result, config):
title = []
for i in result:
title.append(i)
title.append('result')
df = pd.DataFrame(columns=title)
n = len(result)
for combs in combinations(result):
input = []
for i in range(n):
item = 1 if result[i] in combs else 0
input.append(item)
for i in range(r):
input1 = []
for item in input:
input1.append(item)
g_mid = g.__class__()
g_mid.add_nodes_from(g)
g_mid.add_edges_from(g.edges)
model_mid = ep.IndependentCascadesModel(g_mid)
config_mid = mc.Configuration()
config_mid.add_model_initial_configuration('Infected', combs)
for a, b in g_mid.edges():
weight = config.config["edges"]['threshold'][(a, b)]
g_mid[a][b]['weight'] = weight
config_mid.add_edge_configuration('threshold', (a, b), weight)
model_mid.set_initial_status(config_mid)
iterations = model_mid.iteration_bunch(5)
trends = model_mid.build_trends(iterations)
total_no = 0
for j in range(5):
a = iterations[j]['node_count'][1]
total_no += a
input1.append(total_no)
# newdf = pd.DataFrame([[zero, one, two, three, four, total_no]], columns = title)
newdf = pd.DataFrame([input1], columns=title)
# df = df.append({title[0]: zero, title[1]: one, title[2]: two, title[3]: three, title[4]: four, title[5]: total_no},ignore_index=True)
df = pd.concat([df,newdf])
return df
def simulationLT(r, g, result, config):
title = []
for i in result:
title.append(i)
title.append('result')
df = pd.DataFrame(columns=title)
n = len(result)
for combs in combinations(result):
input = []
for i in range(n):
item = 1 if result[i] in combs else 0
input.append(item)
for i in range(r):
input1 = []
for item in input:
input1.append(item)
g_mid = g.__class__()
g_mid.add_nodes_from(g)
g_mid.add_edges_from(g.edges)
model_mid = ep.ThresholdModel(g_mid)
config_mid = mc.Configuration()
config_mid.add_model_initial_configuration('Infected', combs)
for a, b in g_mid.edges():
weight = config.config["edges"]['threshold'][(a, b)]
g_mid[a][b]['weight'] = weight
config_mid.add_edge_configuration('threshold', (a, b), weight)
for i in g_mid.nodes():
threshold = random.randrange(1, 20)
threshold = round(threshold / 100, 2)
config_mid.add_node_configuration("threshold", i, threshold)
model_mid.set_initial_status(config_mid)
iterations = model_mid.iteration_bunch(5)
trends = model_mid.build_trends(iterations)
total_no = iterations[4]['node_count'][1]
input1.append(total_no)
newdf = pd.DataFrame([input1], columns=title)
df = pd.concat([df,newdf])
return df