Пример #1
0
 def test_add_two_edges(self):
     g = Graph()
     g.add_node("new")
     g.add_node("node")
     g.add_node("test")
     g.add_edge("new", "node")
     g.add_edge("node", "test", 45)
     test_list = [(g.node_dict, {
         "new": [],
         "node": [],
         "test": []
     }),
                  (g.adjacency_matrix, {
                      "new": {
                          "new": 999999,
                          "node": 999998,
                          "test": 999999
                      },
                      "node": {
                          "new": 999999,
                          "node": 999999,
                          "test": 45
                      },
                      "test": {
                          "new": 999999,
                          "node": 999999,
                          "test": 999999
                      }
                  }), (g.nb_nodes, 3), (g.nb_edges, 2)]
     for g_element, expected_value in test_list:
         with self.subTest():
             self.assertEqual(g_element, expected_value)
Пример #2
0
def algo_for_combinations(graph, start, algorithm, bar_number, price, is_hk):
    edge_list = graph.edge_bar_list(start)
    print("Edge list ok")
    remaining_bars = []
    min_dist = 9999
    min_path = []
    min_graph = Graph()
    for i in range(len(edge_list)):
        if edge_list[i][2] < 500:
            remaining_bars.append(edge_list[i][1][1])
    if not is_hk:
        bars = [edge_list[0][0][1]] + remaining_bars
        g = Graph()
        g.build_sub_graph(bars)
        print("Subgraph ok")
        distance, path = algorithm(g, 0, int(bar_number), price)
        return distance, path, g
    else:
        print("Number of candidates: ", len(remaining_bars))
        combinations = find_combinations(tuple(remaining_bars), int(bar_number))
        i = 0
        for combination in combinations:
            comb = [edge_list[0][0][1]] + list(combination)
            g = Graph()
            g.build_sub_graph(comb)
            print("Subgraph ok")
            distance, path = algorithm(g, 0)
            print("Iteration ", i, ": Ok")
            i += 1
            if distance < min_dist:
                min_dist = distance
                min_path = path[:]
                min_graph = g
        return min_dist, min_path, min_graph
Пример #3
0
 def test_add_node(self):
     g = Graph()
     g.add_node("test")
     test_list = [(g.node_dict, {
         "test": []
     }), (g.adjacency_matrix, {
         "test": {
             "test": 999999
         }
     }), (g.nb_nodes, 1), (g.nb_edges, 0)]
     for g_element, expected_value in test_list:
         with self.subTest():
             self.assertEqual(g_element, expected_value)
Пример #4
0
 def test_str_full(self):
     g = Graph()
     g.add_node("new")
     g.add_node("node")
     g.add_node("test")
     g.add_edge("new", "node")
     g.add_edge("node", "test", 45)
     out = io.StringIO()
     sys.stdout = out
     print(g)
     self.assertEqual(
         out.getvalue(),
         "{'new': {'new': 999999, 'node': 999998, 'test': 999999}, "
         "'node': {'node': 999999, 'new': 999999, 'test': 45}, "
         "'test': {'test': 999999, 'new': 999999, 'node': 999999}}\n")
Пример #5
0
 def test_init(self):
     g = Graph()
     test_list = [(g.node_dict, {}), (g.adjacency_matrix, {}),
                  (g.nb_nodes, 0), (g.nb_edges, 0)]
     for g_element, expected_value in test_list:
         with self.subTest():
             self.assertEqual(g_element, expected_value)
Пример #6
0
 def test_add_the_same_edge_twice(self):
     g = Graph()
     g.add_node("new")
     g.add_node("node")
     g.add_edge("new", "node")
     g.add_edge("new", "node")
     test_list = [(g.node_dict, {
         "new": [],
         "node": []
     }),
                  (g.adjacency_matrix, {
                      "new": {
                          "new": 999999,
                          "node": 999998
                      },
                      "node": {
                          "new": 999999,
                          "node": 999999
                      }
                  }), (g.nb_nodes, 2), (g.nb_edges, 1)]
     for g_element, expected_value in test_list:
         with self.subTest():
             self.assertEqual(g_element, expected_value)
Пример #7
0
def initialize_problem(address):
    graph = Graph()
    graph.build_graph()
    print("Bar Graph ok")
    position = {'name': 'Your position',
                'address': address}
    locator = Nominatim(user_agent="myGeocoder")
    location = locator.geocode(address)

    if location:
        position["latitude"] = location.latitude
        position["longitude"] = location.longitude
    else:
        print("could not locate bar %s" % address)
    graph.add_node_bar(position, 800)
    print("Bar Graph + position ok")
    return graph, 800
