Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/assign/one-shot.py
169673 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 one-shot.py
15
# @author Daniel Krajzewicz
16
# @author Jakob Erdmann
17
# @author Yun-Pang Floetteroed
18
# @author Michael Behrisch
19
# @author Mirko Barthauer
20
# @date 2008-03-10
21
22
from __future__ import print_function
23
from __future__ import absolute_import
24
import os
25
import sys
26
import subprocess
27
from datetime import datetime
28
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
29
import sumolib # noqa
30
31
32
def call(command, log):
33
print("-" * 79, file=log)
34
print(command, file=log)
35
retCode = subprocess.call(command, stdout=log, stderr=log)
36
if retCode != 0:
37
print("Execution of %s failed. Look into %s for details." %
38
(command, log.name), file=sys.stderr)
39
sys.exit(retCode)
40
41
42
def writeSUMOConf(step, options, files):
43
fd = open("one_shot_" + str(step) + ".sumocfg", "w")
44
print("""<configuration>
45
<files>
46
<net-file value="%s"/>
47
<route-files value="%s"/>
48
<vehroutes value="vehroutes_%s.xml"/>""" % (options.net, files, step), file=fd)
49
if not options.noSummary:
50
print(' <summary value="summary_%s.xml"/>' % step, file=fd)
51
if not options.noTripinfo:
52
print(' <tripinfo value="tripinfo_%s.xml"/>' % step, file=fd)
53
if options.weightfiles:
54
print(' <weight-files value="%s"/>' %
55
options.weightfiles, file=fd)
56
57
add = 'dump_%s.add.xml' % step
58
if options.costmodifier != 'None':
59
add = '%s_dump_%s.add.xml' % (options.costmodifier, step)
60
if options.additional:
61
add += "," + options.additional
62
print(""" <additional-files value="%s"/>
63
</files>
64
<process>
65
<begin value="%s"/>
66
<route-steps value="%s"/>""" % (add, options.begin, options.routeSteps), file=fd)
67
68
if options.end:
69
print(' <end value="%s"/>' % options.end, file=fd)
70
if options.mesosim:
71
print(' <mesosim value="True"/>', file=fd)
72
if options.routingalgorithm:
73
print(' <routing-algorithm value="%s"/>' %
74
options.routingalgorithm, file=fd)
75
print(""" <device.rerouting.probability value="1"/>
76
<device.rerouting.period value="%s"/>
77
<device.rerouting.adaptation-interval value="%s"/>
78
<device.rerouting.with-taz value="%s"/>
79
<device.rerouting.explicit value="%s"/>
80
<vehroute-output.last-route value="%s"/>
81
<vehroute-output.exit-times value="%s"/>
82
<vehroute-output.sorted value="%s"/>
83
</process>
84
<reports>
85
<verbose value="True"/>
86
<no-warnings value="%s"/>
87
</reports>
88
</configuration>""" % (step, options.updateInterval, options.withtaz, options.reroutingexplicit, options.lastRoutes,
89
options.withexittime, options.routesorted, not options.withWarnings), file=fd)
90
fd.close()
91
if options.costmodifier != 'None':
92
fd = open("%s_dump_%s.add.xml" % (options.costmodifier, step), "w")
93
print("""<a>
94
<edgeData id="%s_dump_%s_%s" freq="%s" file="%s_dump_%s_%s.xml" excludeEmpty="true"/>
95
</a>""" % (options.costmodifier, step, options.aggregation, options.aggregation, options.costmodifier, step,
96
options.aggregation), file=fd)
97
fd.close()
98
else:
99
fd = open("dump_%s.add.xml" % step, "w")
100
print("""<a>
101
<edgeData id="dump_%s_%s" freq="%s" file="dump_%s_%s.xml" excludeEmpty="true"/>
102
</a>""" % (step, options.aggregation, options.aggregation, step, options.aggregation), file=fd)
103
fd.close()
104
105
106
parser = sumolib.options.ArgumentParser()
107
parser.add_argument("-W", "--with-warnings", action="store_true", dest="withWarnings",
108
default=False, help="enables warnings")
109
110
parser.add_argument("-n", "--net-file", dest="net", category="input", type=parser.net_file,
111
help="SUMO network (mandatory)", metavar="FILE")
112
parser.add_argument("-t", "--trips", dest="trips", category="input", type=parser.route_file,
113
help="trips in step 0", metavar="FILE")
114
115
parser.add_argument("-b", "--begin", dest="begin",
116
type=parser.time, default=0, help="Set simulation/routing begin")
117
parser.add_argument("-e", "--end", dest="end",
118
type=parser.time, help="Set simulation/routing end")
119
parser.add_argument("-R", "--route-steps", dest="routeSteps",
120
type=int, default=200, help="Set simulation route steps")
121
parser.add_argument("-a", "--aggregation", dest="aggregation",
122
type=parser.time, default=900, help="Set main weights aggregation period")
123
parser.add_argument("-f", "--frequencies", dest="frequencies", type=str,
124
default="-1,1800,300,15", help="Set the frequencies to iterate over")
125
parser.add_argument("-i", "--adaptation-interval", dest="updateInterval",
126
type=parser.time, default=1, help="Set edge weight adaptation interval")
127
128
parser.add_argument("-E", "--disable-summary", "--disable-emissions", action="store_true", dest="noSummary",
129
default=False, category="output", help="No summaries are written by the simulation")
130
parser.add_argument("-T", "--disable-tripinfos", action="store_true", dest="noTripinfo",
131
default=False, help="No tripinfos are written by the simulation")
132
parser.add_argument("-m", "--mesosim", action="store_true", dest="mesosim",
133
default=False, help="Whether mesosim shall be used")
134
parser.add_argument("-w", "--with-taz", action="store_true", dest="withtaz",
135
default=False, help="Whether districts shall be used")
136
parser.add_argument("-+", "--additional", dest="additional", category="input", type=parser.additional_file,
137
default="", help="Additional files")
138
parser.add_argument("-L", "--lastRoutes", action="store_true", dest="lastRoutes",
139
default=False, help="only save the last routes in the vehroute-output")
140
parser.add_argument("-F", "--weight-files", dest="weightfiles", type=parser.file,
141
help="Load edge/lane weights from FILE", metavar="FILE")
142
parser.add_argument("-A", "--routing-algorithm", dest="routingalgorithm", type=str,
143
choices=('dijkstra', 'astar'),
144
default="astar", help="type of routing algorithm [default: %(default)s]")
145
parser.add_argument("-r", "--rerouting-explicit", dest="reroutingexplicit", type=str,
146
default="", help="define the ids of the vehicles that should be re-routed.")
147
parser.add_argument("-x", "--with-exittime", action="store_true", dest="withexittime",
148
default=False, help="Write the exit times for all edges")
149
parser.add_argument("-s", "--route-sorted", action="store_true", dest="routesorted",
150
default=False, help="sorts the output by departure time")
151
parser.add_argument("-p", "--path", dest="path", type=parser.file, help="Path to binaries")
152
parser.add_argument("--cost-modifier", dest="costmodifier", type=str,
153
choices=('grohnde', 'isar', 'None'),
154
default='None', help="Whether to modify link travel costs of the given routes")
155
options = parser.parse_args()
156
157
sumo = "sumo"
158
if options.mesosim:
159
sumo = "meso"
160
sumoBinary = sumolib.checkBinary(sumo, options.path)
161
if options.costmodifier != 'None':
162
pyPath = os.path.abspath(os.path.dirname(sys.argv[0]))
163
sys.path.append(
164
os.path.join(pyPath, "..", "..", "..", "..", "..", "tools", "kkwSim"))
165
from kkwCostModifier import costModifier
166
print('use the cost modifier')
167
168
log = open("one_shot-log.txt", "w")
169
starttime = datetime.now()
170
for step in options.frequencies.split(","):
171
step = int(step)
172
print("> Running simulation with update frequency %s" % step)
173
btime = datetime.now()
174
print(">> Begin time %s" % btime)
175
if options.costmodifier != 'None':
176
currentDir = os.getcwd()
177
print(options.costmodifier)
178
outputfile = '%s_weights_%s.xml' % (options.costmodifier, step)
179
costModifier(outputfile, step, "dump", options.aggregation,
180
currentDir, options.costmodifier, 'one-shot')
181
writeSUMOConf(step, options, options.trips)
182
call([sumoBinary, "-c", "one_shot_%s.sumocfg" % step], log)
183
etime = datetime.now()
184
print(">> End time %s" % etime)
185
print("< Step %s ended (duration: %s)" % (step, etime - btime))
186
print("------------------\n")
187
print("one-shot ended (duration: %s)" % (datetime.now() - starttime))
188
189
log.close()
190
191