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)
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"]
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()
from pathlib import Path from time import time from warnings import warn import yappi from manager import SimulationManager qcachegrind_path = Path( r"C:\Users\avrah\Downloads\qcachegrind074-32bit-x86\qcachegrind074-x86\qcachegrind.exe" ) profile_gen = False if __name__ == "__main__": if profile_gen: yappi.start() sm = SimulationManager() if not profile_gen: yappi.start() sm.run() stats = yappi.get_func_stats() dest_path = "..\profilings\callgrind.out." + str(int(time())) stats.save(dest_path, "CALLGRIND") if not qcachegrind_path: pass if qcachegrind_path.exists(): subprocess.Popen([str(qcachegrind_path.absolute()), dest_path], close_fds=True) else: warn(f"set qqcachegrind_path to a valid path to open results")
import yappi from manager import SimulationManager qcachegrind_path = Path( r"D:\chrome downloads\qcachegrind074-32bit-x86\qcachegrind074-x86\qcachegrind.exe" ) profile_gen = False if __name__ == "__main__": if profile_gen: yappi.start() sm = SimulationManager(( "Recovered", "Deceased", "Symptomatic", "Asymptomatic", "Hospitalized", "ICU", "Latent", "Silent", )) if not profile_gen: yappi.start() sm.run() stats = yappi.get_func_stats() dest_path = r"..\..\profilings\callgrind.out." + str(int(time())) stats.save(dest_path, "CALLGRIND") if not qcachegrind_path: pass if qcachegrind_path.exists():
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()