基于模拟退火算法求解TSP问题(JAVA)

转载http://blog.csdn.net/wangqiuyun/article/details/8918523

一、TSP问题

TSP问题(Travelling Salesman Problem)即旅行商问题,又译为旅行推销员问题、货郎担问题,是数学领域中著名问题之一。假设有一个旅行商人要拜访n个城市,他必须选择所要走的路径,路径的限制是每个城市只能拜访一次,而且最后要回到原来出发的城市。路径的选择目标是要求得的路径路程为所有路径之中的最小值。

TSP问题是一个组合优化问题。该问题可以被证明具有NPC计算复杂性。TSP问题可以分为两类,一类是对称TSP问题(Symmetric TSP),另一类是非对称问题(Asymmetric TSP)。所有的TSP问题都可以用一个图(Graph)来描述:

V={c1, c2, …, ci, …, cn},i = 1,2, …, n,是所有城市的集合.ci表示第i个城市,n为城市的数目;

E={(r, s): r,s∈ V}是所有城市之间连接的集合;

C = {crs: r,s∈ V}是所有城市之间连接的成本度量(一般为城市之间的距离);

如果crs = csr, 那么该TSP问题为对称的,否则为非对称的。


一个TSP问题可以表达为:

求解遍历图G = (V, E, C),所有的节点一次并且回到起始节点,使得连接这些节点的路径成本最低。

二、模拟退火算法

模拟退火法是由 Metropolis 于1953 年提出的,是一种基于热力学原理的随机搜索算法,是局部搜索算法的拓展。它与局部搜索算法的不同之处在于:它以一定的概率选择邻域中目标函数值差的状态。

退火是一种物理过程,一种金属物体在加热至一定的温度后,它的所有分子在其状态空间中自由运动。随着温度的下降,这些分子逐渐停留在不同的状态。在温度最低时,分子重新以一定的结构排列。统计力学的研究表明,在同一个温度,分子停留在能量最小的状态的概率比停留在能量大的状态的概率要大。当温度相当高时,每个状态的概率基本相同,都接近平均值。当温度趋向0时,分子停留在最低能量状态的概率趋向于1。

模拟退火算法是一种基于上述退火原理建立的随机搜索算法。组合优化问题与金属物体的退火过程可进行如下类比:组合优化问题的解类似于金属物体的状态,组合优化问题的最优解类似于金属物体的能量最低的状态,组合优化问题的费用函数类似于金属物体的能量。

为了克服局部搜索算法极易陷入局部最优解的缺点,模拟退火算法使用基于概率的双方向随机搜索技术:当基于邻域的一次操作使当前解的质量提高时,模拟退火算法接受这个被改进的解作为新的当前解;在相反的情况下,算法以一定的概率exp(-ΔC/T)接受相对于当前解来说质量较差的解作为新的当前解,其中Δc为邻域操作前后解的评价值的差,T为退火过程的控制参数(即温度)。模拟退火算法已在理论上被证明是一种以概率1收敛于全局最优解的全局优化算法。

模拟退火算法的实施步:

三、模拟退火算法求解TSP问题

在该JAVA实现中我们选择使用tsplib上的数据att48,这是一个对称TSP问题,城市规模为48,其最优值为10628.其距离计算方法下图所示:

具体代码如下:

