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)
Example #2
0
import pickle
import indp
root = '/home/hesam/Desktop/Files/Game_Shelby_County/results/ng_results_L4_m92_v12_OPTIMISTIC_OPTIMAL/'
with open(root + 'objs_30.pkl', 'rb') as f:
    obj = pickle.load(f)

BASE_DIR = "../data/Extended_Shelby_County/"
DAMAGE_DIR = "../data/Wu_Damage_scenarios/"
obj.net, _, _ = indp.initialize_network(BASE_DIR=BASE_DIR,
                                        external_interdependency_dir=None,
                                        sim_number=0,
                                        magnitude=6,
                                        sample=0,
                                        v=12,
                                        shelby_data='shelby_extended')
indp.add_Wu_failure_scenario(obj.net, DAM_DIR=DAMAGE_DIR, noSet=30, noSce=92)
N_hat_prime = [
    n for n in obj.net.G.nodes(data=True)
    if n[1]['data']['inf_data'].repaired == 0.0
]
for t in range(obj.time_steps):
    obj.objs[t + 1].find_optimal_solution()
    print(obj.objs[t + 1].optimal_solution['total cost'])
    indp.apply_recovery(obj.net, obj.results, t + 1)

obj.save_object_to_file()
Example #3
0
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)  #!!!
Example #4
0
    "NUM_ITERATIONS": 1,
    "OUTPUT_DIR": OUTPUT_DIR + '/ng_results',
    "V": 10,
    "T": 1,
    'L': layers,
    "ALGORITHM": "NORMALGAME",
    'EQUIBALG': 'gnm_solve',
    "RES_ALLOC_TYPE": 'UNIFORM'
}

params["N"], _, _ = indp.initialize_network(BASE_DIR=BASE_DIR,
                                            external_interdependency_dir=None,
                                            sim_number=0,
                                            magnitude=6,
                                            sample=0,
                                            v=params["V"],
                                            shelby_data=True)
params["SIM_NUMBER"] = 1
params["MAGNITUDE"] = 13
indp.add_Wu_failure_scenario(params["N"],
                             DAM_DIR=DAMAGE_DIR,
                             noSet=1,
                             noSce=13)

obj = gameclasses.InfrastructureGame(params)
obj.run_game(compute_optimal=True, plot=True)

# # Getting back the objects ###
# address = '../results/ng_results_L2_m13_v10_UNIFORM/objs_1.pkl'
# with open(address, 'rb') as f:
#     objs_read = pickle.load(f)