From 3475815f3f034def7908d3d610ba212c326f6188 Mon Sep 17 00:00:00 2001 From: "niehues.mark@gmail.com" <niehues.mark@gmail.com> Date: Wed, 18 Mar 2020 16:10:47 +0100 Subject: [PATCH] refactor --- evrouting/charge/routing.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/evrouting/charge/routing.py b/evrouting/charge/routing.py index eb1a24b..8a25a6c 100644 --- a/evrouting/charge/routing.py +++ b/evrouting/charge/routing.py @@ -29,6 +29,7 @@ def shortest_path(G: nx.Graph, charging_stations: set, s: Node, t: Node, cf = ChargingFunctionMap(G=G, capacity=capacity, initial_soc=initial_soc) label_factory = LabelsFactory(G, capacity, cf, initial_soc) + # Init maps to manage labels l_set: Dict[int, set] = {v: set() for v in G} l_uns: Dict[int, LabelPriorityQueue] = {v: LabelPriorityQueue() for v in G} @@ -36,12 +37,14 @@ def shortest_path(G: nx.Graph, charging_stations: set, s: Node, t: Node, entry_label = _create_entry_label(G, charging_stations, s, initial_soc, capacity) l_uns[s].insert(entry_label, cf[entry_label.last_cs]) - q = PriorityQueue() - q.insert(s, 0) + # A priority queue defines which node to visit next. + # The key is the trip time. + prio_queue = PriorityQueue() + prio_queue.insert(s, 0) while True: try: - minimum_node: Node = q.peak_min() + minimum_node: Node = prio_queue.peak_min() except KeyError: # empty queue break @@ -62,7 +65,7 @@ def shortest_path(G: nx.Graph, charging_stations: set, s: Node, t: Node, # Update priority queue. This node might have gotten a new # minimum label spawned is th previous step. - _update_priority_queue(q, l_uns, minimum_node) + _update_priority_queue(prio_queue, l_uns, minimum_node) # scan outgoing arcs for n in G.neighbors(minimum_node): @@ -91,7 +94,7 @@ def shortest_path(G: nx.Graph, charging_stations: set, s: Node, t: Node, pass else: if l_new == l_uns[n].peak_min(): - q.insert(n, l_new.key) + prio_queue.insert(n, l_new.key) def _create_entry_label( @@ -133,7 +136,7 @@ def _is_feasible_path(soc_profile: SoCProfile, capacity: SoC) -> bool: def _update_priority_queue( - q: PriorityQueue, + prio_queue: PriorityQueue, l_uns: Dict[int, LabelPriorityQueue], node: Node): """ @@ -144,6 +147,6 @@ def _update_priority_queue( minimum_label = l_uns[node].peak_min() except KeyError: # l_uns[v] empty - q.delete_min() + prio_queue.delete_min() else: - q.insert(node, minimum_label.key) + prio_queue.insert(node, minimum_label.key) -- GitLab