May 18 / Benjamin Schumann

Doing good... with the AnyLogic road traffic library

I live in a nice little village just north of Berlin. We tend to have more horse traffic than cars, entirely pleasing for my young daughter. However, one part of the village is cut through by a large 4-lane road carrying commuters in and out of Berlin. Next to the large crossroads, there is a primary school.
Unfortunately, we see too many accidents there, including injured pedestrians. In 2013, two persons died. So people started a petition to improve the situation through three specific interventions. And I started thinking: why not support it by providing a simulation model to help estimate the impact on accident rates? And learn the depths of the road traffic library along the way :-)

Overview

This will be a mini-series following my modelling progress on exploring the road traffic library and combining it with pedestrians.

  • This week, we will learn about some road traffic library fundamentals and set up the basic model of the current situation.
  • Next week, we will combine the road traffic model with the pedestrian library to see how they work together.
  • The next step will be to add some model metrics and validation to the base model. What do we actually want to capture?
  • Next, we will amend the model to explore how the petition suggestions would play out. There are three suggestions: additional traffic light phases for left-turning vehicles; speed limits; and longer traffic light phases for pedestrians.

AnyLogic road traffic library

I know, AnyLogic keeps releasing new libraries and it is sometimes hard to keep up. We have seen pedestrians for a while now, then came the train library and recently the fluid library. With AnyLogic 7.3, we can now play with the road traffic library. And this little problem gave me the chance to play around with it some more.


It is important to understand what the AnyLogic team has done at the core of the library. Their idea is as genius as it is elegant: essentially, they took the existing process library (that you use to model discrete-event processes) and amended the behaviour of the entities to match that of cars. Remember that the process library is really good at moving entities through a system of queues and delays while potentially using some resources. However, those entities did not have “behaviour” of their own. Now, entities are coded to behave like cars, including physical properties like acceleration and human-factors like lane-changing.  

Library objects (top) define behavior on animation shapes (bottom).

So we still drag and drop library blocks (source, move-to…) into our model and link them to animation shapes (roads, intersections…). Only that we end up with a road traffic simulation instead of “dumb” entities in a process system. And with some effort, it looks as cool as this:
Write your awesome label here.

AnyLogic example model with heavy traffic, showing what is possible with the library.

The problematic situation

The crossroads has a large 4-lane road cutting through our village north-south. It meets a minor road and is regulated through traffic lights as below:

Overview of the crossroads in question. The school is in very close proximity.

We have quite a bit of north-south traffic and to a lesser extend east-west traffic. We never see traffic jams so there is enough capacity in the road system and traffic lights.

First steps

Let’s get to work. In order to align roads, it is a good idea to include a background image of the situation, easy to retrieve from Google or Bing. Next, start adding roads on top, making sure to be consistent with the drawing direction: I drew them towards the centre crossroads. This helps later when specifying how traffic should flow because you easily remember which lanes are forward and backward.

AnyLogic will automatically allow cars to turn in any direction, i.e. from any lane to any other lane. I recommend to reduce those options to match what happens in reality, as below. 

I reduced the potential turn options to closer resemble reality.

Notice that I also added two bus stops at the bottom, as in reality. Moreover, the 4 lane north-south road has a median strip which can be added in the properties.

Pro-tip (well, not really): name your roads, it makes things vastly easier when we add traffic below :-)

Adding traffic

Got your roads all set? Great, now we add some actual traffic. As with the process library, we do that by drag’n’drop library blocks and link them to our road network animation bits. So a CarSource object creates cars at a given rate and we specify on which road section they should pop up. Now it becomes critical to know where the forward and backward lanes for a particular road are. Below, I want cars to appear at the southern bottom of the model:

Ensure you are aware of your road directions to ensure correct traffic flow.

Next, let’s create car sources for each corner of my model as well as 2 more for buses coming from the north and south.

Each car source moves along its road until it hits the crossroads. I specify each option: move straight on, turn right or turn left (except the buses, they always go straight). Essentially, we cover each possible car-movement option when hitting the crossroads from any direction. Last, we dispose of our cars and buses when they leave the model (see right).

We need to link each block to the correct road segments (now good naming kicks in).

Road traffic library blocks: everything is deleted once leaving the model.

What it looks like

Before running the model, I went into my trusted system of gut-feeling and adjusted the probabilities of car directions (much more likely to go straight on than turn) based on my personal experience. Not great, but good enough for now. I will do some more research here, but let’s get the beauty to work first:
Write your awesome label here.

The final model: cars move and turn based on guess-work data. Buses arrive according to the actual schedule.

To be honest, this resembles my experience of the crossroads quite closely already. We never hit a traffic jam but short queues regularly form. However, the input data is pure guesswork at this point so I will need to do some field investigations on:

  • Number of cars arriving from each corner
  • Traffic light durations
  • Ratio of cars going straight, turn left and turn right

Also, a practical word of warning when working with the road traffic library: you might run into model errors when cars try to turn left but still sit on the wrong lane. Below, a car is on the left lane coming from the south and wants to turn right. However, it wasn’t able to get onto the right lane in time before reaching the crossroads:

Error when not allowing cars enough space to switch lanes beforehand.

The simplest fix is to give cars enough time to switch to the correct lane by simply stretching the incoming road: in the case above, just extend the road towards the south further so cars have enough time to switch to their required lane.

To be continued...

After having set up the basic traffic model as it is now, we will add more realistic data next time and then combine it with the pedestrian library. School children and parents roam this crossroads so why not make use of the existing pedestrian library and see how it works with the road traffic objects?

Stay tuned and let me know what you think. How can I improve the data and actually validate the model? What metrics should we record further down the line?
Created with