Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/averageTripStatistics.py
169659 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 averageTripStatistics.py
15
# @author Jakob Erdmann
16
# @date 2018-08-24
17
18
"""
19
This runs the given configuration multiple times with different random seeds and
20
averages the trip statistics output
21
"""
22
from __future__ import absolute_import
23
from __future__ import print_function
24
import sys
25
import random
26
27
from subprocess import check_output
28
import re
29
30
import sumolib
31
from sumolib.miscutils import Statistics
32
33
34
def getOptions(args=None):
35
optParser = sumolib.options.ArgumentParser()
36
optParser.add_option("-n", "--num-runs", type=int, default=10, dest="numRuns", help="repeat simulation INT times")
37
optParser.add_option("-s", "--seed", type=int, default=42, help="initial seed for generation random seeds")
38
optParser.add_option("--sumo", help="override sumo binary")
39
optParser.add_option("config", help="sumo config file")
40
options = optParser.parse_args(args=args)
41
random.seed(options.seed)
42
return options
43
44
45
def build_retriever(key):
46
reString = r" %s: ([\d.]*)" % key
47
regex = re.compile(reString)
48
# values should all be positive so -1 is a suitable flag
49
invalidResult = -1
50
51
def retriever(output):
52
foundStats = False
53
for line in output.split('\n'):
54
if 'Vehicles:' in line:
55
foundStats = True
56
elif foundStats and key in line:
57
reResult = regex.search(line)
58
if reResult is None:
59
print("could not find key '%s' in output '%s'" % (key, line))
60
return invalidResult
61
else:
62
return float(reResult.group(1))
63
return invalidResult
64
return retriever
65
66
67
def main(options):
68
if options.sumo is None:
69
SUMO = sumolib.checkBinary('sumo')
70
else:
71
SUMO = options.sumo
72
73
statsRetrievers = [(Statistics("%11s" % key), build_retriever(key)) for key in [
74
'Inserted',
75
'Running',
76
'RouteLength',
77
'Duration',
78
'WaitingTime',
79
'TimeLoss',
80
'DepartDelay',
81
]]
82
83
for i in range(options.numRuns):
84
sys.stdout.write('.')
85
sys.stdout.flush()
86
seed = str(int(random.random() * 2**31))
87
output = check_output([SUMO, '-c', options.config,
88
'--duration-log.statistics',
89
'--no-duration-log', 'false',
90
'--seed', seed], universal_newlines=True)
91
for stats, retriever in statsRetrievers:
92
stats.add(retriever(output), seed)
93
print()
94
for stats, retriever in statsRetrievers:
95
print(stats)
96
97
98
if __name__ == "__main__":
99
main(getOptions())
100
101