forked from eclipse-sumo/sumo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateRerouters.py
executable file
·132 lines (110 loc) · 5.29 KB
/
generateRerouters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
# Copyright (C) 2010-2024 German Aerospace Center (DLR) and others.
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0/
# This Source Code may also be made available under the following Secondary
# Licenses when the conditions for such availability set forth in the Eclipse
# Public License 2.0 are satisfied: GNU General Public License, version 2
# or later which is available at
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
# @file generateRerouters.py
# @author Jakob Erdmann
# @date 2023-02-07
"""
This script generates rerouters for a given list of closed edges and
automatically finds upstream rerouter edges that permit rerouting.
"""
from __future__ import print_function
from __future__ import absolute_import
import os
import sys
if 'SUMO_HOME' in os.environ:
sys.path.append(os.path.join(os.environ['SUMO_HOME'], 'tools'))
import sumolib # noqa
def get_options(args=None):
op = sumolib.options.ArgumentParser(description="Generate rerouter definition for closed edges",)
op.add_option("-n", "--net-file", category="input", dest="netfile", type=op.net_file,
help="define the net file (mandatory)", required=True)
op.add_option("-o", "--output-file", category="output", dest="outfile", type=op.additional_file,
help="define the output rerouter filename", default="rerouters.xml")
op.add_option("-x", "--closed-edges", category="input", dest="closedEdges", type=op.edge_list,
help="provide a comma-separated list of edges to close")
op.add_option("-i", "--id-prefix", category="processing", dest="idPrefix", default="rr",
help="id prefix for generated rerouters")
op.add_option("--vclass", category="processing", default="passenger",
help="only consider necessary detours for the given vehicle class (default passenger)")
op.add_option("--allow", category="processing", default="authority",
help="vClasses that shall be permitted on the closed edge")
op.add_option("--disallow", category="processing",
help="vClasses that shall be prohibited on the closed edge")
op.add_option("-b", "--begin", category="time", default=0, type=float,
help="begin time for the closing")
op.add_option("-e", "--end", category="time", default=86400, type=float,
help="end time for the closing (default 86400)")
options = op.parse_args(args=args)
if not options.netfile or not options.closedEdges:
op.print_help()
sys.exit(1)
options.closedEdges = options.closedEdges.split(',')
return options
def findNotifcationEdges(options, net, closedEdges):
result = set()
# close edges in the network
for e in closedEdges:
for lane in e.getLanes():
p = set(lane.getPermissions())
p.remove(options.vclass)
lane.setPermissions(p)
for e in closedEdges:
reachable = set()
for succ in e.getOutgoing().keys():
if succ.allows(options.vclass):
reachable.update(net.getReachable(succ, options.vclass))
upstream = []
seen = set()
for pred in e.getIncoming().keys():
if pred.allows(options.vclass):
upstream.append(pred)
while upstream and reachable:
cand = upstream.pop(0)
seen.add(cand)
reachable2 = net.getReachable(cand, options.vclass)
found = reachable2.intersection(reachable)
if found:
result.add(cand)
reachable.difference_update(found)
for pred in cand.getIncoming().keys():
if pred.allows(options.vclass):
if pred not in seen:
upstream.append(pred)
return result
def main(options):
net = sumolib.net.readNet(options.netfile)
closedEdges = []
for closedID in options.closedEdges:
if not net.hasEdge(closedID):
sys.exit("Unknown closed edge '%s'" % closedID)
closedEdges.append(net.getEdge(closedID))
allowDisallow = ""
if options.disallow is not None:
allowDisallow = ' disallow="%s"' % options.disallow
elif options.allow != "":
allowDisallow = ' allow="%s"' % options.allow
with open(options.outfile, 'w') as outf:
sumolib.writeXMLHeader(outf, "$Id$", "additional", options=options)
rerouterEdges = findNotifcationEdges(options, net, closedEdges)
rerouterEdgeIDs = sorted([e.getID() for e in rerouterEdges])
outf.write(' <rerouter id="%s" edges="%s">\n' % (
options.idPrefix,
' '.join(rerouterEdgeIDs)))
outf.write(' <interval begin="%s" end="%s">\n' % (options.begin, options.end))
for e in closedEdges:
outf.write(' <closingReroute id="%s"%s>\n' % (e.getID(), allowDisallow))
outf.write(' </interval>\n')
outf.write(' </rerouter>\n')
outf.write('</additional>\n')
if __name__ == "__main__":
main(get_options())