self.route, self.cur_cost = self.greedy_solution() while self.temperature >= self.stopping_temperature and self.iteration < self.stopping_iter: guess = list(self.route) left_index = random.randint(2, self.num_cities - 1) right_index = random.randint(0, self.num_cities - left_index) guess[right_index: (right_index + left_index)] = reversed(guess[right_index: (right_index + left_index)]) self.accept(guess) self.temperature *= self.alpha self.iteration += 1 self.progress.append(self.cur_cost) print("Best fitness obtained: ", self.best_fitness) def visualize_routes(self): visualize_tsp('simulated annealing TSP', self.route) def plot_learning(self): fig = plt.figure(1) plt.plot([i for i in range(len(self.progress))], self.progress) plt.ylabel("Distance") plt.xlabel("Iterations") plt.show(block=False) if __name__ == "__main__": cities = read_cities(64) sa = SimAnneal(cities, stopping_iter=15000) sa.run() sa.plot_learning() sa.visualize_routes()
gbest[swap[0]], gbest[swap[1]] = gbest[swap[1]], gbest[ swap[0]] particle.velocity = temp_velocity for swap in temp_velocity: if random.random() <= swap[2]: new_route[swap[0]], new_route[swap[1]] = \ new_route[swap[1]], new_route[swap[0]] particle.route = new_route particle.update_costs_and_pbest() if __name__ == "__main__": cities = read_cities(448) pso = PSO(iterations=3200, population_size=300, pbest_probability=0.9, gbest_probability=0.02, cities=cities) pso.run() print(f'cost: {pso.gbest.pbest_cost}\t| gbest: {pso.gbest.pbest}') x_list, y_list = [], [] for city in pso.gbest.pbest: x_list.append(city.x) y_list.append(city.y) x_list.append(pso.gbest.pbest[0].x) y_list.append(pso.gbest.pbest[0].y) fig = plt.figure(1)
gbest[swap[0]], gbest[swap[1]] = gbest[swap[1]], gbest[ swap[0]] particle.velocity = temp_velocity for swap in temp_velocity: if random.random() <= swap[2]: new_route[swap[0]], new_route[swap[1]] = \ new_route[swap[1]], new_route[swap[0]] particle.route = new_route particle.update_costs_and_pbest() if __name__ == "__main__": cities = read_cities(29) pso = PSO(iterations=1200, population_size=300, pbest_probability=0.9, gbest_probability=0.02, cities=cities) pso.run() print(f'cost: {pso.gbest.pbest_cost}\t| gbest: {pso.gbest.pbest}') x_list, y_list = [], [] for city in pso.gbest.pbest: x_list.append(city.x) y_list.append(city.y) x_list.append(pso.gbest.pbest[0].x) y_list.append(pso.gbest.pbest[0].y) fig = plt.figure(1)
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)