forked from zonghan-zhang/NIB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
74 lines (52 loc) · 1.67 KB
/
util.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
import networkx as nx
import ndlib
import ndlib.models.epidemics as ep
import ndlib.models.ModelConfig as mc
import random
import statistics as s
# Network Gneration
def net_gen():
# g = nx.erdos_renyi_graph(5000, 0.005)
g = nx.barabasi_albert_graph(5000, 10)
cost = {}
for i in range(5000):
C = random.randrange(1, 3)
C = C * g.degree()[i]
cost[i] = C
infected = random.sample(range(0, 5000), 50)
# Model Selection
model = ep.IndependentCascadesModel(g)
config = mc.Configuration()
config.add_model_initial_configuration('Infected', infected)
for a, b in g.edges():
weight = random.randrange(1, 30)
weight = round(weight / 100, 2)
config.add_edge_configuration("threshold", (a, b), weight)
g[a][b]['weight'] = weight
model.set_initial_status(config)
# Simulation
iterations = model.iteration_bunch(10)
trends = model.build_trends(iterations)
total_no = 0
for i in range(10):
a = iterations[i]['node_count'][1]
total_no += a
# print(a)
print('total node #: ', total_no)
infected_no = 0
for i in range(10):
for j in iterations[i]['status']:
a = iterations[i]['status'][j]
if a == 1:
b = cost[j]
else:
b = 0
infected_no += b
print("infected total: ", infected_no)
population = 0
for i in cost:
population += cost[i]
print("population: ", population)
degrees_no = [val for (node, val) in g.degree()]
print("average degree: ", s.mean(degrees_no))
return g, cost, population, infected, infected_no, mc, config