Reputation: 704
I would like to use networkx to calculate energy-efficient routes. To do that I thought about modelling battery constraints as edge cost functions. In the example below, I would like to calculate the shortest route between s and t depending of the battery load of my vehicle (b
). The constraints of my network will determine the edge cost function.
For example between s
and v1
, the edge cost will be 18 if the battery load at s
is superior to 18 or (close to infinite) if it is less than 18.
Is it possible to define edge cost functions that will be used by shortest path algorithms in networkx?
Or do you think the best solution would be to create a new MultiDiGraph where all the different possible edge costs are recalculated according to the initial charge at the starting point?
Edit 1: To simplify the problem we can consider the negative weights (corresponding to a recharge of the battery) as equal to zero. In a second time, I will see how to use Johnson's algorithm. It seems to be implemented in networkx (https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.shortest_paths.weighted.johnson.html)
Edit 2: The maximal battery load is 20 which explains why the battery load didn't increase between s and v_3 and why it is impossible to reach t through s->v3->v2
Upvotes: 0
Views: 760
Reputation: 20457
For example
C:\Users\James\code\PathFinder\bin>gui
l s v1 38
l s v3 0
l v1 v2 0
l v3 v2 38
l v2 t 23
s s
e t
s -> v3 -> v2 -> t ->
( PathFinder is a C++ wrapper for the boost::graph impementation of Dijsktra )
So, now we have a new previously hidden constraint that the battery capacity has a maximum of 20. So I propose another aproach which should be proof against any further constraints that might pop up. It is based on the ideas of the A* algorithm
Reformulate the problem so that the recharging stations are at nodes and the links betwen the recharging stations have constant positive costs. ( This seems to me a more natural model of the real world )
Use Dijsktra, considering only link costs, to find the cheapest path to the destination from every node connected to the current done ( initially the starting node )
For every node connected to the current, sum the cost of the link from the current, the cost to the destination calculated in 1 minus any recharging that the node offers.
From every node that has been costed in 3, choose the cheapest. Keep track of the path used to reach this node and make it the current.
Repeat from step 2 until the destination node is reached.
This is a very specialised problem, ao I will not be adding it to the PathFinder options. If it represents a serious real world problem, not just homework or an acedemic exerise, open a issue on the PathFinder github repository and we can discuss details of developing a one-off application for this.
Upvotes: 1