def batch_run(params, layers, player_ordering=[3, 1]): """ Batch run INDP optimization problem for all samples (currently 1-1000), given global parameters. Format for params: "NUM_ITERATIONS": For single timestep INDP, specifies how many iterations to run. For InfoShare, specifies how many rounds of information sharing to perform. "OUTPUT_DIR" : Directory to output results. "MAGNITUDE" : Magnitude of earthquake. Used to look up failure scenarios. "V" : How many resources used in a scenario. Used to look up failure scenarios, and specify v_r for indp. "T" : Number of timesteps to optimize over. Used for td-INDP and InfoShare. "WINDOW_LENGTH" : Slding time window length for td-INDP (for efficiency). "N" : InfrastructureNetwork to use in indp. "SIM_NUMBER" : What failure scenario simulation to use. Used to look up failure scenarios.""" if "N" not in params: print("Initializing network...") InterdepNet = initialize_network( BASE_DIR="../data/INDP_7-20-2015/", external_interdependency_dir="../data/INDP_4-12-2016", sim_number=0, magnitude=params["MAGNITUDE"], v=params["V"]) print(InterdepNet) else: InterdepNet = params["N"] for i in range(1, 2): print("Running sample", i, "...") add_failure_scenario(InterdepNet, BASE_DIR="../data/INDP_7-20-2015/", magnitude=params["MAGNITUDE"], v=params["V"], sim_number=i) params["N"] = InterdepNet params["SIM_NUMBER"] = i if params["ALGORITHM"] == "INDP": run_indp(params, layers=layers, controlled_layers=layers, T=params["T"]) elif params["ALGORITHM"] == "INFO_SHARE": run_info_share(params, layers=layers, T=params["T"]) elif params["ALGORITHM"] == "INRG": run_inrg(params, layers=layers, player_ordering=player_ordering) elif params["ALGORITHM"] == "BACKWARDS_INDUCTION": gametree.run_backwards_induction(InterdepNet, i, players=layers, player_ordering=player_ordering, T=params["T"], outdir=params["OUTPUT_DIR"])
def batch_run(params, fail_sce_param, player_ordering=[3, 1]): ''' Batch run different methods for a given list of damage scenarios, given global parameters. Parameters ---------- params : dict DESCRIPTION. fail_sce_param : dict DESCRIPTION. player_ordering : list, optional DESCRIPTION. The default is [3, 1]. Returns ------- None. Writes to file ''' # Set root directories base_dir = fail_sce_param['BASE_DIR'] damage_dir = fail_sce_param['DAMAGE_DIR'] layers = params['L'] topology = None shelby_data = True ext_interdependency = None if fail_sce_param['TYPE'] == 'Andres': ext_interdependency = "../data/INDP_4-12-2016" elif fail_sce_param['TYPE'] == 'WU': if fail_sce_param['FILTER_SCE'] is not None: list_high_dam = pd.read_csv(fail_sce_param['FILTER_SCE']) elif fail_sce_param['TYPE'] == 'random': pass elif fail_sce_param['TYPE'] == 'synthetic': shelby_data = False topology = fail_sce_param['TOPO'] for m in fail_sce_param['MAGS']: for i in fail_sce_param['SAMPLE_RANGE']: try: list_high_dam if len(list_high_dam.loc[(list_high_dam.set == i) & (list_high_dam.sce == m)].index) == 0: continue except NameError: pass print('\n---Running Magnitude ' + str(m) + ' sample ' + str(i) + '...') print("Initializing network...") if shelby_data: interdep_net, _, _ = indp.initialize_network( BASE_DIR=base_dir, external_interdependency_dir=ext_interdependency, sim_number=0, magnitude=6, sample=0, v=params["V"], shelby_data=shelby_data) else: interdep_net, no_resource, layers = indp.initialize_network( BASE_DIR=base_dir, external_interdependency_dir=ext_interdependency, magnitude=m, sample=i, shelby_data=shelby_data, topology=topology) params["V"] = no_resource params["N"] = interdep_net params["SIM_NUMBER"] = i params["MAGNITUDE"] = m if fail_sce_param['TYPE'] == 'WU': indp.add_Wu_failure_scenario(interdep_net, DAM_DIR=damage_dir, noSet=i, noSce=m) elif fail_sce_param['TYPE'] == 'ANDRES': indp.add_failure_scenario(interdep_net, DAM_DIR=damage_dir, magnitude=m, v=params["V"], sim_number=i) elif fail_sce_param['TYPE'] == 'random': indp.add_random_failure_scenario(interdep_net, DAM_DIR=damage_dir, sample=i) elif fail_sce_param['TYPE'] == 'synthetic': indp.add_synthetic_failure_scenario(interdep_net, DAM_DIR=base_dir, topology=topology, config=m, sample=i) if params["ALGORITHM"] == "INDP": indp.run_indp(params, layers=layers, controlled_layers=layers, T=params["T"], saveModel=False, print_cmd_line=True) elif params["ALGORITHM"] == "INFO_SHARE": indp.run_info_share(params, layers=layers, T=params["T"]) elif params["ALGORITHM"] == "INRG": indp.run_inrg(params, layers=layers, player_ordering=player_ordering) elif params["ALGORITHM"] == "BACKWARDS_INDUCTION": gametree.run_backwards_induction( interdep_net, i, players=layers, player_ordering=player_ordering, T=params["T"], outdir=params["OUTPUT_DIR"]) elif params["ALGORITHM"] == "JC": dindp.run_judgment_call(params, layers=layers, T=params["T"], save_jc_model=False, print_cmd=True)
def batch_run(params, failSce_param, layers, player_ordering=[3, 1]): """ Batch run INDP optimization problem for all samples (currently 1-1000), given global parameters. Format for params: "NUM_ITERATIONS": For single timestep INDP, specifies how many iterations to run. For InfoShare, specifies how many rounds of information sharing to perform. "OUTPUT_DIR" : Directory to output results. "MAGNITUDE" : Magnitude of earthquake. Used to look up failure scenarios. "V" : How many resources used in a scenario. Used to look up failure scenarios, and specify v_r for indp. "T" : Number of timesteps to optimize over. Used for td-INDP and InfoShare. "WINDOW_LENGTH" : Slding time window length for td-INDP (for efficiency). "N" : InfrastructureNetwork to use in indp. "SIM_NUMBER" : What failure scenario simulation to use. Used to look up failure scenarios.""" # Set root directories if failSce_param['type'] == 'Andres': base_dir = "../data/INDP_7-20-2015/" ext_interdependency = "../data/INDP_4-12-2016" topology = None shelby_data = True elif failSce_param['type'] == 'WU': if failSce_param['filtered_List'] != None: listHD = pd.read_csv(failSce_param['filtered_List']) base_dir = "../data/Extended_Shelby_County/" ext_interdependency = None topology = None shelby_data = True elif failSce_param['type'] == 'synthetic': base_dir = failSce_param['Base_dir'] ext_interdependency = None shelby_data = False topology = failSce_param['topology'] for m in failSce_param['mags']: for i in failSce_param['sample_range']: if failSce_param['filtered_List'] == None or len( listHD.loc[(listHD.set == i) & (listHD.sce == m)].index): print '\n---Running Magnitude ' + ` m ` + ' sample ' + ` i ` + '...' print("Initializing network...") if not shelby_data: InterdepNet, noResource, layers = initialize_network( BASE_DIR=base_dir, external_interdependency_dir=ext_interdependency, magnitude=m, sample=i, shelby_data=shelby_data, topology=topology) params["V"] = noResource else: InterdepNet, _, _ = initialize_network( BASE_DIR=base_dir, external_interdependency_dir=ext_interdependency, sim_number=0, magnitude=6, sample=0, v=params["V"], shelby_data=shelby_data) params["N"] = InterdepNet params["SIM_NUMBER"] = i params["MAGNITUDE"] = m if failSce_param['type'] == 'WU': add_Wu_failure_scenario( InterdepNet, BASE_DIR="../data/Wu_Damage_scenarios/", noSet=i, noSce=m) elif failSce_param['type'] == 'ANDRES': add_failure_scenario(InterdepNet, BASE_DIR="../data/INDP_7-20-2015/", magnitude=m, v=params["V"], sim_number=i) elif failSce_param['type'] == 'synthetic': add_synthetic_failure_scenario(InterdepNet, BASE_DIR=base_dir, topology=topology, config=m, sample=i) if params["ALGORITHM"] == "INDP": run_indp(params, layers=layers, controlled_layers=layers, T=params["T"], saveModel=False, print_cmd_line=False) elif params["ALGORITHM"] == "INFO_SHARE": run_info_share(params, layers=layers, T=params["T"]) elif params["ALGORITHM"] == "INRG": run_inrg(params, layers=layers, player_ordering=player_ordering) elif params["ALGORITHM"] == "BACKWARDS_INDUCTION": gametree.run_backwards_induction( InterdepNet, i, players=layers, player_ordering=player_ordering, T=params["T"], outdir=params["OUTPUT_DIR"]) elif params["ALGORITHM"] == "JUDGMENT_CALL": run_judgment_call(params, layers=layers, T=params["T"], saveJCModel=False, print_cmd=False)
def batch_run(params, fail_sce_param, player_ordering=[3, 1]): ''' Batch run different methods for a given list of damage scenarios, given global parameters. Parameters ---------- params : dict DESCRIPTION. fail_sce_param : dict DESCRIPTION. player_ordering : list, optional DESCRIPTION. The default is [3, 1]. Returns ------- None. Writes to file ''' # Set root directories base_dir = fail_sce_param['BASE_DIR'] damage_dir = fail_sce_param['DAMAGE_DIR'] topology = None infrastructure_data = None ext_interdependency = None if fail_sce_param['TYPE'] == 'Andres': infrastructure_data = 'shelby_old' ext_interdependency = "../data/INDP_4-12-2016" elif fail_sce_param['TYPE'] == 'WU': infrastructure_data = 'shelby_extended' if fail_sce_param['FILTER_SCE'] is not None: list_high_dam = pd.read_csv(fail_sce_param['FILTER_SCE']) elif fail_sce_param['TYPE'] == 'random': infrastructure_data = 'shelby_extended' elif fail_sce_param['TYPE'] == 'synthetic': topology = fail_sce_param['TOPO'] print('----Running for resources: ' + str(params['V'])) for m in fail_sce_param['MAGS']: for i in fail_sce_param['SAMPLE_RANGE']: try: list_high_dam if len(list_high_dam.loc[(list_high_dam.set == i)&\ (list_high_dam.sce == m)].index) == 0: continue except NameError: pass print('---Running Magnitude ' + str(m) + ' sample ' + str(i) + '...') print("Initializing network...") if infrastructure_data: params["N"], _, _ = indp.initialize_network( BASE_DIR=base_dir, external_interdependency_dir=ext_interdependency, sim_number=0, magnitude=6, sample=0, v=params["V"], infrastructure_data=infrastructure_data) else: params["N"], params["V"], params[ 'L'] = indp.initialize_network( BASE_DIR=base_dir, external_interdependency_dir=ext_interdependency, magnitude=m, sample=i, infrastructure_data=infrastructure_data, topology=topology) params["SIM_NUMBER"] = i params["MAGNITUDE"] = m # Check if the results exist output_dir_full = '' if params["ALGORITHM"] in ["INDP"]: output_dir_full = params["OUTPUT_DIR"]+'_L'+str(len(params["L"]))+'_m'+\ str(params["MAGNITUDE"])+"_v"+str(params["V"])+'/actions_'+str(i)+'_.csv' if os.path.exists(output_dir_full): print('results are already there\n') continue if fail_sce_param['TYPE'] == 'WU': indp.add_Wu_failure_scenario(params["N"], DAM_DIR=damage_dir, noSet=i, noSce=m) elif fail_sce_param['TYPE'] == 'ANDRES': indp.add_failure_scenario(params["N"], DAM_DIR=damage_dir, magnitude=m, v=params["V"], sim_number=i) elif fail_sce_param['TYPE'] == 'random': indp.add_random_failure_scenario(params["N"], DAM_DIR=damage_dir, sample=i) elif fail_sce_param['TYPE'] == 'synthetic': indp.add_synthetic_failure_scenario(params["N"], DAM_DIR=base_dir, topology=topology, config=m, sample=i) if params["ALGORITHM"] == "INDP": indp.run_indp(params, layers=params['L'], controlled_layers=params['L'], T=params["T"], saveModel=False, print_cmd_line=False, co_location=True) if params["ALGORITHM"] == "MH": mh.run_mh(params, validate=False, T=params["T"], layers=params['L'], controlled_layers=params['L'], saveModel=True, print_cmd_line=False, co_location=True) elif params["ALGORITHM"] == "INFO_SHARE": indp.run_info_share(params, layers=params['L'], T=params["T"]) elif params["ALGORITHM"] == "INRG": indp.run_inrg(params, layers=params['L'], player_ordering=player_ordering) elif params["ALGORITHM"] == "BACKWARDS_INDUCTION": gametree.run_backwards_induction( params["N"], i, players=params['L'], player_ordering=player_ordering, T=params["T"], outdir=params["OUTPUT_DIR"]) elif params["ALGORITHM"] == "JC": dindputils.run_judgment_call(params, save_jc_model=True, print_cmd=False) elif params["ALGORITHM"] in ["NORMALGAME", "BAYESGAME"]: gameutils.run_game(params, save_results=True, print_cmd=False, save_model=False, plot2D=False) #!!!
def batch_run(params, fail_sce_param): """ Batch run different methods for a given list of damage scenarios, given global parameters. Parameters ---------- params : dict DESCRIPTION. fail_sce_param : dict DESCRIPTION. Returns ------- None. Writes to file """ # Set root directories base_dir = fail_sce_param['BASE_DIR'] damage_dir = fail_sce_param['DAMAGE_DIR'] topology = None infrastructure_data = None ext_interdependency = None if fail_sce_param['TYPE'] == 'Andres': infrastructure_data = 'shelby_old' ext_interdependency = "../data/INDP_4-12-2016" elif fail_sce_param['TYPE'] == 'WU': infrastructure_data = 'shelby_extended' if fail_sce_param['FILTER_SCE'] is not None: list_high_dam = pd.read_csv(fail_sce_param['FILTER_SCE']) elif fail_sce_param['TYPE'] == 'from_csv': infrastructure_data = 'shelby_extended' elif fail_sce_param['TYPE'] == 'synthetic': topology = fail_sce_param['TOPO'] print('----Running for resources: ' + str(params['V'])) for m in fail_sce_param['MAGS']: for i in fail_sce_param['SAMPLE_RANGE']: params["SIM_NUMBER"] = i params["MAGNITUDE"] = m try: list_high_dam if len(list_high_dam.loc[(list_high_dam.set == i) & \ (list_high_dam.sce == m)].index) == 0: continue except NameError: pass # Check if the results exist # !!! move it after initializing network for synthetic nets since L is identified there output_dir_full = '' if params["ALGORITHM"] in ["INDP"]: out_dir_suffix_res = indp.get_resource_suffix(params) output_dir_full = params["OUTPUT_DIR"] + '_L' + str(len(params["L"])) + '_m' + \ str(params["MAGNITUDE"]) + "_v" + out_dir_suffix_res + '/actions_' + str(i) + '_.csv' if os.path.exists(output_dir_full): print('results are already there\n') continue print('---Running Magnitude ' + str(m) + ' sample ' + str(i) + '...') if params['TIME_RESOURCE']: print('Computing repair times...') indp.time_resource_usage_curves(base_dir, damage_dir, i) print("Initializing network...") if infrastructure_data: params["N"], _, _ = indp.initialize_network( base_dir=base_dir, external_interdependency_dir=ext_interdependency, sim_number=0, magnitude=m, sample=i, v=params["V"], infrastructure_data=infrastructure_data, extra_commodity=params["EXTRA_COMMODITY"]) else: params["N"], params["V"], params[ 'L'] = indp.initialize_network( base_dir=base_dir, external_interdependency_dir=ext_interdependency, magnitude=m, sample=i, infrastructure_data=infrastructure_data, topology=topology) if params['DYNAMIC_PARAMS']: print("Computing dislocation data...") dyn_dmnd = dislocationutils.create_dynamic_param( params, N=params["N"], T=params["NUM_ITERATIONS"]) params['DYNAMIC_PARAMS']['DEMAND_DATA'] = dyn_dmnd if fail_sce_param['TYPE'] == 'WU': indp.add_wu_failure_scenario(params["N"], dam_dir=damage_dir, no_set=i, no_sce=m) elif fail_sce_param['TYPE'] == 'ANDRES': indp.add_failure_scenario(params["N"], dam_dir=damage_dir, magnitude=m, v=params["V"], sim_number=i) elif fail_sce_param['TYPE'] == 'from_csv': indp.add_from_csv_failure_scenario(params["N"], sample=i, dam_dir=damage_dir) elif fail_sce_param['TYPE'] == 'synthetic': indp.add_synthetic_failure_scenario(params["N"], dam_dir=base_dir, topology=topology, config=m, sample=i) if params["ALGORITHM"] == "INDP": indp.run_indp(params, layers=params['L'], controlled_layers=params['L'], T=params["T"], save_model=True, print_cmd_line=False, co_location=False) if params["ALGORITHM"] == "MH": mh.run_mh(params, validate=False, T=params["T"], layers=params['L'], controlled_layers=params['L'], saveModel=True, print_cmd_line=False, co_location=True) elif params["ALGORITHM"] == "INRG": gameutils.run_inrg(params, layers=params['L'], player_ordering=player_ordering) elif params["ALGORITHM"] == "BACKWARDS_INDUCTION": gametree.run_backwards_induction( params["N"], i, players=params['L'], player_ordering=player_ordering, T=params["T"], outdir=params["OUTPUT_DIR"]) elif params["ALGORITHM"] == "JC": dindputils.run_judgment_call(params, save_jc_model=False, print_cmd=False) elif params["ALGORITHM"] in ["NORMALGAME", "BAYESGAME"]: gameutils.run_game(params, save_results=True, print_cmd=False, save_model=False, plot2D=False) # !!!