【问题标题】:Optimizing A* Algorithm优化 A* 算法
【发布时间】:2015-02-04 13:23:32
【问题描述】:

我最近为我的基于代理的模型实现了 A* 算法,该模型使用二维数组。搜索的目的是为代理提供通往目标位置的位置列表。我的实现有效,但是有时当我执行算法时,它会返回一个仍然连接到主路径的替代路径。我不明白它为什么这样做。将其编码如下: http://pbrd.co/1DFaeIr

public boolean generatePath(Location startLocation, Location goalLocation) {
        setUpStartingPoint(startLocation, goalLocation); //Set up everything before search 
        boolean pathExist = false;
        int loop = 0;

        openList.add(startNode); //Put start node in openList (Initial starting point)
        while(pathExist == false) {
            if(openList.isEmpty() ==  false) { //More locations to check
                System.out.println("Step: " + loop);
                System.out.println(currentNode);
                System.out.println(openList);
                System.out.println(closedList);

                reOrderList(openList);
                Node lowestFvalueNode = openList.remove(0); //Get the node with the lowest F value in openList
                lowestFvalueNode.setParent(currentNode);
                currentNode = lowestFvalueNode;
                closedList.add(lowestFvalueNode);

                if(checkNodeInList(closedList, goalNode)) {
                    System.out.println("Found");
                    computeCurrentPath(currentNode);
                    pathExist = true;
                }
                else {
                    ArrayList<Node> currentNodeAdjNodes = getAdjacentNodes(currentNode);
                    for(Node adjNode : currentNodeAdjNodes) {
                        if(checkNodeInList(closedList, adjNode)) { //If node is in the closedList

                        }
                        else {
                            if(checkNodeInList(openList, adjNode) == false) {
                                computeNodeValues(adjNode); //Compute the G,H and F values of node
                                adjNode.setParent(currentNode); //Set the nodes parent as current node
                                openList.add(adjNode); //Add node to open list
                            }
                            else {
                                Node actualAdjNodeInOpenList = getNodeInList(openList, adjNode);
                                int currentMovementCostToNode = currentNode.getGvalue() + getMovementCostToNode(currentNode, adjNode);

                                if(currentMovementCostToNode < adjNode.getGvalue()) {
                                    computeNodeValues(adjNode);
                                    adjNode.setParent(currentNode);
                                    reOrderList(openList);
                                }
                            }
                        }
                    }
                }

                loop++;
            }
            else {
                pathExist = false;
                System.out.println("Path doesn't exist");
                return false;
            }
        }
        System.out.println(path);
        return pathExist;
    }

【问题讨论】:

  • “它返回一个仍然连接到主路径的替代路径”有点模棱两可。关于这些路径长度,你能告诉我们什么?这种替代路径不是最优的吗?
  • 查看编辑后的帖子我添加了正在打印的路径的屏幕截图
  • 这里是截图链接:pbrd.co/1DFaeIr

标签: java arrays algorithm a-star


【解决方案1】:

如果路径也是最优的,那么你选择哪一个都没有关系。有一种优化算法的方法值得一试:将 A* 与 Beam 搜索结合使用。束搜索将减少所需的内存空间。

【讨论】:

  • 它的其他位置基本上导致死胡同我不明白为什么alvoirth
  • 它给出的位置基本上会导致死胡同,我认为这是因为它在 openlist 中具有相同 F 值的两个节点之间进行选择。如果它们都具有相同的 F 值,那么确定接下来要选择哪个节点的最佳原因是什么?
  • @user3112844:如果两个节点具有相同的 F = G(x) + H(x) 其中 G 是到达节点 x 的工作/距离,H 是工作/距离的启发式从 x 到 soln 剩余,则相当于选择其中之一。如果您的启发式方法不可接受,那么您就无法保证解决方案的最优性,尽管您在实践中仍然可以做得很好。
  • 感谢 Andy 的解释,最好等待让您的启发式算法可以接受,以便生成最佳路径
  • 在 2D 中,只要不沿对角线移动,就可以使用曼哈顿距离,这对于最短路径问题是最佳的。这是(目标中的列数 + 目标中的行数)。基本上,一个可接受的启发式永远不会高估实现目标的成本(它是“乐观的”)
猜你喜欢
  • 1970-01-01
  • 2011-03-27
  • 1970-01-01
  • 2016-07-30
  • 2023-02-03
  • 2023-03-29
  • 1970-01-01
  • 2020-02-09
相关资源
最近更新 更多