TSP_旅行商问题

来源:互联网 发布:java jna调用64位dll 编辑:程序博客网 时间:2024/05/19 12:11

一、前言

    【旅行商问题】旅行商问题(TravelingSalesmanProblem,TSP)是一个经典的组合优化问题。经典的TSP可以描述为:一个商品推销员要去若干个城市推销商品,该推销员从一个城市出发,需要经过所有城市后,回到出发地。应如何选择行进路线,以使总的行程最短。从图论的角度来看,该问题实质是在一个带权完全无向图中,找一个权值最小的Hamilton回路。由于该问题的可行解是所有顶点的全排列,随着顶点数的增加,会产生组合爆炸,它是一个NP完全问题。由于其在交通运输、电路板线路设计以及物流配送等领域内有着广泛的应用,国内外学者对其进行了大量的研究。早期的研究者使用精确算法求解该问题,常用的方法包括:分枝定界法、线性规划法、动态规划法等。但是,随着问题规模的增大,精确算法将变得无能为力,因此,在后来的研究中,国内外学者重点使用近似算法或启发式算法,主要有遗传算法模拟退火法蚁群算法禁忌搜索算法、贪婪算法神经网络等。【参考百度百科】。

    旅行商求解系列:

-------------------------------------------------------------------------------------------------

(1)TSP_旅行商问题- 蛮力法( 深度遍历优先算法DFS )
(2)TSP_旅行商问题- 动态规划
(3)TSP_旅行商问题- 模拟退火算法
(4)TSP_旅行商问题- 遗传算法
(5)TSP_旅行商问题- 粒子群算法
(6)TSP_旅行商问题- 神经网络

-------------------------------------------------------------------------------------------------

二、本文概要

1. 问题描述:

    在求解实际问题,我们可以采用搜索算法,比如爬山搜索等系列算法。但这些算法都是局部优化算法,在某些实际问题中还是有很多缺点。局部搜索算法(以爬山算法为代表)的缺点:仅适用于某类组合优化问题;所得到的近似解质量通常较差;时间复杂度高,且最坏情况下的时间复杂度未知;最致命的是无法跳离局部最优的“陷阱”。

    人们开始超越数学思维,从自然物理过程中寻找灵感。1982年,Kirkpatrick意识到固体退火算法与组合优化问题之间的类似性Metropolis等对孤立在恒定温度下达到热平衡的过程的模拟的启迪:把Metropolis准则引入优化过程中模拟退火算法(Simulated Annealing Algorithm,简称SAA),源于对固体退火过程的模拟,采用Metropolis接受准则,并用一组称为冷却表的参数控制算法进程,使算法在多项式时间里给出一个近似最优解。

    爬山搜索为代表的局部搜索算法都是仅适用于某类组合优化问题,所得到的近似解的质量通常较差。这类方法最致命的缺点是无法跳离局部最优的“陷阱”,最终停留在某个局部最优解上。为了克服这些弱点,人们开始超脱纯数学思维,到一些自然物理过程中寻找灵感。模拟退火算法就是一个成功的典范,其思想比方法本身更为重要。

2. 模拟退火算法简介:

    模拟退火算法在处理全局优化、离散变量优化等困难问题中,具有传统优化算法无可比拟的优势。模拟退火算法的思想最早由Metorpolis等提出的。其出发点是基于物理中固体物质的退火过程与一般的组合优化问题之间的相似性。模拟退火法是一种通用的优化算法,其物理退火过程由以下三部分组成:

    1)加温过程:其目的是增强粒子的热运动,使其偏离平衡位置。当温度足够高时,固体将熔为液体,从而消除系统原先存在的非均匀状态。

    2)等温过程:对于与周围环境交换热量而温度不变的封闭系统,系统状态的自发变化总是朝自由能减少的方向进行,当自由能达到最小时,系统达到平衡状态。

    3)冷却过程:使粒子热运动减弱,系统能量下降,得到晶体结构。其中,加温过程对应算法的设定初温,等温过程对应算法的Metropolis抽样过程,冷却过程对应控制参数的下降。这里能量的变化就是目标函数,我们要得到的最优解就是能量最低态。其中Metropolis准则是SA算法收敛于全局最优解的关键所在,Metropoli、准则以一定的概率接受恶化解,这样就使算法跳离局部最优的陷阱。

