-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExFmod.R
336 lines (287 loc) · 9.39 KB
/
ExFmod.R
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
library(foreach)
library(doParallel)
library(igraph)
#registerDoParallel(cores=64)
registerDoParallel(cores=8)
#parameters
dataset = "mails" #['mails',...]
norm = 1 #normalization of dt
unit = "hours" #["secs","mins","hours","days","weeks","auto"] - do not use "auto"
symdt = 0.1 #delay between ploting in second
show = FALSE #plot or not
threshold = exp(-720) #level at which edge are deleted
bins = seq(0,1,0.01)
alpha = 60*60 #multipliers: 1-second, 60-minutes, 60*60-hours, 60*60*24-days
infect_start_step = 1000 #event number when infection starts
gamma = 1 #exponential distribution parameter
critical_level = 0.5 #critical level of infected network (stop condition)
limit = 20000 #number of event taking to account - simulation on all events have to high cost
#source_dir = "/home/grid/users/plgmkul/ms/"
source_dir = "d:/codes_R/ExF/"
#results_dir = "/home/grid/users/plgmkul/ms/res/"
results_dir = "d:/epid_sym_man/"
loadData <- function(dataset){
if(dataset=="mails"){
data = read.csv2(paste(source_dir,"manufacturing.csv",sep=""))
return(data)
}
}
findAllNodes <- function(data){
tn <- graph.empty(directed=FALSE)
for(i in 1:nrow(data)){
s = toString(data[i,1])
r = toString(data[i,2])
#add new nodes if not exist
if(length(which(V(tn)$name == s))==0){
tn <- tn + vertex(s, infected=FALSE)
print(paste("Node",s,"added.",sep=" "))
}
if(length(which(V(tn)$name == r))==0){
tn <- tn + vertex(r, infected=FALSE)
print(paste("Node",r,"added.",sep=" "))
}
}
return(tn)
}
recomputeWeights <- function(n, dt, norm, tr){
todelete <- list()
foreach(edge=E(n))%dopar%{
nlt <- E(n)[edge]$lt + (dt/norm)
nw <- exp(-nlt)
E(n)[edge]$weight <- nw
if(nw < tr){
todelete <- append(todelete,edge)
}else{
E(n)[edge]$lt <- nlt
}
}
n <- delete.edges(n,todelete)
return(n)
}
countWeights <- function(n,bins){
if(ecount(n)>0){
h=hist(E(n)$weight,breaks=bins,plot=FALSE)
return(h$counts)
}else{
return(matrix(0,ncol=bins))
}
}
countActiveNodes <- function (n){
an = 0
components = decompose.graph(n)
cn = 0
for(x in components){
if(ecount(x)>0){
an = an + vcount(x)
cn = cn + 1
}
}
return(c(an,cn))
}
runEvents <- function(tr, norm, unit, symdt, show, bins){
lastEventTime = as.POSIXct(data[1,3])
anCnPerEvent = matrix(c(0,0),ncol=2)
weightHist = matrix(ncol = length(bins))
#for(i in 1:50){
for(i in 1:nrow(data)){
print(i)
#read next event
s = toString(data[i,1])
r = toString(data[i,2])
t = as.POSIXct(data[i,3])
#time since last event
dt = as.numeric(difftime(t,lastEventTime, units=unit))
lastEventTime = t
#forgetting
tn <- recomputeWeights(tn, dt, norm, tr)
#add new link if not exist or set weight to 1 on existing link
if(tn[s,r]==0){
tn <- tn + edge(s,r,weight=1.0, lt=0)
}else{
tn[s,r] <- 1
E(tn,P=c(s,r))$lt <- 0
}
#count active nodes and components
an_cn <- countActiveNodes(tn)
#count weights and prepare histogram
h <- countWeights(tn,bins)
weightHist <- rbind(weightHist,h)
anCnPerEvent <- rbind(anCnPerEvent,an_cn)
}
close(pb)
results <- vector(mode = "list", length = 3)
names(results) <- c("tn","an","wh")
results[[1]]=tn
results[[2]]=anCnPerEvent
results[[3]]=weightHist
return(results)
}
countIS <- function(tn){
is = 0
edges = get.edges(tn,E(tn))
if(ecount(tn)>0){
for(i in 1:ecount(tn)){
v1 <- V(tn)[edges[i,1]]$infected
v2 <- V(tn)[edges[i,2]]$infected
if(xor(v1,v2)){
is=is+1
}
}
return(is)
}else{
return(0)
}
}
infectionProgress <- function(tn){
n=0
for(v in V(tn)){
if(V(tn)[v]$infected==TRUE){
n = n+1
}
}
progress=(n/vcount(tn))
return(progress)
}
runInfection <- function(tr, norm, unit, symdt, show, bins,alpha,infect_start_step,gamma,critical_level){
#pb = tkProgressBar("Events progress","%",1,nrow(data))
#ib = tkProgressBar("Infection progress","%",0,1000)
lastEventTime = as.POSIXct(data[1,3])
tnet = tn
niter = 4
out_results = matrix(ncol=8)
step = 0
infected = 0
infT = 0
nth = 0
start = TRUE
tempV = V(tn)[152:168]
foreach(n=tempV)%dopar%{
nth = nth + 1
for(iter in 1:niter){
tryCatch({
#if(nth==1 && iter == 1) start = TRUE
if(start){
tn <- tnet
inf_distr <- c()
steps_time <- c()
inf_time <- c()
n_infected <- c()
n_IS <- c()
step = 0
infected = 0
for(i in 1:(nrow(data)-1)){
step = step + 1
ip=infectionProgress(tn)
if(ip>critical_level){
print("STOP!")
break
}
if(step>limit){
print("STOP! - out of limit")
break
}
ip = round(ip*1000,digits=0)
#read next event
s <- toString(data[i,1]) #sender
r <- toString(data[i,2]) #reciever
t <- as.POSIXct(data[i,3]) #time (N_i)
t_next <- as.POSIXct(data[i+1,3])
steps_time <- c(steps_time,t)
if(i==infect_start_step){
#make nth node as bad one
V(tn)[n]$infected = TRUE
infected = infected + 1
#TODO: IF node already in network compute ExF - E
#progressbar
#info <- paste(toString(ip)," infection done",sep="")
#setTkProgressBar(ib,ip,label=info)
}
if(i>=infect_start_step){ #TODO:
#randomly draw a time for next infection
infT <- rexp(1,rate=gamma)
t_0 <- t
IS <- countIS(tn)
inf_distr <- c(inf_distr,ip)
n_IS <- c(n_IS,IS)
time_to_next_inf = t_0 + (infT/IS)*alpha
c_inf = 0
while(time_to_next_inf<t_next){
t_0 = time_to_next_inf
#print(paste(time_to_next_inf,t_next,infT/IS*alpha))
inf_time <- c(inf_time,time_to_next_inf)
c_inf = c_inf + 1
#infect random node:
#take random infected node regardless of it has neighbors
v = sample(V(tn)[V(tn)$infected==TRUE],1)
if(length((V(tn)[neighbors(tn,v)])[V(tn)[neighbors(tn,v)]$infected==FALSE]$name)>0){
#and select ranom non infected neighbor
toinf = sample((V(tn)[neighbors(tn,v)])[V(tn)[neighbors(tn,v)]$infected==FALSE]$name,1)
V(tn)[toinf]$infected = TRUE
infected = infected + 1
#progressbar
#info <- paste(toString(ip)," infection done",sep="")
#setTkProgressBar(ib,ip,label=info)
}
infT <- rexp(1,rate=gamma)
IS <- countIS(tn)
inf_distr <- c(inf_distr,ip)
time_to_next_inf <- t_0 + infT/IS*alpha
}
n_infected <- c(n_infected,c_inf)
delta = t_next - t_0
infT = infT - delta*IS
t_0 = t_next
}
#time since last event
dt = as.numeric(difftime(t,lastEventTime, units=unit))
lastEventTime = t
#forgetting
tn <- recomputeWeights(tn, dt, norm, tr)
#add new link if not exist or set weight to 1 on existing link
if(tn[s,r]==0){
tn <- tn + edge(s,r,weight=1.0, lt=0)
}else{
tn[s,r] <- 1
E(tn,P=c(s,r))$lt <- 0
}
#output
#info <- paste(toString(i),"/",toString(nrow(data))," done",sep="")
#setTkProgressBar(pb,i, label=info)
#show network
if(show){
plot.igraph(tn, vertex.size=5, layout=layout.sphere(tn), edge.width=E(tn)$weight*10)
}
Sys.sleep(symdt)
}
#close(pb)
#close(ib)
#saving results
results <- vector(mode = "list", length = 8)
names(results) <- c("node","iter","end_step","inf_distr","steps_time","inf_time","number_of_infected","number_of_IS")
results[[1]]=n
results[[2]]=iter
results[[3]]=step
results[[4]]=inf_distr
results[[5]]=steps_time
results[[6]]=inf_time
results[[7]]=n_infected
results[[8]]=n_IS
print(inf_distr)
out_results <- rbind(out_results,results)
path = paste(results_dir,"1epid_full_sym_",toString(n),"_",toString(iter),"_",toString(step),sep="")
save(out_results,file=path)
}
}, error = function(e){
print(e)
msg = paste(n,iter,sep=" ")
loginfo(msg,logger="test")
})
}
}
return(out_results)
}
#data = loadData(dataset)[1:10000,]
#tn = findAllNodes(data)
#alpha = 60*60*24
#runInfection(threshold, norm, unit, symdt, show, bins, alpha, infect_start_step, gamma, critical_level)
out <- runEvents(threshold,norm,unit,symdt,show,bins)