Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 优化A*算法_Java_Arrays_Algorithm_A Star - Fatal编程技术网

Java 优化A*算法

Java 优化A*算法,java,arrays,algorithm,a-star,Java,Arrays,Algorithm,A Star,我最近为基于代理的模型实现了A*算法,该模型使用2D数组。搜索的目的是为代理提供指向目标位置的位置列表。我的实现是有效的,但是有时当我执行算法时,它会返回一条仍然连接到主路径的替代路径。我不明白它为什么这么做。代码如下: public boolean generatePath(位置startLocation,位置goalLocation){ setUpStartingPoint(startLocation,goalLocation);//在搜索之前设置所有内容 布尔路径存在=假; int循环=

我最近为基于代理的模型实现了A*算法,该模型使用2D数组。搜索的目的是为代理提供指向目标位置的位置列表。我的实现是有效的,但是有时当我执行算法时,它会返回一条仍然连接到主路径的替代路径。我不明白它为什么这么做。代码如下:

public boolean generatePath(位置startLocation,位置goalLocation){
setUpStartingPoint(startLocation,goalLocation);//在搜索之前设置所有内容
布尔路径存在=假;
int循环=0;
openList.add(startNode);//将开始节点放入openList(初始起点)
while(pathExist==false){
如果(openList.isEmpty()==false){//要检查的位置更多
System.out.println(“步骤:+循环);
System.out.println(当前节点);
System.out.println(openList);
系统输出打印项次(关闭列表);
重新排序列表(openList);
Node lowestFvalueNode=openList.remove(0);//获取openList中F值最低的节点
setParent(currentNode);
currentNode=最低的fValueNode;
add(最下面的fvalue节点);
if(检查节点列表(关闭列表、目标节点)){
System.out.println(“找到”);
计算当前路径(currentNode);
pathExist=true;
}
否则{
ArrayList currentNodeAdjNodes=getAdjacentNodes(currentNode);
对于(节点adjNode:currentNodeAdjNodes){
if(checkNodeInList(closedList,adjNode)){//if节点在closedList中
}
否则{
if(checkNodeInList(openList,adjNode)==false){
ComputeNodeDevalues(adjNode);//计算节点的G、H和F值
adjNode.setParent(currentNode);//将父节点设置为当前节点
添加(adjNode);//将节点添加到打开列表
}
否则{
节点ActualAdjNodeInvest=getNodeInList(openList,adjNode);
int currentMovementCostToNode=currentNode.getGvalue()+getMovementCostToNode(currentNode,adjNode);
if(currentMovementCostToNode
如果路径也是最优的,那么选择哪一条并不重要。有一种优化算法的方法值得一试:使用*和波束搜索。Beam搜索将减少所需的内存空间。

“它返回一个仍然连接到主路径的替代路径”有点含糊不清。关于这些路径长度,你能告诉我们什么?这条替代路径是否次优?参见编辑的帖子我添加了一个打印路径的截图这里有一个截图链接:其他位置的路径基本上导致了一个死胡同我不明白为什么阿尔沃伊这些位置的路径基本上导致了一个死胡同,我认为这是因为它在openlist中具有相同F值的两个节点之间进行拾取。如果两个节点都有相同的F值,那么确定下一个要拾取的节点的最佳原因是什么?@user3112844:如果两个节点具有相同的F=G(x)+H(x),其中G是到达节点x所做的功/距离,H是从x到soln的剩余功/距离的启发式值,则相当于拾取其中一个。如果你的启发式是不可接受的,那么你就不能保证你的解决方案是最优的,尽管你在实践中仍然可以做得很好。谢谢你的解释,安迪,为了在2D中生成一条最优路径,让你的启发式是可接受的最好的等待时间是什么,只要不沿对角线移动,就可以使用曼哈顿距离,这对于最短路径问题是最佳的。这是(#来自目标的列数+#来自目标的行数)。基本上,一个可接受的启发式永远不会高估实现目标的成本(它是“乐观的”)
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;
    }