模拟退火

来源:互联网 发布:windows 10 一周年 编辑:程序博客网 时间:2024/04/29 03:20

模拟退火算法是用来求解最优化问题的算法。比如著名的TSP问题函数最大值最小值问题等等。接下来将以如下几个方面来详细介绍模拟退火算法。

 

Contents

 

   1. 模拟退火算法认识

   2. 模拟退火算法描述

   3. 费马点问题求解

   4. 最小包含球问题求解

   5. 函数最值问题求解

   6. TSP问题求解

 

 

1. 模拟退火算法认识

 

   爬山算法也是一个用来求解最优化问题的算法,每次都向着当前上升最快的方向往上爬,但是初始化不同可能

   会得到不同的局部最优值,模拟退火算法就可能跳出这种局部最优解的限制。模拟退火算法是模拟热力学系统

   中的退火过程。在退火过程中是将目标函数作为能量函数。大致过程如下

 

   初始高温 => 温度缓慢下降=> 终止在低温 (这时能量函数达到最小,目标函数最小)

 

   在热力学中的退火过程大致是变温物体缓慢降温而达到分子之间能量最低的状态。设热力学系统S中有有限个且

   离散的n个状态,状态的能量为,在温度下,经过一段时间达到热平衡,这时处于状态的概率为

 

                   

 

   模拟退火算法也是贪心算法,但是在其过程中引入了随机因素,以一定的概率接受一个比当前解要差的解,并且

   这个概率随着时间的推移而逐渐降低。

 

  

2. 模拟退火算法描述

 

   若,即移动后得到更优的解,那么总是接受改移动。

   若,即移动后得到更差的解,则以一定的概率接受该移动,并且这个概率随时间推移

   逐渐降低。这个概率表示为

 

   

 

   由于是退火过程,所以dE < 0,这个公式说明了温度越高出现一次能量差为dE的降温概率就越大,温度越低,

   出现降温的概率就越小,由于dE总是小于0,所以P(dE)取值在01之间。伪码如下

 

  

 

   

 

3. 费马点问题求解

 

   题目:http://poj.org/problem?id=2420

 

   题意:给n个点,找出一个点,使这个点到其他所有点的距离之和最小,也就是求费马点。

 

代码:

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <string.h>  
  3. #include <stdlib.h>  
  4. #include <stdio.h>  
  5. #include <time.h>  
  6. #include <math.h>  
  7.   
  8. #define N 1005  
  9. #define eps 1e-8     //搜索停止条件阀值  
  10. #define INF 1e99       
  11. #define delta 0.98   //温度下降速度  
  12. #define T 100        //初始温度  
  13.   
  14. using namespace std;  
  15.   
  16. int dx[4] = {0, 0, -1, 1};  
  17. int dy[4] = {-1, 1, 0, 0};  //上下左右四个方向  
  18.   
  19. struct Point  
  20. {  
  21.     double x, y;  
  22. };  
  23.   
  24. Point p[N];  
  25.   
  26. double dist(Point A, Point B)  
  27. {  
  28.     return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y));  
  29. }  
  30.   
  31. double GetSum(Point p[], int n, Point t)  
  32. {  
  33.     double ans = 0;  
  34.     while(n--)  
  35.         ans += dist(p[n], t);  
  36.     return ans;  
  37. }  
  38.   
  39. //其实我觉得这玩意儿根本不叫模拟退火  
  40. double Search(Point p[], int n)  
  41. {  
  42.     Point s = p[0];    //随机初始化一个点开始搜索  
  43.     double t = T;      //初始化温度  
  44.     double ans = INF;  //初始答案值  
  45.     while(t > eps)  
  46.     {  
  47.         bool flag = 1;  
  48.         while(flag)  
  49.         {  
  50.             flag = 0;  
  51.             for(int i = 0; i < 4; i++)    //上下左右四个方向  
  52.             {  
  53.                 Point z;  
  54.                 z.x = s.x + dx[i] * t;  
  55.                 z.y = s.y + dy[i] * t;  
  56.                 double tp = GetSum(p, n, z);  
  57.                 if(ans > tp)  
  58.                 {  
  59.                     ans = tp;  
  60.                     s = z;  
  61.                     flag = 1;  
  62.                 }  
  63.             }  
  64.         }  
  65.         t *= delta;  
  66.     }  
  67.     return ans;  
  68. }  
  69.   
  70. int main()  
  71. {  
  72.     int n;  
  73.     while(scanf("%d", &n) != EOF)  
  74.     {  
  75.         for(int i = 0; i < n; i++)  
  76.             scanf("%lf %lf", &p[i].x, &p[i].y);  
  77.         printf("%.0lf\n", Search(p, n));  
  78.     }  
  79.     return 0;  
  80. }  


 

