Algorithm A * works fine, but not perfect. What's wrong?

This is my mesh of nodes:

alt text

I move an object around it using the A * pathfinding algorithm. This usually works fine, but sometimes it doesn't work properly:

  • When going from 3 to 1, it correctly passes through 2. When going from 1 to 3, it passes through 4.
  • When moving between 3 and 5, it goes through 4 in any direction, not a shorter path through 6

What could be wrong? Here is my code (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


a source to share


2 answers


Found the error:

                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

(which acts like a break statement in a regular loop) shouldn't be. This leads to the fact that the first node in the open list is always selected, and not the cheapest.

0


a source


The problem I see here is the line

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

      



It might be that adjNode might be easier (shorter) to reach the current node, even though it was reached from another node earlier, which means it is in openList.

+1


a source







All Articles