def targeted_k_opt(edges, colors, path, k): biggest_edge = None biggest_edge_length = -1 for i in range(1, len(path)): if edges[i-1][i] > biggest_edge_length: biggest_edge_length = edges[i-1][i] biggest_edge = i N = len(path) random_edges = random.sample(range(0, N), k - 2) + [N, biggest_edge] random_edges = sorted(random_edges) start = 0 subpaths = [] for i in range(k): subpaths.append(path[start:random_edges[i]]) start = random_edges[i] subpaths = list(filter(lambda x :len(x) > 0, subpaths)) permutations = itertools.permutations(subpaths) optimal = sanity.weight(path,edges) optimal_path = path path = next(permutations) while path != None: path = [item for sublist in path for item in sublist] weight = sanity.weight(path,edges) if optimal > weight and sanity.is_valid_path(path,colors, False): optimal = weight optimal_path = path try: path = next(permutations) except StopIteration: path = None return optimal_path
def targeted_k_opt(edges, colors, path, k): biggest_edge = None biggest_edge_length = -1 for i in range(1, len(path)): if edges[i - 1][i] > biggest_edge_length: biggest_edge_length = edges[i - 1][i] biggest_edge = i N = len(path) random_edges = random.sample(range(0, N), k - 2) + [N, biggest_edge] random_edges = sorted(random_edges) start = 0 subpaths = [] for i in range(k): subpaths.append(path[start:random_edges[i]]) start = random_edges[i] subpaths = list(filter(lambda x: len(x) > 0, subpaths)) permutations = itertools.permutations(subpaths) optimal = sanity.weight(path, edges) optimal_path = path path = next(permutations) while path != None: path = [item for sublist in path for item in sublist] weight = sanity.weight(path, edges) if optimal > weight and sanity.is_valid_path(path, colors, False): optimal = weight optimal_path = path try: path = next(permutations) except StopIteration: path = None return optimal_path
def targeted_improve(path, edges, colors, i=100): best_path = path best_score = sanity.weight(path, edges) curr_path = path for _ in range(i): curr_path = improvement.targeted_k_opt(edges, colors, path, 4) score = sanity.weight(curr_path, edges) if score < best_score: best_path = curr_path best_score = score return best_path
def improve(path, edges, colors, r=100, i=100): best_path = path best_score = sanity.weight(path, edges) curr_path = path for _ in range(r): curr_path = improvement.lin_kernigan_total(curr_path, edges, colors, i) score = sanity.weight(curr_path, edges) if score < best_score: best_path = curr_path best_score = score curr_path = improvement.random_kopt(edges, colors, curr_path, 4) return best_path
def random_kopt(edges, colors, path, k): N = len(path) random_edges = sorted(random.sample(range(0, N), k)) + [N] start = 0 subpaths = [] for i in range(k + 1): subpaths.append(path[start:random_edges[i]]) start = random_edges[i] subpaths = list(filter(lambda x: len(x) > 0, subpaths)) permutations = itertools.permutations(subpaths) path = next(permutations) path_list = [] while path != None: path = [item for sublist in path for item in sublist] weight = sanity.weight(path, edges) if sanity.is_valid_path(path, colors, False): path_list = path_list + [path] try: path = next(permutations) except StopIteration: path = None if len(path_list) == 0: return random_kopt(edges, colors, path, k) else: rand = random.randint(0, len(path_list) - 1) return path_list[rand]
def random_kopt(edges, colors, path, k): N = len(path) random_edges = sorted(random.sample(range(0, N), k)) + [N] start = 0 subpaths = [] for i in range(k+1): subpaths.append(path[start:random_edges[i]]) start = random_edges[i] subpaths = list(filter(lambda x :len(x) > 0, subpaths)) permutations = itertools.permutations(subpaths) path = next(permutations) path_list = [] while path != None: path = [item for sublist in path for item in sublist] weight = sanity.weight(path,edges) if sanity.is_valid_path(path,colors, False): path_list = path_list + [path] try: path = next(permutations) except StopIteration: path = None if len(path_list) == 0: return random_kopt(edges, colors, path, k) else: rand = random.randint(0,len(path_list)-1) return path_list[rand]
def lin_kernigan_iteration(path, edges, colors): results = [None for _ in range(2)] threads = [] for i in range(4,6): t = threading.Thread(target=super_kopt_helper, args=(edges, colors, path, i, results)) threads.append(t) t.start() for i in range(len(threads)): threads[i].join() k_is_four = results[0] k_is_five = results[1] if sanity.weight(k_is_five, edges) < sanity.weight(k_is_four, edges): return k_is_five else: return k_is_four
def lin_kernigan_iteration(path, edges, colors): results = [None for _ in range(2)] threads = [] for i in range(4, 6): t = threading.Thread(target=super_kopt_helper, args=(edges, colors, path, i, results)) threads.append(t) t.start() for i in range(len(threads)): threads[i].join() k_is_four = results[0] k_is_five = results[1] if sanity.weight(k_is_five, edges) < sanity.weight(k_is_four, edges): return k_is_five else: return k_is_four
def find_good_construction(edges, colors, N, selection_method=construction.randomly_select, i=100): best = None best_val = float('inf') for _ in range(i): path = None while path == None: path = construct(edges, colors, N, selection_method) weight = sanity.weight(path, edges) if sanity.is_valid_path(path, colors) and weight < best_val: best_val = weight best = path return best
def good_greedy(edges, colors, N, j=100): best_path = None best_weight = float("inf") for i in range(N): path = greedy(i, edges, colors, N) if path == "FAIL": continue weight = sanity.weight(path, edges) if sanity.is_valid_path(path, colors) and weight < best_weight: best_path, best_weight = path, weight if best_path == None: return algorithm.find_good_construction(edges, colors, N) return best_path
def super_kopt(edges, colors, path, k): N = len(path) random_edges = sorted(random.sample(range(0, N), k)) + [N] start = 0 subpaths = [] for i in range(k+1): subpaths.append(path[start:random_edges[i]]) start = random_edges[i] subpaths = list(filter(lambda x :len(x) > 0, subpaths)) permutations = itertools.permutations(subpaths) optimal = sanity.weight(path,edges) optimal_path = path path = next(permutations) while path != None: path = [item for sublist in path for item in sublist] weight = sanity.weight(path,edges) if optimal > weight and sanity.is_valid_path(path,colors, False): optimal = weight optimal_path = path try: path = next(permutations) except StopIteration: path = None return optimal_path
def super_kopt(edges, colors, path, k): N = len(path) random_edges = sorted(random.sample(range(0, N), k)) + [N] start = 0 subpaths = [] for i in range(k + 1): subpaths.append(path[start:random_edges[i]]) start = random_edges[i] subpaths = list(filter(lambda x: len(x) > 0, subpaths)) permutations = itertools.permutations(subpaths) optimal = sanity.weight(path, edges) optimal_path = path path = next(permutations) while path != None: path = [item for sublist in path for item in sublist] weight = sanity.weight(path, edges) if optimal > weight and sanity.is_valid_path(path, colors, False): optimal = weight optimal_path = path try: path = next(permutations) except StopIteration: path = None return optimal_path
def somewhat_random_super_kopt(edges, colors, path, k): N = len(path) max_edge, max_index = -1, -1 for i in range(len(path) - 1): vertex, next = path[i], path[i+1] weight = edges[vertex][next] if weight > max_edge: max_edge = weight max_index = i random_edges = sorted(random.sample(range(0, N), k-1) + [i] + [N]) start = 0 subpaths = [] for i in range(k+1): subpaths.append(path[start:random_edges[i]]) start = random_edges[i] subpaths = list(filter(lambda x :len(x) > 0, subpaths)) permutations = itertools.permutations(subpaths) path = next(permutations) path_list = [] while path != None: path = [item for sublist in path for item in sublist] weight = sanity.weight(path,edges) if sanity.is_valid_path(path,colors, False): path_list = path_list + [path] try: path = next(permutations) except StopIteration: path = None if len(path_list) == 0: return random_kopt(edges, colors, path, k) else: rand = random.randint(0,len(path_list)-1) return path_list[rand]
def somewhat_random_super_kopt(edges, colors, path, k): N = len(path) max_edge, max_index = -1, -1 for i in range(len(path) - 1): vertex, next = path[i], path[i + 1] weight = edges[vertex][next] if weight > max_edge: max_edge = weight max_index = i random_edges = sorted(random.sample(range(0, N), k - 1) + [i] + [N]) start = 0 subpaths = [] for i in range(k + 1): subpaths.append(path[start:random_edges[i]]) start = random_edges[i] subpaths = list(filter(lambda x: len(x) > 0, subpaths)) permutations = itertools.permutations(subpaths) path = next(permutations) path_list = [] while path != None: path = [item for sublist in path for item in sublist] weight = sanity.weight(path, edges) if sanity.is_valid_path(path, colors, False): path_list = path_list + [path] try: path = next(permutations) except StopIteration: path = None if len(path_list) == 0: return random_kopt(edges, colors, path, k) else: rand = random.randint(0, len(path_list) - 1) return path_list[rand]
for t in range(1, T + 1): fin = open("./instances/" + str(t) + ".in", "r") N = int(fin.readline()) d = [[] for i in range(N)] for i in range(N): d[i] = [int(x) for x in fin.readline().split()] c = fin.readline() # find an answer, and put into assign print("*** TRIAL " + str(t) + " ***") old_sol = sanity.denormalize(old_solutions[t - 1], N) old_score = sanity.weight(old_sol, d) if old_score == 0: path = old_sol else: path = greedy.good_greedy(d, c, N) if path is not None and sanity.is_valid_path(path, c): post = sanity.weight(path, d) if post < old_score: print("NEW: " + str(post)) print("OLD: " + str(old_score)) print("IMPROVEMENT: " + str(old_score - post)) print("")
for t in range(1, T+1): fin = open("./instances/" + str(t) + ".in", "r") N = int(fin.readline()) d = [[] for i in range(N)] for i in range(N): d[i] = [int(x) for x in fin.readline().split()] c = fin.readline() # find an answer, and put into assign if t%50 == 0: print ("*** DIVISION " + str(t) + " ***") old_sol = sanity.denormalize(old_solutions[t-1], N) old_score = sanity.weight(old_sol,d) if old_score == 0 or len(old_sol) < 7: path = old_sol else: print ("*** TRIAL " + str(t) + " ***") print old_score new_edges = supreme_mst.mst_solver(d, N) path = algorithm.find_good_construction(new_edges, c, N, selection_method=construction.domain_randomly_select) print sanity.weight(path, new_edges) path = algorithm.targeted_improve(path, new_edges, c, 500) print sanity.weight(path, new_edges) path = algorithm.improve(path, new_edges, c, 1, 100)
for t in range(1, T+1): fin = open("./instances/" + str(t) + ".in", "r") N = int(fin.readline()) d = [[] for i in range(N)] for i in range(N): d[i] = [int(x) for x in fin.readline().split()] c = fin.readline() # find an answer, and put into assign if t%50 == 0: print ("*** DIVISION " + str(t) + " ***") old_sol = sanity.denormalize(old_solutions[t-1], N) old_score = sanity.weight(old_sol,d) if old_score == 0 or len(old_sol) < 7: path = old_sol else: path = old_sol path = algorithm.improve(path, d, c, 10, 100) path = algorithm.targeted_improve(path, d, c, 500) # path = algorithm.improve(path, d, c, 2, 20) post = sanity.weight(path, d) print ("*** TRIAL " + str(t) + " ***") print ("NEW: " + str(post))
fin = open("./instances/" + str(t) + ".in", "r") N = int(fin.readline()) d = [[] for i in range(N)] for i in range(N): d[i] = [int(x) for x in fin.readline().split()] c = fin.readline() # find an answer, and put into assign print ("*** TRIAL " + str(t) + " ***") old_sol = sanity.denormalize(old_solutions[t-1], N) old_score = sanity.weight(old_sol,d) if old_score == 0: path = old_sol else: path = greedy.good_greedy(d, c, N) if path is not None and sanity.is_valid_path(path, c): post = sanity.weight(path, d) if post < old_score: print ("NEW: " + str(post)) print ("OLD: " + str(old_score)) print ("IMPROVEMENT: " + str(old_score-post)) print ("")