A* 算法 Java [英] A* Algorithm Java

查看:39
本文介绍了A* 算法 Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我整个周末都在玩这个.我正在尝试将节点存储在 PriorityQueue 数据结构中.我的 astar 函数似乎没有做它应该做的.有人介意看看吗?

I have spent entire weekend playing around with this. I am trying to store the nodes in PriorityQueue data structure. My astar function doesnt seem to be doing what it should. Anyone mind having a look?

public void aStar(Node from, Node to) {

    PriorityQueue<Node> exploreList = new PriorityQueue<Node>();
    ArrayList<Node> visited = new ArrayList<Node>();
    ArrayList<Node> successors = new ArrayList<Node>();

    Node current = from;
    System.out.println(current.getName());
    while (current != to) {
            successors = current.getConnected();
            Collections.sort(successors);
            for (Node n : successors) {
                    if (!visited.contains(n)) {
                            exploreList.add(n);
                    }
                    for (Node n1 : successors) {
                            if (n.fSum() > n1.fSum()) {
                            exploreList.remove(n);
                            exploreList.add(n1);
                            }      
                    }
            }
            visited.add(current);
            current = exploreList.remove();
            System.out.println(current.getName());
    }

节点类在这里

   public class Node implements Comparable {
   private String name;
   private int travDist;
   private int straightDist;
   private ArrayList<Arc> arcs;

/**
 * Constructor for a new node
 * 
 * @param n
 */
   public Node(String n, int aTravDist, int aStraightDist) {
       name = n;
       travDist = aTravDist;
       straightDist = aStraightDist;

       arcs = new ArrayList<Arc>();
  }

/**
 * Adds a new arc
 * 
 * @param to
 * @param c
 */
public void addArc(Node to, int c) {
    arcs.add(new Arc(to, c));
}

/**
 * Gets the list of connected nodes to this node
 * 
 * @return
 */
public ArrayList<Node> getConnected() {
    ArrayList<Node> returnData = new ArrayList<Node>();
    for (Arc a : arcs) {
        returnData.add(a.getNode());
    }
    return returnData;
}

@Override
public int compareTo(Object o) {
    //return name.compareTo(((Node) o).getName());
    Integer sum = ((Node)o).fSum();
    return sum.compareTo(fSum());
}

public int fSum () {

    return travDist + straightDist;
}

/**
 * Gets the name of the Node
 * 
 * @return
 */
public String getName() {
    return name;
}


}

推荐答案

你所做的不是一个合适的 A 星算法.

What you are doing is not a proper A star algorithm.

Collections.sort(successors);

你不应该那样做.在 A 明星中,您总是考虑所有的继任者.您无需担心顺序 - 优先级队列会处理这个问题.但是,添加这一行会增加算法的复杂性.

You shouldn't do that. In A star you always consider all the successor. You neededn't worry about the order- the priority queue will take care of that. However, addibng this line you increase the complexity of the algorithm.

for (Node n1 : successors) {
  if (n.fSum() > n1.fSum()) {
    exploreList.remove(n);
    exploreList.add(n1);
  }      
}

这是完全错误的.你在这里做的是:你只添加所有后继者中最接近的.这将是一个光束搜索,光束大小为 1,而不是 A 星 - 只需将它们全部保留.

This is entirely wrong. What you are doing here is: you only add the closest of all the successors. This willl be a beam search with beam of size 1, not A star - just keep them all in.

这篇关于A* 算法 Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