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

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

问题描述

我花了整个周末与此玩耍。我想存储的PriorityQueue数据结构中的节点。我爱仕达功能似乎没有做什么它应该。有任何人脑海中的样子?

 公共无效爱仕达(从节点,节点到){

    的PriorityQueue<节点> exploreList =新的PriorityQueue<节点>();
    ArrayList的<节点>走访=新的ArrayList<节点>();
    ArrayList的<节点>继任者=新的ArrayList<节点>();

    节点的电流=距离;
    的System.out.println(current.getName());
    而(电流!=到){
            继任= current.getConnected();
            Collections.sort(接班人);
            对于(节点N:接班人){
                    如果(!visited.contains(正)){
                            exploreList.add(N);
                    }
                    对于(节点N1:接班人){
                            如果(n.fSum()> n1.fSum()){
                            exploreList.remove(N);
                            exploreList.add(n1)的;
                            }
                    }
            }
            visited.add(电流);
            电流= exploreList.remove();
            的System.out.println(current.getName());
    }
 

下面节点类

 公共类节点实现可比{
   私人字符串名称;
   私人诠释travDist;
   私人诠释straightDist;
   私人的ArrayList<弧GT;弧线;

/ **
 *构造一个新节点
 *
 * @参数ñ
 * /
   公共节点(字符串n,INT aTravDist,诠释aStraightDist){
       NAME = N;
       travDist = aTravDist;
       straightDist = aStraightDist;

       圆弧=新的ArrayList<电弧>();
  }

/ **
 *增加了一个新弧
 *
 * @参数来
 * @参数Ç
 * /
公共无效addArc(节点来,INT C){
    arcs.add(新弧(于C));
}

/ **
 *获取连接节点列表,该节点
 *
 * @返回
 * /
公众的ArrayList<节点> getConnected(){
    ArrayList的<节点> returnData =新的ArrayList<节点>();
    对于(弧:弧){
        returnData.add(a.getNode());
    }
    返回returnData;
}

@覆盖
公众诠释的compareTo(对象o){
    //返回name.compareTo(((节点)O).getName());
    整数金额=((节点)O).fSum();
    返回sum.compareTo(FSUM());
}

公众诠释FSUM(){

    返回travDist + straightDist;
}

/ **
 *获取节点的名称
 *
 * @返回
 * /
公共字符串的getName(){
    返回名称;
}


}
 

解决方案

你在做什么是不正确的星算法。

  Col​​lections.sort(接班人);
 

您不应该这样做。在阿星,你总是认为所有的继任者。您neededn't担心命令─优先级队列将采取照顾。然而,addibng这条线,你增加了算法的复杂性。

 的(节点N1:接班人){
  如果(n.fSum()> n1.fSum()){
    exploreList.remove(N);
    exploreList.add(n1)的;
  }
}
 

这是完全错误的。什么,你在这里做的是:你只添加所有的接班人最接近。这是预订购大小为1,不是明星的光束束搜索 - 只是让他们都在

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());
    }

Node Class here

   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;
}


}

解决方案

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

Collections.sort(successors);

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);
  }      
}

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天全站免登陆