Example #1
0
    def __init__(self, degree, n_nodes):
        # initialize kr
        generator = kr_util()
        self.kr = generator.build_kr(n_nodes, degree)

        g_p = graph_util().generate_random_regular_bipartite(n_nodes, degree)
        arbitrary_p = graph_util().generate_random_regular_bipartite(
            n_nodes, 1)

        # create the C versions of the graphs
        self.g_c_structure = graph.create_graph_structure_test(n_nodes)
        self.g_c = graph.create_graph_edges_test(n_nodes)
        self.g_c_copy = graph.create_graph_edges_test(n_nodes)
        for edge in g_p.edges_iter():
            graph.add_edge(self.g_c_structure, self.g_c, edge[0], edge[1])
        graph.copy_edges(self.g_c, self.g_c_copy, n_nodes)

        self.arbitrary_c = graph.create_graph_edges_test(n_nodes)
        for edge in arbitrary_p.edges_iter():
            graph.add_edge(self.g_c_structure, self.g_c, edge[0], edge[1])
            graph.set_edge(self.g_c_structure, self.g_c_copy, self.arbitrary_c,
                           edge[0], edge[1])

        # allocate solution
        self.solution = kapoorrizzi.create_matching_set()
    def test(self):
        degree = 40
        n_nodes = 15

        # initialize kr
        generator = kr_util()
        kr = generator.build_kr(n_nodes, degree, print_debug=True)
        
        # generate graph and arbitrary matching
        g_p = graph_util().generate_random_regular_bipartite(n_nodes, degree)
        arbitrary_p = graph_util().generate_random_regular_bipartite(n_nodes, 1)

        # create the C versions of the graphs, and copies
        g_c_structure = graph.create_graph_structure_test(n_nodes)
        g_c = graph.create_graph_edges_test(n_nodes)
        g_c_copy = graph.create_graph_edges_test(n_nodes)
        for edge in g_p.edges_iter():
            graph.add_edge(g_c_structure, g_c, edge[0], edge[1])
        graph.copy_edges(g_c, g_c_copy, n_nodes)
        self.assertEqual(graph.get_max_degree(g_c, n_nodes), degree)
        self.assertEqual(graph.get_max_degree(g_c_copy, n_nodes), degree)

        arbitrary_c = graph.create_graph_edges_test(n_nodes)
        for edge in arbitrary_p.edges_iter():
            graph.add_edge(g_c_structure, g_c, edge[0], edge[1])
            graph.set_edge(g_c_structure, g_c_copy, arbitrary_c, edge[0], edge[1])
        self.assertEqual(graph.get_max_degree(arbitrary_c, n_nodes), 1)

        # solve
        solution = kapoorrizzi.create_matching_set()
        kapoorrizzi.solve(kr, g_c_structure, g_c_copy, arbitrary_c, solution)

        # check solution

        # check that we have the correct number of matchings
        num_matchings = kapoorrizzi.get_num_matchings(solution)
        self.assertEqual(num_matchings, degree + 1)

        # check that each matching is a perfect matching
        for i in range(num_matchings):
            matching = kapoorrizzi.get_matching(solution, i)
            self.assertTrue(graph.is_perfect_matching(matching, n_nodes))

        # check sum of matchings equals the original graph
        matchings_graph_c = graph.create_graph_edges_test(n_nodes)
        for i in range(num_matchings):
            matching = kapoorrizzi.get_matching(solution, i)
            graph.add_edges(matchings_graph_c, matching, n_nodes)
        self.assertTrue(graph.are_equal(matchings_graph_c, g_c, n_nodes))

        # clean up
        kapoorrizzi.destroy_kr(kr)
        kapoorrizzi.destroy_matching_set(solution)
        graph.destroy_graph_structure_test(g_c_structure)
        graph.destroy_graph_edges_test(g_c)
        graph.destroy_graph_edges_test(g_c_copy)
        graph.destroy_graph_edges_test(arbitrary_c)

        pass
