Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
graph_tools.py 785 B
from collections import namedtuple

import networkx as nx
from evrouting.T import Wh, ChargingCoefficient, Time, Node, NodeData, EdgeData

TemplateEdge = namedtuple('Edge', ['u', 'v', 'distance', 'consumption'])
TemplateNode = namedtuple(
    'Node', ['label', 'charging_coeff'], defaults=(None, None)
)


def node_convert(n: TemplateNode) -> NodeData:
    return {'label': n.label, 'c': n.charging_coeff}


def edge_convert(e: TemplateEdge) -> EdgeData:
    return {'weight': e.distance, 'c': e.consumption}


def consumption(G: nx.Graph, u: Node, v: Node) -> Wh:
    return G.edges[u, v]['c']


def distance(G: nx.Graph, u: Node, v: Node) -> Time:
    return G.edges[u, v]['weight']


def charging_cofficient(G: nx.Graph, n: Node) -> ChargingCoefficient:
    return G.nodes[n]['c']