3. 编程环境:

  • 本文使用C++语言,在VS2010平台上编写代码和调试,下文有提供相关的代码。

三、模拟退火算法(SA)

1. 模拟退火算法基本步骤

  • (1)选S_0作为初始状态,令S (0)=S_0,同时设初始温度T,令i=0。
  • (2)令 T=T_i,以T和S_i调用Metorpolis抽样算法,返回状态S作为本算法的当前解,S_i=S_0。
  • (3)按照一定方式降温,即T =T_(i+1),其中T_(i+1)<T_i,i=i+1。
  • (4)检查终止条件,如果满足则转步骤 (5),否则转步骤(2)
  • (5)当前解S_i为最优解,输出结果,停止。Metropolis抽样算法描述如下:
  •         1)令k=0时,当前解 S (0)=S_0,在温度T下,进行以下各步操作。
  •         2)按某个规定的方式根据当前解 S(k)所处的状态S产生一个近邻子集N (S(k))+S,从N(S (k))中随机得到一个新状态 S' 作为下一个候选解,计算能量之差△C'                 = C(S') - C(S(k))。
  •         3)如果△C' <  0 ,则接受 S' 作为下一个当前解,否则,以概率exp(一△C' / T)接受 S' 作为下一个当前解。若 S' 被接受,则令S (k十1) = S' ,否则                                             S(k+1)=S(k)。
  •         4)k =k +1,检查算法是否满足终止条件,若满足,则转步骤(5),否则转步骤(2)。
  •         5)返回S(k),结束。

2. Metropolis算法解释

    模拟退火算法用Metropolis算法产生组合优化问题解的序列。并由Metropolis准则对应的转移概率P:


确定是否接受从当前解i 到新解j 的转移。式中t ∈R+ 表示控制参数。开始让t 取较大的值,在进行足够多的转移后,缓慢减小t 的值(初始温度乘以退火系数,如 0.98 等),如此重复直至满足某个停止准则时算法终止。模拟退火算法依据Metropolis准则接受新解,除接受优化解外,还在一个限定范围内接受恶化解。开始时t值较大,可能接受较差的恶化解,随着t值的减小,只能接受较好的恶化解;当t值趋于零值时,就不再接受任何恶化解。这就使得算法可以跳出局部最优陷阱。在算法执行期间,随着控制参数t值的减小,算法返回某个整体最优解得概率单调增大,返回某个非最优解的概率单调减小。

3. 本文使用的结构体:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. typedef struct{  
  2.     int vex_num, arc_num;                   // 顶点数 边数  
  3.     char vexs[max_vexNum];                  // 顶点向量  
  4.     double arcs[max_vexNum][max_vexNum];    // 邻接矩阵  
  5. }Graph;  
  6.   
  7. typedef struct{  
  8.     int length_path;  
  9.     char path[max_vexNum];  
  10. }TSP_solution;  

四、程序流程



五、程序开发 - 模拟退火算法

