def accept(self, guess): guess_cost = path_cost(guess) if guess_cost < self.cur_cost: self.cur_cost, self.route = guess_cost, guess if guess_cost < self.best_fitness: self.best_fitness, self.route = guess_cost, guess else: if random.random() < self.accept_probability(guess_cost): self.cur_cost, self.route = guess_cost, guess
def greedy_solution(self): start_node = random.randint(0, self.num_cities) # start from a random node unvisited = self.cities[:] del unvisited[start_node] route = [cities[start_node]] while len(unvisited): index, nearest_city = min(enumerate(unvisited), key=lambda item: item[1].distance(route[-1])) route.append(nearest_city) del unvisited[index] current_cost = path_cost(route) self.progress.append(current_cost) return route, current_cost
def run(self, plot): if plot: plt.ion() plt.show(block=False) self.init_plot() while len(self.unvisited): index, nearest_city = min( enumerate(self.unvisited), key=lambda item: item[1].distance(self.route[-1])) self.route.append(nearest_city) del self.unvisited[index] self.plot_interactive(False) self.route.append(self.route[0]) self.plot_interactive(False) self.route.pop() return path_cost(self.route)
def path_cost(self): return path_cost(self.route)
def run(self): self.cities = min(itertools.permutations(self.cities), key=lambda path: path_cost(path)) return path_cost(self.cities)
import itertools import matplotlib.pyplot as plt from util import City, read_cities, write_cities_and_return_them, generate_cities, path_cost def solve_tsp_dynamic(cities): distance_matrix = [[x.distance(y) for y in cities] for x in cities] cities_a = {(frozenset([0, idx + 1]), idx + 1): (dist, [0, idx + 1]) for idx, dist in enumerate(distance_matrix[0][1:])} for m in range(2, len(cities)): cities_b = {} for cities_set in [frozenset(C) | {0} for C in itertools.combinations(range(1, len(cities)), m)]: for j in cities_set - {0}: cities_b[(cities_set, j)] = min([(cities_a[(cities_set - {j}, k)][0] + distance_matrix[k][j], cities_a[(cities_set - {j}, k)][1] + [j]) for k in cities_set if k != 0 and k != j]) cities_a = cities_b res = min([(cities_a[d][0] + distance_matrix[0][d[1]], cities_a[d][1]) for d in iter(cities_a)]) return res[1] if __name__ == "__main__": cities = read_cities(16) g = solve_tsp_dynamic(cities) sol = [cities[gi] for gi in g] print(path_cost(sol)) plt.show(block=True)