Road Traffic Library: Vehicle Priority at Junction

AnyLogic Road Traffic Library offers quite some features for modeling car traffic. Junctions are either controlled by general traffic rules (right before left) or a traffic light. Sometimes however, especially in non-public traffic such as on factory roads, special rules may apply. If you want to determine yourself which car gets to go first on a junction, you'll have to add it yourself. In this article, I'll explain how to give privilege entry to a crossing for higher priority vehicles.

Scenario

Traffic comes from two sides. All cars want to enter the downwards direction. Each car has a priority, which is saved as a variable priority in the Car Agent and is shown on the roof of each car's presentation. Higher number means higher priority. The higher priority car shall enter the junction with privilege over lower priority vehicles.

Road Traffic Scenario

Logic

First we'll insert stop lines and traffic lights for each incoming road, in order to control the access to the junction. The heart will be a cyclic event that will do the following:

  1. Compare the priority of the first cars of all incoming road section
  2. Allow traffic to enter on the road with the car of highest priority
  3. Block all other incoming entries

Elements

We will need some basic elements to implement our logic. Those are:

Determining the priority of the first car on a road section:

((Car)road.getCars(roadDirection).get(0)).priority

Manually activating/deactivating a traffic light:

t.turnOff()
//t.turnOn()

Setting a traffic light to permanent red, by just defining one single phase:

Constant Red Traffic Light

Putting it together

Model

The function setTrafficLightsOnPrio to update the junction access is built to be used with two generic incoming roads. It therefore takes the incoming roads (road1,road2 ), traffic lights (trafficLight1,trafficLight2) and directions (roadDirectionIntoCrossing1,roadDirectionIntoCrossing2) of the incoming roads as input parameters. The event cyclically triggers the function setTrafficLightsOnPrio every 0.1 seconds.

//ArrayList<Road> roads = new ArrayList<Road>();
ArrayList<TrafficLight> trafficLightsToBlock = new ArrayList<TrafficLight>();
int maxPriority =-1;
int currentPriority =0;
TrafficLight trafficLightsToOpen = null;

if (road1!=null){
	if (road1.getCars(roadDirectionIntoCrossing1).size()>0){
		currentPriority = ((Car)road1.getCars(roadDirectionIntoCrossing1).get(0)).priority;
	}
	else{
		currentPriority = -1;
	}
	if (currentPriority>maxPriority){
		maxPriority = currentPriority;
		trafficLightsToOpen = trafficLight1;
	}
	else{
		trafficLightsToBlock.add(trafficLight1);
	}
}
if (road2!=null){
	if (road2.getCars(roadDirectionIntoCrossing2).size()>0){
		currentPriority = ((Car)road2.getCars(roadDirectionIntoCrossing2).get(0)).priority;
	}
	else{
		currentPriority = -1;
	}
	if (currentPriority>maxPriority){
		maxPriority = currentPriority;
		trafficLightsToOpen = trafficLight2;
	}
	else{
		trafficLightsToBlock.add(trafficLight2);
	}
}


//no car there, open all
if (trafficLightsToOpen==null){
	for(TrafficLight t:trafficLightsToBlock){
		t.turnOff();
	}
	return;
}

//set the traffic ligths
for(TrafficLight t:trafficLightsToBlock){
	t.turnOn();
}
trafficLightsToOpen.turnOff();

Here is the final working model:

Conclusion

At first the options of the Road Traffic Library seem to be restricting models to only standard cases. This example shows however that the basic modules for traffic modelling in AnyLogic can be used and combined to allow all kinds of custom traffic control. Think for example about those junctions that detect incoming/waiting vehicles with induction coils and set the traffic lights accordingly - this can also be modeled with the methods described in this article.