Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/purgatory/pedestrianFlow.py
169674 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2014-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 pedestrianFlow.py
15
# @author Jakob Erdmann
16
# @date 2014-01-16
17
18
from __future__ import absolute_import
19
import os
20
import sys
21
import random
22
from optparse import OptionParser
23
24
if 'SUMO_HOME' in os.environ:
25
tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
26
sys.path.append(tools)
27
import sumolib # noqa
28
from sumolib.miscutils import Colorgen # noqa
29
else:
30
sys.exit("please declare environment variable 'SUMO_HOME'")
31
32
33
def get_options():
34
optParser = OptionParser()
35
optParser.add_option(
36
"-w", "--width", type="float", default=0.7, help="pedestrian width, negative numbers denote the center " +
37
"of a uniform distribution [x-0.2, x+0.2]")
38
optParser.add_option(
39
"-l", "--length", type="float", default=0.5, help="pedestrian length, negative numbers denote the center " +
40
"of a uniform distribution [x-0.2, x+0.2]")
41
optParser.add_option(
42
"-m", "--minGap", type="float", default=0.2, help="pedestrian min gap, negative numbers denote the center " +
43
"of a uniform distribution [x-0.2, x+0.2]")
44
optParser.add_option(
45
"-s", "--maxSpeed", type="float", default=1.2, help="pedestrian max speed, negative numbers denote the " +
46
"center of a uniform distribution [x-0.4, x+0.4]")
47
optParser.add_option(
48
"-d", "--departPos", type="float", default=0, help="depart position")
49
optParser.add_option(
50
"-a", "--arrivalPos", type="float", default=-1, help="arrival position")
51
optParser.add_option(
52
"-p", "--prob", type="float", default=0.1, help="depart probability per second")
53
optParser.add_option("-r", "--route", help="edge list")
54
optParser.add_option("-t", "--trip", help="Define walking trip as FROM_EDGE,TO_EDGE")
55
optParser.add_option("-c", "--color", help="the color to use or 'random'")
56
optParser.add_option(
57
"-b", "--begin", type="int", default=0, help="begin time")
58
optParser.add_option(
59
"-e", "--end", type="int", default=600, help="end time")
60
optParser.add_option("-i", "--index", type="int",
61
default=0, help="starting index for naming pedestrians")
62
optParser.add_option(
63
"-n", "--name", default="p", help="base name for pedestrians")
64
(options, args) = optParser.parse_args()
65
66
if len(args) != 1:
67
sys.exit("Output file argument missing")
68
options.output = args[0]
69
70
if ((options.route is None and options.trip is None) or
71
(options.route is not None and options.trip is not None)):
72
sys.exit("Either --route or --trip must be given")
73
if options.route is not None:
74
options.route = options.route.split(',')
75
if options.trip is not None:
76
options.trip = tuple(options.trip.split(','))
77
if len(options.trip) != 2:
78
sys.exit("trip must contain of exactly two edges")
79
80
return options
81
82
83
def randomOrFixed(value, offset=0.2):
84
if value >= 0:
85
return value
86
return random.uniform(-value - offset, -value + offset)
87
88
89
def write_ped(f, index, options, depart):
90
if options.color is None:
91
color = ''
92
elif options.color == "random":
93
color = ' color="%s"' % Colorgen(("random", 1, 1))()
94
else:
95
color = ' color="%s"' % options.color
96
97
if options.trip is not None:
98
edges = 'from="%s" to="%s"' % options.trip
99
else:
100
edges = 'edges="%s"' % ' '.join(options.route)
101
102
f.write((' <vType id="%s%s" vClass="pedestrian" width="%s" length="%s" minGap="%s" maxSpeed="%s" ' +
103
'guiShape="pedestrian"%s/>\n') % (
104
options.name, index,
105
randomOrFixed(options.width),
106
randomOrFixed(options.length),
107
randomOrFixed(options.minGap),
108
randomOrFixed(options.maxSpeed, 0.4), color))
109
f.write(' <person id="%s%s" type="%s%s" depart="%s" departPos="%s">\n' %
110
(options.name, index, options.name, index, depart, options.departPos))
111
f.write(' <walk %s arrivalPos="%s"/>\n' %
112
(edges, options.arrivalPos))
113
f.write(' </person>\n')
114
115
116
def main():
117
options = get_options()
118
with open(options.output, 'w') as f:
119
sumolib.writeXMLHeader(f, "$Id$", "routes") # noqa
120
index = options.index
121
for depart in range(options.begin, options.end):
122
if random.random() < options.prob:
123
write_ped(
124
f, index, options, depart)
125
index += 1
126
f.write('</routes>')
127
128
129
if __name__ == "__main__":
130
main()
131
132