Create an Epidemic Simulation in Unity I: Simulating an epidemic spread

Ege Hosgungor
4 min readJul 4, 2020

Motivation

After the Covid-19 Outbreak, many wonderful articles have been written about simulating an epidemic spread. One of them was in Washington Post and other was on 3Blue1Brown Youtube channel. These are wonderful simulations and it pushed me to create my own. This simulation is inspired from SIR Model which is an epidemiological model that computes the theoretical number of people infected with a contagious illness in a closed population over time. Next article is going to be about how to plot these simulations and compare them.

Why Unity ?

There are many advantages of using a game engine. First of all the physics of the simulation can get exponentially get complex when you want to add something new. I tried to create one of my own, and I did however it wasn’t suitable to scale. There are many resources on the internet for Unity so you don’t have to create everything from scratch. In addition I would like to use Unity mlAgents after creating the simulation. mlAgents is a package of Unity for creating Reinforcement Learning environments which you can train your own AI but this topic is for another article.

Getting Started

The source of the project is available on github. You can download or clone from here. We only need the scripts in order to create the simulation.

DummyBot and Pandemic Area Scripts

First one is called PandemicArea.cs and this is going to be the interface between you and your pandemic simulation. There are plenty of different settings here.

PandemicArea.cs in Editor

Range : defines the area that your dummyBots can reach in environment.

Bot Settings: You can choose what kind of bot are you going to use and how many infected-healthy at the start.

Infection Settings: This will provide you total control on your infection spread. Bots are using an infection mechanism with their sphere colliders which provides them some probability of getting infected. You can change this by playing infection coefficient. It is inversely proportional to how infection is the disease. With the exposure radius you can change the collider radius of the bots. The last one gives the recover time which bot needs to wait before it can recover from the infection.

DummyBot Prefab

Healthy-Infected-Recovered

Now it is time to create dummyBots. I am calling this “dummy” because it will act randomly. In the next article we will have an agent who does actually take decisions with a neural network.

Different Materials: It helps us to distinguish healthy agents from others and visualize how infection spread.

Probability: Is the infection probability which is overriden by PandemicArea.cs However it provides a great visualization for how infection mechanism works.

Infection Status: is an enum with 3 different states. Healthy, Infected and Recovered.

I would like to mention how MoveRandomTarget() function works. When the bot is initialized it will calculate a random position and needed time to go that spot inside the area. The time is defined as nextActionTime and when its finished (when it comes to that position) It will choose another random position.

exposeInfection(GameObject Infector) takes the potential infector which is colliding with this dummyBot’s sphere collider. While the distance decrease between the bots the probability of getting infection is increasing.

Next Steps

Once you have created your base simulation, you can add many interesting stuff on top of it and play with it. In the next article I will convert this simulation into a learning environment for an AI agent. In the GIF above you can see blue colored agents .The agent’s purpose is going to learn“how to social distancing” with Reinforcement Learning. Even now I am getting excited about it. I hope this also helps your project. I would be glad to answer if you have any questions, Stay safe!

--

--