A *算法运行正常,但并不完美。怎么了?

时间:2010-05-19 09:02:03

标签: actionscript-3 path-finding a-star

这是我的节点网格:

alt text http://img168.imageshack.us/img168/7536/astartwrong.jpg

我正在使用A *寻路算法移动一个物体。它通常可以正常工作,但有时会出错:

  • 当从3移动到1时,它正确地通过2.当从1到3时,它会经过4。
  • 当在3和5之间移动时,它通过4向任一方向而不是通过6
  • 的短路

有什么不对?这是我的代码(AS3):

    public static function getPath(from:Point, to:Point, grid:NodeGrid):PointLine {

        // get target node
        var target:NodeGridNode = grid.getClosestNodeObj(to.x, to.y);

        var backtrace:Map = new Map();
        var openList:LinkedSet = new LinkedSet();
        var closedList:LinkedSet = new LinkedSet();

        // begin with first node
        openList.add(grid.getClosestNodeObj(from.x, from.y));

        // start A*
        var curNode:NodeGridNode;
        while (openList.size != 0) {

            // pick a new current node
            if (openList.size == 1) {
                curNode = NodeGridNode(openList.first);
            }
            else {
                // find cheapest node in open list
                var minScore:Number = Number.MAX_VALUE;
                var minNext:NodeGridNode;
                openList.iterate(function(next:NodeGridNode, i:int):int {
                    var score:Number = curNode.distanceTo(next) + next.distanceTo(target);
                    if (score < minScore) {
                        minScore = score;
                        minNext = next;
                        return LinkedSet.BREAK;
                    }
                    return 0;
                });
                curNode = minNext;
            }

            // have not reached
            if (curNode == target) break;
            else {
                // move to closed
                openList.remove(curNode);
                closedList.add(curNode);

                // put connected nodes on open list
                for each (var adjNode:NodeGridNode in curNode.connects) {
                    if (!openList.contains(adjNode) && !closedList.contains(adjNode)) {
                        openList.add(adjNode);
                        backtrace.put(adjNode, curNode);
                    }
                }
            }
        }

        // make path
        var pathPoints:Vector.<Point> = new Vector.<Point>();
        pathPoints.push(to);
        while(curNode != null) {
            pathPoints.unshift(curNode.location);
            curNode = backtrace.read(curNode);
        }
        pathPoints.unshift(from);
        return new PointLine(pathPoints);

    }

NodeGridNode :: distanceTo()

    public function distanceTo(o:NodeGridNode):Number {
        var dx:Number = location.x - o.location.x;
        var dy:Number = location.y - o.location.y;
        return Math.sqrt(dx*dx + dy*dy);
    }

2 个答案:

答案 0 :(得分:1)

我在这里看到的问题是

if (!openList.contains(adjNode) && !closedList.contains(adjNode))

可能是这样的情况:adjNode可以更容易(更短)通过当前节点到达,尽管它是从先前的另一个节点到达的,这意味着它在openList中。

答案 1 :(得分:0)

发现错误:

                openList.iterate(function(next:NodeGridNode, i:int):int {
                    var score:Number = curNode.distanceTo(next) + next.distanceTo(target);
                    if (score < minScore) {
                        minScore = score;
                        minNext = next;
                        return LinkedSet.BREAK;
                    }
                    return 0;
                });

return LinkedSet.BREAK(在常规循环中的行为类似于break语句)不应该存在。它会导致打开列表中的第一个节点始终被选中,而不是最便宜的节点。