def twoOptSwap(path): initial_solution = next_nearest.greedy_algo(path)[1] orig_copy = copy.copy(initial_solution) test_copy = copy.copy(initial_solution) for i in orig_copy: #Other points, not including the current point otherpoints = [j for j in orig_copy] otherpoints.remove(i) #Closest 5 points closest = [[fun.pythag_distance(i, j), j] for j in otherpoints] closest.sort(key=lambda s: s[0]) closest5 = closest[0:5] for j in closest5: # Set nodes swap_p = i bswap_p = i new_p = j bnew_p = j # Swap procedure test_copy[test_copy.index(swap_p)] = bnew_p test_copy[test_copy.index(new_p)] = bswap_p #If the swap is optimal, keep it if fun.path_distance(test_copy) < fun.path_distance(orig_copy): orig_copy = copy.copy(test_copy) #If it is not, dont keep it else: test_copy = copy.copy(orig_copy) return fun.path_distance(orig_copy)
def single_path(path): pcopy = copy.copy(path) def mean(iterable): if len(iterable) != 0: return sum(iterable)/len(iterable) else: return 0 def MAD(iterable): m = mean(iterable) return mean([m-i for i in iterable]) ''' This function calculates if a point is in a section (boxx,boxy) == corner towards orgin ''' def point_in_box(point,boxx,boxy,boxw,boxh): if point[0] > boxx and point[0] < boxx+boxw and point[1] > boxy and point[1] < boxy + boxh: return True else: return False num_points = len(pcopy) num_sect = 4 maxx = max([i[0] for i in path]) maxy = max([i[1] for i in path]) xsize = maxx/num_sect ysize = maxy/num_sect sections = [[] for j in range(num_sect)] # NOTE: Gives each point a section for i in pcopy: if i[0]<maxx/2 and i[1]<maxy/2: sections[0].append(i) elif i[0]>=maxx/2 and i[1]<maxy/2: sections[1].append(i) elif i[0]<maxx/2 and i[0]>=maxy/2: sections[2].append(i) else: sections[3].append(i) # NOTE: Sort each section based on the MAD of the x and y for x in range(len(sections)): if MAD([point[0] for point in sections[x]]) > MAD([point[1] for point in sections[x]]): sections[x].sort(key=lambda p:p[1]) else: sections[x].sort(key=lambda p:p[0]) # NOTE: Make end path a combination of each section end_path = [] for i in sections: for p in i: geeb = p end_path.append(geeb) if type(end_path[end_path.index(geeb)]) != type(tuple()): er = geeb del end_path[end_path.index(geeb)] for i in er: end_path.append(i) return (fun.path_distance(end_path),end_path)
def threeOptSwap(path): initial_solution = next_nearest.greedy_algo(path)[1] orig_copy = copy.copy(initial_solution) test_copy = copy.copy(initial_solution) for coord in orig_copy: #Other points, not including the current point otherpoints = [j for j in orig_copy] otherpoints.remove(coord) #Closest 5 points closest = [[fun.pythag_distance(coord, j), j] for j in otherpoints] closest.sort(key=lambda s: s[0]) closest5_duel = closest[0:5] closest5 = [k[1] for k in closest5_duel] for j in closest5: # Set nodes swap_p = coord bswap_p = coord new_p = j bnew_p = j new2_p = choice(closest5) bnew2_p = choice(closest5) # Swapping procedure solutions = ["132", "213", "231", "312", "321"] for i in solutions: nodes = list(i) real_nodes = {"1": swap_p, "2": new_p, "3": new2_p} path_i = { "1": test_copy.index(swap_p), "2": test_copy.index(new_p), "3": test_copy.index(new2_p) } for i in nodes: test_copy[path_i[i]] = real_nodes[i] if fun.path_distance(test_copy) < fun.path_distance( orig_copy): orig_copy = copy.copy(test_copy) else: test_copy = copy.copy(orig_copy) return fun.path_distance(orig_copy)
def greedy_algo(coords): cur_node = random.choice(coords) solution = [cur_node] free_list = copy.copy(coords) free_list.remove(cur_node) dist_matrix = fun.dist_matrix(coords) while free_list: closest_dist = [[fun.pythag_distance(cur_node,j),j] for j in free_list] closest_dist.sort() try: cur_node = closest_dist[0][1] except: print cur_node raise Exception free_list.remove(cur_node) solution.append(cur_node) return [fun.path_distance(solution),solution]
def sort_by_y(path): points = list(path) points.sort(key=lambda y: y[1]) return tsp_functions.path_distance(points)
def find_shortest(perms): t = [] for path in perms: t.append([tsp.path_distance(path), path]) t.sort(key=lambda x: x[0]) return t[0][1]
def getAllPerms(cells): return itertools.product(*(permute(cell) for cell in cells)) def listGoodPerms(cells): products = getAllPerms(cells) return [perms for perms in products] def distance(p1, p2): try: return math.sqrt(((p1[0] - p2[1])**2) + ((p1[1] - p2[1])**2)) except Exception as e: print((p1, p2)) raise e def find_shortest(perms): t = [] for path in perms: t.append([tsp.path_distance(path), path]) t.sort(key=lambda x: x[0]) return t[0][1] coords = tsp.generate_points(15, 50) perms = listGoodPerms(coords) print(tsp.path_distance(find_shortest(perms)))
def sort_by_x(path): points = list(path) points.sort(key=lambda x: x[0] ) # lambda sorting, so no need for operator.itemgetter return tsp_functions.path_distance(points)