Skip to content
Snippets Groups Projects
Commit 973f0e23 authored by markn92's avatar markn92
Browse files

Default layout with edges and marked stations

parent 082d8eb0
No related branches found
No related tags found
No related merge requests found
\documentclass{standalone}
\usepackage{tikz-network}
\begin{document}
\begin{tikzpicture}
\clip (0,0) rectangle (6,6);
\Vertex[x=0.350,y=0.691]{0}
\Vertex[x=1.486,y=1.221]{1}
\Vertex[x=1.561,y=4.476]{3}
\Vertex[x=5.650,y=4.325]{2}
\Vertex[x=5.499,y=5.309]{4}
\Edge[label=0.33](0)(1)
\Edge[label=1.05](0)(3)
\Edge[label=0.53](1)(2)
\Edge[label=0.33](1)(3)
\Edge[label=1.08](3)(2)
\Edge[label=1.06](3)(4)
\Edge[label=0.26](2)(4)
\Vertex[x=0.550,y=1.775,color={white},size={1.0},label={s,1},style={circle,style={draw,double}}]{0}
\Vertex[x=5.450,y=1.775,color={white},size={1.0},label={a,2},style={circle,style={draw,double}}]{1}
\Vertex[x=3.000,y=4.225,color={white},size={1.0},label={t},style={circle}]{2}
\Edge[label={(1, 1)}](0)(1)
\Edge[label={(1, 4)}](0)(2)
\Edge[label={(1, 1)}](1)(2)
\end{tikzpicture}
\end{document}
\ No newline at end of file
from collections import namedtuple
import networkx as nx
Street = namedtuple('Street', ['u', 'v', 'distance', 'consumption'])
Node = namedtuple('Node', ['label', 'charging_coeff'], defaults=(None, None))
edge_case = {
'b_0': 0,
'b_t': 0,
'U': 4,
'nodes': [
Node('s', charging_coeff=1),
Node('a', charging_coeff=2),
Node('t'),
],
'edges': [
Street(0, 1, distance=1, consumption=1),
Street(0, 2, distance=1, consumption=4),
Street(1, 2, distance=1, consumption=1),
]
}
def get_graph(config):
G = nx.Graph()
for node_id, node in enumerate(config['nodes']):
G.add_node(node_id, label=node.label, c=node.charging_coeff)
for edge in config['edges']:
G.add_edge(edge.u, edge.v, weight=edge.distance, c=edge.consumption)
return G
from collections import namedtuple
import pytest
import networkx as nx
Street = namedtuple('Street', ['u', 'v', 'street_type'])
Edge = namedtuple('Edge', ['x', 'y', 'charging_coeff'], defaults=(None,))
config = {
'speed': {
'default': 50, # km/h
'highway': 130 # km/h
},
'consumption': {
'default': 125 # Wh/km
},
'nodes': [
Edge(14, 16, 10),
Edge(29, 23),
Edge(84, 64),
Edge(30, 66),
Edge(82, 77)
],
'edges': [
Street(0, 1, 'default'),
Street(0, 3, 'default'),
Street(1, 2, 'highway'),
Street(1, 3, 'highway'),
Street(2, 3, 'default'),
Street(2, 4, 'default'),
Street(3, 4, 'default')
]
}
from .config import edge_case, get_graph
@pytest.fixture
def G():
G = init_graph()
def G_Edge_Case():
G = get_graph(edge_case)
yield G
del G
def init_graph():
G = nx.Graph()
for edge in config['edges']:
u_x, u_y, u_ch = config['nodes'][edge.u]
v_x, v_y, v_ch = config['nodes'][edge.v]
# Calculate euclidean distance
s = ((v_y - u_y) ** 2 + (v_x - u_x) ** 2) ** (1 / 2) # in km
# Calc travel time via t = s/v
t = s / config['speed'][edge.street_type] # in h
# Calc consumption via c = s * consumption_per_km
c = s * config['consumption']['default']
G.add_edge(edge.u, edge.v, weight=t, c=c, d=s)
return G
import networkx as nx
import network2tikz as tikz
def draw_graph(G: nx.Graph, filename=None):
"""Draw graph marking stations as double circles."""
layout = nx.planar_layout(G)
tikz.plot(
G,
filename=filename,
layout=layout,
standalone=False,
**_get_edge_styles(G),
**_get_node_styles(G)
)
def _get_edge_styles(G):
styles = {
'edges_label': {(u, v): '({}, {})'.format(
G[u][v]['weight'],
G[u][v]['c']) for u, v in G.edges}
}
return styles
def _get_node_styles(G):
styles = {
'vertex_color': ['white' for n in G.nodes],
'vertex_size': 1.
}
nodes_label = {}
nodes_style = {}
for n in G:
style = 'circle'
label = G.nodes[n].get('label', str(n))
c = G.nodes[n].get('c', None)
if c is not None:
style += ',style={draw,double}'
label += ',{}'.format(c)
nodes_label[n] = label
nodes_style[n] = style
styles['nodes_label'] = nodes_label
styles['nodes_style'] = nodes_style
return styles
import networkx as nx
import network2tikz as tikz
from .conftest import config
from .draw import draw_graph
def test_graph_creation(G: nx.Graph):
assert G.number_of_nodes() == 5
assert G.number_of_edges() == 7
def test_graph_creation(G_Edge_Case: nx.Graph):
assert G_Edge_Case.number_of_nodes() == 3
assert G_Edge_Case.number_of_edges() == 3
def test_print_graph(G: nx.Graph):
layout = {i: (d[0], d[1]) for i, d in enumerate(config['nodes'])}
edges_label = {(u, v): '{:.2f}'.format(G[u][v]['weight']).format() for u, v in G.edges}
#layout = nx.planar_layout(G)
tikz.plot(G, 'graph.tex', layout=layout, edges_label=edges_label)
def test_print_graph(G_Edge_Case: nx.Graph):
draw_graph(G_Edge_Case, 'graph.tex')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment