def eval_params(params: np.ndarray, max_episode_length: int) -> np.ndarray: """Perform a single run of the simulator :param params: spread rate and social distancing rate :type params: np.ndarray :param max_episode_length: length of simulation run in days :type max_episode_length: int :returns: an array of cumulative deaths per day in the simulator run :rtype: np.ndarray """ spread_rate = params[:, 0][0] social_distancing = params[:, 1][0] num_seeds = 30 deaths = [] numpy_rng = np.random.RandomState(seed=num_seeds) sim_non_cli_opts = PandemicSimNonCLIOpts( population_params.above_medium_town_population_params) sim_opts = PandemicSimOpts(infection_spread_rate_mean=spread_rate) sim = make_sim(sim_opts, sim_non_cli_opts, numpy_rng=numpy_rng) covid_regulations = PandemicRegulation(social_distancing=social_distancing, stage=0) print( f'Running with spread rate: {spread_rate} and social distancing: {social_distancing}' ) for i in trange(max_episode_length, desc='Simulating day'): sim.execute_regulation(regulation=covid_regulations) for j in trange(sim_opts.sim_steps_per_regulation): sim.step() state = sim.state num_deaths = state.global_infection_summary[InfectionSummary.DEAD] deaths.append(num_deaths) return np.asarray(deaths)
def run(days: int, stage: int, days_per_interval: int): # setup rng numpy_rng = np.random.RandomState(seed=100) # setup simulator options sets sim_opts = PandemicSimOpts(use_contact_tracer=True) sim_non_cli_opts = PandemicSimNonCLIOpts(small_town_population_params) # make sim sim = make_sim(sim_opts, sim_non_cli_opts, numpy_rng=numpy_rng) # setup viz viz = GraphViz(sim, num_stages=len(austin_regulations), days_per_interval=days_per_interval) # run regulation stpes in the simulator stage_to_regulation = {reg.stage: reg for reg in austin_regulations} print(f'Stage {stage}:') for _ in range(100): print(f'{sim.state.sim_time.day + 1} ', end='') stdout.flush() # get the regulation regulation = stage_to_regulation[stage] # stage 0 # execute the given regulation sim.execute_regulation(regulation=regulation) for i in range(sim_opts.sim_steps_per_regulation): # step sim sim.step() # get state state = sim.state # visualize viz.record(state) print() # generate plots viz.plot() return viz._num_components_per_interval
from tqdm import trange from pandemic_simulator.environment import austin_regulations, Hospital, PandemicSimOpts, PandemicSimNonCLIOpts from pandemic_simulator.script_helpers import small_town_population_params, make_sim from pandemic_simulator.viz import MatplotLibViz if __name__ == '__main__': # setup rng numpy_rng = np.random.RandomState(seed=100) # setup simulator options sets sim_opts = PandemicSimOpts() sim_non_cli_opts = PandemicSimNonCLIOpts(small_town_population_params) # make sim sim = make_sim(sim_opts, sim_non_cli_opts, numpy_rng=numpy_rng) # setup viz viz = MatplotLibViz(num_persons=sim_non_cli_opts.population_params.num_persons, hospital_params=sim_non_cli_opts.population_params.location_type_to_params[Hospital], num_stages=len(austin_regulations), show_stages=False) # run regulation stpes in the simulator stage_to_regulation = {reg.stage: reg for reg in austin_regulations} for i in trange(100, desc='Simulating day'): # get the regulation regulation = stage_to_regulation[0] # stage 0 # execute the given regulation sim.execute_regulation(regulation=regulation)