def save_recovery_result_2():
    G0 = case24_ieee_rts()
    G1 = case39()
    G2 = case57()
    G3 = case118()
    G4 = Power_Graph.case_preprocess(case300())
    G_list = [G0, G1, G2, G3, G4]
    G = G4

    P = 30
    R = 16
    round = 9
    delete_num = 15
    #iter_times = 11
    ramp_rate = 0.3
    seq_len = 9
    random_rep_num = 50
    ramp_arr = np.arange(0.1, 1.1, 0.1)
    P_arr = np.arange(10, 32, 2)
    R_arr = np.arange(2, 32, 2)

    for p in P_arr:

        path = 'recovery_result4/case' + str(len(G['bus'])) + '_0.3'
        if os.path.exists(path) == False:
            os.mkdir(path)
        file_name = path + '/case' + str(len(
            G['bus'])) + '_setup_' + str(p) + '.txt'
        new_file = open(file_name, 'w')
        new_file.write('ramp_rate = ' + str(ramp_rate) + '\n')
        new_file.write('P=' + str(p) + ' , ' + 'R=' + str(R) + ' , ' +
                       'round=' + str(round) + '\n')
        new_file.write('length of recovery seq ' + str(seq_len) + '\n')

        g = Power_Graph()
        g.init_by_case(G)
        g.set_ramp_rate(ramp_rate)
        delete_list = []
        while len(delete_list) != delete_num:
            ran_num = random.randint(1, len(G['bus']))
            if ran_num not in delete_list:
                delete_list.append(ran_num)
        delete_list = [
            66, 289, 298, 255, 72, 87, 263, 290, 83, 167, 241, 13, 225, 243,
            232, 34, 147, 269, 132, 249, 122, 2, 37, 127, 94, 199, 202, 76,
            156, 67, 131, 248, 188, 238, 15, 84, 44, 8, 220, 189, 206, 162,
            169, 107, 17, 282, 164, 198, 115, 261
        ]
        #delete_list=[192, 27, 15, 153, 198, 189, 171, 33, 256, 265, 25, 218, 284, 6, 261, 155, 131, 260, 97, 93, 55, 95, 124, 69, 37, 34, 94, 201, 12, 144, 1, 254, 193, 83, 59, 230, 289, 9, 200, 264, 277, 246, 143, 179, 100, 232, 233, 91, 151, 170]
        #delete_list=[105, 49, 69, 110, 28, 62, 31, 41, 96, 15, 108, 67, 63, 115, 79, 101, 83, 76, 61, 75, 45, 12, 32, 113, 98, 111, 91, 95, 68, 9]
        #delete_list = [74, 45, 51, 1, 106, 3, 82, 67, 90, 114, 81, 30, 7, 17, 79, 103, 95, 46, 115, 118, 40, 117, 96, 116, 88, 27, 57, 110, 28, 21]
        #delete_list=[25, 17, 8, 32, 37, 24, 9, 28, 47, 15]
        #delete_list = [37, 18, 31, 20, 1, 35, 15, 57, 54, 2, 17, 6, 51, 3, 55]
        new_file.write('delete length ' + str(len(delete_list)) + '\n')
        new_file.write('delete list ' + str(delete_list) + '\n')

        for item in delete_list:
            g.delete_bus(item)
        cf = Power_Failure(g)
        for item in delete_list:
            cf.failed_bus_id.append(item)

        cf.failure_process()

        new_file.write('num of failed bus ' + str(len(cf.failed_bus_id)) +
                       '\n')
        new_file.write('failed bus ' + str(cf.failed_bus_id) + '\n')
        steady_bus_num = 0
        steady_branch_num = 0
        for graph in cf.steady_list:
            steady_bus_num += len(graph.bus_id)
            steady_branch_num += len(graph.branch)
        new_file.write('num of steady bus ' + str(steady_bus_num) + '\n')
        new_file.write('num of steady branch ' + str(steady_branch_num) +
                       '\n\n')

        ini_g = Power_Graph()
        ini_g.init_by_case(G)
        R_SAG = Recovery_SAG(cf.steady_list, ini_g)
        R_SAG.initialize_param(p, R, round)
        candidate_set = R_SAG.cal_candidate_set()
        RRC_set = R_SAG.cal_RRC_set()
        RRC_seq = {}
        RRC_residual_per = {}
        for i in range(1, seq_len + 1):
            RRC_residual_per[i] = 0.0
        for item in RRC_set.items():
            # print item
            new_file.write(str(item[0]) + '\n')
            current_set = item[1]
            sorted_list = sorted(current_set.items(),
                                 key=lambda item: item[1],
                                 reverse=True)
            for k, v in sorted_list:
                new_file.write(str(k) + ' ' + str(v) + '\n')
                if v > RRC_residual_per[len(k)]:
                    RRC_seq[len(k)] = k
                    RRC_residual_per[len(k)] = v
        new_file.write('\n')

        R_SAG.construct_SAG()
        new_file.write('node num of SRG ' + str(len(R_SAG.SAG.node_list)) +
                       '\n\n')

        for current in range(2, seq_len + 1):
            SAG_seq = R_SAG.cal_SAG_recovery_seq_2(current)
            steady_list = copy.deepcopy(cf.steady_list)
            gr = Grid_Recovery(steady_list, ini_g)
            ini_residual_per = gr.cal_residual_power()
            SRG_residual_per = gr.recover_with_sequence(SAG_seq)

            steady_list = copy.deepcopy(cf.steady_list)
            gr = Grid_Recovery(steady_list, ini_g)
            degree_residual_per, degree_seq = gr.recovery_degree_2(current)

            steady_list = copy.deepcopy(cf.steady_list)
            gr = Grid_Recovery(steady_list, ini_g)
            low_degree_residual_per, low_degree_seq = gr.recovery_low_degree_2(
                current)

            steady_list = copy.deepcopy(cf.steady_list)
            gr = Grid_Recovery(steady_list, ini_g)
            load_residual_per, load_seq = gr.recovery_load_2(current)

            steady_list = copy.deepcopy(cf.steady_list)
            gr = Grid_Recovery(steady_list, ini_g)
            low_load_residual_per, low_load_seq = gr.recovery_low_load_2(
                current)

            sum = 0.0
            for i in range(0, random_rep_num):
                steady_list = copy.deepcopy(cf.steady_list)
                gr = Grid_Recovery(steady_list, ini_g)
                residual_per, seq = gr.recovery_random_2(current)
                sum += residual_per
            aver_residual_per = sum / random_rep_num

            new_file.write('current length of seq: ' + str(current) + '\n')
            new_file.write('SRG recovery seq ' + str(SAG_seq) + '\n')
            new_file.write('RRC recovery seq ' + str(RRC_seq[current]) + '\n')
            new_file.write('high degree based recovery seq ' +
                           str(degree_seq) + '\n')
            new_file.write('low degree based recovery seq ' +
                           str(low_degree_seq) + '\n')
            new_file.write('high load based recovery seq ' + str(load_seq) +
                           '\n')
            new_file.write('low load based recovery seq ' + str(low_load_seq) +
                           '\n')
            new_file.write('\n')

            new_file.write('residual percent of power before recovery ' +
                           str(ini_residual_per) + '\n')
            new_file.write('residual percent of power after SRG recovery ' +
                           str(SRG_residual_per) + '\n')
            new_file.write('residual percent of power after RRC recovery ' +
                           str(RRC_residual_per[current]) + '\n')
            new_file.write(
                'residual percent of power after high degree based recovery ' +
                str(degree_residual_per) + '\n')
            new_file.write(
                'residual percent of power after low degree based recovery ' +
                str(low_degree_residual_per) + '\n')
            new_file.write(
                'residual percent of power after high load based recovery ' +
                str(load_residual_per) + '\n')
            new_file.write(
                'residual percent of power after low load based recovery ' +
                str(low_load_residual_per) + '\n')
            new_file.write(
                'average residual percent of power after random based recovery '
                + str(aver_residual_per) + '\n')
            new_file.write('\n\n')

        new_file.close()
