Path: blob/main/tests/complex/traffic_lights/loop_flow/runner.py
169685 views
#!/usr/bin/env python1# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2# Copyright (C) 2007-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 Daniel Krajzewicz15# @date 2007-07-26161718from __future__ import absolute_import19from __future__ import print_function20import sys21import os22import subprocess23import random24import shutil2526random.seed(42)27if "SUMO_HOME" in os.environ:28sys.path.append(os.path.join(os.environ["SUMO_HOME"], "tools"))29import sumolib # noqa3031types = ["static", "actuated", "sotl_phase", "sotl_platoon", "sotl_request", "sotl_wave", "sotl_marching", "swarm"]32tlType = sys.argv[1]33flow1def = "0;2000;600".split(";")34flow2def = "0;2000;600".split(";")35fillSteps = 120 # 360036measureSteps = 600 # 3600037simSteps = fillSteps + measureSteps383940def buildDemand(simSteps, pWE, pEW, pNS, pSN):41fd = open("input_routes.rou.xml", "w")42# ---routes---43print("""<routes>4445<vType id="type1" accel="2.0" decel="5.0" sigma="0.0" length="6.5" maxSpeed="70"/>4647<route id="WE" edges="1i 3o 5o"/>48<route id="NS" edges="2i 4o 6o"/>49<route id="EW" edges="3i 1o 7o"/>50<route id="SN" edges="4i 2o 8o"/>5152""", file=fd)53vehNr = 054for i in range(simSteps):55if random.uniform(0, 1) < pWE: # Poisson distribution56print(' <vehicle id="%i" type="type1" route="WE" depart="%i" departSpeed="13.89" />' % (57vehNr, i), file=fd)58vehNr += 159if random.uniform(0, 1) < pNS:60print(' <vehicle id="%i" type="type1" route="NS" depart="%i" departSpeed="13.89" />' % (61vehNr, i), file=fd)62vehNr += 163if random.uniform(0, 1) < pEW:64print(' <vehicle id="%i" type="type1" route="EW" depart="%i" departSpeed="13.89" />' % (65vehNr, i), file=fd)66vehNr += 167if random.uniform(0, 1) < pSN:68print(' <vehicle id="%i" type="type1" route="SN" depart="%i" departSpeed="13.89" />' % (69vehNr, i), file=fd)70vehNr += 171print("</routes>", file=fd)72fd.close()737475def patchTLSType(ifile, itype, ofile, otype):76fdi = open(ifile)77fdo = open(ofile, "w")78for line in fdi:79line = line.replace(itype, otype)80fdo.write(line)81fdo.close()82fdi.close()838485def main():86try:87os.mkdir("results")88except OSError:89pass90try:91os.mkdir("gfx")92except OSError:93pass9495sumo = sumolib.checkBinary('sumo')9697for f1 in range(int(flow1def[0]), int(flow1def[1]), int(flow1def[2])):98pWE = float(f1) / 3600 # [veh/s]99pEW = pWE100for f2 in range(int(flow2def[0]), int(flow2def[1]), int(flow2def[2])):101pNS = float(f2) / 3600 # [veh/s]102pSN = pNS103print("Computing for %s<->%s" % (f1, f2))104buildDemand(simSteps, pWE, pEW, pNS, pSN)105print(" for tls-type %s" % tlType)106patchTLSType('input_additional_template.add.xml',107'%tls_type%', 'input_additional.add.xml', tlType)108args = [sumo,109'--no-step-log',110# '--no-duration-log',111# '--verbose',112# '--duration-log.statistics',113'--default.speeddev', '0',114'--net-file', 'input_net.net.xml',115'--route-files', 'input_routes.rou.xml',116'--additional-files', 'input_additional.add.xml',117'--tripinfo-output', 'results/tripinfos_%s_%s_%s.xml' % (118tlType, f1, f2),119'--summary-output', 'results/summary_%s_%s_%s.xml' % (120tlType, f1, f2),121'--device.emissions.probability', '1',122'--queue-output', 'results/queue_%s_%s_%s.xml' % (123tlType, f1, f2),124'--statistic-output', 'statistics.xml',125]126subprocess.call(args)127shutil.move(128"results/e2_output.xml", "results/e2_output_%s_%s_%s.xml" %129(tlType, f1, f2))130shutil.move("results/e2_tl0_output.xml",131"results/e2_tl0_output_%s_%s_%s.xml" % (tlType, f1, f2))132shutil.move("results/edgeData_3600.xml",133"results/edgeData_3600_%s_%s_%s.xml" % (tlType, f1, f2))134shutil.move("results/laneData_3600.xml",135"results/laneData_3600_%s_%s_%s.xml" % (tlType, f1, f2))136shutil.move("results/edgesEmissions_3600.xml",137"results/edgesEmissions_3600_%s_%s_%s.xml" % (tlType, f1, f2))138shutil.move("results/lanesEmissions_3600.xml",139"results/lanesEmissions_3600_%s_%s_%s.xml" % (tlType, f1, f2))140shutil.move(141"results/TLSStates.xml", "results/TLSStates_%s_%s_%s.xml" %142(tlType, f1, f2))143shutil.move("results/TLSSwitchTimes.xml",144"results/TLSSwitchTimes_%s_%s_%s.xml" % (tlType, f1, f2))145shutil.move("results/TLSSwitchStates.xml",146"tls_state.xml")147148149if __name__ == "__main__":150main()151152153