1. 模拟退火过程:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. TSP_solution SA_TSP(Graph G){  
  2.     srand ( unsigned ( time(0) ) );  
  3.   
  4.     // 当前温度  
  5.     double Current_Temperature = INITIAL_TEMPERATURE;  
  6.   
  7.     // 最优解  
  8.     TSP_solution Best_solution;  
  9.     Best_solution.length_path = MAX_INT;  
  10.   
  11.     // 初始路径  
  12.     for (int i = 0;i < G.vex_num; i++)  
  13.     {  
  14.         Best_solution.path[i] = 'A' + i;  
  15.     }  
  16.     random_shuffle(Best_solution.path + 1, Best_solution.path + G.vex_num);  
  17.   
  18.     // 当前解, 与最优解比较  
  19.     TSP_solution Current_solution;  
  20.   
  21.     // 模拟退火过程  
  22.     while(MIN_TEMPERATURE < Current_Temperature){  
  23.         // cout<<"MIN_TEMPERATURE = "<<MIN_TEMPERATURE<<endl;  
  24.         // cout<<"Current_Temperature = "<<Current_Temperature<<endl;  
  25.         // 满足迭代次数  
  26.         for (int i = 0; i < LEGNTH_Mapkob; i++)  
  27.         {  
  28.             Current_solution = FindNewSolution(G, Best_solution);  
  29.             if (Current_solution.length_path <= Best_solution.length_path)   // 接受新解  
  30.             {  
  31.                 if (Current_solution.length_path == Best_solution.length_path)  
  32.                 {  
  33.                     // cout<<"不同路径出现相同的最优解."<<endl;  
  34.                 }  
  35.                 Best_solution = Current_solution;  
  36.             }  
  37.             else{   // 按 Metropolis 判断是否接受  
  38.                 if ((int)exp((Best_solution.length_path - Current_solution.length_path) / Current_Temperature)*100 > (rand()*101))  
  39.                 {  
  40.                     Best_solution = Current_solution;  
  41.                 }  
  42.                 else{  
  43.                     // cout<<"不接受当前解."<<endl;  
  44.                 }  
  45.             }  
  46.         }  
  47.         Current_Temperature *= SPEED;  // 按 SPEED 速率退火  
  48.   
  49.     } // while  
  50.   
  51.     return Best_solution;  
  52. }  

2. 产生新解:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. TSP_solution FindNewSolution(Graph G, TSP_solution bestSolution){  
  2.     // 产生新的解  
  3.     TSP_solution newSolution;  
  4.   
  5.     // 起始城市固定为A, 终点也要返回A, 即需要关注起点A和终点A之间的所有城市  
  6.     int i = rand() % (G.vex_num - 1) + 1;   // % 取余 -> 即将随机数控制在[1, G.vex_num - 1]  
  7.     int j = rand() % (G.vex_num - 1) + 1;  
  8.   
  9.     if (i > j)  
  10.     {  
  11.         int temp = i;  
  12.         i = j;  
  13.         j = temp;  
  14.     }  
  15.     else if (i == j)  
  16.     {   // 表示产生的随机数没有改变的作用, 将此路程设置为最大并结束该函数  
  17.   
  18.         newSolution = bestSolution;  
  19.         return newSolution;  
  20.     }  
  21.   
  22.     /* way 2 */   
  23.     int choose = rand() % 3;  
  24.     if (choose == 0)  
  25.     {   // 随机交换任意两个城市的位置  
  26.         char temp = bestSolution.path[i];  
  27.         bestSolution.path[i] = bestSolution.path[j];  
  28.         bestSolution.path[j] = temp;  
  29.     }else if (choose == 1)  
  30.     {   // 随机逆置城市的位置  
  31.         reverse(bestSolution.path + i, bestSolution.path + j);  
  32.     }  
  33.     else{   // 随机移位城市的位置  
  34.         if (j+1 == G.vex_num) //边界处不处理    
  35.         {    
  36.             newSolution = bestSolution;  
  37.             return newSolution;   
  38.         }    
  39.         rotate(bestSolution.path + i, bestSolution.path + j, bestSolution.path + j + 1);   
  40.     }  
  41.     newSolution = bestSolution;  
  42.     newSolution.path[G.vex_num] = newSolution.path[0];   // 终点与起始点相同  
  43.     newSolution.path[G.vex_num + 1] = '\0';  
  44.   
  45.     newSolution.length_path = CalculateLength(G, newSolution);  
  46.   
  47.     return newSolution;  
  48. }  

