Unable to implement A Star in java

一笑奈何 提交于 2019-11-30 14:49:52

I recently threw this A* code together to solve a Project Euler problem. You'll have to fill in the details for a matrix of Node objects. Use it at your own risk, however I can say it solved the problem :)

public class Node {
    List<Node> neighbors = new ArrayList<Node>();
    Node parent;
    int f;
    int g;
    int h;
    int x;
    int y;
    int cost;
}

public List<Node> aStar(Node start, Node goal) {
    Set<Node> open = new HashSet<Node>();
    Set<Node> closed = new HashSet<Node>();

    start.g = 0;
    start.h = estimateDistance(start, goal);
    start.f = start.h;

    open.add(start);

    while (true) {
        Node current = null;

        if (open.size() == 0) {
            throw new RuntimeException("no route");
        }

        for (Node node : open) {
            if (current == null || node.f < current.f) {
                current = node;
            }
        }

        if (current == goal) {
            break;
        }

        open.remove(current);
        closed.add(current);

        for (Node neighbor : current.neighbors) {
            if (neighbor == null) {
                continue;
            }

            int nextG = current.g + neighbor.cost;

            if (nextG < neighbor.g) {
                open.remove(neighbor);
                closed.remove(neighbor);
            }

            if (!open.contains(neighbor) && !closed.contains(neighbor)) {
                neighbor.g = nextG;
                neighbor.h = estimateDistance(neighbor, goal);
                neighbor.f = neighbor.g + neighbor.h;
                neighbor.parent = current;
                open.add(neighbor);
            }
        }
    }

    List<Node> nodes = new ArrayList<Node>();
    Node current = goal;
    while (current.parent != null) {
        nodes.add(current);
        current = current.parent;
    }
    nodes.add(start);

    return nodes;
}

public int estimateDistance(Node node1, Node node2) {
    return Math.abs(node1.x - node2.x) + Math.abs(node1.y - node2.y);
}

I dont know if you are trying only to use simple types, or if you just didn't think about it, but you need to have a PriorityQueue to get your A* working.

A good way to think is that you put your startpoint into a priority queue with distance 0, and then start a loop that only stops when the prioriy queue is empty.

In the loop you take the min-node out, and check to see if it hasnt been open before, or if it has, if you have now found a shorter way to it. If either these are true, you add the distance to the new node, add the edge/from-square to a map, and then add the distance + heuristic to the priority queue.

I have written this to work on a grid of booleans, and a constant conversion between 1D and 2D arrays, but I hope it is readable:

public void AStarRoute()
{
    gridDist = new double[rows][cols];
    System.out.println("Start of AStarRoute");
    MinPriorityQueue pq = new MinPriorityQueue(rows * cols);
    edgeTo = new HashMap<Integer, Integer>();

    gridDist[x1Dto2D(start)][y1Dto2D(start)] = 0;
    pq.insert(start, 0);
    int from;
    while (!pq.isEmpty()) {
        from = pq.delMin();
        int x = x1Dto2D(from);
        int y = y1Dto2D(from);
        for (int i = -1; i <= 1; i++) {
            for (int j = -1; j <= 1; j++) {
                int newX = x + i;
                int newY = y + j;
                if (newX >= 0 && newY >= 0 && newX < cols && newY < rows && !(i == 0 && j == 0)) {
                    if (grid[newX][newY]) {
                        //System.out.println("NewDist: " + gridDist[newX][newY] + " - OldDist+dist: " + (gridDist[x][y] + ((Math.abs(i) == Math.abs(j)) ? 1.4 : 1.0)) + ":" + (int)(gridDist[x][y] + ((Math.abs(i) == Math.abs(j)) ? 1.4 : 1.0)));
                        if (!edgeTo.containsKey(convert2Dto1D(newX, newY)) || gridDist[newX][newY] > (gridDist[x][y] + ((Math.abs(i) == Math.abs(j)) ? 14 : 10))) {
                            gridDist[newX][newY] = (int)(gridDist[x][y] + ((Math.abs(i) == Math.abs(j)) ? 14 : 10));
                            maxDistToEnd = (int)Math.max(maxDistToEnd, gridDist[newX][newY]);
                            edgeTo.put(convert2Dto1D(newX, newY), convert2Dto1D(x, y));
                            pq.insert(convert2Dto1D(newX, newY), gridDist[newX][newY] + (int)Math.sqrt(Math.pow((newX - x1Dto2D(end))*10, 2) + Math.pow((newY - y1Dto2D(end))*10, 2)));
                            if(convert2Dto1D(newX, newY) == end){
                                System.out.println("End found at (" + newX + ", " + newY + ")");
                                paintGridDist = true;

                                route = new ArrayList<Integer>();
                                int n = convert2Dto1D(newX, newY);
                                route.add(n);
                                do{
                                    n = edgeTo.get(n);
                                    route.add(n);
                                }while(start != n);

                                repaint();
                                return;
                            }
                        }
                    }
                }
            }
        }
    }

    paintGridDist = true;
    repaint();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!