1

我正在尝试为 Java 中的 TSP 设计一个 2-opt 本地搜索启发式,但我的算法似乎有缺陷。给定一个最近邻电路作为输入,它以某种方式使电路变得更糟。最近的邻居:http: //goo.gl/uI5X6;2-opt 十秒后:http: //goo.gl/5AGJ1。我的代码如下。我的实施有什么问题?Location[] location 只是“图”的节点列表,每个节点都有纬度和经度以及它与另一个节点之间的距离计算。

    public HamiltonianCircuit execute(Location[] locations) {
    long startTime = System.currentTimeMillis();
    while (true) {
        for (int i = 0; i < locations.length; i++) {
            for (int k = 0; k < locations.length; k++) {
                if (System.currentTimeMillis() - startTime >= 10000) {
                    return new HamiltonianCircuit(locations);
                }
                Location a = locations[i];
                Location b = locations[(i + 1) % locations.length];
                Location c = locations[k];
                Location d = locations[(k + 1) % locations.length];
                double distanceab = a.distanceBetween(b);
                double distancecd = c.distanceBetween(d);
                double distanceac = a.distanceBetween(c);
                double distancebd = b.distanceBetween(d);
                double change = (distanceab + distancecd) - 
                    (distanceac + distancebd);
                if (change > 0) {
                    locations[k] = a;
                    locations[i] = c;
                }
            }
        }
    }
}
4

2 回答 2

1

2-opt 用边 AC 和 BD 替换边 AB 和 CD,这意味着如果我们有部分电路 0 AB xyz CD 1 开始,我们在完成后想要 0 AC zyx BD 1。
您的代码将生成 0 CB xyz AD 1。

你想交换B和C,还需要把中间的所有东西都颠倒过来。

于 2013-07-26T14:52:24.827 回答
-2

Java 中可能的解决方案可能类似于:(注意:详细)

public HamiltonianCircuit execute(){
ArrayList<Vehicle> locations= new ArrayList<Locations>;
//populate array

    for (int i = 1; i < locations.size()-3; i++) {
            Location a = locations.get(i);
            Location b = locations.get(i+1);
            Location c = locations.get(i+2);
            Location d = locations.get(i+3);

            double distanceab = EuclidianDistance.pair(a, b);
            double distancebd = EuclidianDistance.pair(b, d);
            double distanceac = EuclidianDistance.pair(a, c);
            double distancecd = EuclidianDistance.pair(c, d);
            double abcd = distanceab+distancecd;
            double acbd = distanceac+distancebd;

            if(acbd<abcd){
                Collections.swap(locations, i+1, i+2);
                System.out.println("swapped"+vehicles);
            }
    }
    Locations.setDistance(EuclidianDistance.Collection(locations));
    return locations;
}
于 2016-03-05T12:08:38.267 回答