mercredi 19 mai 2021

Pandemic Simulation with Python [closed]

I was a given a task to do a simulation with Python and two libraries (matplotlib, random):

The citizens of Minervopolis are in a panic! A newly discovered disease, Pymonia, has been detected among the population and it is spreading fast. You are the scientist responsible for modeling this disease in order to better prepare the authorities. Task: Create a function, simulate_pandemic(healthy), that will simulate the spread of a Pymonia in Minervopolis. In the simulated city, you can find 𝑛 people that can be in one of 3 states: healthy, infected, or immune. During each day of your simulation:

  • For each healthy person, there is a n(infected)/5*n chance that person gets infected, where n(infected) is the number of infected people on that day;

  • For each infected person, there is a 1% chance that the person is cured and becomes immune. Immune people do not get reinfected;

  • For each infected person, there is a 4% chance that the person is cured without immunity and becomes healthy;

  • In this simulation, no one dies from the virus. People stay infected until they become immune or healthy.

Start your simulation with 2 infected and 0 immune people. The starting number of healthy people will be given in the argument of the function. Plot the numbers of healthy, infected and immune people over time. It is up to you to decide on a stop condition for the simulation, but make sure that you can clearly see the evolution of the disease over a sufficiently long duration, like in the graph below. Do not forget to add suitable elements to your plot like clear legends, axis labels and a title.

And here is my code:

import random
import matplotlib.pyplot as plt

def simulate_pandemic(healthy):
    infected_plp = 2
    immune_plp = 0
    healthy_plp = healthy
    time = 500
    infected_over_time = [infected_plp]
    healthy_over_time = [healthy]
    immune_over_time = [immune_plp]
    axis_x = [0]
    infected_chance = infected_plp/(5*healthy_plp)
    if infected_plp != 0:
        for j in range(time):
            axis_x.append(j)
            for plp in range(round(healthy_plp)):
                if random.random() <= infected_chance:
                    infected_plp +=1
            for inf_plp in range(round(infected_plp)):
                if random.random() < 1/100:
                    immune_plp += 1
                    infected_plp -= 1
                elif random.random() < 4/100:
                    healthy_plp += 1
                    infected_plp -= 1
            infected_over_time.append(infected_plp)
            immune_over_time.append(immune_plp)
            healthy_over_time.append(healthy_plp)

    print(immune_plp, healthy_plp, infected_plp)
    plt.plot(axis_x, immune_over_time, label = "Immune")
    plt.plot(axis_x, healthy_over_time, label = "Healthy")
    plt.plot(axis_x, infected_over_time, label = "Infected")
    # naming the x axis
    plt.xlabel('days')
    # naming the y axis
    plt.ylabel('people')
    # giving a title to my graph
    plt.title('Spread of pandemic')
    # show a legend on the plot
    plt.legend()
    # function to show the plot
    plt.show()

simulate_pandemic(1000)



Aucun commentaire:

Enregistrer un commentaire