-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgillespie.cpp
More file actions
348 lines (318 loc) · 12.4 KB
/
gillespie.cpp
File metadata and controls
348 lines (318 loc) · 12.4 KB
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
337
338
339
340
341
342
343
344
345
346
347
348
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string>
#include <bits/stdc++.h>
#include <ctime>
using namespace std;
int main(void)
{
//read the edgelist and generate the useful vectors
//-----------------------------------------
string filename;
//cout << "Please, indicate the name of the edgelist (including extension), located in this directory: ";
//cin >> filename;
filename="edgelistint.txt"; //"net1m_2009-04-01_GC.edge" either change the filename here or introduce via the console with the previous two lines and commenting this one
//quitar si hay lineas comentadas al principio de la edgelist
//first i read the nodes and count their degrees
int N; //number of nodes since im reading the edgelist i dont see isolated nodes, this N is <= real N
int E; //number of edges
int lectura[80000]; //the size of the edgelist cant be bigger than this for this program with strings, remake this section of the program for the S1 case
int node[11000]; //>N, stores the names of the nodes
int deg[11000]; //stores the degrees of the nodes
ofstream properties;
properties.open("C:\\Users\\Usuario\\Desktop\\MasterUB\\Docencia\\ComplexNetworks\\network\\part2\\properties.txt");
properties << "This file contains information of the network given by: " << filename << endl << endl;
cout << "The file 'properties.txt' contains several properties of the network." << endl;
int i,j, k, l; //i y j pertenecen a lectura, k y l a node
bool isunique;
int contador[80000]; //
ifstream edgelist;
edgelist.open("C:\\Users\\Usuario\\Desktop\\MasterUB\\Docencia\\ComplexNetworks\\network\\part2\\"+filename);
//leo todos los nodos
i=0;
edgelist >> lectura[i];
while(!edgelist.eof())
{
i++;
edgelist >> lectura[i];
}
edgelist.close();
E=(i+1)/2; //porque i empieza en 0
//paso a mi vector de nombres de nodos y cuento degrees
k=0;
node[k]=lectura[0];
deg[k]=1;
contador[0]=0;
for(i=1;i<2*E;i++)
{
isunique=true;
for(j=0;j<i;j++)
{
if(lectura[j]==lectura[i])
{
//tengo que comprobar no repetir edges al contar el degree
if(i%2==0) //i even, estoy leyendo el primer elemento de un edge, tengo que comprobar el siguiente con el que va con j
{
if(j%2==0) //j tambien el primero, comparo con el siguiente
{
if(lectura[i+1]!=lectura[j+1]){deg[j-contador[j]]++;}
}else if(j>0){ //j odd, comparo con el anterior
if(lectura[i+1]!=lectura[j-1]){deg[j-contador[j]]++;}
}
}else{ //i odd, comparo el anterior a i con los j
if(j%2==0) //j el primero, comparo con el siguiente
{
if(lectura[i-1]!=lectura[j+1]){deg[j-contador[j]]++;}
}else if(j>0){ //j odd, comparo con el anterior
if(lectura[i-1]!=lectura[j-1]){deg[j-contador[j]]++;}
}
}
isunique=false;
goto fuera;
}
}
fuera:
if(isunique)
{
k++;
node[k]=lectura[i];
deg[k]=1;
}
contador[i]=i-k;
}
N=k+1;
properties << "Number of nodes: " << N << endl << "Number of edges: " << E << endl;
double kav; //average degree
kav=(double)2*E/(double)N;
properties << "Average degree: " << kav << endl;
//primero tengo que traducir lectura (edgelist(string) con nombres de los nodos) a edge (edgelist(int) con números de los nodos)
int edge[2*E];
for(i=0;i<2*E;i++)
{
j=0;
while(lectura[i]!=node[j])
{
j++;
}
edge[i]=j;
}
int v[2*E]; //stores the edgelist information
int pointerini[N], pointerfin[N];
pointerini[0]=0;
bool repe;
for(i=0;i<N;i++) //for each node
{
pointerini[i+1]=pointerini[i]+deg[i];
pointerfin[i]=pointerini[i];
for(k=0;k<2*E;k++) //busco en edgelist
{
if(lectura[k]==node[i])
{
if(k%2==0) //k even, estoy leyendo el primer elemento de un edge, tengo que cojer el siguiente
{
//compruebo no repetir con los anteriores (si hay)
repe=false;
for(j=pointerini[i];j<pointerfin[i];j++)
{
if(edge[k+1]==v[j]){repe=true;}
}
if(!repe)
{
v[pointerfin[i]]=edge[k+1];
// edgelistint << i << " " << v[pointerfin[i]] << endl;
pointerfin[i]++;
}
}else { //k odd, cojo el anterior
//compruebo no repetir con los anteriores (si hay)
repe=false;
for(j=pointerini[i];j<pointerfin[i];j++)
{
if(edge[k-1]==v[j]){repe=true;}
}
if(!repe)
{
v[pointerfin[i]]=edge[k-1];
// edgelistint << v[pointerfin[i]] << " " << i << endl;
pointerfin[i]++;
}
}
}
if((pointerfin[i]-pointerini[i])==deg[i]){goto salgo;}
}
salgo:
k=0; //esto no vale pa na pero si no pongo algo después del : no compila
}
//------------------------------------------
//gillespie---------------------
//vectores necesarios
int ninfected[N]; //nodos infectados
int pn; //pointer of the latter vector, number of infected nodes = pn + 1
pn=-1; //a la hora de infectar, primero actualizar el pointer. al recuperar, después.
int eact[E][2]; //lista de links activos
int pe; //pointer of the latter vector, number of active links = pe + 1
pe=-1; //a la hora de infectar, primero actualizar el pointer. al recuperar, después.
int guide[2*E]; //guide that saves the location of an edge in eact[] based on v[]
srand(time(NULL));
//condicion inicial, infecto un x% de nodos
double initialpercent;
initialpercent=0.20;
bool infectado;
for(i=0;i<N;i++) //infecto nodos
{
if((double)rand()/(double)RAND_MAX<initialpercent)
{
pn++;
ninfected[pn]=i;
}
}
int a;
//links activos iniciales
for(i=0;i<(pn+1);i++) //recorro los infectados
{
for(j=pointerini[ninfected[i]];j<pointerfin[ninfected[i]];j++) //para sus vecinos, no se va a repetir
{
infectado=false;
for(k=0;k<(pn+1);k++) //si el vecino está infectado
{
if(v[j]==ninfected[k])
{
infectado=true;
}
}
if(!infectado) //si no se ha infectado, ese link es activo
{
pe++;
eact[pe][0]=ninfected[i];
eact[pe][1]=v[j];
guide[j]=pe;
//guide is symmetrical
for(a=pointerini[v[j]];a<pointerfin[v[j]];a++)
{
if(v[a]==ninfected[i])
{
guide[a]=pe;
}
}
}
}
}
//ciclo de gillespie
double tiempo, t, rando;
tiempo=5000; //tiempo total de simulacion (tiempo real)
t=0;
double lambda;
lambda=0.3;
int recuperado;
//density
ofstream density;
density.open("C:\\Users\\Usuario\\Desktop\\MasterUB\\Docencia\\ComplexNetworks\\network\\part2\\density.txt");
cout << "The file density.txt contains the variation of the density of infected nodes for the time duration." << endl;
density << "#time fraction of infected" << endl << t << " " << (double)(pn+1)/(double)N << endl;
while(t<tiempo)
{
if((pn<0)||(pn>N-2)||(pe<0)){goto exit;}else
{
if((double)rand()/(double)RAND_MAX<(double)(pn+1)/(double)(pn+1+lambda*(pe+1))) //si hay recuperación
{
//escojo un nodo al azar y lo recupero
i=(double)rand()/(double)RAND_MAX*pn;
recuperado=ninfected[i];
ninfected[i]=ninfected[pn];
ninfected[pn]=-1; //dejo un -1 donde ya no hay nodo infectado
pn=pn-1;
//actualizo links activos
//busco los vecinos del nodo recuperado en v
for(j=pointerini[recuperado];j<pointerfin[recuperado];j++)
{
//si infectado, añado link activo
infectado=false;
for(k=0;k<(pn+1);k++) //si el vecino está infectado
{
if(v[j]==ninfected[k])
{
infectado=true;
}
}
if(infectado) //si es infectado, ese link es activo
{
pe++;
eact[pe][0]=v[j];
eact[pe][1]=recuperado;
guide[j]=pe;
//guide is symmetrical
for(a=pointerini[v[j]];a<pointerfin[v[j]];a++)
{
if(v[a]==recuperado)
{
guide[a]=pe;
}
}
}else //si no, era activo y ya no, lo quito
{
//cout << v[j] << " " << eact[guide[j]][1] << endl; //HERE I SEE A MISTAKE after a few iterations
eact[guide[j]][0]=eact[pe][0];
eact[guide[j]][1]=eact[pe][1];
eact[pe][0]=-1;
eact[pe][1]=-1;
pe=pe-1;
}
}
}else //si hay infeccion
{
//escojo un link activo al azar e infecto al nodo sano de los dos, es el de la coordenada 1 porque los he puesto asi
i = eact[(int)((double)rand() / RAND_MAX * pe)][1];
pn++;
ninfected[pn]=i;
//actualizo links activos
//busco los vecinos del nodo (ahora infectado) en v
for(j=pointerini[i];j<pointerfin[i];j++)
{
//si no infectado, añado link activo
infectado=false;
for(k=0;k<(pn+1);k++) //si el vecino está infectado
{
if(v[j]==ninfected[k])
{
infectado=true;
}
}
if(!infectado) //si no es infectado, ese link es activo
{
pe++;
eact[pe][0]=i;
eact[pe][1]=v[j];
guide[j]=pe;
//guide is symmetrical
for(a=pointerini[v[j]];a<pointerfin[v[j]];a++)
{
if(v[a]==i)
{
guide[a]=pe;
}
}
}else //si es infectado, lo quito
{
eact[guide[j]][0]=eact[pe][0];
eact[guide[j]][1]=eact[pe][1];
eact[pe][0]=-1;
eact[pe][1]=-1;
pe=pe-1;
}
}
}
}
exit:
//actualizo el tiempo
do
{
rando=(double)rand()/(double)RAND_MAX;
}while(rando==0);
t=t-(double)log(rando)/(double)(1+lambda);
//record density (normalized)
density << t << " " << (double)(pn+1)/(double)N << endl;
}
density.close();
properties.close();
return 0;
}