From fabb001cfe3a5f3f120f068c340de32982fdfaa4 Mon Sep 17 00:00:00 2001 From: "niehues.mark@gmail.com" <niehues.mark@gmail.com> Date: Thu, 19 Mar 2020 17:33:15 +0100 Subject: [PATCH] add bool function to queue --- evrouting/charge/routing.py | 11 ++++------- evrouting/utils.py | 9 +++++++++ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/evrouting/charge/routing.py b/evrouting/charge/routing.py index b2d48d9..58ee2be 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 99a190c..46baaa9 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 -- GitLab