예제 #1
0
def test_city_15():
    problem = TSP()

    problem.from_array([[],
                        [29],
                        [82, 55],
                        [46, 46, 68],
                        [68, 42, 46, 82],
                        [52, 43, 55, 15, 74],
                        [72, 43, 23, 72, 23, 61],
                        [42, 23, 43, 31, 52, 23, 42],
                        [51, 23, 41, 62, 21, 55, 23, 33],
                        [55, 31, 29, 42, 46, 31, 31, 15, 29],
                        [29, 41, 79, 21, 82, 33, 77, 37, 62, 51],
                        [74, 51, 21, 51, 58, 37, 37, 33, 46, 21, 65],
                        [23, 11, 64, 51, 46, 51, 51, 33, 29, 41, 42, 61],
                        [72, 52, 31, 43, 65, 29, 46, 31, 51, 23, 59, 11, 62],
                        [46, 21, 51, 64, 23, 59, 33, 37, 11, 37, 61, 55, 23, 59]])

    path, length = solve_dp(problem)

    ans = [13, 2, 15, 9, 5, 7, 3, 12, 14, 10, 8, 6, 4, 11, 1]
    ans.reverse()
    assert list(map(lambda node: node.id, path)) == ans
    assert length == 291
예제 #2
0
def tsp():
    # setup
    tsp = TSP()
    tsp.from_array([
        [],
        [3.0],
        [4.0, 4.0],
        [2.0, 6.0, 5.0],
        [7.0, 3.0, 8.0, 6.0]
    ])

    return tsp
예제 #3
0
def test_burma_14():
    problem = TSP()

    problem.from_file('problems/burma14.tsp', 'geo')

    assert len(problem.nodes) == 14

    path, length = solve_dp(problem)

    assert list(map(lambda node: node.id, path)) == [
        1, 2, 14, 3, 4, 5, 6, 12, 7, 13, 8, 11, 9, 10]
    assert int(length) == 3346
예제 #4
0
def test_city_5():
    problem = TSP()

    problem.from_array([
        [],
        [3.0],
        [4.0, 4.0],
        [2.0, 6.0, 5.0],
        [7.0, 3.0, 8.0, 6.0]
    ])

    path, length = solve_dp(problem)

    assert list(map(lambda node: node.id, path)) == [1, 3, 2, 5, 4]
    assert length == 19.0
예제 #5
0
def test_tsp_solve():
    #  Prepare the square symmetric distance matrix for 3 nodes:
    #  Distance from A to B is 1.0
    #                B to C is 3.0
    #                A to C is 2.0
    problem = TSP()

    problem.from_array([
        [],
        [1.0],
        [3.0, 2.0],
        [5.0, 4.0, 1.0]
    ])

    path, length = solve_exhaustive(problem)

    assert list(map(lambda node: node.id, path)) == [1, 2, 3, 4]
    assert length == 9.0
예제 #6
0
def test_read_file():
    problem = TSP()
    problem.from_file('problems/bier127.tsp')

    assert len(problem.nodes) == 127
    assert len(problem.distance_matrix.matrix) == 127
예제 #7
0
    print('distance_path: {}'.format(distance))

    for _ in range(optimize_count):
        distance = optimize(nodes, path, distance_matrix)

    print('optimized_distance: {}'.format(distance))

    for node in nodes:
        # reset nodes
        # TODO: use connected list just as local variable (only used in greedy module)
        node.connected = []
        node.degree = 0

    return path


if __name__ == '__main__':
    from tsp_solver import TSP
    tsp = TSP()
    filename = 'rl11849'

    tsp.from_file('problems/{}.tsp'.format(filename))
    path = solve_greedy(tsp)

    # print(list(map(lambda node: node.id, path)))

    # Save answer
    with open('sol_{}_greedy.csv'.format(filename), 'w') as f:
        f.writelines(list(map(lambda node: str(node.id) + '\n', path)))
예제 #8
0
    args = parser.parse_args()

    verbose = args.verbose
    file_path = args.filepath
    population_size = args.population
    fitness_evaluations = args.evaluations

    logging.basicConfig(filename='run{}.log'.format(int(time.time())),
                        filemode='w',
                        level=logging.INFO)

    from tsp_solver import TSP
    import inspect

    problem = TSP()
    mode = "geo" if args.geo else "euc2d"
    problem.from_file(file_path, mode=mode)

    logging.info('file: {}'.format(file_path))
    logging.info('population size: {}'.format(population_size))
    logging.info('fitness_evaluations: {}'.format(fitness_evaluations))
    logging.info(inspect.getsource(GA._crossover))
    logging.info(inspect.getsource(GA._select))
    logging.info(inspect.getsource(GA._mutate))

    def solve_GA(problem, generations):
        Graph.set_graph(problem.nodes, problem.distance_matrix)

        tours = []
        seed = Tour(path=solve_greedy(problem))
예제 #9
0
def test_greedy_large(tsp):
    tsp = TSP()
    tsp.from_file('problems/fl1400.tsp')
    path = solve_greedy(tsp)

    assert sorted(path) == sorted(tsp.nodes)