diff --git a/evrouting/charge/routing.py b/evrouting/charge/routing.py index b2d48d911fd2a89a087a7686e51dc18dc6f229f0..58ee2be4e0e61576d5451f72cee0b3fa8bfc6358 100644 --- a/evrouting/charge/routing.py +++ b/evrouting/charge/routing.py @@ -52,12 +52,8 @@ def shortest_path(G: nx.Graph, charging_stations: set, s: Node, t: Node, prio_queue = PriorityQueue() prio_queue.insert(s, priority=0, count=0) - while True: - try: - minimum_node: Node = prio_queue.peak_min() - except KeyError: - # empty queue - break + while prio_queue: + minimum_node: Node = prio_queue.peak_min() label_minimum_node: Label = l_uns[minimum_node].delete_min() l_set[minimum_node].add(label_minimum_node) @@ -65,7 +61,7 @@ def shortest_path(G: nx.Graph, charging_stations: set, s: Node, t: Node, if minimum_node == t: return f_soc_factory(label_minimum_node).minimum - # handle charging stations + # Handle charging stations if minimum_node in charging_stations and \ not minimum_node == label_minimum_node.last_cs: f_soc: SoCFunction = f_soc_factory(label_minimum_node) @@ -75,6 +71,7 @@ def shortest_path(G: nx.Graph, charging_stations: set, s: Node, t: Node, capacity=capacity) if t_charge is not None: + # Spawn new label at t_charge l_uns[minimum_node].insert( Label( t_trip=label_minimum_node.t_trip + t_charge, diff --git a/evrouting/utils.py b/evrouting/utils.py index 99a190c5058e269959a3884f6017a3d4a22c2b76..46baaa9f1df42043b2bb6e71b43c66d56eced8b2 100644 --- a/evrouting/utils.py +++ b/evrouting/utils.py @@ -48,3 +48,12 @@ class PriorityQueue: else: heappop(self.pq) raise KeyError('Empty queue.') + + def __bool__(self) -> bool: + """Check if empty.""" + try: + self.peak_min() + except KeyError: + return False + + return True