Create a Pandemic Simulation with Unity II: Analyzing SIR graphs

Ege Hosgungor
5 min readJul 28, 2020

In the last article we have created the simulation environment with unity. By the end of this article we will have an analysis of our outcomes from our Pandemic Simulation.

Getting Started

Creating the simulation was a huge step. But without analyzing the outcomes of the simulation it is only a cute graphics. In this article I will continue from where we left and implement the data collection&plotting mechanism for the simulation.

The source of the project is available on github. You can download or clone from here. We only need the data in order to analyze it.

What is a SIR Model ?

The SIR model is a simple model, due to Kermack and McKendrick, of an epidemic of an infectious disease in a large population. We assume the population consists of three types of individuals, whose numbers are denoted by the letters S, I and R (which is why this is called an SIR model). All these are functions of the time t, and they change according to a system of differential equations.

S is the number of susceptibles, who are not infected but could become infected.I is the number of infectives. These individuals have the disease and can transmit it to the susceptibles. R is the number of recovered or removed individuals.

The article will consist of two parts. First part will be about exporting the game-data to csv, we will write a simple script called “Export Csv.cs” and update “PandemicArea.cs” For the second part, we will analyze the csv data and create “SIRGraph.py” in Python.

Scripts that we will create/update for exporting the data
SIRGraph.py and the exported csv data

Exporting the data from Unity

Unity doesn’t have any built-in function to export data except for monetization called Unity Analytics. We will create our own code to simply record the data when it changes and write on a csv file. The file will be called export.csv. record() function will add a new line to our file each time when we call it. addHeaders() will be called once in Awake() function of Pandemic Area after we established the area object.

In pandemicArea.cs there were basic counters for healthy, infected, recovered individuals. We changed them by adding get set accessor to have full control on these variables. Therefore we don't need to check every frame for “is there any change ?” with a function like Update(). it is basically very inefficient to use like that. To understand why this is necessary I will recommend you this youtube video tutorial : Get And Set Accessors

PandemicArea.cs with get-set accessors

We will also slightly change the function on the dummyBot.cs. The important part in this function is the variables first letters. When you call a variable with its capital letter you call its get accessor. For example in the below we decrease the healthyCounter as previous. On the other hand, increasing InfectedCounter calls the get-set accessor which leads to record the data. The purpose of using both of them in the same code is to prevent duplicate records in the csv.

When you run the simulation it will collect the data for you. Here is the example of the csv file. I started with 2000 healthy cube with 10 infected ones. I’m using real time after the simulation started in seconds. Congrats you just exported your simulation data from Unity to csv👏

Plotting the data

20 groups of

After the hard and mysterious part of the tutorial, we came to the fun part. This python script helps us play with the data we just exported. It is basic plotting except the Kmeans part. K-means clustering is used to group the points so that I can avoid noise. This helped me to smooth my curves and decrease the points and get more generalized curve. I had over 300 points for 100 individuals in the data. I grouped them into 20 groups. Then I draw the lines between them.

Compare to libraries like scipy and csaps, K-means clustering worked like a charm for smoothing my curve. The best result after K-means was Interpolation() function of scipy and you can see how it looks edgy compare to K-means

Smoothing comparison Left: scipy.Interpolate() Right: Kmeans-Clustering are used

Results

Seeing your simulation with graph visualizations helps to understand how your model behave, how it is close to the real world data. By playing with the infectious rate, numbers of healthy and infected individuals at the start, recovery time, infectious radius, one can learn a lot about how SIR Models work. You can also check from here that the results are pretty close to actual SIR models. Dynamic modelling of covid-19

Next Steps

I have been doing this series for my Master Dissertation: “Simulation-based Reinforcement Learning for Social Distance” the first two parts of the series were about setting the environment for the simulation and analyzing how environment behaves including this tutorial. Next I will integrate Artificial Intelligence into the project and convert the simulation into a learning environment for agents. Here is an example gif from the project. Blue agent is controlled by a neural network. The aim is to get the reward without getting sick.

To be continue…

--

--