Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/complex/tutorial/traci_tls/runner.py
169685 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2009-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 runner.py
15
# @author Lena Kalleske
16
# @author Daniel Krajzewicz
17
# @author Michael Behrisch
18
# @author Jakob Erdmann
19
# @date 2009-03-26
20
21
from __future__ import absolute_import
22
from __future__ import print_function
23
24
import os
25
import sys
26
import optparse
27
import random
28
29
# we need to import python modules from the $SUMO_HOME/tools directory
30
if 'SUMO_HOME' in os.environ:
31
tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
32
sys.path.append(tools)
33
else:
34
sys.exit("please declare environment variable 'SUMO_HOME'")
35
36
from sumolib import checkBinary # noqa
37
import traci # noqa
38
39
40
def generate_routefile():
41
random.seed(42) # make tests reproducible
42
N = 3600 # number of time steps
43
# demand per second from different directions
44
pWE = 1. / 10
45
pEW = 1. / 11
46
pNS = 1. / 30
47
with open("data/cross.rou.xml", "w") as routes:
48
print("""<routes>
49
<vType id="typeWE" accel="0.8" decel="4.5" sigma="0.5" length="5" minGap="2.5" maxSpeed="16.67" \
50
guiShape="passenger"/>
51
<vType id="typeNS" accel="0.8" decel="4.5" sigma="0.5" length="7" minGap="3" maxSpeed="25" guiShape="bus"/>
52
53
<route id="right" edges="51o 1i 2o 52i" />
54
<route id="left" edges="52o 2i 1o 51i" />
55
<route id="down" edges="54o 4i 3o 53i" />""", file=routes)
56
vehNr = 0
57
for i in range(N):
58
if random.uniform(0, 1) < pWE:
59
print(' <vehicle id="right_%i" type="typeWE" route="right" depart="%i" />' % (
60
vehNr, i), file=routes)
61
vehNr += 1
62
if random.uniform(0, 1) < pEW:
63
print(' <vehicle id="left_%i" type="typeWE" route="left" depart="%i" />' % (
64
vehNr, i), file=routes)
65
vehNr += 1
66
if random.uniform(0, 1) < pNS:
67
print(' <vehicle id="down_%i" type="typeNS" route="down" depart="%i" color="1,0,0"/>' % (
68
vehNr, i), file=routes)
69
vehNr += 1
70
print("</routes>", file=routes)
71
72
# The program looks like this
73
# <tlLogic id="0" type="static" programID="0" offset="0">
74
# the locations of the tls are NESW
75
# <phase duration="31" state="GrGr"/>
76
# <phase duration="6" state="yryr"/>
77
# <phase duration="31" state="rGrG"/>
78
# <phase duration="6" state="ryry"/>
79
# </tlLogic>
80
81
82
def run():
83
"""execute the TraCI control loop"""
84
step = 0
85
# we start with phase 2 where EW has green
86
traci.trafficlight.setPhase("0", 2)
87
while traci.simulation.getMinExpectedNumber() > 0:
88
traci.simulationStep()
89
if traci.trafficlight.getPhase("0") == 2:
90
# we are not already switching
91
if traci.inductionloop.getLastStepVehicleNumber("0") > 0:
92
# there is a vehicle from the north, switch
93
traci.trafficlight.setPhase("0", 3)
94
else:
95
# otherwise try to keep green for EW
96
traci.trafficlight.setPhase("0", 2)
97
step += 1
98
traci.close()
99
sys.stdout.flush()
100
101
102
def get_options():
103
optParser = optparse.OptionParser()
104
optParser.add_option("--nogui", action="store_true",
105
default=False, help="run the commandline version of sumo")
106
options, args = optParser.parse_args()
107
return options
108
109
110
# this is the main entry point of this script
111
if __name__ == "__main__":
112
options = get_options()
113
114
# this script has been called from the command line. It will start sumo as a
115
# server, then connect and run
116
if options.nogui:
117
sumoBinary = checkBinary('sumo')
118
else:
119
sumoBinary = checkBinary('sumo-gui')
120
121
# first, generate the route file for this simulation
122
generate_routefile()
123
124
# this is the normal way of using traci. sumo is started as a
125
# subprocess and then the python script connects and runs
126
traci.start([sumoBinary, "-c", "data/cross.sumocfg",
127
"--tripinfo-output", "tripinfo.xml"])
128
run()
129
130