Skip to content
Snippets Groups Projects
Commit fabb001c authored by markn92's avatar markn92
Browse files

add bool function to queue

parent 2cfd3e35
No related branches found
No related tags found
No related merge requests found
...@@ -52,12 +52,8 @@ def shortest_path(G: nx.Graph, charging_stations: set, s: Node, t: Node, ...@@ -52,12 +52,8 @@ def shortest_path(G: nx.Graph, charging_stations: set, s: Node, t: Node,
prio_queue = PriorityQueue() prio_queue = PriorityQueue()
prio_queue.insert(s, priority=0, count=0) prio_queue.insert(s, priority=0, count=0)
while True: while prio_queue:
try: minimum_node: Node = prio_queue.peak_min()
minimum_node: Node = prio_queue.peak_min()
except KeyError:
# empty queue
break
label_minimum_node: Label = l_uns[minimum_node].delete_min() label_minimum_node: Label = l_uns[minimum_node].delete_min()
l_set[minimum_node].add(label_minimum_node) 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, ...@@ -65,7 +61,7 @@ def shortest_path(G: nx.Graph, charging_stations: set, s: Node, t: Node,
if minimum_node == t: if minimum_node == t:
return f_soc_factory(label_minimum_node).minimum return f_soc_factory(label_minimum_node).minimum
# handle charging stations # Handle charging stations
if minimum_node in charging_stations and \ if minimum_node in charging_stations and \
not minimum_node == label_minimum_node.last_cs: not minimum_node == label_minimum_node.last_cs:
f_soc: SoCFunction = f_soc_factory(label_minimum_node) 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, ...@@ -75,6 +71,7 @@ def shortest_path(G: nx.Graph, charging_stations: set, s: Node, t: Node,
capacity=capacity) capacity=capacity)
if t_charge is not None: if t_charge is not None:
# Spawn new label at t_charge
l_uns[minimum_node].insert( l_uns[minimum_node].insert(
Label( Label(
t_trip=label_minimum_node.t_trip + t_charge, t_trip=label_minimum_node.t_trip + t_charge,
......
...@@ -48,3 +48,12 @@ class PriorityQueue: ...@@ -48,3 +48,12 @@ class PriorityQueue:
else: else:
heappop(self.pq) heappop(self.pq)
raise KeyError('Empty queue.') raise KeyError('Empty queue.')
def __bool__(self) -> bool:
"""Check if empty."""
try:
self.peak_min()
except KeyError:
return False
return True
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment