TSBLIB测试数据库的 att48.tsp 的最优化路径,数据和示意图

att48的最优路径总长度是:33522

此路径为 四舍五入每一步的城市距离,取整数,然后加总

四舍五入采取通常的逢四就舍去,逢五就进一。

而不是 banker's round(四舍六入五取偶)。

X坐标 Y坐标
3245.00  3305.00
3484.00  2829.00
3023.00  1942.00
3082.00  1644.00
1916.00  1569.00
1633.00  2809.00
1112.00  2049.00
10.00  2676.00
23.00  2216.00
401.00  841.00
675.00  1006.00
2233.00  10.00
3177.00  756.00
4608.00  1198.00
4985.00  140.00
6107.00  669.00
6101.00  1110.00
5530.00  1424.00
5199.00  2182.00
4612.00  2035.00
4307.00  2322.00
4706.00  2674.00
5468.00  2606.00
5989.00  2873.00
6347.00  2683.00
6271.00  2135.00
6898.00  1885.00
6734.00  1453.00
7265.00  1268.00
7392.00  2244.00
7545.00  2801.00
7509.00  3239.00
7462.00  3590.00
7573.00  3716.00
7541.00  3981.00
7608.00  4458.00
7762.00  4595.00
7732.00  4723.00
7555.00  4819.00
7611.00  5184.00
7280.00  4899.00
7352.00  4506.00
7248.00  3779.00
6807.00  2993.00
6426.00  3173.00
5900.00  3561.00
5185.00  3258.00
4483.00  3369.00

以下是使用优化后的退火算法对att48.tsp求解最优路径并绘图的Python代码: ```python import math import random import matplotlib.pyplot as plt # 读取tsp文件 def read_tsp_file(filename): coordinates = [] with open(filename, 'r') as f: lines = f.readlines() for line in lines: if line.startswith('DIMENSION'): n = int(line.split()[-1]) elif line.startswith('NODE_COORD_SECTION'): for i in range(n): line = lines[i+1].strip().split() coordinates.append((float(line[1]), float(line[2]))) return coordinates # 定义距离矩阵 def distance_matrix(city_coordinates): n = len(city_coordinates) matrix = [[0] * n for i in range(n)] for i in range(n): for j in range(n): if i != j: x1, y1 = city_coordinates[i] x2, y2 = city_coordinates[j] matrix[i][j] = math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2) return matrix # 邻域结构:交换两个城市的位置 def swap_cities(path): n = len(path) i, j = random.sample(range(n), 2) new_path = path[:] new_path[i], new_path[j] = new_path[j], new_path[i] return new_path # 计算路径长度 def path_length(path, distance): return sum([distance[path[i-1]][path[i]] for i in range(len(path))]) # 优化后的退火算法 def simulated_annealing(distance, max_iterations=50000, start_temperature=10000, cooling_rate=0.99): n = len(distance) current_path = random.sample(range(n), n) current_length = path_length(current_path, distance) best_path = current_path[:] best_length = current_length temperature = start_temperature for i in range(max_iterations): new_path = swap_cities(current_path) new_length = path_length(new_path, distance) delta = new_length - current_length if delta < 0 or random.random() < math.exp(-delta/temperature): current_path = new_path[:] current_length = new_length if current_length < best_length: best_path = current_path[:] best_length = current_length temperature *= cooling_rate return best_path, best_length # 绘制最优路径 def plot_path(city_coordinates, path): x = [city_coordinates[path[i]][0] for i in range(len(path))] y = [city_coordinates[path[i]][1] for i in range(len(path))] x.append(city_coordinates[path[0]][0]) y.append(city_coordinates[path[0]][1]) plt.plot(x, y, marker='o') plt.show() # 读取tsp文件并求解最优路径 filename = 'att48.tsp' city_coordinates = read_tsp_file(filename) distance = distance_matrix(city_coordinates) best_path, best_length = simulated_annealing(distance) # 输出最优路径和最短距离 print('最优路径:', best_path) print('最短距离:', best_length) # 绘制最优路径 plot_path(city_coordinates, best_path) ``` 在代码中,我们读取了att48.tsp文件并将其转换为城市坐标,然后使用定义的距离矩阵和优化后的退火算法求解最优路径,最后绘制最优路径的图像。代码运行后,会输出最优路径和最短距离,并绘制最优路径的图像。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值