计算科学与人工智能分享 http://blog.sciencenet.cn/u/kinglandom 专注计算科学与人工智能的进展、原理及实现。

博文

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

已有 7196 次阅读 2008-10-30 11:53 |个人分类:技术前沿| 模拟退火算法, TSP问题

介绍

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

常见的启发算法有: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        }


https://blog.sciencenet.cn/blog-85556-44721.html

上一篇:人工神经网络的发展及其哲理
下一篇:(转载)IEEE Transactions on Communications Magazine总编访谈
收藏 IP: .*| 热度|

0

发表评论 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-26 15:44

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部