Virus Simulator

This experiment simulates the evolution of an epidemic based on initial factors.


The purpose of this experiment is to visually show how an epidemic can spread among a population of individuals.
You will find that a little change on the initial factors will dramatically change the outcome of the epidemic development.
Depending on the parameters choice, you will appreciate different compartmental models: SIR, SEIR, SIS, SIRD, SID.
You can find a complete explanation after the experiment

I've built this tool in Vanilla Javascript with an Object Oriented approach.
The rendering part is built using the Canvas APIs

You can play with the parameters and see how the outcome changes.
Current status Time: ticks
Population (TOT):
Susceptible individuals:
Infected individuals:
Dead individuals:
Cured individuals:

R-0:
Population parameters
6
10
Infection parameters
2
10
0.5
0.25
0.01
0.02
Current status Time: ticks
Population (TOT):
Susceptible individuals:
Infected individuals:
Dead individuals:
Cured individuals:

R0:

How it works

In the following paragraphs, I'll go through each part of the simulation. Feel free to jump straight to a specific section if you like.

Parameters and status

Population settings

The population density is the number of individuals that should be created on the board.
This board has size 300x300, so 100% density will lead to a total of 90.000 individuals.
Based on this initial number, you can then pick how many of them will be infected by setting the number of infected people.

Human move strategy

This parameter will set the movement strategy of a single individual among 5 options: "up", "down", "left", "right" and "nothing".

The random strategy is self-explanatory.
The random with direction memory strategy comes from an observation from the simulation: each individual was moving around their initial spawn point, not actually mimicking the real human behavior.
This strategy creates an imbalance in the possible choices, making the "previous move" more likely to be chosen.

The social distancing strategy starts to show some kind of "intelligence".
Before moving, an individual evaluates which move maximizes the distance between itself and any other individual in its range of sight.
This local behaviour results in a very beautiful ordered disposition of individuals on the board.

Infection parameters

These parameters control the infection features.
The infection range defines the area of infection of a single individual.
The incubation time defines the time needed to an individual to move from the susceptible state to the infected state.
The infection and re-infection probabilities define the probability of infection for a susceptible and cured individual respectively.
The cure and death probabilities are self explanatory.

Status

The "status" section and chart give you a visual and textual, real-time status of the simulation.
The R-0 parameter is evaluated as the average of infected people by each individual. (Since this is a simulation, we can do this easily!)

Simulation structure

This simulation makes use of an internal "clock" and performs actions at each "tick".
At each tick, an individual's status can be one of the following: susceptible, exposed, infected, cured, dead.

The initialization phase evaluates the number of individuals from the density parameter and randomly places these individuals on the board.

For each tick, all the individuals perform a sequence of actions based on their current status.
We will cover that in the next section.

Simulation Structure

Human behavior

Each individual will perform the following actions for each tick.
Let's take an individual as an example.

Infect

If the current individual is infectious, the infection could spread on other susceptible or cured individuals, based on the infection radius, infection probability, and re-infection probability.

Get cured

There's a probability that our individual will overcome the infection and be cured.
If this happens, the individual will move from the infected status to cured.

Die

If our individual was not cured, he could eventually die, based on the death probability.
If this happens, the individual will move from the infected status to died.

Move

Let's say that our individual does not die.
In this case, he will try to move around using the pre-set move strategy.

Epidemiology Fundamentals

While developing this tool, I've had the opportunity to conduct some online research into what models are currently used to simulate and analyse the development of an epidemic.
I would like to share some of the theory that relies on this simulation.
SIR diagram

Compartmental models

Compartmental modelling is a methodology that classifies the individuals into a finite number of states and defines which way an individual could move from one state to another.
Each state is associated with a letter.

  • S: Susceptible
    An individual is susceptible when there's a probability of being infected.
  • I: Infectious
    An infectious individual has been infected and can infect susceptible individuals.
  • R: Removed
    A removed individual is either dead or cured (immune).
    Depending on the specific model, this label could be referring to cured people only.
  • D: Dead
    A dead individual is... well... dead and it is out of the simulation.
  • E: Exposed
    An exposed individual has been infected but is not yet contagious.
  • C: Carrier
    A carrier individual does not show symptoms (is basically susceptible) but is still infectious.
  • M: Maternally-derived immune
    For some infections, individuals can be temporarily immune to the disease, then move to the susceptible state.

Using these building blocks, we can model different epidemic behaviors.

The most common model is SIR. This model has been used to describe epidemics like COVID-19.
The key assumption here is that after being cured, an individual cannot get sick again.

Other diseases, like common flu, are modeled using an SIS model, in which the death probability is zero and after being cured, an individual is again susceptible to the disease.