Ejemplo n.º 1
0
def gen_no_policy_simulation():
    """
    for 500 rounds, run the simulation and update each day ranges according to the global min and max values per state.
    """
    consts = Consts.from_file(consts_file)
    range_per_day_dict = [
        deepcopy(test_states) for _ in range(consts.total_steps)
    ]

    for i in range(500):
        print(i)
        sm = SimulationManager(supervisable_makers=test_states.keys(),
                               consts=consts)
        sm.run()

        for supervisable in sm.supervisor.supervisables:
            name = supervisable.name()
            val_per_day = supervisable.y
            for test_state, val in zip(range_per_day_dict, val_per_day):
                test_state[name].update(val)

    # translate to dict
    for i in range(consts.total_steps):
        range_per_day_dict[i] = {
            state: val.to_dict()
            for state, val in range_per_day_dict[i].items()
        }
    # dump to json
    with open("simulation_test_ranges.json", "w") as f:
        json.dump(range_per_day_dict, f)
Ejemplo n.º 2
0
def test_no_policy_simulation():
    """
    run the simulation with no policy and fixed R0
    Ranges for each state was generate from real sim runs, might be flaky.
    """
    consts = Consts.from_file(consts_file)
    keys = range_per_day_dict[0].keys()
    sm = SimulationManager(supervisable_makers=keys, consts=consts)
    sm.run()

    for supervisable in sm.supervisor.supervisables:
        name = supervisable.name()
        val_per_day = supervisable.y
        # check for each day if value is in range
        for test_state, val in zip(range_per_day_dict, val_per_day):
            state = test_state[name]
            assert state["min"] <= val <= state["max"]
Ejemplo n.º 3
0
def run_simulation(args):
    matrix_data = MatrixData.import_matrix_data(args.matrix_data)
    population_data = PopulationData.import_population_data(args.population_data)
    initial_agent_constraints = InitialAgentsConstraints(args.agent_constraints_path)
    if args.simulation_parameters_path:
        consts = Consts.from_file(args.simulation_parameters_path)
    else:
        consts = Consts()
    set_seeds(args.seed)
    sm = SimulationManager(
        (
            # "Latent",
            Supervisable.State.AddedPerDay("Asymptomatic"),
            Supervisable.State.Current("Asymptomatic"),
            Supervisable.State.TotalSoFar("Asymptomatic"),
            # "Silent",
            # "Asymptomatic",
            # "Symptomatic",
            # "Deceased",
            # "Hospitalized",
            # "ICU",
            # "Susceptible",
            # "Recovered",
            Supervisable.Sum(
                "Latent",
                "Latent-Asymp",
                "Latent-Presymp",
                "Asymptomatic",
                "Pre-Symptomatic",
                "Mild-Condition",
                "NeedOfCloseMedicalCare",
                "NeedICU",
                "ImprovingHealth",
                "PreRecovered",
                name="currently sick"
            ),
            # LambdaValueSupervisable("ever hospitalized", lambda manager: len(manager.medical_machine["Hospitalized"].ever_visited)),
            LambdaValueSupervisable(
                "was ever sick",
                lambda manager: len(manager.agents) - manager.medical_machine["Susceptible"].agent_count,
            ),
            Supervisable.NewCasesCounter(),
            Supervisable.Wrappers.Growth(Supervisable.NewCasesCounter(), 1),
            Supervisable.Wrappers.RunningAverage(Supervisable.Wrappers.Growth(Supervisable.NewCasesCounter()), 7),
            Supervisable.Wrappers.Growth(Supervisable.NewCasesCounter(), 7),
            # Supervisable.GrowthFactor(
            #    Supervisable.Sum("Symptomatic", "Asymptomatic", "Latent", "Silent", "ICU", "Hospitalized"),
            # LambdaValueSupervisable("Detected Daily", lambda manager: manager.new_detected_daily),
            # LambdaValueSupervisable("Current Confirmed Cases", lambda manager: sum(manager.tested_positive_vector)),
            # Supervisable.R0(),
            # Supervisable.Delayed("Symptomatic", 3),
        ),
        population_data,
        matrix_data,
        initial_agent_constraints,
        run_args=args,
        consts=consts,
    )
    print(sm)
    sm.run()
    df: pd.DataFrame = sm.dump(filename=args.output)
    df.plot()
    if args.figure_path:
        if not os.path.splitext(args.figure_path)[1]:
            args.figure_path = args.figure_path+'.png'
        plt.savefig(args.figure_path)
    else:
        plt.show()
def monte_carlo_state_machine_analysis(configuration: Dict) -> Dict:
    """

    :param configuration: Dictionary with configuration for the mc run
           configuration must contain:
           * population_size for the mc
           configuration might contain:
           * consts_file - For loading Consts()
           * circle_consts file - for loading CircleConsts
    :return: Dictionary with statistics of the run:
                * population_size
                * days_passed - time it took to all the agents to recover/die
                * time_in_each_state - for each state, total number of days all agents were in it
                * visitors_in_each_state - Number of agents that visited each state
                * average_duration_in_state - Empirical mean time to stay
                                              at state conditioned that we enter it
                * state_duration_expected_time - Empirical mean to stay in state, not conditioned
                * average_time_to_terminal -Empirical mean time until death/recovery

    """
    if 'consts_file' in configuration:
        consts = Consts.from_file(configuration['consts_file'])
    else:
        consts = Consts()

    if "circle_consts_file" in configuration:
        circle_const = CirclesConsts.from_file(
            configuration['circle_consts_file'])
    else:
        circle_const = CirclesConsts()

    population_size = configuration["monte_carlo_size"]

    medical_state_machine = consts.medical_state_machine()
    medical_machine_manager = MedicalStateManager(
        medical_state_machine=medical_state_machine)
    agents_list = _generate_agents_randomly(population_size=population_size,
                                            circle_consts=circle_const)
    _infect_all_agents(agents_list, medical_machine_manager,
                       medical_state_machine)
    medical_states = medical_state_machine.states
    terminal_states = list(filter(_is_terminal_state, medical_states))

    state_counter = Counter({m.name: m.agent_count for m in medical_states})
    sum_days_to_terminal = 0
    days_passed = 1
    number_terminals_agents = 0

    while number_terminals_agents != population_size:
        # No manager so we don't update it
        previous_terminal_agents = sum(
            [m.agent_count for m in terminal_states])
        medical_machine_manager.step(list())
        number_terminals_agents = sum([m.agent_count for m in terminal_states])
        new_terminals = number_terminals_agents - previous_terminal_agents
        for m in medical_states:
            state_counter[m.name] += m.agent_count
        days_passed += 1
        sum_days_to_terminal += days_passed * new_terminals

    average_state_time_duration, state_duration_expected_time = _get_empirical_state_times(
        medical_state_machine, population_size, state_counter)

    return dict(population_size=population_size,
                days_passed=days_passed,
                time_in_each_state=dict(state_counter),
                visitors_in_each_state={
                    m.name: len(m.ever_visited)
                    for m in medical_state_machine.states
                },
                average_duration_in_state=average_state_time_duration,
                state_duration_expected_time=state_duration_expected_time,
                average_time_to_terminal=sum_days_to_terminal /
                population_size)