简述

代码我是基于我之前写的两篇,一篇是遗传算法TSP的Python实现,一篇是模拟退火算法的解决TSP的C++实现。

  • 模拟退火算法理论+Python解决函数极值+C++实现解决TSP问题
  • 遗传算法解决TSP问题 Python实现【160行以内代码】

效果演示

对比

相比于遗传算法来说没有保持历史中的较优数据,但是通过退火的算法思维,很邻近点搜索的想法,任然能保持较为正确的收敛结果

代码

import numpy as np
import random
import matplotlib.pyplot as plt
import os
import shutil
import imageiodef create_data(N, xu=25, yu=25, xd=-25, yd=-25):fx = lambda: random.random() * (xu - xd) + xdfy = lambda: random.random() * (yu - yd) + ydcalDistance = lambda x, y: np.sqrt((x[0] - y[0]) ** 2 + (x[1] - y[1]) ** 2)points = [(0, 0)] * Nfor i in range(N):points[i] = (fx(), fy())Mat = np.zeros((N, N))for i in range(N):for j in range(i + 1, N):dv = calDistance(points[i], points[j])Mat[i][j], Mat[j][i] = dv, dvreturn points, Matdef calpathValue(path):global Mattemp = Mat[0][path[0]]for i in range(len(path) - 1):temp += Mat[path[i]][path[i + 1]]temp += Mat[path[-1]][0]return tempdef initial():global Ninit = list(range(1, N, 1))random.shuffle(init)packValue = calpathValue(init)return init, packValuedef draw(path, pv):global points, N, TIMESIT, PNGFILE, PNGLISTplt.cla()plt.title('cross=%.4f' % pv)xs = [p[0] for p in points]ys = [p[1] for p in points]plt.scatter(xs, ys, color='b')xs = np.array(xs)ys = np.array(ys)plt.plot(xs[[0, path[0]]], ys[[0, path[0]]], color='r')for i in range(N - 2):plt.plot(xs[[path[i], path[i + 1]]], ys[[path[i], path[i + 1]]], color='r')plt.plot(xs[[path[N - 2], 0]], ys[[path[N - 2], 0]], color='r')plt.scatter(xs[0], ys[0], color='k', linewidth=10)for i, p in enumerate(points):plt.text(*p, '%d' % i)plt.savefig('%s/%d.png' % (PNGFILE, TIMESIT))PNGLIST.append('%s/%d.png' % (PNGFILE, TIMESIT))TIMESIT += 1if __name__ == '__main__':# N, Mat = read_data()TIMESIT = 0PNGFILE = './png/'PNGLIST = []if not os.path.exists(PNGFILE):os.mkdir(PNGFILE)else:shutil.rmtree(PNGFILE)os.mkdir(PNGFILE)N = 30points, Mat = create_data(N)T = 1000  # 起始温度alpha = 0.995  # T_{k+1} = alpha * T_k方式更新温度limitedT = 1.  # 最小值的TiterTime = 1000  # 每个温度下迭代的次数K = 0.8  # 系数Kp = 0path, value = initial()tempPath, tempValue = [], 0global_Best = value  # 画图while T > limitedT:print(T)for i in range(iterTime):tempPath = path.copy()tx = random.randint(0, N - 2)ty = random.randint(0, N - 2)if tx != ty:tempPath[tx], tempPath[ty] = tempPath[ty], tempPath[tx]tempValue = calpathValue(tempPath)if tempValue <= value:path = tempPath.copy()value = tempValue.copy()else:p = np.exp((value - tempValue) / (K * T))if random.random() < p:path = tempPath.copy()value = tempValue.copy()if value < global_Best:global_Best = valuedraw(path, value)T *= alphaprint(value)print(0, end='-->')for i in path:print(i, end='-->')generated_images = []for png_path in PNGLIST:generated_images.append(imageio.imread(png_path))shutil.rmtree(PNGFILE)  # 可删掉generated_images = generated_images + [generated_images[-1]] * 5imageio.mimsave('TSP-SAA.gif', generated_images, 'GIF', duration=0.5)

