【问题标题】:Setting up an A* search设置 A* 搜索
【发布时间】:2013-04-27 08:26:50
【问题描述】:

我正在编写程序的一小部分,我必须在其中编写寻路算法。该函数采用所谓的“路线”,每个路线都定义了 2D 空间中的起点和终点。该算法需要找到最短和最有效(在一定程度上)的路径(从原点开始)以通过这些路线,从而最大限度地减少总体行驶的总距离。

我做了一些研究,并开始了一条我认为可行的道路。到目前为止,我已经将路线转换为一个有向图,它全部链接起来,就好像它是一个理想化的路线图一样。然后我尝试在该图上执行 A* 搜索。我使用的启发式计算了剩余的“路线”的总距离,而我使用的距起点 (G) 值的距离只是到达当前点的累积距离。这适用于某些输入,但其他输入根本不返回路径,我似乎无法弄清楚原因。

我的启发式方法是否有可能是错误的,这会导致某个地方的计算错误,或者 A* 过程本身是否更可能是错误的?还是我在这里完全走错了路?

我会将 getPath 函数放在下面(用 Java 编写)以防万一。

提前致谢。

public ArrayList<Vector2> getPath()
{
    PriorityQueue<SearchNode> openList = new PriorityQueue<SearchNode>(10, new SearchNodeComparator());
    ArrayList<SearchNode> closedList = new ArrayList<SearchNode>();

    map.startJobs();
    searchDepth = 0;

    SearchNode start = searchableGraph.getNode(new Vector2(0, 0));
    int goalsLeft = map.getJobCount();

    start.setDistanceTraveled(0);

    openList.add(start);

    while (openList.size() > 0)
    {
        SearchNode current = openList.peek();
        searchDepth++;

        if (map.isJobEndPoint(current.getValue()))
        {
            map.completeJob(current.getValue());
            goalsLeft--;

        }

        if (reachedGoalState(current, searchableGraph.getNodes().size()))
        {
            return getFinalPath(current);
        }
        else
        {
            ArrayList<SearchNode> neighbours = getNeighbours(current);

            for (int i = 0; i < neighbours.size(); i++)
            {
                SearchNode node = neighbours.get(i);        
                System.out.print("Inspecting node" + node.getValue().toString());

                float distanceTraveled = current.getDistanceTraveled() + getDistance(current.getValue(), node.getValue());

                float heuristic = heuristic(node);

                if (!openList.contains(node) && !closedList.contains(node))
                {

                    node.setDistanceTraveled(distanceTraveled);

                    node.setDistanceToGoal(distanceTraveled + heuristic);

                    node.setParent(current);

                    openList.add(node);
                }
                else if(openList.contains(node))
                {
                    if (node.getDistanceTraveled() <= distanceTraveled)
                    {

                        node.setDistanceToGoal(distanceTraveled + heuristic);


                        node.setParent(current);
                    }

                }
            }

            openList.remove(current);
            closedList.add(current);
        }
    }

    return new ArrayList<Vector2>();
}

【问题讨论】:

    标签: java path-finding a-star heuristics


    【解决方案1】:

    我使用的启发式计算“路线”的总距离

    A*使用的启发式必须是admissible;也就是说,它必须永远超过​估计到终点的距离

    如果我正确理解你的描述,你的启发式是不可接受的,所以不能保证有效。

    【讨论】:

      猜你喜欢
      • 2011-08-16
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多