Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/game/minwait.py
169674 views
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
4
# Copyright (C) 2010-2025 German Aerospace Center (DLR) and others.
5
# This program and the accompanying materials are made available under the
6
# terms of the Eclipse Public License 2.0 which is available at
7
# https://www.eclipse.org/legal/epl-2.0/
8
# This Source Code may also be made available under the following Secondary
9
# Licenses when the conditions for such availability set forth in the Eclipse
10
# Public License 2.0 are satisfied: GNU General Public License, version 2
11
# or later which is available at
12
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
13
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
14
15
# @file minwait.py
16
# @author Michael Behrisch
17
# @date 2023-03-29
18
19
"""
20
This script runs the gaming scenarios which have a custom tls file
21
with the "actuated" and the "delay_based" control and inserts the resulting scores
22
as a reference into the refscores.pkl file.
23
"""
24
from __future__ import absolute_import
25
from __future__ import print_function
26
import os
27
import subprocess
28
import sys
29
import glob
30
import pickle
31
import re
32
33
from runner import computeScore, parseEndTime, REFSCOREFILE
34
35
SUMO_HOME = os.environ.get('SUMO_HOME',
36
os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..'))
37
sys.path.append(os.path.join(SUMO_HOME, 'tools'))
38
import sumolib # noqa
39
40
41
def computeHighScore(scen, high, alg=""):
42
score = computeScore(scen)
43
high[scen].append(("SUMO " + alg, "", score[0]))
44
high[scen].sort(key=lambda x: -x[2])
45
46
47
def main():
48
base = os.path.dirname(os.path.abspath(__file__))
49
os.makedirs(os.path.join(base, "tls"), exist_ok=True)
50
os.makedirs(os.path.join(base, "output"), exist_ok=True)
51
high = {}
52
for config in sorted(glob.glob(os.path.join(base, "*.sumocfg"))):
53
scen = os.path.basename(config[:-8])
54
if "demo" in scen or "bs3" in scen:
55
continue
56
tls = None
57
add = []
58
for a in sumolib.xml.parse_fast(config, "additional-files", "value"):
59
for f in a.value.split(","):
60
add.append(os.path.join(base, f))
61
if ".tll" in f or ".tls" in f:
62
tls = add[-1]
63
if tls:
64
with open(tls) as tls_in:
65
lines = tls_in.readlines()
66
else:
67
lines = []
68
for n in sumolib.xml.parse_fast(config, "net-file", "value"):
69
net_file = os.path.join(base, n.value)
70
net = sumolib.net.readNet(net_file, withPrograms=True)
71
for t in net.getTrafficLights():
72
xml = t.toXML().replace('programID="0"', 'programID="1"')
73
if 'duration="1000' in xml:
74
if not lines:
75
lines.append("<a>")
76
lines += xml.splitlines()
77
if lines:
78
lines.append("</a>")
79
tls = net_file
80
if lines:
81
high[scen] = []
82
for alg, minDur in (("actuated", 3), ("delay_based", 1), ("no_switch", 0)):
83
print("running scenario '%s' with algorithm '%s'" % (scen, alg))
84
if minDur:
85
with open(os.path.join(base, "tls", ".".join((os.path.basename(tls), scen, alg))), "w") as tls_out:
86
for line in lines:
87
line = line.replace('type="static"', 'type="%s"' % alg)
88
if "phase" in line:
89
line = re.sub(r'duration="1000\d+',
90
'duration="10" minDur="%s" maxDur="10000' % minDur, line)
91
tls_out.write(line)
92
addStr = ",".join(add)
93
if minDur:
94
if tls in add:
95
addStr = addStr.replace(tls, tls_out.name)
96
else:
97
addStr += "," + tls_out.name
98
subprocess.call([sumolib.checkBinary('sumo'), "-c", config,
99
"-a", addStr,
100
'--output-prefix', os.path.join("output", scen + "."),
101
'-l', 'log', '--duration-log.statistics',
102
'--statistic-output', 'stats.xml',
103
'-v', 'false', '--no-warnings', '--no-step-log',
104
'--tripinfo-output.write-unfinished'], cwd=base)
105
computeHighScore(scen, high, alg)
106
if scen == "rail":
107
high[scen] = []
108
print("running scenario 'rail'")
109
subprocess.call([sumolib.checkBinary('sumo'), "-c", config.replace("rail", "rail_demo"),
110
'--output-prefix', os.path.join("output", scen + "."),
111
'-l', "log", '--duration-log.statistics',
112
'--statistic-output', 'stats.xml',
113
'-v', 'false', '--no-warnings', '--no-step-log',
114
'--tripinfo-output.write-unfinished', "-e", str(parseEndTime(config))], cwd=base)
115
computeHighScore(scen, high)
116
print(high)
117
with open(os.path.join(base, REFSCOREFILE), 'wb') as pkl:
118
pickle.dump(high, pkl)
119
120
121
if __name__ == "__main__":
122
main()
123
124