def check_persistence(graph, terminals, persistence_type): """Tests for WEAK or STRONG persistence in graph with terminals. WEAK persistence means that values which are 1 in the LP relaxation remain 1 in an optimal IP solution. STRONG persistence means that values which are 0 in the LP relaxation remain 0 in an optimal IP solution. Args: graph: the undirected NetworkX graph for testing persistence terminals: the vertices in the graph which are terminals persistence_type: "strong" or "weak" Returns: test_result: TRUE if persistence holds, FALSE if it does not """ _, unseeded_value, _ = isolation_branching(graph, terminals) _, seeded_value, _ = isolation_branching(graph, terminals, persistence=persistence_type) test_result = round(unseeded_value, 8) == round(seeded_value, 8) return test_result
def time_test_simple( graph, terminals, test_bb, test_bb_weak, test_bb_strong, test_ip_cbc, test_ip_gurobi ): """Runs a single time test of each of the algorithms (B&B, B&B-weak, B&B-strong, IP).""" t1 = time.time() ib_rpt = None if test_bb: _, _, ib_rpt = isolation_branching(graph.copy(), terminals, reporting=False, time_limit=3600) t2 = time.time() ib_weak_rpt = None if test_bb_weak: _, _, ib_weak_rpt = isolation_branching( graph.copy(), terminals, persistence="weak" ) t3 = time.time() ib_strong_rpt = None if test_bb_strong: _, _, ib_strong_rpt = isolation_branching( graph.copy(), terminals, persistence="strong" ) t4 = time.time() ip_cbc_rpt = None if test_ip_cbc: _, _ = ip_algorithm(graph.copy(), terminals) t5 = time.time() ip_gurobi_rpt = None if test_ip_gurobi: _, _ = ip_algorithm( graph.copy(), terminals, solver=GUROBI(msg=False) ) t6 = time.time() return ( (t2 - t1, ib_rpt), (t3 - t2, ib_weak_rpt), (t4 - t3, ib_strong_rpt), (t5 - t4, ip_cbc_rpt), (t6 - t5, ip_gurobi_rpt), )
def test_graph_5(): from ktcut.isolation_branching import isolation_branching from ktcut.lp_algorithm import lp_algorithm test_graphs = SmallGraphs() test_graphs.set_test_graph(5) graph, terminals = test_graphs.get_graph(), test_graphs.get_terminals() _, cut_value, _ = isolation_branching(graph, terminals, persistence='strong') assert cut_value == 110 cut_value = lp_algorithm(graph, terminals) assert cut_value == 110
def test_graph_4(): from ktcut.isolation_branching import isolation_branching from ktcut.ip_algorithm import ip_algorithm from ktcut.lp_algorithm import lp_algorithm from ktcut.persistence import check_persistence test_graphs = SmallGraphs() test_graphs.set_test_graph(4) graph, terminals = test_graphs.get_graph(), test_graphs.get_terminals() _, cut_value, _ = isolation_branching(graph, terminals) assert cut_value == 27 _, cut_value = ip_algorithm(graph, terminals) assert cut_value == 27 cut_value = lp_algorithm(graph, terminals) assert cut_value == 26 assert check_persistence(graph, terminals, 'weak') assert check_persistence(graph, terminals, 'strong')
def test_graph_tutte(): from networkx.generators.small import tutte_graph from ktcut.isolation_branching import isolation_branching graph = tutte_graph() terminals = [1, 17, 34] partition, cut_value, _ = isolation_branching(graph, terminals)