Ejemplo n.º 1
0
def swap_path(tour, i, j, dist):
    A = tour[i]
    swaped_tour = tour.copy()
    swaped_tour.remove(A)
    swaped_tour.insert(j, A)
    if get_tour_length(swaped_tour, dist) < get_tour_length(tour, dist):
        tour.remove(A)
        tour.insert(j, A)
Ejemplo n.º 2
0
def improve_tour(dist, tour):
    N = len(tour)
    can_improve = True
    while can_improve:
        original_length = get_tour_length(tour, dist)
        for i in range(N):
            for j in range(i + 2, N):
                swap_path(tour, i, j, dist)
        if get_tour_length(tour, dist) >= original_length:
            can_improve = False
Ejemplo n.º 3
0
def improve_tour2(dist, tour):
    N = len(tour)
    can_improve = True
    while can_improve:
        original_length = get_tour_length(tour, dist)
        for i in range(N):
            # 閾値を超えている都市にだけ適用
            if dist[i][(i + 1) % N] + dist[(i + 1) % N][(i + 2) % N] < 1500:
                continue
            for j in range(N):
                swap_path(tour, i, j, dist)
        if get_tour_length(tour, dist) >= original_length:
            can_improve = False
Ejemplo n.º 4
0
def improve_tour2(dist, tour):
    N = len(tour)
    can_improve = True
    while can_improve:
        original_length = get_tour_length(tour, dist)
        for i in range(N):
            # 計算量削減のため近傍都市(かつ経路でiより後ろの都市)のみ探索
            neighbor_city = get_neighbor_city(dist[i])
            remaining_tour = set(tour[i + 1:])
            neighbor_city_index = [
                tour.index(city) for city in neighbor_city & remaining_tour
            ]
            for j in neighbor_city_index:
                # for j in range(i+2, N):
                swap_path(tour, i, j, dist)
        if get_tour_length(tour, dist) >= original_length:
            can_improve = False
Ejemplo n.º 5
0
def solve(cities):
    N = len(cities)
    dist = get_all_distance(cities)
    tours = []
    # スタート地点を変えて実行する
    for i in range(N):
        tours.append(solve_each(dist, i))
    tours_length = [get_tour_length(tour, dist) for tour in tours]
    shortest_tour = tours[tours_length.index(min(tours_length))]
    # 一番短い経路を返す
    return shortest_tour
Ejemplo n.º 6
0
def solve(cities):
    N = len(cities)
    start_points = range(N)
    # challenge6はランダムな点一つで実行
    if N >= 2048:
        start_points = [random.randint(0, N - 1)]
    # 全ての距離
    dist = get_all_distance(cities)
    tours = []
    # スタート地点を変えて実行する
    for i in start_points:
        tours.append(solve_each(dist, i))
    tours_length = [get_tour_length(tour, dist) for tour in tours]
    shortest_tour = tours[tours_length.index(min(tours_length))]
    # 一番短い経路を返す
    return shortest_tour
Ejemplo n.º 7
0
def solve(cities):
    N = len(cities)
    start_points = range(N)
    tours = []
    dist = [[0] * N for i in range(N)]
    # 距離記録
    for i in range(N):
        for j in range(i, N):
            dist[i][j] = dist[j][i] = distance(cities[i], cities[j])
            
    # challenge6, 7: ランダムに選んだ1点で実行
    if N >= 2048:
        start_points = [random.randint(0, N-1)]

    # スタート地点を変えて実行
    for i in start_points:
        tours.append(solve_each(dist, i))
    tours_length = [get_tour_length(tour, dist) for tour in tours]
    shortest_tour = tours[tours_length.index(min(tours_length))]
    return shortest_tour
Ejemplo n.º 8
0
def solve(cities):
    N = len(cities)
    tours = []
    dist = [[0] * N for i in range(N)]
    # 距離記録
    for i in range(N):
        for j in range(i, N):
            dist[i][j] = dist[j][i] = distance(cities[i], cities[j])

    # challenge6: ランダムに選んだ1点で実行
    #if N >= 2048:
    #start_points = [random.randint(0, N-1)]

    # ALEX_COMMENT:  the loop below is N * the greedy.
    #                thats very expensive - even though it may be precise
    # スタート地点を変えて実行
    for i in range(N):
        tours.append(solve_each(dist, i))
    tours_length = [get_tour_length(tour, dist) for tour in tours]
    shortest_tour = tours[tours_length.index(min(tours_length))]
    return shortest_tour