Skip to content
Snippets Groups Projects
utils.py 1.4 KiB
Newer Older
markn92's avatar
markn92 committed
from typing import Set, Any
markn92's avatar
markn92 committed
from math import inf

from evrouting.utils import PriorityQueue
markn92's avatar
markn92 committed
from evrouting.T import SoC, Time
markn92's avatar
markn92 committed

markn92's avatar
markn92 committed
from .T import Label
from .factories import SoCFunctionFactory
markn92's avatar
markn92 committed


class LabelPriorityQueue(PriorityQueue):
markn92's avatar
markn92 committed
    def __init__(self, f_soc: SoCFunctionFactory, l_set: Set[Label]):
markn92's avatar
markn92 committed
        super().__init__()
markn92's avatar
markn92 committed
        self.f_soc_factory: SoCFunctionFactory = f_soc
markn92's avatar
markn92 committed
        self.l_set: Set[Label] = l_set
markn92's avatar
markn92 committed

    def insert(self, label: Label):
markn92's avatar
markn92 committed
        """Breaking ties with lowest soc at t_min."""
markn92's avatar
markn92 committed
        soc_function = self.f_soc_factory(label)
markn92's avatar
markn92 committed
        t_min: Time = soc_function.minimum

        # Might happen because of dummy charge stations
        if t_min == -inf:
            raise ValueError('Infeasible label.')

        soc_min: SoC = soc_function(t_min)

        super().insert(
            item=label,
            priority=t_min,
            count=soc_min
        )
markn92's avatar
markn92 committed

        if self.peak_min() == label:
            self.dominance_check()

    def delete_min(self) -> Any:
        min_label = super().delete_min()
        self.dominance_check()
        return min_label

    def dominance_check(self):
        try:
            label: Label = self.peak_min()
        except KeyError:
            return

markn92's avatar
markn92 committed
        soc = self.f_soc_factory(label)
markn92's avatar
markn92 committed

        for other_label in self.l_set:
markn92's avatar
markn92 committed
            if self.f_soc_factory(other_label) > soc:
markn92's avatar
markn92 committed
                self.remove_item(label)
                return