题目:平面上给定n条线段,找出一个点,使这个点到这n条线段的距离和最小。

 

代码:

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <string.h>  
  3. #include <stdlib.h>  
  4. #include <stdio.h>  
  5. #include <time.h>  
  6. #include <math.h>  
  7.   
  8. #define N 1005  
  9. #define eps 1e-8     //搜索停止条件阀值  
  10. #define INF 1e99       
  11. #define delta 0.98   //温度下降速度  
  12. #define T 100        //初始温度  
  13.   
  14. using namespace std;  
  15.   
  16. int dx[4] = {0, 0, -1, 1};  
  17. int dy[4] = {-1, 1, 0, 0};  //上下左右四个方向  
  18.   
  19. struct Point  
  20. {  
  21.     double x, y;  
  22. };  
  23.   
  24. Point s[N], t[N];  
  25.   
  26. double cross(Point A, Point B, Point C)    
  27. {    
  28.     return (B.x - A.x) * (C.y - A.y) - (B.y - A.y) * (C.x - A.x);    
  29. }    
  30.   
  31. double multi(Point A, Point B, Point C)    
  32. {    
  33.     return (B.x - A.x) * (C.x - A.x) + (B.y - A.y) * (C.y - A.y);    
  34. }    
  35.   
  36. double dist(Point A, Point B)  
  37. {  
  38.     return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y));  
  39. }  
  40.   
  41. double GetDist(Point A, Point B, Point C)    
  42. {    
  43.     if(dist(A, B) < eps) return dist(B, C);    
  44.     if(multi(A, B, C) < -eps) return dist(A, C);    
  45.     if(multi(B, A, C) < -eps) return dist(B, C);    
  46.     return fabs(cross(A, B, C) / dist(A, B));    
  47. }    
  48.   
  49. double GetSum(Point s[], Point t[], int n, Point o)  
  50. {  
  51.     double ans = 0;  
  52.     while(n--)  
  53.         ans += GetDist(s[n], t[n], o);  
  54.     return ans;  
  55. }  
  56.   
  57. double Search(Point s[], Point t[], int n, Point &o)  
  58. {  
  59.     o = s[0];      
  60.     double tem = T;        
  61.     double ans = INF;  
  62.     while(tem > eps)  
  63.     {  
  64.         bool flag = 1;  
  65.         while(flag)  
  66.         {  
  67.             flag = 0;  
  68.             for(int i = 0; i < 4; i++)    //上下左右四个方向  
  69.             {  
  70.                 Point z;  
  71.                 z.x = o.x + dx[i] * tem;  
  72.                 z.y = o.y + dy[i] * tem;  
  73.                 double tp = GetSum(s, t, n, z);  
  74.                 if(ans > tp)  
  75.                 {  
  76.                     ans = tp;  
  77.                     o = z;  
  78.                     flag = 1;  
  79.                 }  
  80.             }  
  81.         }  
  82.         tem *= delta;  
  83.     }  
  84.     return ans;  
  85. }  
  86.   
  87. int main()  
  88. {  
  89.     int n;  
  90.     while(scanf("%d", &n) != EOF)  
  91.     {  
  92.         for(int i = 0; i < n; i++)  
  93.             scanf("%lf %lf %lf %lf", &s[i].x, &s[i].y, &t[i].x, &t[i].y);  
  94.         Point o;  
  95.         double ans = Search(s, t, n, o);  
  96.         printf("%.1lf %.1lf %.1lf\n", o.x, o.y, ans);    
  97.     }  
  98.     return 0;  
  99. }  


 

4. 最小包含球问题求解

 

   题目:http://poj.org/problem?id=2069

 

   题意:给定三维空间的n点,找出一个半径最小的球把这些点全部包围住。

 

代码:

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <string.h>  
  3. #include <stdio.h>  
  4. #include <math.h>  
  5.   
  6. #define N 55  
  7. #define eps 1e-7  
  8. #define T 100  
  9. #define delta 0.98  
  10. #define INF 1e99  
  11.   
  12. using namespace std;  
  13.   
  14. struct Point  
  15. {  
  16.     double x, y, z;  
  17. };  
  18.   
  19. Point p[N];  
  20.   
  21. double dist(Point A, Point B)  
  22. {  
  23.     return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y) + (A.z - B.z) * (A.z - B.z));  
  24. }  
  25.   
  26. double Search(Point p[], int n)  
  27. {  
  28.     Point s = p[0];  
  29.     double t = T;  
  30.     double ans = INF;  
  31.     while(t > eps)  
  32.     {  
  33.         int k = 0;  
  34.         for(int i = 0; i < n; i++)  
  35.             if(dist(s, p[i]) > dist(s, p[k]))  
  36.                 k = i;  
  37.         double d = dist(s, p[k]);  
  38.         ans = min(ans, d);  
  39.         s.x += (p[k].x - s.x) / d * t;  
  40.         s.y += (p[k].y - s.y) / d * t;  
  41.         s.z += (p[k].z - s.z) / d * t;  
  42.         t *= delta;  
  43.     }  
  44.     return ans;  
  45. }  
  46.   
  47. int main()  
  48. {  
  49.     int n;  
  50.     while(cin >> n && n)  
  51.     {  
  52.         for(int i = 0; i < n; i++)  
  53.             cin >> p[i].x >> p[i].y >> p[i].z;  
  54.         double ans = Search(p, n);  
  55.         printf("%.5lf\n", ans);  
  56.     }  
  57.     return 0;  
  58. }  


 