3. 计算TSP路径长度:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. int CalculateLength(Graph G,TSP_solution newSolution){  
  2.     int _length = 0;  
  3.   
  4.     for (int i = 0; i < G.vex_num - 1; i++)  
  5.     {  
  6.         int _startCity = (int)newSolution.path[i] - 65;  
  7.         int _endCity = (int)newSolution.path[i+1] - 65;  
  8.         if (G.arcs[_startCity][_endCity] == -1)  
  9.         {  
  10.             return MAX_INT;  
  11.         }  
  12.         else{  
  13.             _length += G.arcs[_startCity][_endCity];  
  14.         }  
  15.     }  
  16.   
  17.     // 判断该路径是否能回到起始城市  
  18.     if (G.arcs[(int)newSolution.path[G.vex_num - 1] - 65][(int)newSolution.path[0]-65] == -1)  
  19.     {  
  20.         return MAX_INT;  
  21.     }  
  22.     else{  
  23.         _length += G.arcs[(int)newSolution.path[G.vex_num - 1] - 65][(int)newSolution.path[0]-65];  
  24.         // cout<<"_length = "<<_length<<endl;  
  25.         return _length;  
  26.     }  
  27.       
  28. }  

六、测试数据及其程序运行结果

1.1 第一组测试数据(5个城市):


1.2. 第一组数据运行结果(5个城市):


用DFS-TSP求得的最短路径如下,最短路程为 43,耗时为0.047秒

使用SA-TSP求得的近似最短路径,最短路程为 43(全局最优),耗时为0.266秒
此时,采用DFS和SA算法得到最优解都是43,且程序耗时SA>DFS,在小规模城市组合问题(城市个数小于5),采用DFS更优。

2.1 第二组测试数据(10个城市):


采用10个城市作为测试数据

2.2 第二组数据运行结果(10个城市):


DFS-TSP:处理10个城市组合的问题,程序用时3.574秒

SA-TSP:处理10个城市组合的问题,程序用时0.385秒(程序近似全局最优解)
此时,以DFS得到的解为标准(最优解),SA算法得到近似最优解,程序耗时得到优化,在较大规模的城市组合问题,采用SA的整体性能更优。

3. SA-TSP:处理26个城市的组合


SA-TSP:当城市个数达到26个的时候,模拟退火算法所需要的时间仍是0.837s
此时,蛮力法(DFS)已经无法在有限的时间内遍历出所有可能的城市路径情况。

七、总结 - 模拟退火算法解决TSP问题

1. 总结

    从实验结果来看,通过DFS进行所有路径的遍历可知,模拟退火算法得到的解是全局最优解,取得较好的效果。但是目前由于实验数据不够庞大(五个城市),没能体现出模拟退火算法的优势。通过增加测试数据的数量(增加至10个城市以及26个城市),进一步体现模拟退火算法在大规模城市下的优势。
    理论上,模拟退火算法在某一温度下(此处初始温度为1000,最低温度为0.001,温度的下降率为0.98,Mapkov链长为500),只要计算时间足够长,就可以保证以概率1收敛于全局最优解。但在算法的实现过程中,由于计算速度和时间的限制,往往在优化结果和计算时间之间存在着矛盾,这是有待我们解决的。但模拟退火算法在解决中小规模组合优化问题上的优势是显而易见的。

2. 模拟退火算法的优缺点:(尚待完善,后续补充)

  -- 优点:

    1)描述简单,使用灵活,运用广泛,运行效率高;

    2)需要较少的初始化条件约束,以概率 P(i) 收敛于全局最优;

    3)具有并行性;

  -- 缺点:

    1)收敛速度慢,执行时间长;

    2)参数依赖较大;

八、程序源码:

1. SA.h

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #ifndef _SA_H_  
  2. #define _SA_H_  
  3.   
  4.     #define max_vexNum 30  
  5.     #define MAX_CITYNUM 150  
  6.   
  7.     const int LEGNTH_Mapkob = 500;  
  8.     const double SPEED = 0.98;                  // 退火速度  
  9.     const double INITIAL_TEMPERATURE = 1000.0;  // 初始温度  
  10.     const double MIN_TEMPERATURE = 0.001;       // 最低温度  
  11.     const int MAX_INT = 999999;  
  12.   
  13.     typedef struct{  
  14.         int vex_num, arc_num;                   // 顶点数 边数  
  15.         char vexs[max_vexNum];                  // 顶点向量  
  16.         double arcs[max_vexNum][max_vexNum];    // 邻接矩阵  
  17.     }Graph;  
  18.   
  19.     typedef struct{  
  20.         int length_path;  
  21.         char path[max_vexNum];  
  22.     }TSP_solution;  
  23.   
  24.     void CreateGraph(Graph &G);  
  25.     TSP_solution SA_TSP(Graph G);  
  26.     TSP_solution FindNewSolution(Graph G, TSP_solution bestSolution);  
  27.     int CalculateLength(Graph G,TSP_solution newSolution);  
  28.     bool Is_Accepted();  
  29.     void Display(Graph G,TSP_solution bestSoluion);  
  30.   
  31. #endif  

