A *实现中的逻辑错误

时间:2014-04-29 20:07:53

标签: java graph-algorithm path-finding

我一直在使用其他人的例子'将A *寻路算法的实现作为拐杖来帮助我编写我的第一个实现。我在我发现的一个更具可读性的例子中遇到了一些逻辑问题。

我不是来挑选这段代码,真的,我想弄清楚我是对的,还是我误解了这里的机制。如果我需要的话回顾一下A *的工作原理,但如果这段代码不正确,我需要找到其他资料来学习。

在我看来,此处发现的逻辑在两个地方存在缺陷

for(Node neighbor : current.getNeighborList()) {
    neighborIsBetter;
    //if we have already searched this Node, don't bother and continue to the next 
    if (closedList.contains(neighbor))
        continue;

    //also just continue if the neighbor is an obstacle
    if (!neighbor.isObstacle) {

        // calculate how long the path is if we choose this neighbor as the next step in the path 
        float neighborDistanceFromStart = (current.getDistanceFromStart() + map.getDistanceBetween(current, neighbor));

        //add neighbor to the open list if it is not there
        if(!openList.contains(neighbor)) {
-->         openList.add(neighbor);
            neighborIsBetter = true;
            //if neighbor is closer to start it could also be better
-->     } else if(neighborDistanceFromStart < current.getDistanceFromStart()) {
            neighborIsBetter = true;
        } else {
            neighborIsBetter = false;
        }
        // set neighbors parameters if it is better
        if (neighborIsBetter) {
            neighbor.setPreviousNode(current);
            neighbor.setDistanceFromStart(neighborDistanceFromStart);
            neighbor.setHeuristicDistanceFromGoal(heuristic.getEstimatedDistanceToGoal(neighbor.getX(), neighbor.getY(), map.getGoalLocationX(), map.getGoalLocationY()));
        }
    }
}

source

我标记的第一行( - &gt;),对我来说似乎不对。如果您查看implementation of the list being used(下方),则会根据heuristicDistanceFromGoal进行排序,.addpublic int compareTo(Node otherNode) { float thisTotalDistanceFromGoal = heuristicDistanceFromGoal + distanceFromStart; float otherTotalDistanceFromGoal = otherNode.getHeuristicDistanceFromGoal() + otherNode.getDistanceFromStart(); if (thisTotalDistanceFromGoal < otherTotalDistanceFromGoal) { return -1; } else if (thisTotalDistanceFromGoal > otherTotalDistanceFromGoal) { return 1; } else { return 0; } } 下面设置几行。

} else if(neighborDistanceFromStart < current.getDistanceFromStart()) {

我标记的第二行应始终评估为false。它写着:

if((current.getDistanceFromStart() + map.getDistanceBetween(current, neighbor)) < current.getDistanceFromStart())

可以简化为:

if(map.getDistanceBetween(current, neighbor) < 0)

再次:

getDistanceBetween()

除了{{1}}之外应该总是返回正值(see here)。

我是开场还是场外?

1 个答案:

答案 0 :(得分:1)

首先,你大部分时间都在轨道上。我强烈怀疑你发布的代码仍处于开发阶段并且存在一些问题。但是,你的距离假设仍然是正面的。 A *是图搜索算法,并且边通常也可以具有负权重。因此,我认为他们试图实施最一般的情况。 openList.add对我来说似乎完全没问题。您的队列应按启发式距离排序。只需检查维基页面https://en.wikipedia.org/wiki/A_star,相关的行是f_score [neighbor]:= g_score [neighbor] + heuristic_cost_estimate(neighbor,goal)。而且,这背后的主要思想是,你总是低估距离(可接受的启发式);因此,如果找到目标,则未探索的节点都不是最佳的。您可以在http://en.wikipedia.org/wiki/Admissible_heuristic

了解更多信息

其次,如果你想要一个稳定的AI代码库,你可以简单地使用http://code.google.com/p/aima-java/。它是AIMA(人工智能现代方法)中算法的实现