5. 函数最值问题求解

 

   题目:http://acm.hdu.edu.cn/showproblem.php?pid=2899

 

   题意:给出方程,其中,输入,求的最小值。

 

   分析:本题可以用经典的二分法求解,这种方法比较简单,就不说了。主要来说模拟退火做法。

 

代码:

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <string.h>  
  3. #include <stdlib.h>  
  4. #include <algorithm>  
  5. #include <stdio.h>  
  6. #include <time.h>  
  7. #include <math.h>  
  8.   
  9. #define ITERS 10  
  10. #define T 100  
  11. #define eps 1e-8  
  12. #define delta 0.98  
  13. #define INF 1e99  
  14.   
  15. using namespace std;  
  16.   
  17. double x[ITERS];  
  18.   
  19. int Judge(double x, double y)  
  20. {  
  21.     if(fabs(x - y) < eps) return 0;  
  22.     return x < y ? -1 : 1;  
  23. }  
  24.   
  25. double Random()  
  26. {  
  27.     double x = rand() * 1.0 / RAND_MAX;  
  28.     if(rand() & 1) x *= -1;  
  29.     return x;  
  30. }  
  31.   
  32. double F(double x, double y)  
  33. {  
  34.     return 6 * x * x * x * x * x * x * x + 8 * x * x * x * x * x * x + 7 * x * x * x + 5 * x * x - y * x;  
  35. }  
  36.   
  37. void Init()  
  38. {  
  39.     for(int i = 0; i < ITERS; i++)  
  40.         x[i] = fabs(Random()) * 100;  
  41. }  
  42.   
  43. double SA(double y)  
  44. {  
  45.     double ans = INF;  
  46.     double t = T;  
  47.     while(t > eps)  
  48.     {  
  49.         for(int i = 0; i < ITERS; i++)  
  50.         {  
  51.             double tmp = F(x[i], y);  
  52.             for(int j = 0; j < ITERS; j++)  
  53.             {  
  54.                 double _x = x[i] + Random() * t;  
  55.                 if(Judge(_x, 0) >= 0 && Judge(_x, 100) <= 0)  
  56.                 {  
  57.                     double f = F(_x, y);  
  58.                     if(tmp > f)   
  59.                         x[i] = _x;  
  60.                 }  
  61.             }   
  62.         }  
  63.         t *= delta;  
  64.     }  
  65.     for(int i = 0; i < ITERS; i++)  
  66.         ans = min(ans, F(x[i], y));  
  67.     return ans;  
  68. }  
  69.   
  70. int main()  
  71. {  
  72.     int t;  
  73.     scanf("%d", &t);  
  74.     while(t--)  
  75.     {  
  76.         double y;  
  77.         scanf("%lf", &y);  
  78.         srand(time(NULL));  
  79.         Init();  
  80.         printf("%.4lf\n", SA(y));  
  81.     }  
  82.     return 0;  
  83. }  


 

6. TSP问题求解

 

   TSP问题是一个NP问题,但是可以求近似解,通过模拟退火算法实现,代码如下

 

