forked from hsayama/PyCX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net-SIS-synchronous-update.py
38 lines (32 loc) · 1.04 KB
/
net-SIS-synchronous-update.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
import pycxsimulator
from pylab import *
import networkx as nx
def initialize():
global g, nextg
g = nx.karate_club_graph()
g.pos = nx.spring_layout(g)
for i in g.nodes:
g.node[i]['state'] = 1 if random() < .5 else 0
nextg = g.copy()
nextg.pos = g.pos
def observe():
global g
cla()
nx.draw(g, cmap = cm.Wistia, vmin = 0, vmax = 1,
node_color = [g.node[i]['state'] for i in g.nodes],
pos = g.pos)
p_i = 0.5 # infection probability
p_r = 0.5 # recovery probability
def update():
global g, nextg
for a in g.nodes:
if g.node[a]['state'] == 0: # if susceptible
nextg.node[a]['state'] = 0
for b in g.neighbors(a):
if g.node[b]['state'] == 1: # if neighbor b is infected
if random() < p_i:
nextg.node[a]['state'] = 1
else: # if infected
nextg.node[a]['state'] = 0 if random() < p_r else 1
nextg, g = g, nextg
pycxsimulator.GUI().start(func=[initialize, observe, update])