top of page

Building a large scale traffic simulation in AnyLogic

Updated: Jul 30, 2023

In this blog post, I will be telling the story of my final year project for university, where I created a detailed traffic simulation using AnyLogic for a large suburb in Vanderbijlpark South Africa.

When the project sponsor Jaco-Ben Vosloo offered me the opportunity to work on this project in 2022 I thought: "So I just needed to create a simulation model of this suburb and run some tests." Simple right?

All jokes aside, it was not simple. It was one of the most challenging and most rewarding projects I've ever had to complete. And opened my eyes to the power of simulation.

So for the purpose of this post, this won't be a step-by-step guide on how it was created, but a high-level overview of the project. Hopefully, this post will shed some light on the diverse uses for simulation and provide some insight into AnyLogic's versatility. And maybe share some of the lessons I learned along the way.

I have already written 3 posts as a guest writer previously which were inspired by the work done on this project

A little bit of background

At the beginning of 2022 I was heading into my final year of Industrial Engineering as a student at the University of Pretoria. One of the most important parts of your whole Engineering degree is your Final Year Project. The initial step was identifying and selecting a problem that could be solved with engineering methods. The project had two main parts, consisting of a few months of research, followed by a few months of design (to implement a solution).

At the beginning of the year, I was unsure of where to look for a project, with a glimpse of hope that I could try to find a project in simulation or industrial analysis - modules that I had both enjoyed and excelled at during my degree. I contacted my simulation modeling lecturer at the time, Johan Joubert, asking if he knew of any available projects in these fields. And just my luck, there was a project sponsor that had just approached him for a student.

This project sponsor was Jaco-Ben Vosloo, Co-founder of The AnyLogic Modeler. The project in question was a traffic impact study for a community that consisted of about 1700 homes and a few small businesses. This community had been struggling with a gradual increase in crime over the years and was looking for a solution. The solution they landed on was implementing gated access, whereby most open access points (roads) into the community are fenced off, and the remaining ones are turned into checkpoints where vehicles can be monitored. This is a very popular design in South African communities, and has had great success in crime prevention. However, this design has a drawback because the traffic flow becomes more restricted.

Before approving the changes, the local municipality required evidence that the addition of the gated access would not significantly impact the flow of traffic in and around the community. As such, the aim of the project was to determine what the impact of these changes could be on the traffic.


After conducting extensive research into the problem, I determined that the ideal approach was an activity-based time-dependent mesoscopic simulation model using probabilistic activity-chains. Bit of a mouthful, I know, but it sounded good on my thesis!

Creating the Digital Twin

Ever heard of a digital twin?

Before this project I hadn't, but now the term is now engrained in my memory. This is one of the biggest advantages of simulation, and it is exactly what it sounds like, a copy of a real-world system or object in the virtual/digital world. This virtual copy allows you to run tests and make predictions much faster and a lot cheaper than real-world testing, with comparatively accurate results. So that's exactly what I set out to do.

But before any changes and tests could be run on the virtual system I needed to make sure it matched the real-world system as closely as possible. So using the Road Traffic Library in the AnyLogic palette and an arial image of the community, I created the road network. This basically involved tracing the digital road segments on top of the image, copying the details as precisely as possible. Due to the size of the community this was quite a tedious process, and took longer than I care to admit. But eventually, a basic layout of the virtual community was created. The blue circles are the locations of the homes and businesses.

I learned a valuable lesson during this process. Presentations in AnyLogic can be quite resource intensive if they are large. In my case the large high-resolution image didn't help either. This can make navigation in the AnyLogic editor slow and stuttery, in my case, quite unbearable actually. So lesson 1: Separate the animation from the logic and make the presentation dynamic, so it is hidden when working on the model's logic but reappears when you run the simulation. For an in-depth explanation of how to do this, have a look at this blog post.

After constructing the road network I could begin to create logic that moved vehicles around the suburb. But during testing, I discovered a bit of a flaw. The vehicles in the model would get impatient during high-flow situations and drive into intersections blocking opposing traffic, creating massive traffic jams. A funny coincidence that pretty accurately reflects a lot of South African driving when the traffic lights are off... Especially during load shedding...

However, this was a big problem for my model. And after some tinkering, and a question on the AnyLogic LinkedIn group here, I came up with a pretty neat solution, the smart traffic light. In short, this gave the AnyLogic traffic lights the ability to detect traffic ahead of the intersection and prevent vehicles from moving forward in that case. Lesson 2: If something doesn't work as expected when creating a model, there are almost always ways to code it to your needs. Such is the power of Java in AnyLogic and the beauty of creating a virtual world, you're in control. If you'd like to read further into the smart traffic light and how it works, have a look at this blog post.

