Пример #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)
Пример #2
0
    def __init__(self, consts=Consts()):
        self.consts = consts

        self.logger = logging.getLogger("simulation")
        logging.basicConfig()
        self.logger.setLevel(logging.INFO)
        self.logger.info("Creating new simulation.")
        self.matrix = AffinityMatrix(self.consts.population_size, consts)
        self.agents = self.matrix.agents

        self.stats_plotter = plotting.StatisticsPlotter()
        self.update_matrix_manager = update_matrix.UpdateMatrixManager(self.matrix)
        self.infection_manager = infection.InfectionManager(self)

        self.step_counter = 0

        self.per_generation = {}
        self.counters = {}
        for state in MedicalState:
            self.per_generation[state] = [0] * self.consts.total_steps
            self.counters[state] = 0
        self.per_generation["total infected"] = [0] * self.consts.total_steps
        self.per_generation["total current sick"] = [0] * self.consts.total_steps

        self.logger.info("Created new simulation.")

        # todo merge sick_agents and sick_agents_vector to one DS
        self.sick_agents = set()
        self.sick_agent_vector = np.zeros(self.consts.population_size, dtype=bool)
Пример #3
0
def compare_simulations_example():
    sm1 = SimulationManager(
        (
            Supervisable.Sum(
                "Symptomatic", "Asymptomatic", "Latent", "Silent", "ICU", "Hospitalized", "Recovered", "Deceased"
            ),
            "Symptomatic",
            "Recovered",
        ),
        consts=Consts(r0=1.5),
    )
    sm1.run()

    sm2 = SimulationManager(
        (
            Supervisable.Sum(
                "Symptomatic", "Asymptomatic", "Latent", "Silent", "ICU", "Hospitalized", "Recovered", "Deceased"
            ),
            "Symptomatic",
            "Recovered",
        ),
        consts=Consts(r0=1.8),
    )
    sm2.run()
Пример #4
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"]
Пример #5
0
from consts import Consts
from ways import load_map_from_csv
from problems import BusProblem
from path import Path
from matplotlib import pyplot as plt
from ways.draw import plotPath, plotOrders
from ways.tools import compute_distance
import numpy as np

# Read files
roads = load_map_from_csv(Consts.getDataFilePath("israel.csv"))
prob = BusProblem.load(Consts.getDataFilePath("TLV_5.in"))

# Print details of a random order
order = prob.orders[np.random.choice(np.arange(len(prob.orders)))]
print("One of the orders is from junction #{} at ({}, {}) to #{} at ({}, {})".
      format(order[0], roads[order[0]].lat, roads[order[0]].lon, order[1],
             roads[order[1]].lat, roads[order[1]].lon))
print(
    "A lower bound on the distance we need to drive for this order is: {:.2f}km"
    .format(
        compute_distance(roads[order[0]].coordinates,
                         roads[order[1]].coordinates) / 1000))

# Create hard coded example path
examplePath = Path(roads, [
    2744, 2745, 2746, 2747, 85561, 62583, 46937, 42405, 19096, 17273, 46582,
    43933, 465367, 57190, 819204, 819205, 47816, 16620, 819206, 465324, 3421,
    819207, 19950, 819208, 529485, 646688, 646689, 646690, 646691, 646692,
    646693, 646694, 47335, 646695, 646696, 522500, 646680, 646681, 646682,
    646683, 7372, 867063, 867064, 867065, 867066, 867067, 867068, 867069,
Пример #6
0
    def __init__(
            self,
            supervisable_makers: Iterable[Union[str, Supervisable, Callable]],
            population_data: PopulationData,
            matrix_data: MatrixData,
            inital_agent_constraints: InitialAgentsConstraints,
            run_args,
            consts: Consts = Consts(),
    ):
        # setting logger
        self.logger = logging.getLogger("simulation")
        logging.basicConfig()
        self.logger.setLevel(logging.INFO)
        self.logger.info("Creating a new simulation.")

        # unpacking data from generation
        self.initial_agent_constraints = inital_agent_constraints
        self.agents = population_data.agents
        self.geographic_circles = population_data.geographic_circles
        self.social_circles_by_connection_type = population_data.social_circles_by_connection_type
        self.geographic_circle_by_agent_index = population_data.geographic_circle_by_agent_index
        self.social_circles_by_agent_index = population_data.social_circles_by_agent_index

        self.matrix_type = matrix_data.matrix_type
        self.matrix = matrix_data.matrix
        self.depth = matrix_data.depth

        self.run_args = run_args

        # setting up medical things
        self.consts = consts
        self.medical_machine = consts.medical_state_machine()
        initial_state = self.medical_machine.initial

        self.pending_transfers = PendingTransfers()

        # the manager holds the vector, but the agents update it
        self.contagiousness_vector = np.zeros(len(
            self.agents), dtype=float)  # how likely to infect others
        self.susceptible_vector = np.zeros(len(self.agents),
                                           dtype=bool)  # can get infected

        # healthcare related data
        self.living_agents_vector = np.ones(len(self.agents), dtype=bool)
        self.test_willingness_vector = np.zeros(len(self.agents), dtype=float)
        self.tested_vector = np.zeros(len(self.agents), dtype=bool)
        self.tested_positive_vector = np.zeros(len(self.agents), dtype=bool)
        self.ever_tested_positive_vector = np.zeros(len(self.agents),
                                                    dtype=bool)
        self.date_of_last_test = np.zeros(len(self.agents), dtype=int)
        self.pending_test_results = PendingTestResults()

        # initializing agents to current simulation
        for agent in self.agents:
            agent.add_to_simulation(self, initial_state)
        initial_state.add_many(self.agents)

        # initializing simulation modules
        self.simulation_progression = SimulationProgression(
            [Supervisable.coerce(a, self) for a in supervisable_makers], self)
        self.update_matrix_manager = update_matrix.UpdateMatrixManager(self)
        self.infection_manager = infection.InfectionManager(self)
        self.healthcare_manager = healthcare.HealthcareManager(self)
        self.medical_state_manager = MedicalStateManager(self)
        self.policy_manager = PolicyManager(self)

        self.current_step = 0

        # initializing data for supervising
        # dict(day:int -> message:string) saving policies messages
        self.policies_messages = defaultdict(str)
        self.sick_agents = SickAgents()

        self.new_sick_counter = 0
        self.new_detected_daily = 0

        self.logger.info("Created new simulation.")
        self.simulation_progression.snapshot(self)
Пример #7
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)