模拟退火算法解旅行商(TSP)问题

来源:互联网 发布:淘宝贷款入口在哪里 编辑:程序博客网 时间:2024/04/30 13:15

该帖子的代码主要转自模拟退火算法1.
该文对模拟退火算法作了较好的分析,不过该文中举例的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;}

数据文件如下:

2741 9437 8453 6725 627  642  9968 5871 4454 6283 6964 6018 5422 6083 4691 3825 3824 4258 6971 7174 7887 7618 4013 4082  762 3258 3545 21

修改过的地方,代码中写了注释。
其中几个重要的、需要解释的地方有如下几个:

  1. 记录全局最优解

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

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

这样显然记录的是当前温度中的最优解。更合理的做法是记录全局出现过的最优解,即

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

(…这里不加文字,markdown有序列表编号会出错…)
2. 连续搜索到最差结果的处理
代码中的 P_L用于标示连续搜索到比当前更差结果的次数。代码中,如果连续发生了LIMIT次(代码中是10000次),就意味着当前解空间已经找不到更好的结果了。
多次执行代码测试会发现,原文代码更换城市次数却仅仅在1w次左右,最终路径在450附近,这就说明了原文代码会陷入局部最优解。即使每次把P_L复位为0,执行次数是上去了,但是仍然会陷入局部最优解。
3. 必须以一定概率接受更差结果
能够跳出全局最优,正是模拟退火算法最大的优势所在。
原来,原文中计算的是exp(dE / t),注意dE是为正的,因此exp(dE / t)恒大于1. 因此更差的结果永远不会被选中。而由于交换操作中每次只交换curPath的一对城市,因此陷入了curPath的局部最优解。
修改为exp(-dE / t)后,curPath有一定概率被替换。测试结果发现,更换城市次数在700w次左右,最终路径在370附近。显然获得了全局最优解。

其他不错的资料,如随机模拟的基本思想和常用采样方法(sampling)2.


  1. http://blog.csdn.net/xianlingmao/article/details/7768833. ↩
  2. http://blog.csdn.net/xianlingmao/article/details/7768833. ↩
0 0
原创粉丝点击