forked from zonghan-zhang/NIB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deepIBM.py
134 lines (102 loc) · 3.55 KB
/
deepIBM.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import torch
import torch.nn as nn
import torch.nn.functional as func
from torch.autograd import Variable
import numpy as np
import time
import statistics as s
import ndlib.models.epidemics as ep
import ndlib.models.ModelConfig as mc
# To-Do
# skip-connection -> not work
# initialization ->
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.linear1 = nn.Linear(2, 1)
# self.linear2 = nn.Linear(3, 1)
def forward(self, x):
z = self.linear1(x)
# y = torch.cat((z, x), 1)
# y = self.linear2(y)
return torch.sigmoid(z)
def init_weights(m):
if type(m) == nn.Linear:
torch.nn.init.constant_(m.weight, val=1)
m.bias.data.fill_(0.01)
def deepIBM(g, cost, population, infected, infected_no, mc, config, value, time2):
v = []
c = []
p = []
for i in g.nodes:
if i not in infected:
v.append(float(value[i]))
c.append(float(cost[i]))
p.append(i)
X = np.array(list(zip(v, c)))
X = torch.from_numpy(X)
v = torch.FloatTensor(v)
c = torch.FloatTensor(c)
for percent in range(5, 95, 5):
start = time.time()
k = int(population * percent / 100)
model = Net()
model.apply(init_weights)
optimizer = torch.optim.Adam(model.parameters(), lr=0.01, weight_decay=5e-4)
model.train()
for epoch in range(200):
optimizer.zero_grad()
y = model(X.float())
loss = max(0, torch.matmul(c, y) - k) - 7 * torch.matmul(v, y)
loss.backward()
optimizer.step()
z = y.detach().numpy()
pr = {}
for i in range(4950):
pr[p[i]] = z[i][0]
sorted_pr = []
for node in sorted(pr, key=pr.get, reverse=False):
sorted_pr.append(node)
current_dl = 0
dl = []
for node in sorted_pr:
C = cost[node]
if (current_dl + C <= k):
dl.append(node)
current_dl += C
else:
continue
period = time.time() - start + time2
# after immunizing with the dl algorithm
g_dl = g.__class__()
g_dl.add_nodes_from(g)
g_dl.add_edges_from(g.edges)
for node in dl:
g_dl.remove_node(node)
config_dl = mc.Configuration()
config_dl.add_model_initial_configuration('Infected', infected)
for a, b in g_dl.edges():
weight = config.config["edges"]['threshold'][(a, b)]
config_dl.add_edge_configuration('threshold', (a, b), weight)
g_dl[a][b]['weight'] = weight
# Simulation 10 times
result = []
for ii in range(10):
model_dl = ep.IndependentCascadesModel(g_dl)
model_dl.set_initial_status(config_dl)
iterations_dl = model_dl.iteration_bunch(10)
trends_dl = model_dl.build_trends(iterations_dl)
infected_dl = 0
for i in range(10):
for j in iterations_dl[i]['status']:
a = iterations_dl[i]['status'][j]
if a == 1:
b = cost[j]
else:
b = 0
infected_dl += b
effect = (infected_no - infected_dl - current_dl) / population
result.append(effect)
print("Immuned partition: ", percent, end=', ')
print("Protect number of individuals: ", s.mean(result), " +- ", s.stdev(result), end=', ')
print("Time: ", period)