branch and bound(分支定界)算法求解TSP旅行商问题

转载自:分枝定界算法求解TSP

 

整个程序如下所示:

      Image

其中各个模块说明如下:

- Timer:计时用。

- TSPInstanceReader:TSPLIB标准算例读取用。

- PriorityQueue:优先队列。

- Node:搜索树的节点。

- City:保存城市的坐标,名字等。

- BranchBound_TSP:BB算法主程序。

 

branch and bound 过程

搜索树的节点定义,节点定义了原问题的solution子问题的solutionNode节点定义如下:

public class Node {    private ArrayList<Integer> path;    private double bound;    private int level;        public double computeLength(double[][] distanceMatrix) {        // TODO Auto-generated method stub        double distance = 0;        for(int i=0;i<this.getPath().size()-1;i++){            distance = distance + distanceMatrix[this.getPath().get(i)][this.getPath().get(i+1)];        }        return distance;    }

其余不重要的接口略过。如下:

- path:保存该节点目前已经走过的城市序列。

- bound:记录该节点目前所能达到的最低distance。

- level:记录节点处于搜索树的第几层。

- computeLength:记录当前城市序列的distance。

可能大家还没理解节点是如何分支的,看一张图大家就懂了。我们知道TSP问题的一个solution是能用一个序列表示城市的先后访问顺序,比如现在有4座城市(1,2,3,4):

              Image

图中每个节点的数字序列就是path保存的。大家都看到了吧,其实分支就是一个穷枚举的过程。

 

相对于穷举,分支定界算法的优越之处就在于其加入了定界过程,在分支的过程中就砍掉了某些不可能的支,减少了枚举的次数,大大提高了算法的效率。如下:

                Image

分支定界算法的主过程如下:

private static void solveTSP(double[][] distanceMatrix) {

        int totalCities = distanceMatrix.length;
        ArrayList<Integer> cities = new ArrayList<Integer>();
        for (int i = 0; i < totalCities; i++) {
            cities.add(i);
        }
        ArrayList<Integer> path;
        double initB = initbound(totalCities, distanceMatrix);
        
        Node v = new Node(new ArrayList<>(), 0, initB, 0);
        queue.add(v);
        queueCount++;
        while (!queue.isEmpty()) {
            v = queue.remove();
            if (v.getBound() < shortestDistance) {
                Node u = new Node();
                u.setLevel(v.getLevel() + 1);
                for (int i = 1; i < totalCities; i++) {
                    path = v.getPath();
                    if (!path.contains(i)) {
                        u.setPath(v.getPath());
                        path = u.getPath();
                        path.add(i);
                        u.setPath(path);
                        if (u.getLevel() == totalCities - 2) {
                            // put index of only vertex not in u.path at the end
                            // of u.path
                            for (int j = 1; j < cities.size(); j++) {
                                if (!u.getPath().contains(j)) {
                                    ArrayList<Integer> temp = new ArrayList<>();
                                    temp = u.getPath();
                                    temp.add(j);
                                    u.setPath(temp);
                                }
                            }
                            path = u.getPath();
                            path.add(0);
                            u.setPath(path);
                            if (u.computeLength(distanceMatrix) < shortestDistance) {
                                shortestDistance = u.computeLength(distanceMatrix);// implement
                                shortestPath = u.getPath();
                            }
                        } else {
                            u.setBound(computeBound(u, distanceMatrix, cities));
                            //u.getBound()获得的是不完整的解,如果一个不完整的解bound都大于当前最优解,那么完整的解肯定会更大,那就没法玩了。
                            //所以这里只要u.getBound() < shortestDistance的分支
                            if (u.getBound() < shortestDistance) {
                                queue.add(u);
                                queueCount++;
                            }
                            else {
                                System.out.println("currentBest = "+shortestDistance+" cut bound >>> "+u.getBound());
                            }
                        }
                    }
                }
            }
        }
    }

1. 首先initbound利用贪心的方式获得一个bound,作为初始解。

2. 而后利用优先队列遍历搜索树,进行branch and bound算法。对于队列里面的任意一个节点,只有(v.getBound() < shortestDistance)条件成立我们才有分支的必要。不然将该支砍掉。

3. 分支以后判断该支是否到达最底层,这样意味着我们获得了一个完整的解。那么此时就可以更新当前的最优解了。

4. 如果没有到达最底层,则对该支进行定界操作。如果该支的bound也比当前最优解还要大,那么也要砍掉的

 

然后讲讲定界过程,TSP问题是如何定界的呢?

private static double computeBound(Node u, double[][] distanceMatrix, ArrayList<Integer> cities) {
        double bound = 0;
        ArrayList<Integer> path = u.getPath();
        for (int i = 0; i < path.size() - 1; i++) {
            bound = bound + distanceMatrix[path.get(i)][path.get(i + 1)];
        }
        int last = path.get(path.size() - 1);
        List<Integer> subPath1 = path.subList(1, path.size());
        double min;
        //回来的
        for (int i = 0; i < cities.size(); i++) {
            min = Integer.MAX_VALUE;
            if (!path.contains(cities.get(i))) {
                for (int j = 0; j < cities.size(); j++) {
                    if (i != j && !subPath1.contains(cities.get(j))) {
                        if (min > distanceMatrix[i][j]) {
                            min = distanceMatrix[i][j];
                        }
                    }
                }
            }
            if (min != Integer.MAX_VALUE)
                bound = bound + min;
        }
        
        //出去的
        min = Integer.MAX_VALUE;
        for (int i = 0; i < cities.size(); i++) {
            if (/*cities.get(i) != last && */!path.contains(i) && min > distanceMatrix[last][i]) {
                min = distanceMatrix[last][i];
            }
        }
        bound = bound + min;
        //System.out.println("bound = "+bound);
        return bound;
    }

我们知道,每个节点保存的城市序列可能不是完整的解。bound的计算方式:bound = 当前节点path序列的路径距离 + 访问下一个城市的最短路径距离 + 从下一个城市到下下城市(有可能是起点)的最短路径距离。

比如城市节点5个{1,2,3,4,5}。当前path = {1,2},那么:

 

- 当前节点path序列的路径距离 = d12  

- 访问下一个城市的最短路径距离 = min (d2i), i in {3,4,5}   (与下一站相关)

- 从下一个城市到下下城市(有可能是起点)的最短路径距离=min (dij), i in {3,4,5} , j in {3,4,5,1}, i != j 。  (与剩余站有关)

注意这两个是可以不相等的。

Image

3、运行说明

目前分支定界算法解不了大规模的TSP问题,10个节点以内吧差不多。input里面有算例,可以更改里面的DIMENSION值告诉算法需要读入几个节点。

Image

更改算例在main函数下面,改名字就行,记得加上后缀。

Image

 

 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值