具有最近邻算法的Java ECLIPSE最近TSP

时间:2016-12-12 22:35:01

标签: java eclipse algorithm traveling-salesman

我在论坛上提出的第一个问题,在这里发布的许多问题都有用处。我坚持目前的任务:我需要用最近邻算法制作一个TSP,然后存储最短的路由"在数组中。

到目前为止,我提出了这个问题:

public class Q2b {

    int route[] = new int[179];//create route array
    private int nFacilities; // 179.
    private int nLocations; // 179.
    private DistanceMatrix dm; // ass_1distances.
    private boolean[] isIncluded;//create boolean for customer cluster open/closed.

    public Q2b(int nClusters, String dmFile) {

        this.isIncluded = new boolean [179];//set the length of the array isIncluded equal to n locations.
        for (int i= 0; i < nLocations; i++)
            isIncluded[i]=false;//set the value of customer cluster included to false
        this.nFacilities = nClusters;
        this.nLocations = nClusters;
        dm = new DistanceMatrix(nFacilities, nLocations);
        dm.loadFromFile(dmFile);
        Q2bsolution();
    }


    public void setIncluded (int location, boolean isIncluded)  {
        this.isIncluded[location]=isIncluded; //set the location to is included.
    }

    public double Q2bsolution() {
        double totalDistance = 0;
        for (int i = 0; i < this.nLocations; i++) {
            while (isIncluded[i]==false)    {

                double min = Double.MAX_VALUE;

                for (int j = 0; j < this.nFacilities; j++) {

                    if (i != j) {
                        if (this.dm.getDistance(i, j) < min) {
                            min = this.dm.getDistance(i, j);
                            this.isIncluded[i]=true;
                            route[i]=j;


                        }
                    }
                }
                System.out.println(route[i]);
                //System.out.println("Distance to closest connection to customer cluster " + i + " = " + min);
            }

        }
        return totalDistance;
    }
}

为了引导您完成我的代码而无需自己解决问题:

nLocationnFacilities指的是我的距离矩阵,有179个位置供应,设施可以放在相同的179个位置。首先,我将所有位置设置为isIncluded = false,以声明阵列路径中没有添加任何位置。然后我说,对于我的179个位置,我从第0行开始采用最小距离。我的代码现在给我以下输出:

2 2 0 8 20 4 ...等等,这意味着到位置0的最近位置是位置2,到位置1的最近位置是位置2,到位置2的最近位置是位置0 ....等等。但是,我需要弄清楚如何从位置0开始,将位置2作为最短距离,然后从位置2继续以获得与该位置的最短距离,等等。

我遇到的问题是,一旦我计算出来,我就不知道如何继续使用最短的位置,你能帮我解决一下吗?

亲切的问候,

斯蒂芬

0 个答案:

没有答案