def main(load_inter_flag=False): if not load_inter_flag: real_data = list(pd.read_csv(get_data_path(True))['adcode'].unique()) start_date = date(2020, 1, 24) real_data1 = pd.read_csv(get_data_path(True)) history_real = prepareData(real_data1) rerun_cities = None for i in range(get_seed_num()): total_params = pd.read_csv( os.path.join(os.path.dirname(__file__), './params_foreign{}.csv'.format(int(i)))) for ind, city in enumerate(real_data): print(i, ind, city, str(get_important_date(city))) x = get_params(total_params, city) if rerun_cities is None or city in rerun_cities: run_simulation2(x, city, 60, 60, start_date, get_important_date(city), history_real, unob_flow_num=None, json_name='data_run_foreign{}.json'.format( int(i))) # choose the final parameters max_data, min_data, mean_data, best_data, \ std_data, middle_data, xmin_data, xmax_data, \ xmean_data, xstd_data = load_and_save('./data_run_foreign{}.json', './data_run_foreign_{}.json') """
def run_simulation(x, city, simulate_day1, simulate_day2, start_date, simulate_date, incubation=3, unob_flow_num=None, json_name='data_run.json'): real_data = pd.read_csv(get_data_path(True)) history_real = prepareData(real_data) run_simulation2(x, city, simulate_day1, simulate_day2, start_date, simulate_date, history_real, incubation, unob_flow_num, json_name)
def prepare_data_china(load_inter_flag=False): real_data = pd.read_csv(get_data_path())['adcode'].unique() rerun_cities = None if rerun_cities is not None: real_data = rerun_cities.copy() start_date = date(2020, 1, 11) real_data1 = pd.read_csv(get_data_path()) history_real = prepareData(real_data1) if not load_inter_flag: print('re-simulate the progress') province_travel_dict = flowHubei() for i in range(get_seed_num()): total_params = pd.read_csv( os.path.join(os.path.dirname(__file__), './params{}.csv'.format(int(i)))) unob_flow_num = initHubei( get_params(total_params, 420000), start_date, important_date=[get_important_date(420000)], travel_from_hubei=province_travel_dict) for ind, city in enumerate(real_data): print(i, ind, city) x = get_params(total_params, city) run_simulation2(x, city, 90, 90, start_date, get_important_date(city), history_real, unob_flow_num=unob_flow_num, json_name='data_run{}.json'.format(int(i))) else: print('directly load the pre-simulated data') for i in range(get_seed_num()): get_total_province('data_run{}.json'.format(int(i))) # choose the final parameters max_data, min_data, mean_data, best_data, std_data, middle_data, \ xmin_data, xmax_data, xmean_data, xstd_data = load_and_save( './data_run{}.json', './data_run_{}.json') """
def run_opt(city, budget, start_date, important_dates, infectratio_range=None, dummy_range=None, unob_flow_num=None, repeat_time=1, init_samples=None, training_end_date=None, json_name='data_run.json', seed=3, loss_ord=0.0, touch_range=None,iso_range=None): assert infectratio_range is not None and dummy_range is not None days_predict = 0 # load data real_data = pd.read_csv(get_data_path(True)) history_real = prepareData(real_data) flow_out_data = None # initialize models infectratio = InfectRatio(1, [infectratio_range], [True]) if touch_range is None: touchratio = TouchRatio(1, [[0.999, 1.0000]], [True]) else: touchratio = TouchRatio(1, [touch_range], [True]) touchratiointra = TouchRatio(1, [[0, 1]], [True]) obs = ObservationRatio(1, [[0.0, 0.3]], [True]) dead = DeadRatio(1, [[0., 0.01]], [True]) if iso_range is None: isoratio = IsolationRatio(1, [[0.03, 0.12]], [True]) else: isoratio = IsolationRatio(1, [iso_range], [True]) dummy = DummyModel(1, [dummy_range], [True, True]) cure_ratio = InfectRatio(1, [[0., 0.15]], [True]) simulator = Simulator(city, infectratio, touchratio, obs, dead, dummy, isoratio, touchratiointra, cure_ratio, important_dates, unob_flow_num=unob_flow_num, flow_out_data=flow_out_data, training_date_end=training_end_date) test_date = datetime.strptime(history_real['date'].max(), '%Y-%m-%d').date() - timedelta(days_predict) history_real = history_real[history_real['adcode'] == city] history_real = history_real[history_real['date'] >= str(start_date)] history_train = history_real[history_real['date'] <= str(test_date)] x, y = simulator.fit(history_train, budget=budget, server_num=get_core_num(), repeat=repeat_time, seed=seed, intermediate_freq=10000, init_samples=init_samples, loss_ord=loss_ord) print('best_solution: x = ', x, 'y = ', y) simulator.set_param(x) run_simulation(x, city, 60, 60, start_date, get_important_date(city), unob_flow_num=unob_flow_num, json_name=json_name) return x
def run_opt(city, budget, start_date, important_dates, infectratio_range=None, dummy_range=None, unob_flow_num=None, repeat_time=1, init_samples=None, training_date_end=None, json_name='data_run.json', seed=1, loss_ord=0.0, unob_period=None, obs_period=None, iso_period=None, cure_period=None, isoratio_it=None): if city == 420000: infectratio_range = [0., 0.05] dummy_range = [0.0000, 400.00001] else: assert infectratio_range is not None and dummy_range is not None days_predict = 0 # load data real_data = pd.read_csv(get_data_path()) history_real = prepareData(real_data) flow_out_data = flowOutData() # initialize models infectratio = InfectRatio(1, [infectratio_range], [True]) touchratio = TouchRatio(1, [[0.0, 0.6]], [True]) touchratiointra = TouchRatio(1, [[0, 1]], [True]) obs = ObservationRatio(1, [[0.0, 0.3]], [True]) dead = DeadRatio(1, [[0., 0.01]], [True]) if isoratio_it is None: isoratio = IsolationRatio(1, [[0.2, 0.5]], [True]) else: isoratio = IsolationRatio(1, [isoratio_it], [True]) dummy = DummyModel(1, [dummy_range], [True, True]) cure_ratio = InfectRatio(1, [[0., 0.1]], [True]) # set the time of applying touchratio simulator = Simulator(city, infectratio, touchratio, obs, dead, dummy, isoratio, touchratiointra, cure_ratio, important_dates, unob_flow_num=unob_flow_num, flow_out_data=flow_out_data, training_date_end=training_date_end) # set period here simulator.set_period() test_date = datetime.strptime(history_real['date'].max(), '%Y-%m-%d').date() - timedelta(days_predict) history_real = history_real[history_real['adcode'] == city] history_real = history_real[history_real['date'] >= str(start_date)] history_train = history_real[history_real['date'] <= str(test_date)] x, y = simulator.fit(history_train, budget=budget, server_num=get_core_num(), repeat=repeat_time, seed=seed, intermediate_freq=10000, init_samples=init_samples, loss_ord=loss_ord) print('best_solution: x = ', x, 'y = ', y) simulator.set_param(x) run_simulation(x, city, 60, 60, start_date, get_important_date(city), unob_flow_num=unob_flow_num, json_name=json_name) duration = len(real_data["date"].unique()) - 1 sim_res, _ = simulator.simulate(str(start_date), duration) print('RMSE: ', get_newly_loss(sim_res, history_real)) return x, sim_res
def initHubei(x, start_date, important_date, travel_from_hubei): if travel_from_hubei is None: return None days_predict = 0 # load data real_data = pd.read_csv(get_data_path()) history_real = prepareData(real_data) infectratio = InfectRatio(1, [[0, 1]], [True]) touchratio = TouchRatio(1, [[0., 0.3]], [True]) touchratiointra = TouchRatio(1, [[0, 10]], [True]) obs = ObservationRatio(1, [[0.0, 1.]], [True]) dead = DeadRatio(1, [[0., 0.1]], [True]) isoratio = IsolationRatio(1, [[0., 1]], [True]) cure_ratio = InfectRatio(1, [[0., 100]], [True]) dummy = DummyModel(1, [[0, 200000]], [True, True]) flow_out_data = flowOutData() test_date = datetime.strptime(history_real['date'].max(), '%Y-%m-%d').date() - timedelta(days_predict) history_real = history_real[history_real['adcode'] == 420000] history_real = history_real[history_real['date'] >= str(start_date)] history_train = history_real[history_real['date'] <= str(test_date)] duration = len(history_train["date"].unique()) city = 420000 simulator = Simulator(city, infectratio, touchratio, obs, dead, dummy, isoratio, touchratiointra, cure_ratio, important_date, flow_out_data=flow_out_data) simulator.set_param(x) total_population = get_populations()[420000] simulated_result, detailed_result = simulator.simulate( str(start_date), duration + 60) init_unob = [item for item in reversed(detailed_result.unob_total_init)] unob_ratio = {} for i, item in enumerate(init_unob): date_now = start_date - timedelta(i + 1) unob_ratio[date_now] = item / total_population for it_date, unob_num in zip(simulated_result['date'], simulated_result['total_unobserved']): unob_ratio[datetime.strptime( it_date, '%Y-%m-%d').date()] = unob_num / total_population unob_flow_num = {} lst_travel = {} lst_date = None for it_date in travel_from_hubei: if it_date not in unob_ratio: continue if it_date not in unob_flow_num: unob_flow_num[it_date] = {} for it_code in travel_from_hubei[it_date]: unob_flow_num[it_date][it_code] = travel_from_hubei[it_date][ it_code] * unob_ratio[it_date] lst_travel = travel_from_hubei[it_date] lst_date = it_date lst_date = lst_date + timedelta(1) while True: if lst_date in unob_ratio: if lst_date not in unob_flow_num: unob_flow_num[lst_date] = {} for it_code in lst_travel: unob_flow_num[lst_date][ it_code] = lst_travel[it_code] * unob_ratio[lst_date] else: break lst_date = lst_date + timedelta(1) return unob_flow_num