简体   繁体   English

A *图形搜索启发式

[英]A* Graph Search Good Heuristic

I'm implementing the A* path find algorithm for a simple multi exit maze with varying distances, however I am unable to find an appropriate heuristic, it seems to perform a breadth-first search. 我正在为具有不同距离的简单多出口迷宫实现A *路径查找算法,但是我找不到合适的启发式方法,似乎执行了广度优先搜索。

Cost is initial set to 1 成本初始设置为1

Heres my attempt: 这是我的尝试:

public void search(Node startNode, Node goalNode){
    System.out.println("Search Started");
    boolean found = false;
    boolean noPath = false;

    Queue<Node> open = new PriorityQueue<Node>(10,new Comparator<Node>(){

        @Override
        public int compare(Node t, Node t1) {
            return Double.compare(t.getCost(), t1.getCost());
        }
    });

    visited[startNode.getX()][startNode.getY()] = true;
    order[startNode.getX()][startNode.getY()] = 0;
    startNode.setCost(h(startNode, goalNode));
    open.add(startNode);

    while (found == false && noPath == false) {
        if (open.isEmpty()) {
            noPath = true;

        }else{

             Node current = open.remove();

            if(current.getX() == goalNode.getX() && current.getY() == goalNode.getY()){

                System.out.println("found Node");
                printOrder();
                found = true;
                break;


            }else{
                for (int i = 0; i < 4; i++){
                    Node nextNode = getAction(current.getX(),current.getY());
                    System.out.println("getting node:" + nextNode.getX() + " " + nextNode.getY());
                    int g2 = current.getCost()+cost+h(current, goalNode);
                    System.err.println(g2);
                    nextNode.setCost(g2);

                    if (nextNode.getX() != -1) {
                      count++;
                      visited[nextNode.getX()][nextNode.getY()] = true;
                      order[nextNode.getX()][nextNode.getY()] = count;
                      open.add(nextNode);

                    }
                }    
            }                
        }
    }
}

Heuristic function 启发式功能

public int h(Node current, Node goal){

    return (goal.getX() - current.getX()) + (goal.getY() - current.getY());

}

Help would be much appreciated 帮助将不胜感激

For the typical maze - one exit, only one path, path-width=1 - the A* has no advantage over breadth-first. 对于典型的迷宫-一个出口,只有一条路径,路径宽度= 1-A *与广度优先没有优势。 Its difference is only visible for maps like you would see in strategy games where there is an advantage to trying to walk in the by-air-direction of the target and where the distance previously traveled has significance. 它的区别仅在战略游戏中看到的地图上可见,在战略游戏中尝试沿目标的空中方向行走是有利的,并且先前行进的距离也很重要。 If you like, have a look at the other algorithms: 如果愿意,请查看其他算法:

https://en.wikipedia.org/wiki/Maze_solving_algorithm https://en.wikipedia.org/wiki/Maze_solving_algorithm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM