Simulates one or several traffic lights. Traffic light (also known as traffic signal, traffic semaphore, signal light, stop light) is a signaling device positioned at road intersections, pedestrian crossings, and other locations to control conflicting flows of traffic.
In AnyLogic, you can set TrafficLight to control car traffic at specified intersection or stop-line(s).
Demo model: Traffic Light Open the model page in AnyLogic Cloud. There you can run the model or download it (by clicking Model source files).To learn how to create a controlled intersection, see Controlled intersection.
Demo model: Traffic Light Phases Optimization Open the model page in AnyLogic Cloud. There you can run the model or download it (by clicking Model source files).- Defines the mode for
-
Here you define whether this TrafficLight block defines the working mode for:
- Intersection’s stop lines — The traffic light will control traffic at the stop lines of the specified intersection. You specify the intersection in the Intersection control below, and then configure the traffic light schedule in the Phases table by specifying red / (yellow) / green phases and their durations for the required stop lines of the selected intersection. This mode is used when traffic light stops / enables traffic for all lanes of each road connected to the intersection at once. If you need to customize traffic on certain road lanes only, you should use the Intersection’s lane connectors option.
- Intersection’s lane connectors — Use this option to control the traffic on the intersection where different lanes of the same road may be in different (red / green) phases. Specify the intersection to be controlled by this block in the Intersection property, then configure the traffic light schedule in the Phases table by specifying red / (yellow) / green phases and their durations for specific lane connectors of the selected intersection.
- Specified stop lines — The traffic light will control traffic at specified stop line(s). This mode is typically used when you simulate pedestrian crossings. In this case you draw a stop line and then define red / (yellow) / green phases and their durations for this stop line using the Phases property of the TrafficLight block.
- Intersection
-
Visible and applies only if the Defines the mode for: Intersection’s stop lines or Intersection’s lane connectors option is selected above] Intersection controlled by this traffic light.
Value type: Intersection
- Phases
- Specifies the behavior at stop line. Here you should define the traffic light phase durations for roads forming the selected intersection. You can find the short tutorial describing how to make your intersection controlled by traffic light in Controlled intersection.
- Actions
- On phase change
-
Code that is executed in when the traffic light phase changes or when the traffic light is turned on / off (with the turnOn(...) / turnOff() function calls).
Local variable: int currentPhaseIndex — The number of the of the traffic light phase (after the change)
- Control
-
Function Description boolean isOn() Returns true if the traffic light is on, false otherwise. turnOn() Turns on traffic light. turnOff() Turns off traffic light. turnOn(double offset) Turns on traffic light with offset in model time units.
offset — Offset in model time units.turnOn(double offset, TimeUnits units) Turns on traffic light with offset in specified time units. For example, the function call turnOn(1, MINUTE) will turn the traffic light on in 1 minute from the current time.
offset — Offset in specified time units.
units — Time unit constant.switchToNextPhase() Switches traffic light to the next phase. Typical use case: current phase has infinite duration and should be switched. - Current status
-
Function Description double getCurrentPhaseDuration() Returns the duration of the current phase in model time units. If traffic light is off, the function returns -1. double getCurrentPhaseDuration(TimeUnits units) Returns the duration of the current phase in specified time units. For example, if the duration of the current phase is 30 seconds, the function call getCurrentPhaseDuration(MINUTE) will return 0.5. If traffic light is off, the function returns -1.
units — Time unit constant.double getCurrentPhaseElapsedTime() Returns the time elapsed from the beginning of the current phase in model time units or -1 if the traffic light is off. double getCurrentPhaseElapsedTime(TimeUnits units) Returns the time elapsed from the beginning of the current phase in specified time units. For example, if 15 seconds have elapsed, the function call getCurrentPhaseElapsedTime(MINUTE) will return 0.25. If traffic light is off, the function returns -1.
units — Time unit constant.double getCurrentPhaseRemainingTime() Returns the remaining time to the end of the current phase in model time units. If traffic light is off, the function returns -1. double getCurrentPhaseRemainingTime(TimeUnits units) Returns the remaining time to the end of the current phase in specified time units. For example, if the remaining time is 15 seconds, the function call getCurrentPhaseRemainingTime(MINUTE) will return 0.25. If traffic light is off, the function returns -1.
units — Time unit constant.TrafficLightSignal getCurrentSignal(RoadLanesConnector roadLaneConnector) Returns current signal for the specified road lane connector. Returns null, if the traffic light is off or if it does not control the road lane connector.
roadLaneConnector — The specified road lane connector.TrafficLightSignal getCurrentSignal(StopLine stopLine) Returns current signal for the specified stop line. Returns null, if the traffic light is off, or if it does not control the stop line.
stopLine — The specified stop line. - Phases
-
Function Description int getPhasesNumber() Returns the number of phases. int getCurrentPhaseIndex() Returns 0-based index of the current phase, or -1 if the traffic light is off. double getPhaseDuration(int index) Returns the duration of the phase in model time units. You specify the phase by its index. For example, to get the duration of the first phase, you should call getPhaseDuration(0).
index — Phase index (0, 1, 2, ...).double getPhaseDuration(int index, TimeUnits units) Returns the duration of the phase in given time units. You specify the phase by its index. For example, to get the duration of the second phase in seconds, you should call getPhaseDuration(1, SECOND).
index — Phase index (0, 1, 2, ...).
units — Time unit constant.List<List<ListTrafficLightSignal>> getPhaseSignals() Returns a list of lists where each list contains the phases for one stop-line in the specified order, for example:
[[SIGNAL_RED, SIGNAL_GREEN, SIGNAL_YELLOW], [SIGNAL_GREEN, SIGNAL_RED, SIGNAL_YELLOW]].List<T> getSignalElements() Returns list of controlled stop lines or lane connectors. - Road network
-
Function Description setRoadNetwork(RoadNetwork roadNetwork) Sets a road network.
roadNetwork — Road network that the created traffic light belongs to.RoadNetwork getRoadNetwork() Returns road network containing the markup element the specified traffic light is controlling.
-
How can we improve this article?
-