예제 #1
0
def solve_kep(cfg, formulation, use_relabelled=True):

    formulations = {
        "uef": ("Uncapped edge formulation", kidney_ip.optimise_uuef),
        "eef": ("EEF", kidney_ip.optimise_eef),
        "eef_full_red": ("EEF with full reduction by cycle generation",
                         kidney_ip.optimise_eef_full_red),
        "hpief_prime": ("HPIEF'", kidney_ip.optimise_hpief_prime),
        "hpief_prime_full_red":
        ("HPIEF' with full reduction by cycle generation",
         kidney_ip.optimise_hpief_prime_full_red),
        "hpief_2prime": ("HPIEF''", kidney_ip.optimise_hpief_2prime),
        "hpief_2prime_full_red":
        ("HPIEF'' with full reduction by cycle generation",
         kidney_ip.optimise_hpief_2prime_full_red),
        "picef": ("PICEF", kidney_ip.optimise_picef),
        "nhs": ("PICEF-NHS", kidney_ip.optimise_picef_nhs),
        #"nhs_chains": ("PICEF-NHS-CHAINS", kidney_ip.optimise_picef_nhs_chains),
        "cf": ("Cycle formulation", kidney_ip.optimise_ccf)
    }

    if formulation in formulations:
        formulation_name, formulation_fun = formulations[formulation]
        if use_relabelled:
            opt_result = kidney_ip.optimise_relabelled(formulation_fun, cfg)
        else:
            opt_result = formulation_fun(cfg)
        if opt_result:
            kidney_utils.check_validity(opt_result, cfg.digraph, cfg.ndds,
                                        cfg.max_cycle, cfg.max_chain)
            opt_result.formulation_name = formulation_name
        return opt_result
    else:
        raise ValueError("Unrecognised IP formulation name")
예제 #2
0
def test_weighted_instance():
    """Checks that the capped formulations agree on the optimal
    result for an instance with weighted edges.
    """
    EPS = 0.000001
    d, ndds = read_with_ndds("test-fixtures/100-random-weights")
    fns = [
        k_ip.optimise_hpief_prime, k_ip.optimise_hpief_2prime,
        k_ip.optimise_hpief_prime_full_red,
        k_ip.optimise_hpief_2prime_full_red, k_ip.optimise_picef,
        k_ip.optimise_ccf, k_ip.optimise_eef, k_ip.optimise_eef_full_red
    ]
    for max_cycle in [0, 1, 2, 3, 4]:
        for max_chain in [0, 1, 2, 3]:
            opt_result_0 = fns[0](k_ip.OptConfig(d, ndds, max_cycle,
                                                 max_chain))
            k_utils.check_validity(opt_result_0, d, ndds, max_cycle, max_chain)
            for fn in fns[1:]:
                opt_result = fn(k_ip.OptConfig(d, ndds, max_cycle, max_chain))
                k_utils.check_validity(opt_result, d, ndds, max_cycle,
                                       max_chain)
                print(max_cycle, max_chain, opt_result.total_score, \
                        opt_result.ip_model.obj_val, opt_result_0.total_score, fn)
                assert abs(opt_result.total_score -
                           opt_result_0.total_score) < EPS
예제 #3
0
def test_preflib_instance_with_zero_chain_cap():
    d, ndds = read_with_ndds("test-fixtures/MD-00001-00000100")
    fns = [ k_ip.optimise_eef, k_ip.optimise_eef_full_red,
            k_ip.optimise_hpief_prime_full_red, k_ip.optimise_hpief_2prime_full_red,
            k_ip.optimise_hpief_prime, k_ip.optimise_hpief_2prime,
            k_ip.optimise_eef, k_ip.optimise_eef_full_red,
            k_ip.optimise_picef, k_ip.optimise_ccf]
    for fn in fns:
        max_cycle = 4
        max_chain = 0
        opt_result = fn(k_ip.OptConfig(d, ndds, max_cycle, max_chain))
        k_utils.check_validity(opt_result, d, ndds, max_cycle, max_chain)
        print fn
        assert opt_result.total_score == 39 
예제 #4
0
def test_failure_aware():
    # Use instance with a 3-cycle and a 3-chain
    d, ndds = read_with_ndds("test-fixtures/3cycle_and_3chain")
    fns = [k_ip.optimise_picef, k_ip.optimise_ccf]
    for fn in fns:
        for max_cycle, max_chain, expected_score in [
                (3, 2, 3*.125 + .75),
                (3, 3, 3*.125 + .875),
                (0, 3, .875),
                (3, 0, 3*.125)]:
        
            opt_result = fn(k_ip.OptConfig(d, ndds, max_cycle, max_chain, edge_success_prob=.5))
            # 3 edges * .5**3 for the cycle; .5 + .5**2 for the chain
            assert opt_result.total_score == expected_score
            k_utils.check_validity(opt_result, d, ndds, max_cycle, max_chain)
