Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/complex/traci/trafficlight/actuated/runner.py
169689 views
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
4
# Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.
5
# This program and the accompanying materials are made available under the
6
# terms of the Eclipse Public License 2.0 which is available at
7
# https://www.eclipse.org/legal/epl-2.0/
8
# This Source Code may also be made available under the following Secondary
9
# Licenses when the conditions for such availability set forth in the Eclipse
10
# Public License 2.0 are satisfied: GNU General Public License, version 2
11
# or later which is available at
12
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
13
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
14
15
# @file runner.py
16
# @author Michael Behrisch
17
# @author Daniel Krajzewicz
18
# @date 2011-03-04
19
20
21
from __future__ import print_function
22
from __future__ import absolute_import
23
import os
24
import sys
25
26
if "SUMO_HOME" in os.environ:
27
sys.path.append(os.path.join(os.environ["SUMO_HOME"], "tools"))
28
import traci # noqa
29
import sumolib # noqa
30
31
tlsID = "C"
32
traci.start([sumolib.checkBinary('sumo'), "-c", "sumo.sumocfg"] + sys.argv[1:])
33
34
logics = traci.trafficlight.getAllProgramLogics(tlsID)
35
programID = traci.trafficlight.getProgram(tlsID)
36
logic = None
37
for cand in logics:
38
if cand.programID == programID:
39
logic = cand
40
41
assert logic
42
numPhases = len(logic.phases)
43
print("current program '%s' has %s phases" % (programID, numPhases))
44
45
46
def check():
47
print("%s phase=%s" % (
48
traci.simulation.getTime(),
49
traci.trafficlight.getPhase(tlsID)))
50
51
52
# default actuated
53
print("max-gap:", traci.trafficlight.getParameter(tlsID, "max-gap"))
54
# make the first actuated phase 2 seconds longer
55
traci.trafficlight.setPhaseDuration(tlsID, 7)
56
traci.trafficlight.setPhase(tlsID, 0)
57
for i in range(180):
58
check()
59
if i == 50:
60
phase = traci.trafficlight.getPhase(tlsID)
61
newPhase = (phase + 3) % numPhases
62
print("jumping from phase %s to %s" % (phase, newPhase))
63
traci.trafficlight.setPhase(tlsID, newPhase)
64
traci.trafficlight.setPhaseDuration(tlsID, 20)
65
traci.simulationStep()
66
67
# change parameter
68
traci.trafficlight.setParameter(tlsID, "max-gap", "10")
69
print("max-gap:", traci.trafficlight.getParameter(tlsID, "max-gap"))
70
prolonged = False
71
for i in range(180):
72
if not prolonged and traci.trafficlight.getPhase(tlsID) == 0:
73
# make the first actuated phase shorter once
74
traci.trafficlight.setPhaseDuration(tlsID, 30)
75
prolonged = True
76
traci.simulationStep()
77
78
print("cycleSecond (before):", traci.trafficlight.getParameter(tlsID, "cycleSecond"))
79
for key, value in [
80
("cycleTime", "55"),
81
("coordinated", "true"),
82
("offset", "12")]:
83
print("%s (before):" % key, traci.trafficlight.getParameter(tlsID, key))
84
traci.trafficlight.setParameter(tlsID, key, value)
85
print("%s (after):" % key, traci.trafficlight.getParameter(tlsID, key))
86
print("cycleSecond (after):", traci.trafficlight.getParameter(tlsID, "cycleSecond"))
87
88
traci.close()
89
90