Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/route/route2alts.py
169674 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.
4
# This program and the accompanying materials are made available under the
5
# terms of the Eclipse Public License 2.0 which is available at
6
# https://www.eclipse.org/legal/epl-2.0/
7
# This Source Code may also be made available under the following Secondary
8
# Licenses when the conditions for such availability set forth in the Eclipse
9
# Public License 2.0 are satisfied: GNU General Public License, version 2
10
# or later which is available at
11
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
14
# @file route2alts.py
15
# @author Daniel Krajzewicz
16
# @author Michael Behrisch
17
# @date 11.09.2009
18
19
"""
20
Counts possible routes for all depart/arrival edges.
21
Builds route alternatives assigning the so determined probabilities to use a route.
22
23
Please note that the cost of the route is not computed!
24
"""
25
from __future__ import absolute_import
26
from __future__ import print_function
27
import sys
28
import os
29
from xml.sax import make_parser, handler
30
31
if 'SUMO_HOME' in os.environ:
32
tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
33
sys.path.append(os.path.join(tools))
34
from sumolib.options import ArgumentParser
35
else:
36
sys.exit("please declare environment variable 'SUMO_HOME'")
37
38
39
def get_options(args=None):
40
optParser = ArgumentParser()
41
optParser.add_argument("input", category='input', help="Provide an input network")
42
optParser.add_argument("output", category='output', help="Provide an output name")
43
return optParser.parse_args(args=args)
44
45
46
class RouteCounter(handler.ContentHandler):
47
48
def __init__(self):
49
self._routeCounts = {}
50
self._odCounts = {}
51
self._odRoutes = {}
52
53
def startElement(self, name, attrs):
54
if "edges" in attrs:
55
route = attrs["edges"]
56
edges = route.split(" ")
57
od = (edges[0], edges[-1])
58
# count od occurrences
59
if od in self._odCounts:
60
self._odCounts[od] = self._odCounts[od] + 1
61
else:
62
self._odCounts[od] = 1
63
# count route occurrences
64
if route in self._routeCounts:
65
self._routeCounts[route] = self._routeCounts[route] + 1
66
else:
67
self._routeCounts[route] = 1
68
# build map od->routes
69
if od not in self._odRoutes:
70
self._odRoutes[od] = []
71
if route not in self._odRoutes[od]:
72
self._odRoutes[od].append(route)
73
74
def endDocument(self):
75
self._odRouteProbs = {}
76
for od in self._odCounts:
77
self._odRouteProbs[od] = {}
78
absNo = float(self._odCounts[od])
79
for route in self._odRoutes[od]:
80
self._odRouteProbs[od][route] = float(
81
self._routeCounts[route]) / absNo
82
83
84
class RoutePatcher(handler.ContentHandler):
85
86
def __init__(self, stats, out):
87
self._stats = stats
88
self._out = out
89
90
def startElement(self, name, attrs):
91
if name != "route":
92
self._out.write('<' + name)
93
for a in attrs.keys():
94
self._out.write(' ' + a + '="' + attrs[a] + '"')
95
self._out.write('>')
96
else:
97
self._out.write('\n <routeDistribution last="0">\n')
98
route = attrs["edges"]
99
edges = route.split(" ")
100
od = (edges[0], edges[-1])
101
self._out.write(' <route cost="1" probability="' + str(
102
self._stats._odRouteProbs[od][route]) + '" edges="' + route + '"/>\n')
103
for r in self._stats._odRouteProbs[od]:
104
if r == route:
105
continue
106
else:
107
self._out.write(' <route cost="1" probability="' + str(
108
self._stats._odRouteProbs[od][r]) + '" edges="' + r + '"/>\n')
109
self._out.write(' </routeDistribution>\n ')
110
111
def endElement(self, name):
112
if name != "route":
113
self._out.write('</' + name + '>')
114
115
def characters(self, content):
116
self._out.write(content)
117
118
119
def main(options):
120
# if len(sys.argv) < 3:
121
# print("Usage: route2alts.py <INPUT_FILE> <OUTPUT_FILE>")
122
# sys.exit()
123
# count occurrences
124
print("Counting alternative occurrences...")
125
parser = make_parser()
126
counter = RouteCounter()
127
parser.setContentHandler(counter)
128
parser.parse(options.input)
129
# build alternatives
130
print("Building alternatives...")
131
with open(options.output, "w") as out:
132
parser = make_parser()
133
parser.setContentHandler(RoutePatcher(counter, out))
134
parser.parse(options.input)
135
136
137
if __name__ == "__main__":
138
main(get_options())
139
140