Skip to content

Instantly share code, notes, and snippets.

@sourceperl
Created December 6, 2017 18:48
Show Gist options
  • Save sourceperl/cef6d49b4c3bb2c0d19bd2ecd3963c00 to your computer and use it in GitHub Desktop.
Save sourceperl/cef6d49b4c3bb2c0d19bd2ecd3963c00 to your computer and use it in GitHub Desktop.
Test with networkx to emulate gas flow
#!/usr/bin/env python3
import matplotlib.pyplot as plt
import networkx as nx
class GasGraph(nx.Graph):
def update(self):
for node in self:
# Valve
if isinstance(node, Valve):
if not node.is_open():
for edge in self.edges(node):
nx.set_edge_attributes(self, {edge: {'cost': float('inf')}})
class Node:
def __init__(self, name, gas_graph=None):
self.name = name
if isinstance(gas_graph, GasGraph):
gas_graph.add_node(self)
def __str__(self):
return self.name
def __repr__(self):
return '%s(%s)' % (self.__class__.__name__, self.name)
class Valve(Node):
ERROR = 0
OPEN = 1
CLOSE = 2
def __init__(self, *args, **kwargs):
Node.__init__(self, *args, **kwargs)
self.status = Valve.ERROR
def open(self):
self.status = Valve.OPEN
return self
def close(self):
self.status = Valve.CLOSE
return self
def error(self):
self.status = Valve.ERROR
return self
def is_open(self):
return self.status is Valve.OPEN
def is_close(self):
return self.status is Valve.CLOSE
def color(self):
if self.status is Valve.OPEN:
return 'green'
elif self.status is Valve.CLOSE:
return 'red'
else:
return 'yellow'
g = GasGraph()
a = Node('A', g)
b = Node('B', g)
c = Node('C', g)
s = Node('S', g)
v1 = Valve('V1', g).open()
v2 = Valve('V2', g).open()
v3 = Valve('V3', g).open()
v4 = Valve('V4', g).close()
p1 = Node('P1')
p2 = Node('P2')
p3 = Node('P3')
# links
g.add_edge(a, v1)
g.add_edge(b, v2)
g.add_edge(c, v3)
g.add_edge(c, v4)
g.add_edge(s, a)
g.add_edge(s, b)
g.add_edge(v1, p1)
g.add_edge(v3, p1)
g.add_edge(v2, p2)
g.add_edge(v4, p2)
g.add_edge(p1, p2)
# update graph
g.update()
# find short path from A to C
if nx.shortest_path_length(g, source=s, target=c, weight='cost') < float('inf'):
paths = [p for p in nx.all_shortest_paths(g, source=s, target=c, weight='cost')]
else:
paths = []
print(paths)
# draw graph
# nodes positions
fixed_positions = {a: (100, 100),
b: (100, 50),
c: (10, 75),
s: (100, 75),
v1: (80, 100),
v2: (80, 50),
v3: (30, 100),
v4: (30, 50),
p1: (60, 100),
p2: (60, 50)}
pos = nx.spring_layout(g, pos=fixed_positions, fixed=fixed_positions.keys())
# add nodes with colors
n_colors = list()
for item in g:
# Valve
if isinstance(item, Valve):
n_colors.append(item.color())
else:
n_colors.append('#AB2929')
nx.draw_networkx_nodes(g, pos, node_color=n_colors)
# add labels
nx.draw_networkx_labels(g, pos)
# draw edge with path in green
nx.draw_networkx_edges(g, pos)
for path in paths:
path_edges = tuple(zip(path, path[1:]))
nx.draw_networkx_edges(g, pos, edgelist=path_edges, edge_color='#97EC97', width=4)
# draw labels
nx.draw_networkx_edge_labels(g, pos=pos)
plt.title('sim gas')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment