Path: blob/main/tests/complex/traci/trafficlight/actuated/runner.py
169689 views
#!/usr/bin/env python1# -*- coding: utf-8 -*-2# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo3# Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.4# This program and the accompanying materials are made available under the5# terms of the Eclipse Public License 2.0 which is available at6# https://www.eclipse.org/legal/epl-2.0/7# This Source Code may also be made available under the following Secondary8# Licenses when the conditions for such availability set forth in the Eclipse9# Public License 2.0 are satisfied: GNU General Public License, version 210# or later which is available at11# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html12# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later1314# @file runner.py15# @author Michael Behrisch16# @author Daniel Krajzewicz17# @date 2011-03-04181920from __future__ import print_function21from __future__ import absolute_import22import os23import sys2425if "SUMO_HOME" in os.environ:26sys.path.append(os.path.join(os.environ["SUMO_HOME"], "tools"))27import traci # noqa28import sumolib # noqa2930tlsID = "C"31traci.start([sumolib.checkBinary('sumo'), "-c", "sumo.sumocfg"] + sys.argv[1:])3233logics = traci.trafficlight.getAllProgramLogics(tlsID)34programID = traci.trafficlight.getProgram(tlsID)35logic = None36for cand in logics:37if cand.programID == programID:38logic = cand3940assert logic41numPhases = len(logic.phases)42print("current program '%s' has %s phases" % (programID, numPhases))434445def check():46print("%s phase=%s" % (47traci.simulation.getTime(),48traci.trafficlight.getPhase(tlsID)))495051# default actuated52print("max-gap:", traci.trafficlight.getParameter(tlsID, "max-gap"))53# make the first actuated phase 2 seconds longer54traci.trafficlight.setPhaseDuration(tlsID, 7)55traci.trafficlight.setPhase(tlsID, 0)56for i in range(180):57check()58if i == 50:59phase = traci.trafficlight.getPhase(tlsID)60newPhase = (phase + 3) % numPhases61print("jumping from phase %s to %s" % (phase, newPhase))62traci.trafficlight.setPhase(tlsID, newPhase)63traci.trafficlight.setPhaseDuration(tlsID, 20)64traci.simulationStep()6566# change parameter67traci.trafficlight.setParameter(tlsID, "max-gap", "10")68print("max-gap:", traci.trafficlight.getParameter(tlsID, "max-gap"))69prolonged = False70for i in range(180):71if not prolonged and traci.trafficlight.getPhase(tlsID) == 0:72# make the first actuated phase shorter once73traci.trafficlight.setPhaseDuration(tlsID, 30)74prolonged = True75traci.simulationStep()7677print("cycleSecond (before):", traci.trafficlight.getParameter(tlsID, "cycleSecond"))78for key, value in [79("cycleTime", "55"),80("coordinated", "true"),81("offset", "12")]:82print("%s (before):" % key, traci.trafficlight.getParameter(tlsID, key))83traci.trafficlight.setParameter(tlsID, key, value)84print("%s (after):" % key, traci.trafficlight.getParameter(tlsID, key))85print("cycleSecond (after):", traci.trafficlight.getParameter(tlsID, "cycleSecond"))8687traci.close()888990