旅行推销员距离误算

时间:2014-05-03 22:09:33

标签: java algorithm nearest-neighbor traveling-salesman

在我的空闲时间过去一周左右的旅行推销员优化的最近邻居解决方案中,距离计算存在一些问题。基本上,正在发生的事情是当我打印出我的结果时,"总计"与功能检查器小程序中给出的解决方案相比,它计算(包含行程中行进的总距离)的输入不正确。但是,它并没有以任何明显的方式错误地返回。它总是在+/- 300km的范围内。有时是一个比它应该更大的结果,有时它是更低的。我已经尝试了硬编码最后一步以循环回到第一个城镇,这是在代码的当前迭代中,它使事情更接近,但发生了相同的行为。

以下是问题的概述以及我所做的事情:   - 我有一个输入文件,其中包含160行,每对行是爱尔兰随机城镇的纬度和经度,80个城镇   - 此输入文件随后在主方法中使用hasrsine函数创建大小为[80] [80]的单位矩阵,其中每个元素是从i到j的距离。您可以假设此部分已正确计算。   - 半影类将地球半径设为6378.1km,并在其他问题中正确计算距离。   - 然后将此单位矩阵传递给TSP方法进行计算。

以下是导致问题的源代码,我确信它发生在这里的某个地方,而不是在我的主要方法或者半身像中,我只是不能指出它:

//Main calculation method that takes in the adjacency matrix
    public void tsp(double[][] adjacency_matrix)
    {
        int last = 0;
        int count = 0;

        //Array that will store if a town has been previously visited, first town will be visited from the start
        boolean[] visited = new boolean[numberOfNodes];

        //Creates an element variable which takes in the current town were at on the stack
        //also creates a shortest variable to store the shortest distance each time
        //lastly an i to run through the loop with
        int element; 
        int shortest = 0;
        int i = 0;

        //Sets the default minimum value to max size
        double min = 0.0;

        //Total distance variable to be used
        double total = 0.0;
        double tempDistance = 0.0;

        //Used to say we found a shortest route this time
        boolean minFlag = false;

        //Run Nearest Neighbour for each starting town
        for(int k=0; k<numberOfNodes; k++)
        {
            visited[k] = true;

            //Pushes the first town onto the stack
            stack.push(k);

            //Always starting at the first town, so it prints this out
            System.out.print(k+1 + ".");

            //While the stack isn't empty, we see what town were at, start from town 0
            //and calculate all distances from town element to town i MAX
            while (!stack.isEmpty())
            {
                element = stack.peek();
                i = 0;

                //This min is used as a default biggest distance
                min = Double.MAX_VALUE;

                //Go up to the number of nodes
                while (i < numberOfNodes)
                {
                    //If the distance from this town to another is big enough and isn't already visited
                    if (adjacency_matrix[element][i] > 0.1 && visited[i] == false)
                    {
                        //Then if this distance is smaller than the last
                        if (min > adjacency_matrix[element][i])
                        {
                            //The minimum distance is now from element to i, shortest town is i, and the minflag is set
                            min = adjacency_matrix[element][i];
                            shortest = i;
                            minFlag = true;

                            tempDistance = adjacency_matrix[element][i];
                        }

                    }

                    i++;
                }

                //If we found a shortest route
                if (minFlag)
                {
                    //we visited it
                    visited[shortest] = true;

                    //It's now the town on the top of stack
                    stack.push(shortest);

                    //Print this out to the screen
                    System.out.print(shortest +1 + ".");

                    //Adds this distance into the total distance travelled
                    total += tempDistance;
                    tempDistance = 0.0;

                    for(int l=0; l<numberOfNodes; l++)
                    {
                        if(visited[l] == true)
                        {
                            count++;
                        }

                        if(count == 80)
                        {
                            last = stack.peek();
                        }

                        count = 0;
                    }

                    //Reset
                    minFlag = false;

                    //go back up the loops
                    continue;
                }

                stack.pop();
            }

            total +=adjacency_matrix[last][k];
            System.out.println("\nTotal distance was " + (total) + " km approximately.\n");

            //reset all variables for next run
            last = 0;
            count = 0;

            for(int reset=0; reset< numberOfNodes; reset++)
            {
                visited[reset] = false;
            }

            element =0; 
            shortest = 0;
            min = 0.0;
            total = 0.0;
            tempDistance = 0.0;
            minFlag = false;            
        }

    }

这是算法正在做的事情:   - 它采用单位矩阵和图中的节点数。   - 它使用堆栈来查看它目前所处的城镇。   - 它还使用布尔数组来查看它到目前为止的访问位置   - 它在一个控制循环中运行80次,使每个城镇成为每次迭代的起点。   - 然后主计算部分检查从当前城镇到另一个城镇的矩阵中的最短距离,然后将其推入堆栈,标记它被访问并从那里重复。   - 最后一点,我硬编码将城镇80添加回城镇1作为最后一点距离。 - 然后打印出距离和路线,然后返回并检查从2镇的路线。

任何帮助都会受到赞赏,如果评论中没有任何内容可以让我知道。

0 个答案:

没有答案