Ai 9 Thprogram
Ai 9 Thprogram
# Recreate and save the commented ACO TSP code after environment reset
import numpy as np
import matplotlib.pyplot as plt
import random
# Update pheromones
pheromone *= (1 - evaporation)
for tour, length in zip(all_tours, all_lengths):
for i in range(num_cities):
a = tour[i % num_cities]
b = tour[(i + 1) % num_cities]
pheromone[a][b] += Q / length
pheromone[b][a] += Q / length # for symmetry
# Plotting results
best_tour.append(best_tour[0])
tour_coords = cities[best_tour]
plt.figure(figsize=(12, 6))
plt.tight_layout()
plt.savefig("aco_tsp_result.png")
plt.show()
Output