Example #3
0
    def __init__(self, degree, n_nodes):
        # initialize kr
        generator = kr_util()
        self.kr = generator.build_kr(n_nodes, degree)

        g_p = graph_util().generate_random_regular_bipartite(n_nodes, degree)
        arbitrary_p = graph_util().generate_random_regular_bipartite(n_nodes, 1)
        
        # create the C versions of the graphs
        self.g_c_structure = graph.create_graph_structure_test(n_nodes)
        self.g_c = graph.create_graph_edges_test(n_nodes)
        self.g_c_copy = graph.create_graph_edges_test(n_nodes)
        for edge in g_p.edges_iter():
            graph.add_edge(self.g_c_structure, self.g_c, edge[0], edge[1])
        graph.copy_edges(self.g_c, self.g_c_copy, n_nodes)
 
        self.arbitrary_c = graph.create_graph_edges_test(n_nodes)
        for edge in arbitrary_p.edges_iter():
            graph.add_edge(self.g_c_structure, self.g_c, edge[0], edge[1])
            graph.set_edge(self.g_c_structure, self.g_c_copy, self.arbitrary_c, edge[0], edge[1])

        # allocate solution
        self.solution = kapoorrizzi.create_matching_set()
Example #4
0
    def test(self):
        degree = 40
        n_nodes = 15

        # initialize kr
        generator = kr_util()
        kr = generator.build_kr(n_nodes, degree, print_debug=True)

        # generate graph and arbitrary matching
        g_p = graph_util().generate_random_regular_bipartite(n_nodes, degree)
        arbitrary_p = graph_util().generate_random_regular_bipartite(
            n_nodes, 1)

        # create the C versions of the graphs, and copies
        g_c_structure = graph.create_graph_structure_test(n_nodes)
        g_c = graph.create_graph_edges_test(n_nodes)
        g_c_copy = graph.create_graph_edges_test(n_nodes)
        for edge in g_p.edges_iter():
            graph.add_edge(g_c_structure, g_c, edge[0], edge[1])
        graph.copy_edges(g_c, g_c_copy, n_nodes)
        self.assertEqual(graph.get_max_degree(g_c, n_nodes), degree)
        self.assertEqual(graph.get_max_degree(g_c_copy, n_nodes), degree)

        arbitrary_c = graph.create_graph_edges_test(n_nodes)
        for edge in arbitrary_p.edges_iter():
            graph.add_edge(g_c_structure, g_c, edge[0], edge[1])
            graph.set_edge(g_c_structure, g_c_copy, arbitrary_c, edge[0],
                           edge[1])
        self.assertEqual(graph.get_max_degree(arbitrary_c, n_nodes), 1)

        # solve
        solution = kapoorrizzi.create_matching_set()
        kapoorrizzi.solve(kr, g_c_structure, g_c_copy, arbitrary_c, solution)

        # check solution

        # check that we have the correct number of matchings
        num_matchings = kapoorrizzi.get_num_matchings(solution)
        self.assertEqual(num_matchings, degree + 1)

        # check that each matching is a perfect matching
        for i in range(num_matchings):
            matching = kapoorrizzi.get_matching(solution, i)
            self.assertTrue(graph.is_perfect_matching(matching, n_nodes))

        # check sum of matchings equals the original graph
        matchings_graph_c = graph.create_graph_edges_test(n_nodes)
        for i in range(num_matchings):
            matching = kapoorrizzi.get_matching(solution, i)
            graph.add_edges(matchings_graph_c, matching, n_nodes)
        self.assertTrue(graph.are_equal(matchings_graph_c, g_c, n_nodes))

        # clean up
        kapoorrizzi.destroy_kr(kr)
        kapoorrizzi.destroy_matching_set(solution)
        graph.destroy_graph_structure_test(g_c_structure)
        graph.destroy_graph_edges_test(g_c)
        graph.destroy_graph_edges_test(g_c_copy)
        graph.destroy_graph_edges_test(arbitrary_c)

        pass