-
Notifications
You must be signed in to change notification settings - Fork 81
/
net-cascade-of-failure.py
51 lines (39 loc) · 1.46 KB
/
net-cascade-of-failure.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
import pycxsimulator
from pylab import *
import networkx as nx
functioning = 1
failed = 0
capacity = 1.0
maxInitialLoad = 0.6
def initialize():
global time, network, positions
time = 0
network = nx.watts_strogatz_graph(200, 4, 0.02)
for nd in network.nodes:
network.nodes[nd]['state'] = functioning
network.nodes[nd]['load'] = random() * maxInitialLoad
network.nodes[choice(list(network.nodes))]['load'] = 2.0 * capacity
positions = nx.circular_layout(network)
def observe():
cla()
nx.draw(network, with_labels = False, pos = positions,
cmap = cm.jet, vmin = 0, vmax = capacity,
node_color = [network.nodes[nd]['load'] for nd in network.nodes])
axis('image')
title('t = ' + str(time))
def update():
global time, network
time += 1
node_IDs = list(network.nodes)
shuffle(node_IDs)
for nd in node_IDs:
if network.nodes[nd]['state'] == functioning:
ld = network.nodes[nd]['load']
if ld > capacity:
network.nodes[nd]['state'] = failed
nbs = [nb for nb in network.neighbors(nd) if network.nodes[nb]['state'] == functioning]
if len(nbs) > 0:
loadDistributed = ld / len(nbs)
for nb in nbs:
network.nodes[nb]['load'] += loadDistributed
pycxsimulator.GUI().start(func=[initialize, observe, update])