Now comes the challenging part of the project. The only data we were provided to create the model was a traffic count of the community's intersections. This data told us the number of vehicles entering and exiting at any given time, but could not show us what was happening inside the community. Now I could have used the traffic count data to create a simple model which forces vehicles to enter and exit these intersections at specific times, but that would have left a big blind spot in the model where I would not know how the traffic inside the community was behaving. So to overcome this I used an approach known as activity-based modelling. Which, in essence, tries to replicate the real world behavior of people to the agents in the model, with the goal of determining where they could be going throughout the day. This is more true to real life as we do not simply travel from A to B, but complete a chain of activities throughout our days, often with multiple trips.

In applying this to the model, I chose a few activities that people would most likely go about during their day in this community. This consisted of shopping, dropping kids off at school, going home, and going to work. These activities were then combined into a list of possible activity chains that agents could complete.

To create the correct behaviour of vehicles in the model, and generate their trips, I needed to know what the most likely activity chains were throughout the day. Now this is where the probabilistic part comes in. I developed the model in such a way that each hour of the day would have probabilities for each activity chain, making vehicles travel around, and leave the community to complete them.

To find the probabilities which best represented the behaviour of the community, I used a method of sensitivity analysis where I would run the model with random probabilities for each hour. Then using optimisation to find the probabilities for these activity chains which resulted in the virtual intersection counts matching as closely as possible to the real-world intersection counts. This sensitivity analysis consisted of approximately 360 thousand runs of the model at various seeds, resulting in over 4 million data points to be analysed!

Unfortunately, I didn't have access to a supercomputer, which would have been nice.... So this process took a little over two weeks with multiple computers running almost continuously, which I dubbed the Calibration Factory! At times I think I had up to nine computers running at the same time. A part of this calibration factory can be seen below. Big shout out to my friends who gave up their computers to help me out with this!

I admit now I may have gotten carried away a bit with the extant of the sensitivity testing, but ultimately this was successful and resulted in a MAE (mean average error) of about seven vehicles an hour compared to the actual traffic count. For perspective, there were on average 1,000 vehicles tracked every hour, so this is only a 0.7% error!

Here is a quick video showing the digital twin in action.

So now we had a working replica of the community and the study on the impact of the gates could begin.

Impact of gated access

From the communities proposal, three layouts of gated access were tested. With 6 of the access points closed and only 4 open and monitored for vehicles to enter and exit. This required creating multiple versions of the model network with minor tweaks to the access points.

To answer the question of how the proposed changes would impact traffic in the community, many aspects were evaluated; including trip times, delays at intersections, flow intensity and LOS (level of service) of the roads. For context, a few of these results can be seen below.

In summary, the results from the simulations indicated that gated access was in fact feasible with minimal impact on traffic.

If you'd like to play around with the model I created, it has been uploaded here or play it directly in the window below. Enjoy!

(P.S. The model contains a detailed graphic and thousands of agents so give it some time to initialize, make it full screen for a better viewing experience)

Big thanks to Jaco-Ben for making the project available and mentoring me along the way.

If you're interested in learning more about simulation with AnyLogic or have a project that you think could benefit greatly from analysis through simulation, don't hesitate to contact Jaco-Ben Vosloo. He has extensive knowledge of all things simulation and runs a consulting firm providing AnyLogic training and model-building support.

I am grateful having received the support and mentoring without it the model would not have been so powerful and the overall project not such a big success.

Getting support earlly on during your AnyLogic model-building journey is a worthwhile investment as it saves you tons of wasted time and frustration later!

Devon Cowling


Devon Cowling is now a formal writer for the AnyLogic Modeler, and engineer at Jaco-Ben Vosloo Consulting part-time, working on AnyLogic training material and small simulation projects. Feel free to connect with him over LinkedIn.

What next?

If you liked this post, you are welcome to read more posts by following the links above to similar posts. Why not subscribe to our blog or follow us on any of the social media accounts for future updates? The links are in the Menu bar at the top, or the footer at the bottom. You can also join the mobile app here!

If you want to contact us for some advice, maybe a potential partnership or project or just to say "Hi!", feel free to get in touch here, and we will get back to you soon!

623 views1 comment

1 Comment

Jaco-Ben Vosloo
Jaco-Ben Vosloo
Jul 27, 2023

I love the picture about the calibration factory!

Post: Blog2_Post
bottom of page