Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/complex/traffic_lights/loop_flow/runner.py
169685 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2007-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 runner.py
15
# @author Daniel Krajzewicz
16
# @date 2007-07-26
17
18
19
from __future__ import absolute_import
20
from __future__ import print_function
21
import sys
22
import os
23
import subprocess
24
import random
25
import shutil
26
27
random.seed(42)
28
if "SUMO_HOME" in os.environ:
29
sys.path.append(os.path.join(os.environ["SUMO_HOME"], "tools"))
30
import sumolib # noqa
31
32
types = ["static", "actuated", "sotl_phase", "sotl_platoon", "sotl_request", "sotl_wave", "sotl_marching", "swarm"]
33
tlType = sys.argv[1]
34
flow1def = "0;2000;600".split(";")
35
flow2def = "0;2000;600".split(";")
36
fillSteps = 120 # 3600
37
measureSteps = 600 # 36000
38
simSteps = fillSteps + measureSteps
39
40
41
def buildDemand(simSteps, pWE, pEW, pNS, pSN):
42
fd = open("input_routes.rou.xml", "w")
43
# ---routes---
44
print("""<routes>
45
46
<vType id="type1" accel="2.0" decel="5.0" sigma="0.0" length="6.5" maxSpeed="70"/>
47
48
<route id="WE" edges="1i 3o 5o"/>
49
<route id="NS" edges="2i 4o 6o"/>
50
<route id="EW" edges="3i 1o 7o"/>
51
<route id="SN" edges="4i 2o 8o"/>
52
53
""", file=fd)
54
vehNr = 0
55
for i in range(simSteps):
56
if random.uniform(0, 1) < pWE: # Poisson distribution
57
print(' <vehicle id="%i" type="type1" route="WE" depart="%i" departSpeed="13.89" />' % (
58
vehNr, i), file=fd)
59
vehNr += 1
60
if random.uniform(0, 1) < pNS:
61
print(' <vehicle id="%i" type="type1" route="NS" depart="%i" departSpeed="13.89" />' % (
62
vehNr, i), file=fd)
63
vehNr += 1
64
if random.uniform(0, 1) < pEW:
65
print(' <vehicle id="%i" type="type1" route="EW" depart="%i" departSpeed="13.89" />' % (
66
vehNr, i), file=fd)
67
vehNr += 1
68
if random.uniform(0, 1) < pSN:
69
print(' <vehicle id="%i" type="type1" route="SN" depart="%i" departSpeed="13.89" />' % (
70
vehNr, i), file=fd)
71
vehNr += 1
72
print("</routes>", file=fd)
73
fd.close()
74
75
76
def patchTLSType(ifile, itype, ofile, otype):
77
fdi = open(ifile)
78
fdo = open(ofile, "w")
79
for line in fdi:
80
line = line.replace(itype, otype)
81
fdo.write(line)
82
fdo.close()
83
fdi.close()
84
85
86
def main():
87
try:
88
os.mkdir("results")
89
except OSError:
90
pass
91
try:
92
os.mkdir("gfx")
93
except OSError:
94
pass
95
96
sumo = sumolib.checkBinary('sumo')
97
98
for f1 in range(int(flow1def[0]), int(flow1def[1]), int(flow1def[2])):
99
pWE = float(f1) / 3600 # [veh/s]
100
pEW = pWE
101
for f2 in range(int(flow2def[0]), int(flow2def[1]), int(flow2def[2])):
102
pNS = float(f2) / 3600 # [veh/s]
103
pSN = pNS
104
print("Computing for %s<->%s" % (f1, f2))
105
buildDemand(simSteps, pWE, pEW, pNS, pSN)
106
print(" for tls-type %s" % tlType)
107
patchTLSType('input_additional_template.add.xml',
108
'%tls_type%', 'input_additional.add.xml', tlType)
109
args = [sumo,
110
'--no-step-log',
111
# '--no-duration-log',
112
# '--verbose',
113
# '--duration-log.statistics',
114
'--default.speeddev', '0',
115
'--net-file', 'input_net.net.xml',
116
'--route-files', 'input_routes.rou.xml',
117
'--additional-files', 'input_additional.add.xml',
118
'--tripinfo-output', 'results/tripinfos_%s_%s_%s.xml' % (
119
tlType, f1, f2),
120
'--summary-output', 'results/summary_%s_%s_%s.xml' % (
121
tlType, f1, f2),
122
'--device.emissions.probability', '1',
123
'--queue-output', 'results/queue_%s_%s_%s.xml' % (
124
tlType, f1, f2),
125
'--statistic-output', 'statistics.xml',
126
]
127
subprocess.call(args)
128
shutil.move(
129
"results/e2_output.xml", "results/e2_output_%s_%s_%s.xml" %
130
(tlType, f1, f2))
131
shutil.move("results/e2_tl0_output.xml",
132
"results/e2_tl0_output_%s_%s_%s.xml" % (tlType, f1, f2))
133
shutil.move("results/edgeData_3600.xml",
134
"results/edgeData_3600_%s_%s_%s.xml" % (tlType, f1, f2))
135
shutil.move("results/laneData_3600.xml",
136
"results/laneData_3600_%s_%s_%s.xml" % (tlType, f1, f2))
137
shutil.move("results/edgesEmissions_3600.xml",
138
"results/edgesEmissions_3600_%s_%s_%s.xml" % (tlType, f1, f2))
139
shutil.move("results/lanesEmissions_3600.xml",
140
"results/lanesEmissions_3600_%s_%s_%s.xml" % (tlType, f1, f2))
141
shutil.move(
142
"results/TLSStates.xml", "results/TLSStates_%s_%s_%s.xml" %
143
(tlType, f1, f2))
144
shutil.move("results/TLSSwitchTimes.xml",
145
"results/TLSSwitchTimes_%s_%s_%s.xml" % (tlType, f1, f2))
146
shutil.move("results/TLSSwitchStates.xml",
147
"tls_state.xml")
148
149
150
if __name__ == "__main__":
151
main()
152
153