Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/route/addStopDelay.py
169674 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2012-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 addStopDelay.py
15
# @author Jakob Erdmann
16
# @date 2020-08-25
17
18
"""
19
Increase stop duration by a fixed or random time
20
"""
21
22
from __future__ import absolute_import
23
from __future__ import print_function
24
25
import os
26
import sys
27
import random
28
import re
29
30
if 'SUMO_HOME' in os.environ:
31
sys.path.append(os.path.join(os.environ['SUMO_HOME'], 'tools'))
32
import sumolib # noqa
33
from sumolib.miscutils import parseTime # noqa
34
35
36
def get_options(args=None):
37
parser = sumolib.options.ArgumentParser(description="Sample routes to match counts")
38
parser.add_argument("-r", "--route-file", category='input', dest="routeFile",
39
help="Input route file")
40
parser.add_argument("-o", "--output-file", category='output', dest="out", default="out.rou.xml",
41
help="Output route file")
42
parser.add_argument("-s", "--seed", type=int, default=42,
43
help="random seed")
44
parser.add_argument("-p", "--probability", type=float, default=1,
45
help="apply delay with the given probability ]0, 1]")
46
parser.add_argument("--min", type=float, default=0,
47
help="minimum delay value (default 0)")
48
parser.add_argument("--max", type=float, default=3600,
49
help="maximum delay value (default 3600)")
50
parser.add_argument("--mean", type=float, default=0,
51
help="mean delay (default 0)")
52
parser.add_argument("--dev", type=float, default=60,
53
help="mean standard deviation of delay (default 60)")
54
55
options = parser.parse_args(args=args)
56
if options.routeFile is None:
57
parser.print_help()
58
sys.exit()
59
60
if options.min != options.max and (
61
options.min > options.mean or options.mean > options.max):
62
sys.stderr.write("mean should lie between min and max")
63
sys.exit()
64
65
return options
66
67
68
def main(options):
69
if options.seed:
70
random.seed(options.seed)
71
72
with open(options.routeFile) as routes, open(options.out, 'w') as outf:
73
pat = re.compile(r'(.* duration=")([^"]*)(".*)')
74
for line in routes:
75
if "<stop" in line and "duration" in line:
76
if random.random() < options.probability:
77
if options.min == options.max:
78
delay = options.min
79
else:
80
delay = None
81
while (delay is None
82
or delay < options.min
83
or delay > options.max):
84
delay = random.gauss(options.mean, options.dev)
85
86
mo = pat.match(line)
87
newDuration = int((parseTime(mo.group(2)) + delay))
88
line = mo.group(1) + str(newDuration) + mo.group(3) + "\n"
89
outf.write(line)
90
91
92
if __name__ == "__main__":
93
main(get_options())
94
95