模拟退火算法解决TSP(python实现 110+行代码)【gif生成】相关推荐

  1. 【算法】模拟退火算法解决TSP问题的matlab实现

    [算法]模拟退火算法解决TSP问题的matlab实现 参考文章: (1)[算法]模拟退火算法解决TSP问题的matlab实现 (2)https://www.cnblogs.com/wenyehoush ...

  2. 模拟退火算法解决TSP问题

    模拟退火算法解决TSP问题 参考文章: (1)模拟退火算法解决TSP问题 (2)https://www.cnblogs.com/yangmingustb/p/8641124.html (3)https ...

  3. 使用模拟退火算法解决TSP问题

    1.实验目的 掌握模拟退火算法解决旅行商问题的方法. 2.实验环境 Matlab 3.实验内容 使用模拟退火算法解决14个城市的TSP问题,使得从一个城市出发,遍历所有城市回到起点的路线最短.已知14 ...

  4. 人工智能 -- 模拟退火算法解决TSP问题(JAVA版)

    计算由广州市出发走遍省内所有市的最短距离. 模拟退火算法:由两规则三函数组成. 两规则指:外层循环结束规则.内层循环结束规则. 三函数指:温度更新函数(控制温度的变化).状态产生函数(用于产生邻结点) ...

  5. 蚁群算法解决TSP问题(2#JAVA代码+详细注释+对比动态规划【JAVA】)

    第一部分:原理 TSP10cities.txt 1 2066 2333 2 935 1304 3 1270 200 4 1389 700 5 984 2810 6 2253 478 7 949 302 ...

  6. 模拟退火算法(TSP问题)

    模拟退火算法解决TSP问题 算法思想 模拟退火算法(Simulate Anneal,SA)是一种通用概率演算法,用来在一个大的搜寻空间内找寻命题的最优解 模拟退火算法来源于固体退火原理,将固体加温至充 ...

  7. 蚁群算法解决 TSP 问题

    蚁群算法解决 TSP 问题 数据集 Tools.py Ant.py ACO_G.py 运行效果 数据集 json 形式(c.json)的中国各省市经纬度数据集,一共 2241 个市的数据,为后来的 T ...

  8. 蚁群算法解决tsp问题python_蚁群算法在解决TSP问题中的应用

    陈灵佳 文章首先对蚁群算法与TSP问题进行简要介绍,在此基础上对蚁群算法在解决TSP问题中的应用进行论述.期望通过本文的研究能够对TSP问题的解决有所帮助. [关键词]蚁群算法 TSP问题 最优解 1 ...

  9. 《MATLAB智能算法30个案例》:第19章 基于模拟退火算法的TSP算法

    <MATLAB智能算法30个案例>:第19章 基于模拟退火算法的TSP算法 1. 前言 2. MATLAB 仿真示例 3. 小结 1. 前言 <MATLAB智能算法30个案例分析&g ...

最新文章

  1. ScriptManager.RegisterStartupScript方法和Page.ClientScript.RegisterStartupScript() 区别
  2. java 字符串数组连接
  3. 外卖和快递行业数据_下周一起,整治全面启动!锁定全市外卖、快递行业!
  4. 面试官:. NET5源码里用到了哪些设计模式?懵!
  5. Gridview SummaryItem 格式化数字
  6. 理论基础 —— 查找 —— 斐波那契查找
  7. 安卓项目中的R.java文件丢失如何解决
  8. 让你认识Android 开发简介及应用程序架构示例
  9. 条件指示符 #ifdef 的用法
  10. MySQL 游标的详解
  11. 怎么完全卸载赛门铁克_Symantec卸载方法,赛门铁克卸载
  12. Skywalking vs Pinpoint
  13. 编码,隐匿在计算机软硬件背后的语言读书笔记(1)
  14. linux bootrom ftp,H3C交换机通过以太口应用ftp方式升级bootrom软件
  15. 苹果浏览器限制input框输入是数字
  16. 《深渊古纪》古剑奇谭衍生小说 阅读笔记
  17. ipad9.7 能搭建php,9.7寸ipad pro能用pencil吗?ipad pro全面支持Apple Pencil
  18. BT-Panel Linux自动磁盘挂载工具
  19. 万年历-java课程设计题_万年历-Java课程设计题(eclipse编辑器)
  20. kingscada3.52 工程文件说明

热门文章

  1. R.java文件介绍
  2. S3C6410 KeyPad驱动(下)
  3. WINCE5.0添加Alphablend组件时遇到的问题
  4. Leetcode PHP题解--D25 500. Keyboard Row
  5. Android 百度鹰眼轨迹SDK(v2.1.6)
  6. 云计算产业被市场广泛看好 未来市场规模达4300亿元
  7. JavaScript Array.prototype.some()
  8. 监控HP服务器cpu状态脚本
  9. Stumpwm的编译安装
  10. Struts2核心工作原理解析