代码:

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <string.h>  
  3. #include <stdlib.h>  
  4. #include <algorithm>  
  5. #include <stdio.h>  
  6. #include <time.h>  
  7. #include <math.h>  
  8.   
  9. #define N     30      //城市数量  
  10. #define T     3000    //初始温度  
  11. #define EPS   1e-8    //终止温度  
  12. #define DELTA 0.98    //温度衰减率  
  13. #define LIMIT 10000   //概率选择上限  
  14. #define OLOOP 1000    //外循环次数  
  15. #define ILOOP 15000   //内循环次数  
  16.   
  17. using namespace std;  
  18.   
  19. //定义路线结构体  
  20. struct Path  
  21. {  
  22.     int citys[N];  
  23.     double len;  
  24. };  
  25.   
  26. //定义城市点坐标  
  27. struct Point  
  28. {  
  29.     double x, y;  
  30. };  
  31.   
  32. Path path;        //记录最优路径  
  33. Point p[N];       //每个城市的坐标  
  34. double w[N][N];   //两两城市之间路径长度  
  35. int nCase;        //测试次数  
  36.   
  37. double dist(Point A, Point B)  
  38. {  
  39.     return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y));  
  40. }  
  41.   
  42. void GetDist(Point p[], int n)  
  43. {  
  44.     for(int i = 0; i < n; i++)  
  45.         for(int j = i + 1; j < n; j++)  
  46.             w[i][j] = w[j][i] = dist(p[i], p[j]);  
  47. }  
  48.   
  49. void Input(Point p[], int &n)  
  50. {  
  51.     scanf("%d", &n);  
  52.     for(int i = 0; i < n; i++)  
  53.         scanf("%lf %lf", &p[i].x, &p[i].y);  
  54. }  
  55.   
  56. void Init(int n)  
  57. {  
  58.     nCase = 0;  
  59.     path.len = 0;  
  60.     for(int i = 0; i < n; i++)  
  61.     {  
  62.         path.citys[i] = i;  
  63.         if(i != n - 1)  
  64.         {  
  65.             printf("%d--->", i);  
  66.             path.len += w[i][i + 1];  
  67.         }  
  68.         else  
  69.             printf("%d\n", i);  
  70.     }  
  71.     printf("\nInit path length is : %.3lf\n", path.len);  
  72. }  
  73.   
  74. void Print(Path t, int n)  
  75. {  
  76.     printf("Path is : ");  
  77.     for(int i = 0; i < n; i++)  
  78.     {  
  79.         if(i != n - 1)  
  80.             printf("%d-->", t.citys[i]);  
  81.         else   
  82.             printf("%d\n", t.citys[i]);  
  83.     }  
  84.     printf("\nThe path length is : %.3lf\n", t.len);  
  85. }  
  86.   
  87. Path GetNext(Path p, int n)  
  88. {  
  89.     Path ans = p;  
  90.     int x = (int)(n * (rand() / (RAND_MAX + 1.0)));  
  91.     int y = (int)(n * (rand() / (RAND_MAX + 1.0)));  
  92.     while(x == y)  
  93.     {  
  94.         x = (int)(n * (rand() / (RAND_MAX + 1.0)));  
  95.         y = (int)(n * (rand() / (RAND_MAX + 1.0)));  
  96.     }  
  97.     swap(ans.citys[x], ans.citys[y]);  
  98.     ans.len = 0;  
  99.     for(int i = 0; i < n - 1; i++)  
  100.         ans.len += w[ans.citys[i]][ans.citys[i + 1]];  
  101.     cout << "nCase = " << nCase << endl;  
  102.     Print(ans, n);  
  103.     nCase++;  
  104.     return ans;  
  105. }  
  106.   
  107. void SA(int n)  
  108. {  
  109.     double t = T;  
  110.     srand(time(NULL));  
  111.     Path curPath = path;  
  112.     Path newPath = path;  
  113.     int P_L = 0;  
  114.     int P_F = 0;  
  115.     while(1)       //外循环,主要更新参数t,模拟退火过程  
  116.     {  
  117.         for(int i = 0; i < ILOOP; i++)    //内循环,寻找在一定温度下的最优值  
  118.         {  
  119.             newPath = GetNext(curPath, n);  
  120.             double dE = newPath.len - curPath.len;  
  121.             if(dE < 0)   //如果找到更优值,直接更新  
  122.             {  
  123.                 curPath = newPath;  
  124.                 P_L = 0;  
  125.                 P_F = 0;  
  126.             }  
  127.             else  
  128.             {  
  129.                 double rd = rand() / (RAND_MAX + 1.0);      
  130.                 if(exp(dE / t) > rd && exp(dE / t) < 1)   //如果找到比当前更差的解,以一定概率接受该解,并且这个概率会越来越小  
  131.                     curPath = newPath;  
  132.                 P_L++;  
  133.             }  
  134.             if(P_L > LIMIT)  
  135.             {  
  136.                 P_F++;  
  137.                 break;  
  138.             }  
  139.         }  
  140.         if(curPath.len < newPath.len)  
  141.             path = curPath;  
  142.         if(P_F > OLOOP || t < EPS)  
  143.             break;  
  144.         t *= DELTA;  
  145.     }  
  146. }  
  147.   
  148. int main()  
  149. {  
  150.     freopen("TSP.data""r", stdin);  
  151.     int n;  
  152.     Input(p, n);  
  153.     GetDist(p, n);  
  154.     Init(n);  
  155.     SA(n);  
  156.     Print(path, n);  
  157.     printf("Total test times is : %d\n", nCase);  
  158.     return 0;  
  159. }  


 

原创粉丝点击