Traffic Light


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 Traffic Light to control car traffic at specified intersection or stop-line(s). 

Demo model: Traffic Light

To know how to create a controlled intersection, please refer here

Demo model: Traffic Light Phases Optimization

Parameters

Defines the mode for
Here you define whether this Traffic Light 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 Traffic Light 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.
Type of value: 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 here.
Actions
On phase change [code]
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)

Functions

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.

Parameter: 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.

Parameters:
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.

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.

Parameter: unitstime 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.

Parameter: 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.

Parameter: unitstime unit constant

int getPhasesNumber() - Returns 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).

Parameter: 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)

Parameters: index - phase index (0, 1, 2, ...)
          unitstime unit constant

List<List<TrafficLightSignal>> getPhaseSignals() - Returns two-dimensional list of signals where a row is the signal of one line and a column is the signal of one phase.

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.

Parameter: stopLine - the specified stop line.

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.

Parameter: roadLaneConnector - the specified road lane connector

List<T> getSignalElements() - Returns list of controlled stop lines or lane connectors.

setRoadNetwork(RoadNetwork roadNetwork) - Sets a road network.

Parameter: 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.