Пример #8
0
 def test_out_degree(self):
     g = Graph()
     g.add_node("new")
     g.add_node("node")
     g.add_node("test")
     g.add_edge("new", "node")
     g.add_edge("node", "test", 45)
     deg = g.out_degree("new")
     self.assertEqual(deg, 1)
Пример #9
0
 def test_order(self):
     g = Graph()
     self.assertEqual(g.order(), 0)
Пример #10
0
 def test_node_list(self):
     g = Graph()
     g.add_node("new")
     g.add_node("node")
     g.add_node("test")
     self.assertEqual(["new", "node", "test"], g.node_list())
Пример #11
0
 def test_str(self):
     g = Graph()
     out = io.StringIO()
     sys.stdout = out
     print(g)
     self.assertEqual(out.getvalue(), "{}\n")
Пример #12
0
 def test_get_dist_matrix(self):
     g = Graph()
     g.add_node("new")
     g.add_node("node")
     g.add_node("test")
     g.add_edge("new", "node")
     g.add_edge("node", "test", 45)
     mat = g.get_dist_matrix()
     self.assertEqual(
         mat, {
             "new": {
                 "new": 999999,
                 "node": 999998,
                 "test": 999999
             },
             "node": {
                 "new": 999999,
                 "node": 999999,
                 "test": 45
             },
             "test": {
                 "new": 999999,
                 "node": 999999,
                 "test": 999999
             }
         })
Пример #13
0
 def test_get_dist_matrix_empty_graph(self):
     g = Graph()
     mat = g.get_dist_matrix()
     self.assertEqual(mat, {})
Пример #14
0
 def test_edge_list(self):
     g = Graph()
     g.add_node("new")
     g.add_node("node")
     g.add_node("test")
     g.add_edge("new", "node")
     g.add_edge("node", "test", 45)
     edges = g.edge_list()
     self.assertEqual(edges, [("new", "node", 999998),
                              ("node", "test", 45)])
Пример #15
0
 def test_edge_list_empty_graph(self):
     g = Graph()
     edges = g.edge_list()
     self.assertEqual(edges, [])
Пример #16
0
 def test_node_list_empty(self):
     g = Graph()
     self.assertEqual([], g.node_list())
Пример #17
0
 def test_get_in_neighbors_unknown_key(self):
     g = Graph()
     g.add_node("new")
     g.add_node("node")
     g.add_node("test")
     g.add_edge("new", "node")
     g.add_edge("node", "test", 45)
     with self.assertRaises(IndexError) as context:
         g.get_in_neighbors("blip")
         self.assertTrue(context.exception)
Пример #18
0
 def test_get_out_neighbors(self):
     g = Graph()
     g.add_node("new")
     g.add_node("node")
     g.add_node("test")
     g.add_edge("new", "node")
     g.add_edge("node", "test", 45)
     neighbors = g.get_out_neighbors("new")
     self.assertEqual(neighbors, ["node"])
Пример #19
0
 def test_get_out_neighbors_empty_graph(self):
     g = Graph()
     with self.assertRaises(IndexError) as context:
         g.get_out_neighbors("test")
         self.assertTrue(context.exception)
Пример #20
0
 def test_size(self):
     g = Graph()
     self.assertEqual(g.size(), 0)
Пример #21
0
 def test_out_degree_unknown_key(self):
     g = Graph()
     g.add_node("new")
     g.add_node("node")
     g.add_node("test")
     g.add_edge("new", "node")
     g.add_edge("node", "test", 45)
     with self.assertRaises(IndexError) as context:
         g.out_degree("blip")
         self.assertTrue(context.exception)
Пример #22
0
            best_distance = path_length(path, graph)
            for i in range(2, len(path) - 2):
                for k in range(i + 1, len(path) - 1):
                    new_path = two_opt_swap(path, i, k)
                    new_indexes = two_opt_swap(indexes, i, k)
                    new_distance = path_length(new_path, graph)
                    if new_distance < best_distance:
                        path = new_path.copy()
                        indexes = new_indexes.copy()
                        start_again = True
                        break
                if start_again is True:
                    break
    return path, indexes


def two_opt_swap(route, i, k):
    new_route = route[:i - 1] + route[i - 1:k][::-1] + route[k:]
    return new_route


if __name__ == '__main__':
    # locations = [((0, 0), 0), ((0, 1), 5), ((0, 2), 5),
    #              ((1, 0), 5), ((1, 1), 5), ((1, 2), 5),
    #              ((2, 0), 5), ((2, 1), 5), ((2, 2), 1)]
    g = Graph()
    g.build_graph()
    max_len = 9
    length, final_path = grasp_sr(g, 0, max_len, 0)
    print(f'with max_len {max_len}\nfinal_path: {final_path}\nreward: {0}\nlength: {length}')