Skip to content
Snippets Groups Projects
factories.py 1.85 KiB
Newer Older
markn92's avatar
markn92 committed
from typing import Dict
markn92's avatar
markn92 committed

markn92's avatar
markn92 committed
import networkx as nx

markn92's avatar
markn92 committed
from evrouting.T import Node, SoC, ConsumptionFunction
from evrouting.graph_tools import charging_cofficient
markn92's avatar
markn92 committed
from evrouting.charge.T import SoCProfile, SoCFunction, ChargingFunction, Label
markn92's avatar
markn92 committed


markn92's avatar
markn92 committed
class SoCProfileFactory:
    """Maps Nodes to their (cached) charging functions."""
markn92's avatar
markn92 committed

markn92's avatar
markn92 committed
    def __init__(self, G: nx.Graph, capacity: SoC, c: ConsumptionFunction):
        """
        :param G:
        :param capacity:
        :param c: Function to calc consumption for an edge.
        """
markn92's avatar
markn92 committed
        self.G: nx.Graph = G
        self.capacity: SoC = capacity
markn92's avatar
markn92 committed
        self.c = c
markn92's avatar
markn92 committed

markn92's avatar
markn92 committed
    def __call__(self, u: Node, v: Node = None) -> SoCProfile:
markn92's avatar
markn92 committed
        path_cost = 0 if v is None else self.c(self.G, u, v)
markn92's avatar
markn92 committed

markn92's avatar
markn92 committed
        return SoCProfile(path_cost, self.capacity)
markn92's avatar
markn92 committed


class ChargingFunctionMap:
markn92's avatar
markn92 committed
    """Maps Nodes to their (cached) charging functions."""
markn92's avatar
markn92 committed

    def __init__(self, G: nx.Graph, capacity: SoC, initial_soc: SoC = None):
        self.map: Dict[Node, ChargingFunction] = {}
        self.G: nx.Graph = G
        self.capacity: SoC = capacity
        self.initial_soc: SoC = initial_soc

    def __getitem__(self, node: Node) -> ChargingFunction:
        """
        Try to get charging function from cache,
        else create function and add to cache.
        """
        try:
            cf = self.map[node]
        except KeyError:
markn92's avatar
markn92 committed
            cf = ChargingFunction(
                c=charging_cofficient(self.G, node),
markn92's avatar
markn92 committed
                capacity=self.capacity,
                initial_soc=self.initial_soc
            )
            self.map[node] = cf

        return cf


markn92's avatar
markn92 committed
class SoCFunctionFactory:
    """Maps Nodes to their charging functions."""

    def __init__(self, cf: ChargingFunctionMap):
        self.cf: ChargingFunctionMap = cf

markn92's avatar
markn92 committed
    def __call__(self, label: Label) -> SoCFunction:
        return SoCFunction(label, self.cf[label.last_cs])