-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimeAware.py
111 lines (89 loc) · 3.95 KB
/
TimeAware.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
import math
import time
import future.utils
import numpy as np
from ndlib.models.DiffusionModel import DiffusionModel
class TimeAware(DiffusionModel):
def __init__(self, graph):
# Call the super class constructor
super(self.__class__, self).__init__(graph)
# Method name
self.name = "TimeAware"
# Available node statuses
self.available_statuses = {
"Susceptible": 0,
"Infected": 1,
}
# Exposed Parameters
self.parameters = {
"model": {},
"nodes": {
"Time": { # Activation Time fo each node
"descr": "Time Node",
"range": [0, 1],
"optional": True,
"default": 0
}
},
"edges": {
"alpha": { # Alpha denotes the strength
"descr": "Alpha Value",
"range": [0, 1],
"optional": True,
"default": 0.3
}
},
}
def iteration(self, node_status=True):
self.clean_initial_status(self.available_statuses.values())
actual_status = {node: nstatus for node, nstatus in future.utils.iteritems(self.status)} # Copy of Statuses
# if first iteration return the initial node status
if self.actual_iteration == 0:
self.actual_iteration += 1
for u in self.graph.nodes():
timeActivated = time.time()
self.params['nodes']['Time'][u] = timeActivated
delta, node_count, status_delta = self.status_delta(actual_status)
if node_status:
return {"iteration": 0, "status": actual_status.copy(),
"node_count": node_count.copy(), "status_delta": status_delta.copy()}
else:
return {"iteration": 0, "status": {},
"node_count": node_count.copy(), "status_delta": status_delta.copy()}
# iteration inner loop
for u in self.graph.nodes():
if actual_status[u] == 1: # Check if not Infected
continue
if self.params['nodes']['Time'][u] == 0:
Time = time.time()
self.params['nodes']['Time'][u] = Time
else:
Time = self.params['nodes']['Time'][u]
neighbors = list(self.graph.neighbors(u)) # Neighbours of U
for v in neighbors:
if actual_status[v] == 1: # If already Infected Continue
continue
if (self.params['nodes']['Time'][v] == 0):
Time1 = time.time()
self.params['nodes']['Time'][v] = Time1
else:
Time1 = self.params['nodes']['Time'][v]
if (u, v) not in self.params['edges']['alpha']:
continue
Alpha = self.params['edges']['alpha'][(u, v)]
Equation1 = -Alpha * (Time1 - Time)
Equation2 = Alpha * (math.e ** Equation1)
flip = np.random.random_sample()
if (Equation2 >= flip):
actual_status[v] = 1
delta, node_count, status_delta = self.status_delta(actual_status)
# update the actual status and iterative step
self.status = actual_status
self.actual_iteration += 1
# return the actual configuration (only nodes with status updates)
if node_status:
return {"iteration": self.actual_iteration - 1, "status": delta.copy(),
"node_count": node_count.copy(), "status_delta": status_delta.copy()}
else:
return {"iteration": self.actual_iteration - 1, "status": {},
"node_count": node_count.copy(), "status_delta": status_delta.copy()}