Week 1: Monte Carlo COVID Simulation in Python

Solution

Click here to see the full description, and Click here to see one possible solution. Below is this solution broken down into each step
  1. Create a local variable for storing state, and make everyone in the simulation start off healthy except for the first person in the list.

    Click here to see changes to the code for this step. Note how we use a different parameter to the constructor for the first person in the list of all people.

  2. Color people who are healthy BLUE, people who are sick RED, and people who are recovered MAGENTA

    Click here to see changes to the code for this step. Note that we are changing the color property of the cylinder object, not the cylinder itself

  3. Create a local method to the Person class that accepts another person object, and which causes the calling person to become sick if they are currently healthy but the other person is sick and close enough. By close enough, we mean that the distance should be within the infect_radius parameter passed to the simulation. Recall that the formula for the distance between the coordinate pair (x1, y1) and the coordinate pair (x2, y2) is

    \[ \sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2} \]

    Click here to see changes to the code for this step. We just have to add a single method that does the distance computation and updates a HEALTHY state to a SICK state when appropriate

  4. Modify code in the timestep method to keep track of how long someone has been sick, and to change them to RECOVERED if they have been sick longer than the variable RECOVERY_TIME

    Click here to see changes to the code for this step. We have to add an instance method, and then we update the timestep method to add on time as time elapses

  5. Finally, put everything together in the simulation loop to make the simulation run. Simply do a brute force solution here where for each person, you check every other person to see if they will infect. Once this is working, you should see something like this:

    Click here to see changes to the code. For every person at each time step, we check every other person to see if an infection will occur.

  6. The infection spreads very quickly in the above simulation, and all but one people get infected! Modify the code so that only a certain number of people are moving at any given time. What differences do you see?

    Click here to see changes to the code. We add a parameter to the constructor that will zero out the velocity if the person isn't moving, and we add a parameter to the simulation to specify how many people are moving. When fewer people are moving, we see that the infection spreads much more slowly. This is why sheltering in place is such an important part of social distancing.

Going Further...

For those who are curious, click here to see how to do faster proximity checks using a KD Tree data structure. This helps each person to quickly rule out people who are too far away to possibly infect them given a particular infection radius