2

我正在努力了解大学的 TSP 计划,我不得不承认我发现它非常困难。

基本上我有一个 Lat 值数组和一个 Lng 值数组,使用haversine,我已经将距离转移到一个矩阵中。
现在从这里去哪里?
我必须找到最短的距离,走遍所有 80 个城镇。我试图想出最好的方法来做到这一点。我应该做一个最近邻算法吗?我应该只存储排列并计算距离。或者,还有更好的方法?我不得不承认,我觉得这对于一个一年级的学生来说有点困难!无论如何,这是我的代码,太糟糕了,小程序等都给了我们。

public class Brain {

    //These are the names of the 80 towns and their north and west GPS co-ordinates

    static double north[] = {53.855,52.794,54.350,53.433,52.992,54.117,53.328,54.800,54.863,55.071,54.502,54.343,51.746,54.660,51.680,54.597,53.091,53.175,55.136,52.831,53.976,53.944,53.861,53.991,51.622,52.354,51.897,54.996,54.322,53.714,53.348,54.009,54.500,52.085,53.345,52.846,52.502,54.345,53.272,52.677,53.728,53.106,52.648,52.059,51.708,53.783,54.851,54.957,55.053,52.665,52.447,53.727,53.197,51.904,54.750,52.131,53.382,52.266,54.248,53.116,53.522,52.863,52.396,54.210,52.451,54.590,53.633,52.714,54.267,53.245,54.830,52.679,52.474,52.268,53.515,53.267,52.257,53.800,52.334,51.952};
    static double west[] = {-6.538,-6.165,-6.655,-7.950,-6.987,-9.167,-8.219,-7.790,-6.284,-6.508,-8.190,-6.260,-8.735,-5.670,-9.453,-5.930,-7.913,-6.525,-7.456,-6.932,-6.719,-8.095,-9.299,-7.360,-8.886,-7.712,-8.470,-7.307,-5.703,-6.350,-6.260,-6.405,-6.770,-7.640,-7.051,-8.981,-6.566,-7.640,-9.049,-6.292,-6.878,-6.065,-7.256,-9.507,-8.531,-8.917,-5.811,-7.720,-6.946,-8.624,-9.486,-7.800,-8.567,-8.957,-6.610,-8.642,-6.591,-8.270,-6.971,-7.324,-7.338,-8.200,-6.945,-5.882,-9.055,-7.290,-8.183,-8.869,-8.483,-9.306,-7.470,-7.814,-8.162,-9.696,-8.851,-7.500,-7.129,-9.533,-6.458,-7.846};
    String names[] = {"Ardee","Arklow","Armagh","Athlone","Athy","Ballina","Ballinasloe","Ballybofe","Ballymena","Ballymoney","Ballyshannon","Banbridge","Bandon","Bangor","Bantry","Belfast","Birr","Blessington","Buncrana","Carlow","Carrickmacross","Carrick-On-Shannon","Castlebar","Cavan","Clonakilty","Clonmel","Cork","Derry","Downpatrick","Drogheda","Dublin","Dundalk","Dungannon","Dungarvan","Edenderry","Ennis","Enniscorthy","Enniskillen","Galway","Gorey","Kells","Kilcoole","Kilkenny","Killarney","Kinsale","Knock","Larne","Letterkenny","Limavady","Limerick","Listowel","Longford","Loughrea","Macroom","Magherafelt","Mallow","Maynooth","Mitchelstown","Monaghan","Mountmellick","Mullingar","Nenagh","New-Ross","Newcastle","Newcastle-West","Omagh","Roscommon","Shannon","Sligo","Spiddal","Strabane","Thurles","Tipperary","Tralee","Tuam","Tullamore","Waterford","Westport","Wexford","Youghal"};
    static double[][] matrix = new double[80][80];
    boolean visit[]=new boolean[80];
    boolean valid = true;

    public static void fillmatrix (){
        double tote = 0;
        for(int i=1;i<80;i++){
            for(int j=1;j<80;j++){
                matrix[i][j]=getDistance(north[i],west[j],north[j],west[j]);
            }
        }
    }

    public String compute () {          
        String solution ="";
        for (int i=0;i<80;i++){
            solution+=(char)(i+40);
        }
        solution+="Anything you add on after the 80 characters will be " + 
            "printed in the textbox (e.g. you can compute the distance)";
        return solution;    
    }

    public static double getDistance(double lat1, double lon1, double lat2, double lon2){
        double R = 6371;
        double dLat = Math.toRadians((lat2-lat1));
        double dLon = Math.toRadians((lon2-lon1)); 
        double a = Math.sin(dLat/2) * Math.sin(dLat/2) + 
            Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
            Math.sin(dLon/2) * Math.sin(dLon/2); 
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
        double d = R * c;         
        return d;
    }    
}
4

3 回答 3

1

除了最小的实例之外,检查所有排列将非常昂贵。

幸运的是,有一大堆著名的 TSP 启发式算法。任你选

于 2012-05-03T17:14:05.450 回答
1

解决最近邻启发式的最佳方法是将算法分解为子问题。

  1. 随机选择一个城市。
  2. 找到最近的未访问城市并前往那里。
  3. 是否还有未访问的城市?如果是,请重复步骤 2。
  4. 返回第一个城市。

接下来,解决每个子问题。

  1. 在名称数组中选择一个随机值
  2. 通过遍历北和西数组并计算距离来检查所有城市到随机城市的距离。以数组形式存储访问过的城市。
  3. 重复 2
  4. 返回第一个城市

然后,考虑伪代码并尝试编写 Java 代码。

于 2012-05-03T17:41:04.053 回答
0

关于源代码

首先,我可能会建议您将城市坐标的定义更改为 City 类,并且由于您的城市是预定义的,您可能希望将其“导出”到外部文件中并在文件开始时加载它。例如,

public class City{
    public double North;
    public double West;
    public String Name;
    public double getDistanceToCity(City target){
        double R = 6371;
        double dLat = Math.toRadians((target.North-this.North));
        double dLon = Math.toRadians((target.West-this.West)); 
        double a = Math.sin(dLat/2) * Math.sin(dLat/2) + 
            Math.cos(Math.toRadians(this.North)) * Math.cos(Math.toRadians(this.West)) *
            Math.sin(dLon/2) * Math.sin(dLon/2); 
        double d = R * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));         
        return d;}}

然后,例如,在将文件读入“ArrayList Cities”后,您将通过以下方式对矩阵进行 bild:

double[][] distances = new double[Cities.size()][Cities.size()];
int i=0;
int j=0;
for(City start:Cities){
    for(City end:Cities){
        distances[i][j]=start.getDistanceToCity(end);
        j++;}
    i++;}

并得到相同的矩阵。在这种情况下,您可以更改输入数据的大小,例如用 10 个城市测试您的算法的正确性,毕竟在 80 个城市上运行良好。此外,在这种情况下,您将自己看到这个问题的难度 - 简单在 40 和 41 城市运行它...

接下来,关于算法...

这是 NP 难题,因此您的排列将工作很长时间。我会推荐 Held–Karp 算法,该算法速度更快,但实现起来非常困难,并且需要大量内存。(例如,建议的方法 if (n*n*2^n),对于 n=80 是 10^27,排列是 n!,对于 n=80 是 10^121 - 我的意思是解决任务所需的操作) .

无论如何,目前最好的精确解决方案是线性规划(不记得确切的方法,但是......)。可能你会发现一些考虑使用图形方法的算法。

于 2012-05-04T14:14:54.973 回答