2744557306 发表于 2026-8-23 17:33

旅行商问题(TSP):数模优化题里"最简单的难题"









from itertools import permutations
import math

def tsp_brute(dist):
    """dist: n×n 距离矩阵,返回 (最短长度, 最优回路)"""
    n = len(dist)
    best_len, best_tour = math.inf, None
    for perm in permutations(range(1, n)):          # 固定起点 0
        tour = + list(perm)
        L = sum(dist]] for i in range(n-1)) + dist]
        if L < best_len:
            best_len, best_tour = L, tour
    return best_len, best_tour

# ---------- 2-opt 局部搜索(中大规模) ----------
def tsp_2opt(dist, max_iter=1000):
    n = len(dist)
    tour = list(range(n))                          # 初始:自然顺序
    improved = True
    while improved and max_iter > 0:
        improved = False
        for i in range(1, n-2):
            for k in range(i+1, n):
                # 翻转 tour
                new_tour = tour[:i] + tour[::-1] + tour
                cur = sum(dist]] for j in range(n-1)) + dist]]
                new = sum(dist]] for j in range(n-1)) + dist]]
                if new < cur:
                    tour, improved = new_tour, True
                    max_iter -= 1
    L = sum(dist]] for j in range(n-1)) + dist]]
    return L, tour

# ---------- 测试:4 城市 ----------
dist = [
    ,
    ,
    ,
   
]
print("暴力 DFS:", tsp_brute(dist))   # 最短长度 80,回路 0→1→3→2→0
print("2-opt   :", tsp_2opt(dist))7.2 运行结果暴力 DFS: (80, )
2-opt   : (95, )观察:暴力 DFS 保证全局最优( 时可行)。2-opt 从自然顺序出发卡在局部最优 95,未能到达最优解 80——这正是 2-opt 的经典陷阱:单次运行可能陷入局部最优。竞赛中应多次随机重启或结合其他启发式(如先用最近邻初始化再 2-opt)。这也是为什么论文里必须报告"与精确解的 gap",而不是只报一个数字。8. 竞赛真题映射
年份CUMCM 题目方向TSP 变体
1998B灾情巡视路线巡检 TSP(带时间窗 / 部分覆盖)
2001C公交车线路设计路径规划 + 容量约束(VRP 变体)
2008B啤酒运输问题配送路径优化
2011A高速公路收费站路网遍历 + 费用优化
2015A太阳能小屋设备布点 + 巡检路径
2017B煤炭产量预测无关(数据题)
2020A炉温曲线无关(拟合题)
近年物流配送 / 应急巡检 / 传感器网络TSP / VRP / 覆盖路径
核心规律:只要赛题出现"一条路走遍多个地点",TSP 或其变体(VRP、TSP with Time Windows、多车 TSP)就是最自然的建模原型。先用 TSP 建原型,再根据约束升级为 VRP,是数模竞赛的成熟套路。9. 常见误区与赛场自查
#误区正解
1"TSP 可以用贪心算法精确求解"贪心(最近邻)不保证最优,存在反例
2"2-opt 一定得到全局最优"2-opt 是局部搜索,可能陷入局部最优
3"TSP 和指派问题等价"TSP 要求单一回路,指派问题允许子回路
4"子回路消除约束可以一次性写完"数量  级别,实际用逐步加入(lazy constraint)
5"DP 复杂度 "准确是 ,每状态需  转移
6"所有 TSP 都有常数近似比"仅度量 TSP(三角不等式)有 Christofides  近似
7"对称 TSP 和非对称 TSP 一样"对称: 种回路;非对称: 种
赛场自查清单:
[*]我确认了距离矩阵是否对称、是否满足三角不等式(影响算法选择)。
[*]我估计了可行解数量 ,判断暴力 / B&B / 启发式的适用性。
[*]若用 B&B:下界函数是否安全(不高于最优值)且紧?
[*]若用 2-opt:是否报告了与精确解的偏差(小规模对比)?
[*]若用 Christofides:是否验证了三角不等式成立?
[*]论文中是否报告了运行时间与最优性差距(gap)?
[*]是否对结果做了敏感性分析(距离矩阵微调时回路是否稳定)?
[*]是否给出了子回路消除的处理方式(lazy constraint 或逐步加入)?
10. 参考文献 Dantzig, G. B., Fulkerson, D. R., & Johnson, S. M. (1954). Solution of a Large-Scale Traveling-Salesman Problem. Operations Research, 2(4), 393–410. DOI: 10.1287/opre.2.4.393. https://doi.org/10.1287/opre.2.4.393 Held, M., & Karp, R. M. (1961). A dynamic programming approach to sequencing problems. Proceedings of the 1961 16th ACM National Meeting, pp. 71.201–71.204. DOI: 10.1145/800029.808532. https://doi.org/10.1145/800029.808532 Held, M., & Karp, R. M. (1962). The traveling-salesman problem and minimum spanning trees: Part II. Operations Research, 18(6), 1138–1162. DOI: 10.1287/opre.18.6.1138. Lawler, E. L., Lenstra, J. K., Rinnooy Kan, A. H. G., & Shmoys, D. B. (Eds.) (1985). The Traveling Salesman Problem: A Guided Tour of Combinatorial Optimization. Wiley-Interscience Series in Discrete Mathematics, Vol. 12. Chichester: John Wiley & Sons. ISBN 978-0-471-90413-7. Applegate, D. L., Bixby, R. E., Chvátal, V., & Cook, W. J. (2011). The Traveling Salesman Problem: A Computational Study. Princeton Series in Applied Mathematics. Princeton, NJ: Princeton University Press. ISBN 978-0-691-12993-8 (print), 978-1-400-84110-3 (ebook). https://press.princeton.edu/books/ebook/9781400841103/the-traveling-salesman-problem-pdf Cook, W. (2012). In Pursuit of the Traveling Salesman: Mathematics at the Limits of Computation. Princeton, NJ: Princeton University Press. ISBN 978-0-691-16352-9. 姜启源, 谢金星, 叶俊. 数学模型. 5版. 北京: 高等教育出版社, 2018. ISBN 978-7-04-049222-4. 司守奎, 孙玺菁. 数学建模算法与应用. 3版. 北京: 国防工业出版社, 2021. ISBN 978-7-118-12278-7. 胡运权, 主编. 运筹学教程. 5版. 北京: 清华大学出版社, 2018. ISBN 978-7-302-48125-6. https://www.tup.tsinghua.edu.cn/wap/tsxqy.aspx?id=07656604给参赛学生的一句话:TSP 是数模优化题里"最简单的难题"——问题描述一句话,求解却需要一整套组合优化工具箱。小规模用 B&B/DP 保证最优,大规模用 2-opt 给出好解并报告 gap,这是评委最认可的"务实"策略。记住:能证明最优的解,比看似漂亮但无保证的解更有说服力。

页: [1]
查看完整版本: 旅行商问题(TSP):数模优化题里"最简单的难题"