[java]  view plain copy
  1. package noah;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.FileInputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStreamReader;  
  7. import java.util.Random;  
  8.   
  9. public class SA {  
  10.   
  11.     private int cityNum; // 城市数量,编码长度  
  12.     private int N;// 每个温度迭代步长  
  13.     private int T;// 降温次数  
  14.     private float a;// 降温系数  
  15.     private float t0;// 初始温度  
  16.   
  17.     private int[][] distance; // 距离矩阵  
  18.     private int bestT;// 最佳出现代数  
  19.   
  20.     private int[] Ghh;// 初始路径编码  
  21.     private int GhhEvaluation;  
  22.     private int[] bestGh;// 最好的路径编码  
  23.     private int bestEvaluation;  
  24.     private int[] tempGhh;// 存放临时编码  
  25.     private int tempEvaluation;  
  26.   
  27.     private Random random;  
  28.   
  29.     public SA() {  
  30.   
  31.     }  
  32.   
  33.     /** 
  34.      * constructor of GA 
  35.      *  
  36.      * @param cn 
  37.      *            城市数量 
  38.      * @param t 
  39.      *            降温次数 
  40.      * @param n 
  41.      *            每个温度迭代步长 
  42.      * @param tt 
  43.      *            初始温度 
  44.      * @param aa 
  45.      *            降温系数 
  46.      *  
  47.      **/  
  48.     public SA(int cn, int n, int t, float tt, float aa) {  
  49.         cityNum = cn;  
  50.         N = n;  
  51.         T = t;  
  52.         t0 = tt;  
  53.         a = aa;  
  54.     }  
  55.   
  56.     // 给编译器一条指令,告诉它对被批注的代码元素内部的某些警告保持静默  
  57.     @SuppressWarnings("resource")  
  58.     /** 
  59.      * 初始化Tabu算法类 
  60.      * @param filename 数据文件名,该文件存储所有城市节点坐标数据 
  61.      * @throws IOException 
  62.      */  
  63.     private void init(String filename) throws IOException {  
  64.         // 读取数据  
  65.         int[] x;  
  66.         int[] y;  
  67.         String strbuff;  
  68.         BufferedReader data = new BufferedReader(new InputStreamReader(  
  69.                 new FileInputStream(filename)));  
  70.         distance = new int[cityNum][cityNum];  
  71.         x = new int[cityNum];  
  72.         y = new int[cityNum];  
  73.         for (int i = 0; i < cityNum; i++) {  
  74.             // 读取一行数据,数据格式1 6734 1453  
  75.             strbuff = data.readLine();  
  76.             // 字符分割  
  77.             String[] strcol = strbuff.split(" ");  
  78.             x[i] = Integer.valueOf(strcol[1]);// x坐标  
  79.             y[i] = Integer.valueOf(strcol[2]);// y坐标  
  80.         }  
  81.         // 计算距离矩阵  
  82.         // ,针对具体问题,距离计算方法也不一样,此处用的是att48作为案例,它有48个城市,距离计算方法为伪欧氏距离,最优值为10628  
  83.         for (int i = 0; i < cityNum - 1; i++) {  
  84.             distance[i][i] = 0// 对角线为0  
  85.             for (int j = i + 1; j < cityNum; j++) {  
  86.                 double rij = Math  
  87.                         .sqrt(((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j])  
  88.                                 * (y[i] - y[j])) / 10.0);  
  89.                 // 四舍五入,取整  
  90.                 int tij = (int) Math.round(rij);  
  91.                 if (tij < rij) {  
  92.                     distance[i][j] = tij + 1;  
  93.                     distance[j][i] = distance[i][j];  
  94.                 } else {  
  95.                     distance[i][j] = tij;  
  96.                     distance[j][i] = distance[i][j];  
  97.                 }  
  98.             }  
  99.         }  
  100.         distance[cityNum - 1][cityNum - 1] = 0;  
  101.   
  102.         Ghh = new int[cityNum];  
  103.         bestGh = new int[cityNum];  
  104.         bestEvaluation = Integer.MAX_VALUE;  
  105.         tempGhh = new int[cityNum];  
  106.         tempEvaluation = Integer.MAX_VALUE;  
  107.         bestT = 0;  
  108.         random = new Random(System.currentTimeMillis());  
  109.           
  110.         System.out.println(cityNum+","+N+","+T+","+a+","+t0);  
  111.   
  112.     }  
  113.   
  114.     // 初始化编码Ghh  
  115.     void initGroup() {  
  116.         int i, j;  
  117.         Ghh[0] = random.nextInt(65535) % cityNum;  
  118.         for (i = 1; i < cityNum;)// 编码长度  
  119.         {  
  120.             Ghh[i] = random.nextInt(65535) % cityNum;  
  121.             for (j = 0; j < i; j++) {  
  122.                 if (Ghh[i] == Ghh[j]) {  
  123.                     break;  
  124.                 }  
  125.             }  
  126.             if (j == i) {  
  127.                 i++;  
  128.             }  
  129.         }  
  130.     }  
  131.   
  132.     // 复制编码体,复制编码Gha到Ghb  
  133.     public void copyGh(int[] Gha, int[] Ghb) {  
  134.         for (int i = 0; i < cityNum; i++) {  
  135.             Ghb[i] = Gha[i];  
  136.         }  
  137.     }  
  138.   
  139.     public int evaluate(int[] chr) {  
  140.         // 0123  
  141.         int len = 0;  
  142.         // 编码,起始城市,城市1,城市2...城市n  
  143.         for (int i = 1; i < cityNum; i++) {  
  144.             len += distance[chr[i - 1]][chr[i]];  
  145.         }  
  146.         // 城市n,起始城市  
  147.         len += distance[chr[cityNum - 1]][chr[0]];  
  148.         return len;  
  149.     }  
  150.   
  151.     // 邻域交换,得到邻居  
  152.     public void Linju(int[] Gh, int[] tempGh) {  
  153.         int i, temp;  
  154.         int ran1, ran2;  
  155.   
  156.         for (i = 0; i < cityNum; i++) {  
  157.             tempGh[i] = Gh[i];  
  158.         }  
  159.         ran1 = random.nextInt(65535) % cityNum;  
  160.         ran2 = random.nextInt(65535) % cityNum;  
  161.         while (ran1 == ran2) {  
  162.             ran2 = random.nextInt(65535) % cityNum;  
  163.         }  
  164.         temp = tempGh[ran1];  
  165.         tempGh[ran1] = tempGh[ran2];  
  166.         tempGh[ran2] = temp;  
  167.     }  
  168.   
  169.     public void solve() {  
  170.         // 初始化编码Ghh  
  171.         initGroup();  
  172.         copyGh(Ghh, bestGh);// 复制当前编码Ghh到最好编码bestGh  
  173.         bestEvaluation = evaluate(Ghh);  
  174.         GhhEvaluation = bestEvaluation;  
  175.         int k = 0;// 降温次数  
  176.         int n = 0;// 迭代步数  
  177.         float t = t0;  
  178.         float r = 0.0f;  
  179.           
  180.         while (k < T) {  
  181.             n = 0;  
  182.             while (n < N) {  
  183.                 Linju(Ghh, tempGhh);// 得到当前编码Ghh的邻域编码tempGhh  
  184.                 tempEvaluation = evaluate(tempGhh);  
  185.                 if (tempEvaluation < bestEvaluation)  
  186.                 {  
  187.                     copyGh(tempGhh, bestGh);  
  188.                     bestT = k;  
  189.                     bestEvaluation = tempEvaluation;  
  190.                 }  
  191.                 r = random.nextFloat();  
  192.                 if (tempEvaluation < GhhEvaluation  
  193.                         || Math.exp((GhhEvaluation - tempEvaluation) / t) > r) {  
  194.                     copyGh(tempGhh, Ghh);  
  195.                     GhhEvaluation = tempEvaluation;  
  196.                 }  
  197.                 n++;  
  198.             }  
  199.             t = a * t;  
  200.             k++;  
  201.         }  
  202.           
  203.         System.out.println("最佳长度出现代数:");  
  204.         System.out.println(bestT);  
  205.         System.out.println("最佳长度");  
  206.         System.out.println(bestEvaluation);  
  207.         System.out.println("最佳路径:");  
  208.         for (int i = 0; i < cityNum; i++) {  
  209.             System.out.print(bestGh[i] + ",");  
  210.             if (i % 10 == 0 && i != 0) {  
  211.                 System.out.println();  
  212.             }  
  213.         }  
  214.     }  
  215.   
  216.     /** 
  217.      * @param args 
  218.      * @throws IOException 
  219.      */  
  220.     public static void main(String[] args) throws IOException {  
  221.         System.out.println("Start....");  
  222.         SA sa = new SA(4840400250.0f, 0.98f);  
  223.         sa.init("c://data.txt");  
  224.         sa.solve();  
  225.     }  
  226. }  

运行结果截图:

四、总结

模拟算法其特点是在开始搜索阶段解的质量提高比较缓慢,但是到了迭代后期,它的解的质量提高明显,所以如果在求解过程中,对迭代步数限制比较严格的话,模拟退火算法在有限的迭代步数内很难得到高质量的解。总体而言模拟退火算法比较适合用于有充足计算资源的问题求解。

(截止目前,关于TSP问题系列目前已经使用了爬山、禁忌、蚁群和退火四种算法来求解,计划还有一篇遗传算法的,敬请各位期待!)

注:本文部分内容来源于网络,但程序以及分析结果属于本人成果,转载请注明!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值