Example #1
0
def execute_example(distance_matrix, algorithm, propose_type, path1, path2,
                    history):
    dist_1 = calculate_distance(distance_matrix, path1)
    dist_2 = calculate_distance(distance_matrix, path2)

    starting_dist = dist_1 + dist_2
    path1, path2, history = algorithm(distance_matrix, path1, path2,
                                      propose_type, history)

    dist_1 = calculate_distance(distance_matrix, path1)
    dist_2 = calculate_distance(distance_matrix, path2)

    return starting_dist, (dist_1 + dist_2)
Example #2
0
def execute_example_random(distance_matrix, algorithm, propose_type,
                           starting_history, picked_nodes):
    path1 = starting_history[-1][0]
    path2 = starting_history[-1][1]
    dist_1 = calculate_distance(distance_matrix, path1)
    dist_2 = calculate_distance(distance_matrix, path2)

    starting_dist = dist_1 + dist_2
    path1, path2, history = algorithm(distance_matrix, path1, path2,
                                      propose_type, starting_history)

    dist_1 = calculate_distance(distance_matrix, path1)
    dist_2 = calculate_distance(distance_matrix, path2)

    return starting_dist, (dist_1 + dist_2), picked_nodes
Example #3
0
def k_regret_connector(algos, distance_matrix, start_with=None, k=1):
    cycles = sample(list(range(len(distance_matrix))), len(algos))

    if start_with is not None:
        cycles = start_with
    picked_nodes = deepcopy(cycles)

    cycles = [[c] for c in cycles]
    visited = []
    for c in cycles:
        visited += c

    history = [deepcopy(cycles)]
    enough = []

    while sum([len(c) for c in cycles]) < len(distance_matrix):
        min_regret = int(10e20)
        best_algo = -1
        for i, algo in enumerate(algos):
            if i in enough:
                continue
            best_cycle, best_node = algo(distance_matrix, visited, cycles[i])
            best_cost = calculate_distance(distance_matrix,
                                           best_cycle) - calculate_distance(
                                               distance_matrix, cycles[i])
            regret = 0
            sub_visited = visited + [best_node]
            for ik in range(k - 1):
                sub_cycle, sub_node = algo(distance_matrix, sub_visited,
                                           cycles[i])
                sub_cost = calculate_distance(distance_matrix,
                                              sub_cycle) - calculate_distance(
                                                  distance_matrix, cycles[i])
                regret += sub_cost - best_cost
                sub_visited.append(sub_node)
            if regret < min_regret:
                min_regret = regret
                best_algo = i
        res_cycle, res_node = algos[best_algo](distance_matrix, visited,
                                               cycles[best_algo])
        cycles[best_algo] = deepcopy(res_cycle)
        visited.append(res_node)
        history.append(deepcopy(cycles))
        if len(cycles[best_algo]) >= len(distance_matrix) // len(cycles) \
                and len(enough) + 1 != len(cycles):
            enough.append(best_algo)
    return history, picked_nodes
Example #4
0
    res = k_regret(distance_matrix)[-1]

    start = time()
    checkpoint = time()
    done = False
    while True:
        if not (k_regret_only and done):
            res = steepest(distance_matrix, res[0], res[1])[-1]
            done = True
        checkpoint = time()
        if checkpoint-start > time_allowed:
            break
        perturbateBig_(res[0], res[1])
        res = k_regret(distance_matrix, start_with=res)[-1]
    return res

if __name__ == '__main__':
    data_set = 'kroB'
    overview, coordinates = read_file('./data/' + data_set + '200.tsp')
    distance_matrix = count_dist(coordinates)

    # # MSLS:
    # msls_results = []
    # for i in range(10):
    c1, c2 = ils1(distance_matrix)
    dist = calculate_distance(distance_matrix, c1) + \
           calculate_distance(distance_matrix,c2)
    print(dist)
    animate([c1, c2], coordinates, isCycle=[True, True])