-
Notifications
You must be signed in to change notification settings - Fork 81
/
net-SIS-large-graph-adaptive.py
70 lines (55 loc) · 1.93 KB
/
net-SIS-large-graph-adaptive.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
import pycxsimulator
from pylab import *
import networkx as nx
populationSize = 500
linkProbability = 0.01
initialInfectedRatio = 0.01
infectionProb = 0.2
recoveryProb = 0.5
linkCuttingProb = 0.1
susceptible = 0
infected = 1
def initialize():
global time, network, positions, nextNetwork
time = 0
network = nx.erdos_renyi_graph(populationSize, linkProbability)
positions = nx.random_layout(network)
for i in network.nodes:
if random() < initialInfectedRatio:
network.nodes[i]['state'] = infected
else:
network.nodes[i]['state'] = susceptible
nextNetwork = network.copy()
def observe():
cla()
nx.draw(network,
pos = positions,
node_color = [network.nodes[i]['state'] for i in network.nodes],
cmap = cm.Wistia,
vmin = 0,
vmax = 1)
axis('image')
title('t = ' + str(time))
def update():
global time, network, nextNetwork
time += 1
for i in network.nodes:
if network.nodes[i]['state'] == susceptible:
nextNetwork.nodes[i]['state'] = susceptible
for j in network.neighbors(i):
if network.nodes[j]['state'] == infected:
if random() < infectionProb:
nextNetwork.nodes[i]['state'] = infected
break
else: # adaptive link cutting behavior
if random() < linkCuttingProb:
if nextNetwork.has_edge(i, j):
nextNetwork.remove_edge(i, j)
else:
if random() < recoveryProb:
nextNetwork.nodes[i]['state'] = susceptible
else:
nextNetwork.nodes[i]['state'] = infected
del network
network = nextNetwork.copy()
pycxsimulator.GUI().start(func=[initialize, observe, update])