예제 #5
0
def test_preflib_instance_with_zero_chain_cap():
    d, ndds = read_with_ndds("test-fixtures/MD-00001-00000100")
    fns = [
        k_ip.optimise_eef, k_ip.optimise_eef_full_red,
        k_ip.optimise_hpief_prime_full_red,
        k_ip.optimise_hpief_2prime_full_red, k_ip.optimise_hpief_prime,
        k_ip.optimise_hpief_2prime, k_ip.optimise_eef,
        k_ip.optimise_eef_full_red, k_ip.optimise_picef, k_ip.optimise_ccf
    ]
    for fn in fns:
        max_cycle = 4
        max_chain = 0
        opt_result = fn(k_ip.OptConfig(d, ndds, max_cycle, max_chain))
        k_utils.check_validity(opt_result, d, ndds, max_cycle, max_chain)
        print(fn)
        assert opt_result.total_score == 39
예제 #6
0
def test_failure_aware():
    # Use instance with a 3-cycle and a 3-chain
    d, ndds = read_with_ndds("test-fixtures/3cycle_and_3chain")
    fns = [k_ip.optimise_picef, k_ip.optimise_ccf]
    for fn in fns:
        for max_cycle, max_chain, expected_score in [(3, 2, 3 * .125 + .75),
                                                     (3, 3, 3 * .125 + .875),
                                                     (0, 3, .875),
                                                     (3, 0, 3 * .125)]:

            opt_result = fn(
                k_ip.OptConfig(d,
                               ndds,
                               max_cycle,
                               max_chain,
                               edge_success_prob=.5))
            # 3 edges * .5**3 for the cycle; .5 + .5**2 for the chain
            assert opt_result.total_score == expected_score
            k_utils.check_validity(opt_result, d, ndds, max_cycle, max_chain)
예제 #7
0
def test_failure_aware():
    # Use instance with a 3-cycle and a 3-chain
    d, ndds = read_with_ndds("test-fixtures/3cycle_and_3chain")
    fns = [k_ip.optimise_picef, k_ip.optimise_ccf]
    edge_weight = 1 * 0.5
    for fn in fns:
        for max_cycle, max_chain, expected_score in [
            (3, 0, 1.5), (3, 2, 1.5 + 0.5 + 0.25),
            (3, 3, 1.5 + 0.5 + 0.25 + 0.125), (0, 3, 0.5 + 0.25 + 0.125)
        ]:
            opt_result = fn(
                k_ip.OptConfig(d,
                               ndds,
                               max_cycle,
                               max_chain,
                               edge_success_prob=.5))
            assert_almost_equal(opt_result.total_score, expected_score)
            LOGGER.debug("%s got %s, expected %s", fn, opt_result.total_score,
                         expected_score)
            k_utils.check_validity(opt_result, d, ndds, max_cycle, max_chain)
예제 #8
0
def test_weighted_instance():
    """Checks that the capped formulations agree on the optimal
    result for an instance with weighted edges.
    """
    EPS = 0.000001
    d, ndds = read_with_ndds("test-fixtures/100-random-weights")
    fns = [k_ip.optimise_hpief_prime,
            k_ip.optimise_hpief_2prime,
            k_ip.optimise_hpief_prime_full_red,
            k_ip.optimise_hpief_2prime_full_red,
            k_ip.optimise_picef, k_ip.optimise_ccf,
            k_ip.optimise_eef, k_ip.optimise_eef_full_red]
    for max_cycle in [0, 1, 2, 3, 4]:
        for max_chain in [0, 1, 2, 3]:
            opt_result_0 = fns[0](k_ip.OptConfig(d, ndds, max_cycle, max_chain))
            k_utils.check_validity(opt_result_0, d, ndds, max_cycle, max_chain)
            for fn in fns[1:]:
                opt_result = fn(k_ip.OptConfig(d, ndds, max_cycle, max_chain))
                k_utils.check_validity(opt_result, d, ndds, max_cycle, max_chain)
                print max_cycle, max_chain, opt_result.total_score, \
                        opt_result.ip_model.obj_val, opt_result_0.total_score, fn
                assert abs(opt_result.total_score - opt_result_0.total_score) < EPS