【问题标题】:Java A* Implementation IssuesJava A* 实施问题
【发布时间】:2015-04-23 15:49:43
【问题描述】:

我写了一个A*算法的实现,主要取自This wiki page,但是我有一个大问题;因为我相信我在计算路线时访问了太多节点,因此破坏了我的表现。几天来我一直试图找出问题所在,但我看不出有什么问题。请注意,我所有的数据结构都是自我实现的,但是我已经对其进行了测试并相信它们不是问题。 为了以防万一,我已经包含了我的优先队列实现。

closedVertices 是 Vertices 的哈希映射。

private Vertex routeCalculation(Vertex startLocation, Vertex endLocation, int routetype) 
{  
    Vertex vertexNeighbour;            

    pqOpen.AddItem(startLocation);

    while (!(pqOpen.IsEmpty())) 
    {       
        tempVertex = pqOpen.GetNextItem();


            for (int i = 0; i < tempVertex.neighbors.GetNoOfItems(); i++) //for each neighbor of tempVertex
            {
                currentRoad = tempVertex.neighbors.GetItem(i);
                currentRoad.visited = true;

                vertexNeighbour = allVertices.GetNewValue(currentRoad.toid);

                //if the neighbor is in closed set, move to next neighbor   
                checkClosed();              
                nodesVisited++;
                setG_Score();
                //checks if neighbor is in open set
                findNeighbour();
                //if neighbour is not in open set 
                if (!foundNeighbor || temp_g_score < vertexNeighbour.getTentativeDistance()) 
                {
                    vertexNeighbour.setTentativeDistance(temp_g_score);
                    //calculate H once, store it and then do an if statement to see if it's been used before - if true, grab from memory, else calculate.
                    if (vertexNeighbour.visited == false)
                        vertexNeighbour.setH(heuristic(endLocation, vertexNeighbour)); 


                        vertexNeighbour.setF(vertexNeighbour.getH() + vertexNeighbour.getTentativeDistance());                    

                    // if neighbor isn't in open set, add it to open set
                    if (!(foundNeighbor)) 
                    {    
                        pqOpen.AddItem(vertexNeighbour); 
                    }
                    else
                    {
                        pqOpen.siftUp(foundNeighbourIndex);
                    } 
                }

            }
        }
    }
    return null;
}

谁能看到我在哪里探索了太多节点?

另外,我尝试通过根据道路速度修改 F 来实现一种计算最快(定时)路线的方法。我说这是正确的方法吗? (我将道路的速度除以 100,因为否则执行需要很长时间)。

【问题讨论】:

    标签: java path path-finding a-star


    【解决方案1】:

    我发现了自己的错误;我已经实现了为每个节点计算错误的启发式方法 - 我有一个 IF 语句来查看是否已经计算了 H 但是我做错了,因此它实际上从未计算过某些节点的 H;导致过度的节点探索。我只是删除了这一行: if (vertexNeighbour.visited == false) 现在我有了完美的计算。

    但是,我仍在试图弄清楚如何计算时间方面的最快路线。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-16
      • 2013-09-16
      相关资源
      最近更新 更多