Newer
Older
def shortest_path(G: nx.Graph, charging_stations: set, s: Node, t: Node,
initial_soc: SoC, final_soc: SoC, capacity: SoC):
:param G:
:param charging_stations:
:param s:
:param t:
:param initial_soc:
:param final_soc:
:param capacity:
# Add node that is only connected to the final node and takes no time
# to travel but consumes exactly the amount of energy that should be
# left at t (final_soc). The node becomes the new final node.
dummy_final_node: Node = len(G)
G.add_node(dummy_final_node)
G.add_edge(t, dummy_final_node, weight=0, c=final_soc)
t = dummy_final_node
# Init factories
cf_map = ChargingFunctionMap(G=G, capacity=capacity, initial_soc=initial_soc)
f_soc_factory = SoCFunctionFactory(cf_map)
soc_profile_factory = SoCProfileFactory(G, capacity)
l_uns: Dict[int, LabelPriorityQueue] = {v: LabelPriorityQueue(f_soc_factory, l_set[v]) for v in G}
# Add dummy charging station with charging function
# cf(t) = initial_soc (ie charging coefficient is zero).
dummy_node: Node = len(G.nodes)
G.add_node(dummy_node, c=0)
charging_stations.add(dummy_node)
# Register dummy charging station as the last
# seen charging station before s.
l_uns[s].insert(Label(
t_trip=0,
soc_last_cs=initial_soc,
last_cs=dummy_node,
soc_profile_cs_v=soc_profile_factory(s)
))
# A priority queue defines which node to visit next.
# The key is the trip time.
prio_queue = PriorityQueue()
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)
if minimum_node in charging_stations and \
not minimum_node == label_minimum_node.last_cs:
f_soc: SoCFunction = f_soc_factory(label_minimum_node)
t_charge = f_soc.calc_optimal_t_charge(cf_map[minimum_node])
l_uns[minimum_node].insert(
Label(
t_trip=label_minimum_node.t_trip + t_charge,
soc_last_cs=f_soc(label_minimum_node.t_trip + t_charge),
last_cs=minimum_node,
soc_profile_cs_v=soc_profile_factory(minimum_node)
)
)
# Update priority queue. This node might have gotten a new
# minimum label spawned is th previous step.
try:
prio_queue.insert(
item=minimum_node,
**keys(f_soc_factory(l_uns[minimum_node].peak_min()))
)
except KeyError:
# l_uns[v] empty
prio_queue.delete_min()
for n in G.neighbors(minimum_node):
# Create SoC Profile for getting from minimum_node to n
soc_profile = label_minimum_node.soc_profile_cs_v + \
t_trip=label_minimum_node.t_trip + distance(G, minimum_node, n),
soc_last_cs=label_minimum_node.soc_last_cs,
last_cs=label_minimum_node.last_cs,
soc_profile_cs_v=soc_profile
# Infeasible because last_cs might be an
# dummy charging station. Therefore, the path might
# be infeasible even though one could reach it with a full
# battery, because charging is not possible at dummy
# stations.
#
# That means, the SoC and thereby the range is restricted
# to the SoC at the last cs (soc_last_cs).
continue
try:
is_new_min_label: bool = l_new == l_uns[n].peak_min()
except KeyError:
continue
if is_new_min_label:
prio_queue.insert(n, **keys(f_soc_factory(l_new)))