# 模拟退火算法解旅行商（TSP）问题

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <stdio.h>
#include <time.h>
#include <math.h>

#define N     30      //城市数量
#define T     3000    //初始温度
#define EPS   1e-8    //终止温度
#define DELTA 0.98    //温度衰减率
#define LIMIT 10000   //概率选择上限
#define OLOOP 1000    //外循环次数
#define ILOOP 15000   //内循环次数

using namespace std;

//定义路线结构体
struct Path
{
int citys[N];
double len;
};

//定义城市点坐标
struct Point
{
double x, y;
};

Path path;        //记录最优路径
Point p[N];       //每个城市的坐标
double w[N][N];   //两两城市之间路径长度
int nCase;        //测试次数

double dist(Point A, Point B)
{
return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y));
}

void GetDist(Point p[], int n)
{
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
w[i][j] = w[j][i] = dist(p[i], p[j]);
}

void Input(Point p[], int &n)
{
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%lf %lf", &p[i].x, &p[i].y);
}

void Init(int n)
{
nCase = 0;
path.len = 0;
for (int i = 0; i < n; i++)
{
path.citys[i] = i;
if (i != n - 1)
{
printf("%d--->", i);
path.len += w[i][i + 1];
}
else
printf("%d\n", i);
}
printf("\nInit path length is : %.3lf\n", path.len);
}

void Print(Path t, int n)
{
printf("Path is : ");
for (int i = 0; i < n; i++)
{
if (i != n - 1)
printf("%d-->", t.citys[i]);
else
printf("%d\n", t.citys[i]);
}
printf("\nThe path length is : %.3lf\n", t.len);
}

// 随机交换2个城市的位置
Path GetNext(Path p, int n)
{
// Modify by DD: 确保x！=y
int x = 0, y = 0;
while (x == y)
{
x = (int)(n * (rand() / (RAND_MAX + 1.0)));
y = (int)(n * (rand() / (RAND_MAX + 1.0)));
}

Path ans = p;
swap(ans.citys[x], ans.citys[y]);
ans.len = 0;
for (int i = 0; i < n - 1; i++)
ans.len += w[ans.citys[i]][ans.citys[i + 1]];
//cout << "nCase = " << nCase << endl;
//Print(ans, n);
nCase++;
return ans;
}

// 模拟退火
void SA(int n)
{
double t = T;
srand(time(NULL));
Path curPath = path;
Path newPath = path;
int P_L = 0;    // 连续找到更差结果的次数
int P_F = 0;    // while循环次数
while (1)       //外循环，主要更新参数t，模拟退火过程
{
for (int i = 0; i < ILOOP; i++)    //内循环，寻找在一定温度下的最优值
{
newPath = GetNext(curPath, n);
double dE = newPath.len - curPath.len;
if (dE < 0)   //如果找到更优值，直接更新
{
curPath = newPath;
P_L = 0;
P_F = 0;
}
else
{
double rd = rand() / (RAND_MAX + 1.0);

// Modify by DD: dE取负数才有可能接受更差解,否则e>1
double e = exp(-dE / t);
if ( e > rd && e < 1)   //如果找到比当前更差的解，以一定概率接受该解，并且这个概率会越来越小
curPath = newPath;
P_L++;
}
if (P_L > LIMIT)
{
P_F++;
break;
}
}

// Modify by DD: 记录全局最优解
if (curPath.len < path.len)
path = curPath;
if (P_F > OLOOP || t < EPS)
break;
t *= DELTA;
}
}

int main()
{
freopen("TSP.txt", "r", stdin);
int n;
Input(p, n);
GetDist(p, n);
Init(n);

SA(n);

Print(path, n);
printf("Total test times is : %d\n", nCase);
return 0;
}

27
41 94
37 84
53 67
25 62
7  64
2  99
68 58
71 44
54 62
83 69
64 60
18 54
22 60
83 46
91 38
25 38
24 42
58 69
71 71
74 78
87 76
18 40
13 40
82  7
62 32
58 35
45 21

1. 记录全局最优解

记录最优解时，原文使用的是

if (curPath.len < newPath.len)
path = curPath;

if(curPath.len < path .len)
path= curPath;

(…这里不加文字，markdown有序列表编号会出错…)
2. 连续搜索到最差结果的处理

3. 必须以一定概率接受更差结果