2. SA.cpp

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. #include <stdlib.h>   
  3. #include <queue>  
  4. #include <stack>  
  5. #include <fstream>  
  6. #include <iomanip>    // 本文用于输出对齐  
  7. #include <ctime>  
  8. #include <algorithm>  
  9.   
  10. #include "SA.h"  
  11.   
  12. using namespace std;  
  13.   
  14. // 城市数据格式转化  
  15. void CityDataTranslate(){  
  16.     ifstream read_in;  
  17.     read_in.open("L:\\Coding\\TSP_SA模拟退火算法\\TSP_SA模拟退火算法\\ch150.txt");      // 待转换数据  
  18.     if (!read_in.is_open())  
  19.     {  
  20.         cout<<"文件读取失败."<<endl;  
  21.         return;  
  22.     }  
  23.   
  24.     ofstream fout("L:\\Coding\\TSP_SA模拟退火算法\\TSP_SA模拟退火算法\\city_150.txt");      // 转换后的数据存入文档 city_150.txt  
  25.   
  26.     double city_table[MAX_CITYNUM][MAX_CITYNUM];  
  27.     int city_No[MAX_CITYNUM];  
  28.     double city_x[MAX_CITYNUM];  
  29.     double city_y[MAX_CITYNUM];  
  30.   
  31.     int vex_num;  
  32.     read_in >> vex_num;  
  33.     fout << vex_num << endl;  
  34.   
  35.     for (int i = 0; i < vex_num; i++)  
  36.     {  
  37.         read_in >> city_No[i] >> city_x[i] >> city_y[i];  
  38.   
  39.         fout << i + 1 <<" ";  
  40.     }  
  41.     fout<<endl;  
  42.   
  43.     for (int i = 0; i < vex_num; i++)  
  44.     {  
  45.         city_table[i][i] = 0;  
  46.         for (int j = 0; j < vex_num; j++)  
  47.         {  
  48.             double temp = (city_x[i] - city_x[j])*(city_x[i] - city_x[j]) + (city_y[i] - city_y[j])*(city_y[i] - city_y[j]);  
  49.             city_table[i][j] = sqrt(temp);  
  50.             fout << city_table[i][j]<<" ";  
  51.         }  
  52.         fout<<endl;  
  53.     }  
  54. }  
  55.   
  56. int main(){  
  57.     // CityDataTranslate();   // 创建城市表  
  58.   
  59.     time_t T_begin = clock();  
  60.     Graph G;  
  61.     CreateGraph(G);  
  62.   
  63.     // 1. initial w and target function f(w)  
  64.     // 2. random production new solution - eg: ABCDEA --> ABCEDA  
  65.     // 3. judge whether accepted new solution or not   
  66.     // 4. Simulate Annealing  
  67.   
  68.     TSP_solution bestSoluion = SA_TSP(G);  
  69.     Display(G, bestSoluion);  
  70.   
  71.     time_t T_end = clock();  
  72.     double RunningTime = double(T_end - T_begin)/CLOCKS_PER_SEC;  
  73.   
  74.     cout<<"程序运行时间 RunningTime = "<<RunningTime<<endl;  
  75.   
  76.     system("pause");  
  77.     return 0;  
  78. }  
  79.   
  80. void CreateGraph(Graph &G){  
  81.     ifstream read_in;  
  82.     read_in.open("L:\\Coding\\TSP_SA模拟退火算法\\TSP_SA模拟退火算法\\city_15.txt");  
  83.     if (!read_in.is_open())  
  84.     {  
  85.         cout<<"文件读取失败."<<endl;  
  86.         return;  
  87.     }  
  88.   
  89.     read_in >> G.vex_num;  
  90.     // read_in >> G.arc_num;  
  91.     G.arc_num = 0;  
  92.     for (int i = 0;i < G.vex_num; i++)  
  93.     {  
  94.         read_in >> G.vexs[i];  
  95.     }  
  96.     G.vexs[G.vex_num] = '\0';   // char的结束符.  
  97.   
  98.     for (int i = 0; i < G.vex_num;i++)  
  99.     {  
  100.         for (int j = 0; j < G.vex_num; j++)  
  101.         {  
  102.             read_in >> G.arcs[i][j];  
  103.   
  104.             // calculate the arc_num  
  105.             if (G.arcs[i][j] > 0)  
  106.             {  
  107.                 G.arc_num++;  
  108.             }  
  109.         }  
  110.     }  
  111.   
  112.     // display  
  113.     cout<<"无向图创建完毕,相关信息如下:"<<endl;  
  114.     cout<<"【顶点数】 G.vex_num = "<<G.vex_num<<endl;  
  115.     cout<<"【边数】 G.arc_num = "<<G.arc_num<<endl;  
  116.     cout<<"【顶点向量】 vexs[max_vexNum] = ";  
  117.     for (int i = 0; i < G.vex_num; i++)  
  118.     {  
  119.         cout << G.vexs[i] << " ";  
  120.     }  
  121.     cout<<endl<<"【邻接矩阵】 arcs[max_vexNum][max_vexNum] 如下:"<<endl;  
  122.     for (int i = 0; i < G.vex_num;i++)  
  123.     {  
  124.         for (int j = 0; j < G.vex_num; j++)  
  125.         {  
  126.             cout << std::right << setw(4) << G.arcs[i][j]<<" ";  
  127.         }  
  128.         cout<<endl;  
  129.     }  
  130. }  
  131.   
  132. TSP_solution SA_TSP(Graph G){  
  133.     srand ( unsigned ( time(0) ) );  
  134.   
  135.     // 当前温度  
  136.     double Current_Temperature = INITIAL_TEMPERATURE;  
  137.   
  138.     // 最优解  
  139.     TSP_solution Best_solution;  
  140.     Best_solution.length_path = MAX_INT;  
  141.   
  142.     // 初始路径  
  143.     for (int i = 0;i < G.vex_num; i++)  
  144.     {  
  145.         Best_solution.path[i] = 'A' + i;  
  146.     }  
  147.     random_shuffle(Best_solution.path + 1, Best_solution.path + G.vex_num);  
  148.   
  149.     // 当前解, 与最优解比较  
  150.     TSP_solution Current_solution;  
  151.   
  152.     // 模拟退火过程  
  153.     while(MIN_TEMPERATURE < Current_Temperature){  
  154.         // 满足迭代次数  
  155.         for (int i = 0; i < LEGNTH_Mapkob; i++)  
  156.         {  
  157.             Current_solution = FindNewSolution(G, Best_solution);  
  158.             if (Current_solution.length_path <= Best_solution.length_path)   // 接受新解  
  159.             {  
  160.                 if (Current_solution.length_path == Best_solution.length_path)  
  161.                 {  
  162.                     // cout<<"不同路径出现相同的最优解."<<endl;  
  163.                 }  
  164.                 Best_solution = Current_solution;  
  165.             }  
  166.             else{   // 按 Metropolis 判断是否接受  
  167.                 if ((int)exp((Best_solution.length_path - Current_solution.length_path) / Current_Temperature)*100 > (rand()*101))  
  168.                 {  
  169.                     Best_solution = Current_solution;  
  170.                 }  
  171.                 else{  
  172.                     // cout<<"不接受当前解."<<endl;  
  173.                 }  
  174.             }  
  175.         }  
  176.         Current_Temperature *= SPEED;  // 按 SPEED 速率退火  
  177.   
  178.     } // while  
  179.   
  180.     return Best_solution;  
  181. }  
  182.   
  183. TSP_solution FindNewSolution(Graph G, TSP_solution bestSolution){  
  184.     // 产生新的解  
  185.     TSP_solution newSolution;  
  186.   
  187.     // 起始城市固定为A, 终点也要返回A, 即需要关注起点A和终点A之间的所有城市  
  188.     int i = rand() % (G.vex_num - 1) + 1;   // % 取余 -> 即将随机数控制在[1, G.vex_num - 1]  
  189.     int j = rand() % (G.vex_num - 1) + 1;  
  190.   
  191.     if (i > j)  
  192.     {  
  193.         int temp = i;  
  194.         i = j;  
  195.         j = temp;  
  196.     }  
  197.     else if (i == j)  
  198.     {   // 表示产生的随机数没有改变的作用, 将此路程设置为最大并结束该函数  
  199.   
  200.         newSolution = bestSolution;  
  201.         return newSolution;  
  202.     }  
  203.   
  204.     /* way 2 */   
  205.     int choose = rand() % 3;  
  206.     if (choose == 0)  
  207.     {   // 随机交换任意两个城市的位置  
  208.         char temp = bestSolution.path[i];  
  209.         bestSolution.path[i] = bestSolution.path[j];  
  210.         bestSolution.path[j] = temp;  
  211.     }else if (choose == 1)  
  212.     {   // 随机逆置城市的位置  
  213.         reverse(bestSolution.path + i, bestSolution.path + j);  
  214.     }  
  215.     else{   // 随机移位城市的位置  
  216.         if (j+1 == G.vex_num) //边界处不处理    
  217.         {    
  218.             newSolution = bestSolution;  
  219.             return newSolution;   
  220.         }    
  221.         rotate(bestSolution.path + i, bestSolution.path + j, bestSolution.path + j + 1);   
  222.     }  
  223.     newSolution = bestSolution;  
  224.     newSolution.path[G.vex_num] = newSolution.path[0];   // 终点与起始点相同  
  225.     newSolution.path[G.vex_num + 1] = '\0';  
  226.     newSolution.length_path = CalculateLength(G, newSolution);  
  227.   
  228.     return newSolution;  
  229. }  
  230.   
  231. int CalculateLength(Graph G,TSP_solution newSolution){  
  232.     int _length = 0;  
  233.   
  234.     for (int i = 0; i < G.vex_num - 1; i++)  
  235.     {  
  236.         int _startCity = (int)newSolution.path[i] - 65;  
  237.         int _endCity = (int)newSolution.path[i+1] - 65;  
  238.         if (G.arcs[_startCity][_endCity] == -1)  
  239.         {  
  240.             return MAX_INT;  
  241.         }  
  242.         else{  
  243.             _length += G.arcs[_startCity][_endCity];  
  244.         }  
  245.     }  
  246.   
  247.     // 判断该路径是否能回到起始城市  
  248.     if (G.arcs[(int)newSolution.path[G.vex_num - 1] - 65][(int)newSolution.path[0]-65] == -1)  
  249.     {  
  250.         return MAX_INT;  
  251.     }  
  252.     else{  
  253.         _length += G.arcs[(int)newSolution.path[G.vex_num - 1] - 65][(int)newSolution.path[0]-65];  
  254.   
  255.         return _length;  
  256.     }  
  257.       
  258. }  
  259.   
  260. void Display(Graph G,TSP_solution bestSoluion){  
  261.     cout<<"****************************** TSP_SA - BestSolution ******************************"<<endl;  
  262.   
  263.     cout<<"最优路径,bestSoluion.path[ ] = ";  
  264.     for (int i = 0; i < G.vex_num; i++)  
  265.     {  
  266.         cout<<bestSoluion.path[i]<<"-->";  
  267.     }  
  268.     cout<<bestSoluion.path[G.vex_num]<<endl;  
  269.   
  270.     cout<<"最优路径,bestSoluion.length_path = "<<bestSoluion.length_path<<endl;;  
  271.   
  272.   
  273.     cout<<"***********************************************************************************"<<endl;  
  274. }  

九、参考文献:

  1. 本文相关代码以及数据:http://download.csdn.net/detail/houchaoqun_xmu/9740079
1 0