Path: blob/main/tests/complex/tutorial/traci_tls/runner.py
169685 views
#!/usr/bin/env python1# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2# Copyright (C) 2009-2025 German Aerospace Center (DLR) and others.3# This program and the accompanying materials are made available under the4# terms of the Eclipse Public License 2.0 which is available at5# https://www.eclipse.org/legal/epl-2.0/6# This Source Code may also be made available under the following Secondary7# Licenses when the conditions for such availability set forth in the Eclipse8# Public License 2.0 are satisfied: GNU General Public License, version 29# or later which is available at10# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later1213# @file runner.py14# @author Lena Kalleske15# @author Daniel Krajzewicz16# @author Michael Behrisch17# @author Jakob Erdmann18# @date 2009-03-261920from __future__ import absolute_import21from __future__ import print_function2223import os24import sys25import optparse26import random2728# we need to import python modules from the $SUMO_HOME/tools directory29if 'SUMO_HOME' in os.environ:30tools = os.path.join(os.environ['SUMO_HOME'], 'tools')31sys.path.append(tools)32else:33sys.exit("please declare environment variable 'SUMO_HOME'")3435from sumolib import checkBinary # noqa36import traci # noqa373839def generate_routefile():40random.seed(42) # make tests reproducible41N = 3600 # number of time steps42# demand per second from different directions43pWE = 1. / 1044pEW = 1. / 1145pNS = 1. / 3046with open("data/cross.rou.xml", "w") as routes:47print("""<routes>48<vType id="typeWE" accel="0.8" decel="4.5" sigma="0.5" length="5" minGap="2.5" maxSpeed="16.67" \49guiShape="passenger"/>50<vType id="typeNS" accel="0.8" decel="4.5" sigma="0.5" length="7" minGap="3" maxSpeed="25" guiShape="bus"/>5152<route id="right" edges="51o 1i 2o 52i" />53<route id="left" edges="52o 2i 1o 51i" />54<route id="down" edges="54o 4i 3o 53i" />""", file=routes)55vehNr = 056for i in range(N):57if random.uniform(0, 1) < pWE:58print(' <vehicle id="right_%i" type="typeWE" route="right" depart="%i" />' % (59vehNr, i), file=routes)60vehNr += 161if random.uniform(0, 1) < pEW:62print(' <vehicle id="left_%i" type="typeWE" route="left" depart="%i" />' % (63vehNr, i), file=routes)64vehNr += 165if random.uniform(0, 1) < pNS:66print(' <vehicle id="down_%i" type="typeNS" route="down" depart="%i" color="1,0,0"/>' % (67vehNr, i), file=routes)68vehNr += 169print("</routes>", file=routes)7071# The program looks like this72# <tlLogic id="0" type="static" programID="0" offset="0">73# the locations of the tls are NESW74# <phase duration="31" state="GrGr"/>75# <phase duration="6" state="yryr"/>76# <phase duration="31" state="rGrG"/>77# <phase duration="6" state="ryry"/>78# </tlLogic>798081def run():82"""execute the TraCI control loop"""83step = 084# we start with phase 2 where EW has green85traci.trafficlight.setPhase("0", 2)86while traci.simulation.getMinExpectedNumber() > 0:87traci.simulationStep()88if traci.trafficlight.getPhase("0") == 2:89# we are not already switching90if traci.inductionloop.getLastStepVehicleNumber("0") > 0:91# there is a vehicle from the north, switch92traci.trafficlight.setPhase("0", 3)93else:94# otherwise try to keep green for EW95traci.trafficlight.setPhase("0", 2)96step += 197traci.close()98sys.stdout.flush()99100101def get_options():102optParser = optparse.OptionParser()103optParser.add_option("--nogui", action="store_true",104default=False, help="run the commandline version of sumo")105options, args = optParser.parse_args()106return options107108109# this is the main entry point of this script110if __name__ == "__main__":111options = get_options()112113# this script has been called from the command line. It will start sumo as a114# server, then connect and run115if options.nogui:116sumoBinary = checkBinary('sumo')117else:118sumoBinary = checkBinary('sumo-gui')119120# first, generate the route file for this simulation121generate_routefile()122123# this is the normal way of using traci. sumo is started as a124# subprocess and then the python script connects and runs125traci.start([sumoBinary, "-c", "data/cross.sumocfg",126"--tripinfo-output", "tripinfo.xml"])127run()128129130