Пример #1
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
Пример #2
0
    def test_keeps_edge_set(self):
        generator = graph_util()
        for deg in xrange(2, 11, 2):
            for n_side in xrange(2 * deg + 4, 33, 7):
                g_p = generator.generate_random_regular_bipartite(n_side, deg)

                # Create the graph in C
                g_c_structure = graph.create_graph_structure_test(n_side)
                g_c = graph.create_graph_edges_test(n_side)
                g_c_copy = graph.create_graph_edges_test(n_side)
                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_side)
                self.assertEqual(graph.get_max_degree(g_c, n_side), deg)
                self.assertEqual(graph.get_max_degree(g_c_copy, n_side), deg)

                # Create the output graphs
                g1_c = graph.create_graph_edges_test(n_side)
                g2_c = graph.create_graph_edges_test(n_side)

                eulersplit.split(g_c_structure, g_c_copy, g1_c, g2_c)

                # Check that the input graph is now empty
                self.assertEqual(graph.get_max_degree(g_c_copy, n_side), 0)
                for node in xrange(2 * n_side):
                    self.assertEqual(graph.get_degree(g_c_copy, node), 0)

                # Check that graphs have the correct degree
                self.assertEqual(graph.get_max_degree(g1_c, n_side), deg / 2)
                self.assertEqual(graph.get_max_degree(g2_c, n_side), deg / 2)

                # Check that vertices have the correct degree
                for node in xrange(2 * n_side):
                    self.assertEqual(graph.get_degree(g1_c, node), deg / 2)
                    self.assertEqual(graph.get_degree(g2_c, node), deg / 2)

                # Check that the combination of the two graphs equals the original graph
                graph.add_edges(g1_c, g2_c, n_side)
                self.assertTrue(graph.are_equal(g_c, g1_c, n_side))

                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(g1_c)
                graph.destroy_graph_edges_test(g2_c)

        pass
Пример #3
0
    def test_keeps_edge_set(self):
        generator = graph_util()
        for deg in xrange(2, 11, 2):
            for n_side in xrange(2*deg+4,33,7):
                g_p = generator.generate_random_regular_bipartite(n_side, deg)
                
                # Create the graph in C
                g_c_structure = graph.create_graph_structure_test(n_side);
                g_c = graph.create_graph_edges_test(n_side);
                g_c_copy = graph.create_graph_edges_test(n_side)
                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_side)
                self.assertEqual(graph.get_max_degree(g_c, n_side), deg)
                self.assertEqual(graph.get_max_degree(g_c_copy, n_side), deg)
 
                # Create the output graphs
                g1_c = graph.create_graph_edges_test(n_side)
                g2_c = graph.create_graph_edges_test(n_side)

                eulersplit.split(g_c_structure, g_c_copy, g1_c, g2_c)

                # Check that the input graph is now empty
                self.assertEqual(graph.get_max_degree(g_c_copy, n_side), 0)
                for node in xrange(2 * n_side):
                    self.assertEqual(graph.get_degree(g_c_copy, node), 0)

                # Check that graphs have the correct degree
                self.assertEqual(graph.get_max_degree(g1_c, n_side), deg / 2)
                self.assertEqual(graph.get_max_degree(g2_c, n_side), deg / 2)

                # Check that vertices have the correct degree
                for node in xrange(2 * n_side):
                    self.assertEqual(graph.get_degree(g1_c, node), deg / 2)
                    self.assertEqual(graph.get_degree(g2_c, node), deg / 2)

                # Check that the combination of the two graphs equals the original graph
                graph.add_edges(g1_c, g2_c, n_side)
                self.assertTrue(graph.are_equal(g_c, g1_c, n_side))
                   
                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(g1_c)
                graph.destroy_graph_edges_test(g2_c)

        pass
Пример #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
Пример #5
0
        '35': [('45', 100)],
        '41': [('42', 7)],
        '42': [('43', 1)],
        '43': [('34', 7), ('44', 0.001)],
        '44': [('34', 7), ('45', 0.001)],
        '45': [('45', 0.3)]
    }

    emptyGraph = utils.creerGraph(0)

    graphWithoutConnections = utils.add_vertices(emptyGraph,
                                                 vertices,
                                                 src='31',
                                                 dest='45')

    g = utils.add_edges(graphWithoutConnections, edges)

    utils.drawGraph(g)

    pop = utils.creerPopulation(g, 200, True)

    distance_initial = utils.getCout(g, utils.getFittest(g, pop))

    print("Première génération : ")
    utils.displayPop(pop)

    ga = utils.GA()

    for i in range(0, 100):
        pop = utils.evoluerPopulation(ga, g, pop)
        print("G-" + str(i + 1))