Example #1
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
Example #2
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}')