[英]A* Graph Search Good Heuristic
我正在为具有不同距离的简单多出口迷宫实现A *路径查找算法,但是我找不到合适的启发式方法,似乎执行了广度优先搜索。
成本初始设置为1
这是我的尝试:
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);
}
}
}
}
}
}
启发式功能
public int h(Node current, Node goal){
return (goal.getX() - current.getX()) + (goal.getY() - current.getY());
}
帮助将不胜感激
对于典型的迷宫-一个出口,只有一条路径,路径宽度= 1-A *与广度优先没有优势。 它的区别仅在战略游戏中看到的地图上可见,在战略游戏中尝试沿目标的空中方向行走是有利的,并且先前行进的距离也很重要。 如果愿意,请查看其他算法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.