利用模拟退火算法求解TSP问题

 示例代码下载 /Files/gpcuster/TSP.zip

介绍

组合优化算法用于解决在一个解空间非常大的情况下快速地求解近似解。这类算法可用于资源管理,操作管理,质量控制等等问题,并且可以在有效的时间里给出一个足够好的近似解。

常见的启发算法有:simulated annealing, tabu search, harmony search, scatter search, genetic algorithms, ant colony optimization等等。在这篇文章中,我们将讨论simulated annealing(模拟退火算法)和他在解决TSP(旅行商)问题中的实际应用。

背景

模拟退火算法来源于固体退火原理,将固体加温至充分高,再让其徐徐冷却,加温时,固体内部粒子随温升变为无序状,内能增大,而徐徐冷却时粒子渐趋有序,在每个温度都达到平衡态,最后在常温时达到基态,内能减为最小。所以,模拟退火算法的目标就是通过一个目标函数来随机搜索。(这也是所有启发搜索算法的一个共性)

模拟退火算法的优势在于能够有效地避免局部最优问题。在这里,我们现在讨论的这个算法并不总是拒绝使总体目标函数值上升(变差)的结果,而是依据这样一个概率函数:

P = exp (-∆f/T)

T代表温度控制参数(类比问题),∆f是新参数的结果和当前最优解的差值。

这个概率函数是由Metropolis准则而得来的。

旅行商问题

旅行商问题就是指旅行商按一定的顺序访问N个城市的每个城市,使得每个城市都能被访问且仅能被访问一次,最后回到起点,而使花费的代价最小。

为了解决该问题,我们需要了解下面两个问题:

1 产生新解的策略: 我们将随机交换已有解中的任意2个城市的顺序,来产生新的访问顺序(新解)。
2 目标函数 (用于求最小值): 按照对所有城市的访问顺序来求解路径的长度。

代码的使用

TravellingSalesmanProblem.cs是最要的类. 调用Anneal()方法将计算出访问各个城市的最短路径。

            TravellingSalesmanProblem problem  =   new  TravellingSalesmanProblem();
            problem.FilePath 
=   " Cities.txt " ;
            problem.Anneal();


下面的代码说明了模拟退火算法的基本流程:

 1          /// <summary>
 2        /// Annealing Process
 3        /// </summary>

 4          public   void  Anneal()
 5          {
 6            int iteration = -1;
 7
 8            double temperature = 10000.0;
 9            double deltaDistance = 0;
10            double coolingRate = 0.9999;
11            double absoluteTemperature = 0.00001;
12
13            LoadCities();
14
15            double distance = GetTotalDistance(currentOrder);
16
17            while (temperature > absoluteTemperature)
18            {
19                nextOrder = GetNextArrangement(currentOrder);
20
21                deltaDistance = GetTotalDistance(nextOrder) - distance;
22
23                //if the new order has a smaller distance
24                //or if the new order has a larger distance but satisfies Boltzman condition then accept the arrangement
25                if ((deltaDistance < 0|| (distance > 0 && Math.Exp(-deltaDistance / temperature) > random.NextDouble()))
26                {
27                    for (int i = 0; i < nextOrder.Count; i++)
28                        currentOrder[i] = nextOrder[i];
29
30                    distance = deltaDistance + distance;
31                }

32
33                //cool down the temperature
34                temperature *= coolingRate;
35
36                iteration++;
37            }

38
39            shortestDistance = distance;
40        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值