如何使用A *显示和获取路线

时间:2015-02-15 11:32:10

标签: java android algorithm

我有一个Pathfinding算法正在运行,但问题是我无法获得主要课程的指示。换句话说,每次我不显示它输出空值的方向。

这是我的代码。

Astar.java

package path;

import java.util.ArrayList;
import java.util.Collections;

import path.Node.Directions;
import map.Area;

public class Astar {

    public ArrayList<Node> open;
    public ArrayList<Node> closed;
    public Area area;
    public Node start;
    public Node finish;

    public Astar() {
        this.area = new Area(5, 5);
        this.start = new Node(1, 1, area);
        this.finish = new Node(4, 4, area);
        this.open = new ArrayList<Node>();
        this.closed = new ArrayList<Node>();
    }

    public void calculate() {

        start.distanceFromStart = 0;
        closed.clear();
        open.clear();
        open.add(start);
        Node current = null;

        while (open.size() != 0) {
            current = open.get(0);
            if (current.equals(finish)) {
                break;
            }
            open.remove(current);
            closed.add(current);
            for (Node n : current.getNeighborsList()) {
                boolean neighborIsBetter;

                if (closed.contains(n))
                    continue;

                if (!n.isObstacle) {
                    double neighborDistanceFromStart = current.distanceFromStart + getDistanceBetween(current, n);
                    if (!open.contains(n)) {
                        open.add(n);
                        Collections.sort(open);
                        neighborIsBetter = true;
                    }
                    else if (neighborDistanceFromStart < current.distanceFromStart)
                        neighborIsBetter = true;
                    else
                        neighborIsBetter = false;
                    if (neighborIsBetter) {
                        n.parent = current;
                        searchPath(current);
                        n.distanceFromStart = neighborDistanceFromStart;
                        n.heuristicDistanceFromGoal = getEstimatedDistanceToGoal(n, finish);
                    }
                    System.out.println("Distance from start: " + neighborDistanceFromStart);
                }
                else if(n.isObstacle){
                    closed.add(n);
                }

            }

        }
        if (current != null)
            reconstructPath(current);
    }
    public void searchPath(Node node){

        System.out.printf("\nNode: (%d, %d)\n", node.x, node.y);

    }

    public void reconstructPath(Node node) {

        while (!(node.parent == null)) {
            System.out.printf("\nNode: (%d, %d)\n", node.x, node.y);
            node = node.parent;

        }
    }

    private double getDistanceBetween(Node n1, Node n2) {
        if ((n1.x == n2.x) || (n1.y == n2.y))
            return 1;
        else
            return 1.9;
    }

    private double getEstimatedDistanceToGoal(Node start, Node finish) {
        //Manhattan Calculation closest heuristic
        double dx = start.x - finish.x;
        double dy = start.y - finish.y;
        return dx + dy;

    }

    public static void main(String[] a) {
        Astar astar = new Astar();
        astar.calculate();
        System.out.println("End");
    }
}

Node.java

package path;

import java.util.ArrayList;
import map.Area;

public class Node implements Comparable<Node> {
    public boolean isClosed;
    public boolean isGoal;
    public boolean isObstacle;
    public boolean isVisited;
    public boolean isStart;
    public Node parent;
    public Node north;
    public Node south;
    public Node east;
    public Node west;

    public Area map;

    public int x;
    public int y;

    public double heuristicDistanceFromGoal;
    public double distanceFromStart;

    public enum Directions {
        NORTH, SOUTH, EAST, WEST
    };

    public Node(int x, int y, Area map) {
        this.x = x;
        this.y = y;
        this.isVisited = false;
        this.distanceFromStart = Integer.MAX_VALUE;
        this.isObstacle = false;
        this.isStart = false;
        this.isGoal = false;
        this.map = map;
    }

    public boolean equals(Node node) {
        return (this.x == node.x) && (this.y == node.y);
    }

    public void setNode(Directions dir, Node node) {
        Node temp = getDirectionalNode(dir);
        ArrayList<Node> list = this.getNeighborsList();
        if (list.contains(temp))
            list.remove(temp);
        list.add(node);
        setDirectionalNode(dir, node);
    }

    public Node getDirectionalNode(Directions dir) {    
        switch (dir) {
            case NORTH:
                return north;
            case SOUTH:
                return south;
            case EAST:
                return east;
            case WEST:
                return west;        
        }
        return null;
    }

    public void setDirectionalNode(Directions dir, Node node) { 
        switch (dir) {
            case NORTH:
                this.north = node;
            case SOUTH:
                this.south = node;
            case EAST:
                this.east = node;
            case WEST:
                this.west = node;
        }
    }

    public ArrayList<Node> getNeighborsList() {
        ArrayList<Node> neighborList = new ArrayList<Node>();

        if (!(y == (map.height + 1))) {
            neighborList.add(map.getNode(x, y + 1));
        }

        return neighborList;
    }

    @Override
    public int compareTo(Node other) {
        double totalDistanceFromGoal = this.distanceFromStart - this.heuristicDistanceFromGoal;
        double otherDistanceFromGoal = other.distanceFromStart - other.heuristicDistanceFromGoal;
        if (totalDistanceFromGoal < otherDistanceFromGoal)
            return -1;
        if (otherDistanceFromGoal > totalDistanceFromGoal)
            return 1;
        return 0;
    }

}

Area.java

package map;

import java.util.ArrayList;
import path.Node;

public class Area {
    public int width;
    public int height;
    public ArrayList<ArrayList<Node>> map;

    public Area(int w, int h) {
        this.width = w;
        this.height = h;

        createMap();

    }

    public Node getNode(int x, int y) {
        return map.get(y).get(x);
    }

    private void createMap() {
        Node node;
        map = new ArrayList<ArrayList<Node>>();
        for (int y = 0; y < height; y++) {
            map.add(new ArrayList<Node>());
            for (int x = 0; x < width; x++) {
                node = new Node(x, y, this);
                map.get(y).add(node);
            }
        }
    }

}

输出:

Traceback: (4, 4) Traceback: (4, 3) Traceback: (4, 2) 
Traceback: (4, 1) Traceback: (3, 1)Traceback: (2, 1)

问题:无指示:当我调用node.north

时,我得到null

1 个答案:

答案 0 :(得分:0)

public Node parent;
public Node north;
public Node south;
public Node east;
public Node west;

public enum Directions {
    NORTH, SOUTH, EAST, WEST
};

这似乎很奇怪。我想你想这样做:

public Node parent;
public Direction north;
public Direction south;
public Direction east;
public Direction west;

public enum Direction {
    NORTH, SOUTH, EAST, WEST
};

整件事让我感到困惑,你从不设定轴承。节点通常没有方向。 如果您想要当前头像从最后一个节点到当前节点的方向。所以它的轴承基本上。 这可能有帮助吗? http://mathforum.org/library/drmath/view/55417.html