Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/game/DRT/randomRides.py
169679 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2010-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 randomRides.py
15
# @author Jakob Erdmann
16
# @date 2019-02-24
17
18
from __future__ import print_function
19
from __future__ import absolute_import
20
import os
21
import sys
22
import random
23
import optparse
24
25
if 'SUMO_HOME' in os.environ:
26
sys.path.append(os.path.join(os.environ['SUMO_HOME'], 'tools'))
27
import sumolib # noqa
28
29
30
def get_options(args=None):
31
optParser = optparse.OptionParser()
32
optParser.add_option("-n", "--net-file", dest="netfile",
33
help="define the net file")
34
optParser.add_option("-a", "--additional-files", dest="additional",
35
help="define additional files for loading busStops (mandatory)")
36
optParser.add_option("-o", "--output-file", dest="outfile",
37
help="define the output trip filename")
38
optParser.add_option("--poi-output", dest="poiout",
39
help="define the output file for busStop pois")
40
optParser.add_option("--prefix", dest="tripprefix",
41
default="", help="prefix for the trip ids")
42
optParser.add_option("-t", "--trip-attributes", dest="tripattrs",
43
default="", help="additional trip attributes. When generating pedestrians, attributes for " +
44
"<person> and <walk> are supported.")
45
optParser.add_option("-b", "--begin", type="float", default=0, help="begin time")
46
optParser.add_option("-e", "--end", type="float", default=3600, help="end time (default 3600)")
47
optParser.add_option("--poi-offset", dest="poiOffset", type="float",
48
default=12, help="offset of stop-poi from the lane in m")
49
optParser.add_option("--initial-duration", dest="duration", type="int", default=5, help="inital stop duration in s")
50
optParser.add_option("-p", "--period", type="float", default=1,
51
help="Generate vehicles with equidistant departure times and period=FLOAT (default 1.0).")
52
optParser.add_option("-s", "--seed", type="int", help="random seed")
53
optParser.add_option("--min-distance", type="float", dest="min_distance",
54
default=0.0, help="require start and end edges for each trip to be at least <FLOAT> m apart")
55
optParser.add_option("--max-distance", type="float", dest="max_distance",
56
default=None, help="require start and end edges for each trip to be at most <FLOAT> m " +
57
"apart (default 0 which disables any checks)")
58
optParser.add_option("-v", "--verbose", action="store_true",
59
default=False, help="tell me what you are doing")
60
(options, args) = optParser.parse_args(args=args)
61
62
if not options.additional or not options.outfile:
63
optParser.print_help()
64
sys.exit(1)
65
66
if options.period <= 0:
67
print("Error: Period must be positive", file=sys.stderr)
68
sys.exit(1)
69
70
if options.poiout is not None and options.netfile is None:
71
print("Error: poi-output requires a net-file", file=sys.stderr)
72
sys.exit(1)
73
74
return options
75
76
77
def main(options):
78
if options.seed:
79
random.seed(options.seed)
80
busStops = [bs.id for bs in sumolib.xml.parse_fast(options.additional, 'busStop', ['id'])]
81
stopColors = {}
82
if options.poiout:
83
colorgen = sumolib.miscutils.Colorgen(('distinct', 'distinct', 'distinct'))
84
net = sumolib.net.readNet(options.netfile)
85
with open(options.poiout, 'w') as outf:
86
outf.write('<additional>\n')
87
for bs in sumolib.xml.parse(options.additional, 'busStop'):
88
laneShape = net.getLane(bs.lane).getShape()
89
sideShape = sumolib.geomhelper.move2side(laneShape, options.poiOffset)
90
offset = (float(bs.startPos) + float(bs.endPos)) / 2
91
x, y = sumolib.geomhelper.positionAtShapeOffset(sideShape, offset)
92
stopColors[bs.id] = colorgen()
93
outf.write(' <poi id="%s" x="%s" y="%s" color="%s" type="%s"/>\n' % (
94
bs.id, x, y, stopColors[bs.id], bs.attr_name))
95
outf.write('</additional>\n')
96
97
if len(busStops) < 2:
98
print("Error: At least two busStops are required", file=sys.stderr)
99
sys.exit(1)
100
101
depart = options.begin
102
idx = 0
103
with open(options.outfile, 'w') as outf:
104
outf.write('<routes>\n')
105
while depart < options.end:
106
bsFrom = random.choice(busStops)
107
bsTo = random.choice(busStops)
108
while bsTo == bsFrom:
109
bsTo = random.choice(busStops)
110
color = ""
111
if options.poiout:
112
color = ' color="%s"' % stopColors[bsTo]
113
outf.write(' <person id="%s%s" depart="%s"%s>\n' % (
114
options.tripprefix, idx, depart, color))
115
outf.write(' <stop busStop="%s" duration="%s"/>\n' % (bsFrom, options.duration))
116
outf.write(' <ride busStop="%s" lines="ANY"/>\n' % (bsTo))
117
outf.write(' </person>\n')
118
depart += options.period
119
idx += 1
120
outf.write('</routes>\n')
121
122
123
if __name__ == "__main__":
124
if not main(get_options()):
125
sys.exit(1)
126
127