Skip to content

Commit

Permalink
better results for networks without internal edges, fix eclipse-sumo#…
Browse files Browse the repository at this point in the history
  • Loading branch information
behrisch committed Jun 2, 2020
1 parent 29d414d commit 238614b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
21 changes: 11 additions & 10 deletions tools/route/implausibleRoutes.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,21 @@ def get_options():
return options


def getRouteLength(net, edges):
return sum([net.getEdge(e).getLength() for e in edges])


class RouteInfo:
def __init__(self, route):
self.edges = route.edges.split()


def calcDistAndLoops(rInfo, net, options):
rInfo.airDist = euclidean(
net.getEdge(rInfo.edges[0]).getShape()[0],
net.getEdge(rInfo.edges[-1]).getShape()[-1])
rInfo.length = getRouteLength(net, rInfo.edges)
if net.hasInternal:
rInfo.airDist = euclidean(
net.getEdge(rInfo.edges[0]).getShape()[0],
net.getEdge(rInfo.edges[-1]).getShape()[-1])
else:
rInfo.airDist = euclidean(
net.getEdge(rInfo.edges[0]).getFromNode().getCoord(),
net.getEdge(rInfo.edges[-1]).getToNode().getCoord())
rInfo.length = sumolib.route.getLength(net, rInfo.edges)
rInfo.airDistRatio = rInfo.length / rInfo.airDist
rInfo.edgeLoop = False
rInfo.nodeLoop = False
Expand Down Expand Up @@ -147,7 +148,7 @@ def main():
options = get_options()
if options.verbose:
print("parsing network from", options.network)
net = readNet(options.network)
net = readNet(options.network, withInternal=True)
read = 0
routeInfos = {} # id-> RouteInfo
skipped = set()
Expand Down Expand Up @@ -232,7 +233,7 @@ def main():
oldCosts = float(routeAlts[0].cost)
newCosts = float(routeAlts[1].cost)
assert(routeAlts[0].edges.split() == routeInfos[vehicle.id].edges)
routeInfos[vehicle.id].shortest_path_distance = getRouteLength(net, routeAlts[1].edges.split())
routeInfos[vehicle.id].shortest_path_distance = sumolib.route.getLength(net, routeAlts[1].edges.split())
if oldCosts <= newCosts:
routeInfos[vehicle.id].detour = 0
routeInfos[vehicle.id].detourRatio = 1
Expand Down
27 changes: 27 additions & 0 deletions tools/sumolib/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,33 @@
from sumolib.geomhelper import polygonOffsetWithMinimumDistanceToPoint # noqa


def getLength(net, edges):
"""
Calculates the length of a route including internal edges.
The input network has to contain internal edges (withInternal needs to be set when parsing).
The list of edges can either contain edge objects or edge ids as strings.
If there is no connection between two consecutive edges, length 0 is assumed (no error is thrown).
If there are multiple connections of different length, the shortest is used.
"""
if len(edges) == 0:
return 0
if isinstance(edges[0], str):
edges = [net.getEdge(e) for e in edges]
last = edges[0]
length = last.getLength()
for e in edges[1:]:
if net.hasInternal:
minInternalCost = 1e400
for c in last.getConnections(e):
if c.getViaLaneID() != "":
minInternalCost = min(minInternalCost, net.getLane(c.getViaLaneID()).getLength())
if minInternalCost < 1e400:
length += minInternalCost
length += e.getLength()
last = e
return length


def _getMinPath(paths):
minDist = 1e400
minPath = None
Expand Down

0 comments on commit 238614b

Please sign in to comment.