def save_recovery_result():
    G0 = case24_ieee_rts()
    G1 = case39()
    G2 = case57()
    G3 = case118()
    G4 = Power_Graph.case_preprocess(case300())
    G = G2

    round = 5
    delete_num = 15
    ramp_rate = 0.3
    seq_len = 5

    path = 'exhaustive/case' + str(len(G['bus']))
    if os.path.exists(path) == False:
        os.mkdir(path)
    file_name = path + '/case' + str(len(G['bus'])) + '_exh2' + '.txt'
    new_file = open(file_name, 'w')
    new_file.write('ramp_rate = ' + str(ramp_rate) + '\n')
    new_file.write('length of recovery seq ' + str(seq_len) + '\n')

    g = Power_Graph()
    g.init_by_case(G)
    g.set_ramp_rate(ramp_rate)
    delete_list = []
    while len(delete_list) != delete_num:
        ran_num = random.randint(1, len(G['bus']))
        if ran_num not in delete_list:
            delete_list.append(ran_num)

    #delete_list =  [66, 289, 298, 255, 72, 87, 263, 290, 83, 167, 241, 13, 225, 243, 232, 34, 147, 269, 132, 249, 122, 2, 37, 127, 94, 199, 202, 76, 156, 67, 131, 248, 188, 238, 15, 84, 44, 8, 220, 189, 206, 162, 169, 107, 17, 282, 164, 198, 115, 261]
    #delete_list = [44, 54, 11, 3, 6, 28, 2, 15, 30, 9]
    new_file.write('delete length ' + str(len(delete_list)) + '\n')
    new_file.write('delete list ' + str(delete_list) + '\n')

    for item in delete_list:
        g.delete_bus(item)
    cf = Power_Failure(g)
    for item in delete_list:
        cf.failed_bus_id.append(item)

    cf.failure_process()

    new_file.write('num of failed bus ' + str(len(cf.failed_bus_id)) + '\n')
    new_file.write('failed bus ' + str(cf.failed_bus_id) + '\n')
    steady_bus_num = 0
    steady_branch_num = 0
    for graph in cf.steady_list:
        steady_bus_num += len(graph.bus_id)
        steady_branch_num += len(graph.branch)
    new_file.write('num of steady bus ' + str(steady_bus_num) + '\n')
    new_file.write('num of steady branch ' + str(steady_branch_num) + '\n\n')

    ini_g = Power_Graph()
    ini_g.init_by_case(G)

    gr = Grid_Recovery(cf.steady_list, ini_g)
    candidate_num = len(gr.connect_bus_list(cf.steady_list))
    new_file.write('num of failed bus which can be recovered ' +
                   str(candidate_num) + '\n')
    ini_residual_per = gr.cal_residual_power()
    new_file.write('residual power before recovery ' + str(ini_residual_per) +
                   '\n\n')
    R_srg = Recovery_SAG.Recovery_SAG(cf.steady_list, ini_g)
    R_srg.initialize_param(44, 16, round)
    candidate_P_set = R_srg.cal_candidate_set()
    sorted_list = sorted(candidate_P_set.items(),
                         key=lambda item: item[1],
                         reverse=True)
    new_file.write('candidate P set ' + '\n')
    for item in sorted_list:
        new_file.write(str(item[0]) + ' ' + str(item[1]) + '\n')
    new_file.write('\n')

    R_ex = Recovery_exhaustive(cf.steady_list, ini_g)

    for num in range(1, seq_len + 1):
        seq_dic = R_ex.recovery_exhaustive(num)
        sorted_list = sorted(seq_dic.items(),
                             key=lambda item: item[1],
                             reverse=True)
        new_file.write('exhaustive search recovery sequence length: ' +
                       str(num) + '\n')
        for item in sorted_list:
            new_file.write(str(item[0]) + ' ' + str(item[1]) + '\n')

        new_file.write('\n')

    new_file.close()
    #num = 80
    delete_list = [
        30, 279, 264, 138, 143, 9, 56, 202, 225, 90, 140, 256, 173, 247, 185,
        60, 144, 33, 171, 91, 63, 97, 74, 122, 258, 134, 166, 62, 51, 54, 48,
        165, 43, 36, 42, 101, 178, 257, 170, 286, 148, 119, 208, 283, 260, 273,
        81, 151, 44, 167, 133, 154, 244, 92, 201, 194, 16, 251, 252, 175
    ]
    '''
    while len(delete_list) != num:
        ran_num = random.randint(1, 300)
        if ran_num not in delete_list:
            delete_list.append(ran_num)
    '''
    for item in delete_list:
        g.delete_bus(item)

    cf = Power_Failure(g)
    for item in delete_list:
        cf.failed_bus_id.append(item)

    cf.failure_process()
    for graph in cf.steady_list:
        print 'steady graph', graph.bus_id
    print 'failed bus', cf.failed_bus_id
    Power_Graph.draw_graph_list(cf.steady_list)

    ini_g = Power_Graph()
    ini_g.init_by_case(G)
    R_SAG = Recovery_SAG(cf.steady_list, ini_g)
    R_SAG.initialize_param(P=30, R=16, round=15)