forked from hsayama/PyCX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net-SIS-large-graph-parametersweep.py
81 lines (65 loc) · 2.31 KB
/
net-SIS-large-graph-parametersweep.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
import pycxsimulator
from pylab import *
import networkx as nx
populationSize = 2000
numberOfLinks = 5000
initialInfectedRatio = 0.01
maxTime = 200
susceptible = 0
infected = 1
er_network = 0
ba_network = 1
def networksimulation(networkType, infectionProb, recoveryProb):
if networkType == er_network:
linkProbability = float(numberOfLinks) / float(populationSize * (populationSize - 1) / 2)
network = nx.erdos_renyi_graph(populationSize, linkProbability)
else:
network = nx.barabasi_albert_graph(populationSize, int(numberOfLinks / populationSize))
for i in network.nodes:
if random() < initialInfectedRatio:
network.node[i]['state'] = infected
else:
network.node[i]['state'] = susceptible
nextNetwork = network.copy()
for time in range(maxTime):
for i in network.nodes:
if network.node[i]['state'] == susceptible:
nextNetwork.node[i]['state'] = susceptible
for j in network.neighbors(i):
if network.node[j]['state'] == infected:
if random() < infectionProb:
nextNetwork.node[i]['state'] = infected
break
else:
if random() < recoveryProb:
nextNetwork.node[i]['state'] = susceptible
else:
nextNetwork.node[i]['state'] = infected
network, nextNetwork = nextNetwork, network
return [network.node[i]['state'] for i in network.nodes].count(infected)
infectionRates = arange(0, 0.3, 0.01)
result = []
print('Starting simulations on Erdos-Renyi networks...')
for i in infectionRates:
print('Infection rate =', i)
s = 0
for k in range(5):
s += networksimulation(er_network, i, 0.5)
s /= 5.0
result.append(s)
subplot(1, 2, 1)
plot(infectionRates, result)
title("Erdos-Renyi Network")
result = []
print('Starting simulations on Barabasi-Albert networks...')
for i in infectionRates:
print('Infection rate =', i)
s = 0
for k in range(5):
s += networksimulation(ba_network, i, 0.5)
s /= 5.0
result.append(s)
subplot(1, 2, 2)
plot(infectionRates, result)
title("Barabasi-Albert Network")
show()