在A *寻路算法中做父母?

问题描述

因此,我正在尝试将A *寻路算法实现为C ++程序。我大部分都想通了,该算法最终找到了结束的方法,但是没有在过程中创建其他随机的不必要的路径,如下所示:

S is starting node,E is end node,p is the path,and X are the walls to avoid

S是开始节点,E是结束节点,p是路径,X是要避开的墙。 当我追溯从端节点到其父节点的路径时,它还包括 作为父节点,所有其他未到达终点但在技术上较近的节点,例如 节点(2,3)。我的问题是,在什么条件下节点应该成为另一个节点的父节点?如何确保仅追溯最佳路径?这是递归程序的一个片段 功能:

void Node::RecursiveScan(NodeMap &map) {

    this->ScanAdjacents(map.NodeMapContainer);

    for (auto &adjacentNode : this->AdjacentNodes) {
        cout << "start" << endl;
        cout << this->GetDistanceCost() << " " << this->distanceToEnd << " " << this->GetNodeCoords().first << "," << this->GetNodeCoords().second << endl;
        if (adjacentNode->GetNodeType() != 1 && adjacentNode->nodeScanned == false) {
            adjacentNode->CalcDistances(map);
            adjacentNode->nodeScanned = true;
            map.ScannedNodes.emplace(adjacentNode);
        }
    }
    for (auto &scannedNode : map.ScannedNodes) { // ScannedNodes is an ordered multiset of shared ptrs (open set)
        if (scannedNode->GetNodeType() == 2 && *map.found == false) {
            *map.found = true;
            map.EndNode->ParentNode.reset(this);
            map.VisitedNodes.emplace(map.EndNode);
            return;
        }
        else if (scannedNode->GetNodeType() == 0 && *map.found == false && scannedNode->nodeVisited == false) {
            scannedNode->ParentNode.reset(this); // Parenting here
            scannedNode->nodeVisited = true;
            map.VisitedNodes.emplace(scannedNode); // VisitedNodes is an ordered multiset of shared ptrs (close set)
            scannedNode->RecursiveScan(map);
        }
}
}

该算法在更简单的直接路径中也可以正常工作:

enter image description here

解决方法

您保持路径成本的运行总和。当您命中的节点的路径成本低于其已包含的路径成本时,即更新parent时。完成整个计算后,parent链基本上应该可以使您从目标节点返回到起始节点。

当我过去完成路由算法时,我使用优先级队列来提供“下一个节点”提要,而不是使用任何类型的递归(并且将启发式方法应用于优先级评分,以哄骗首选节点首先弹出)。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...