分支机构绑定错误:无法将Node1强制转换为java.lang.Comparable

时间:2015-05-14 05:37:35

标签: java algorithm comparator comparable branch-and-bound

我正在尝试实现分支& java中的绑定搜索算法。我知道它的概念(它是如何工作的),但我不确定如何实现它。

我在谷歌上发现了一些例子,但它们更复杂,我似乎无法理解它们。我想以一种简单的方式实现它。其中大多数都不在java中。

以下是我的搜索开始的相关方法(这只是我的代码的一部分)。 我认为我的for循环需要适当修改以存储前沿节点和成本,然后以最低成本获得节点,然后再次执行搜索,直到找到目标节点添加累积成本。
所以我认为递归方法最适合这个。但我不确定如何实施。

以下内容没有给我任何编译错误,但是我的运行时错误为Node1 cannot be cast to java.lang.Comparable。有人善意地研究这个问题吗?我一直试图这么做,但似乎无法找到任何解决方案。

此外,任何指导我走正确道路的小代码都会有所帮助。

 public void Search(Node1[] nodes, String startNode, int size){

  List<String> frontierNodes = new ArrayList<String>();
  Queue<Node1> frontierCosts = new PriorityQueue<Node1>();

    for(int i=0; i<size;i++) {

        if(startNode.equals(nodes[i].getStartNode())) { // user enters the startNode and goalNode          

           frontierNodes.add(nodes[i].getEndNode());               
           frontierCosts.add(new Node1(nodes[i].getCost())); 
           frontierCosts.peek();
           System.out.println("Min cost" +frontierCosts.peek());
           int nodesLength = nodes.length - (frontierNodes.size()); 
           i--;
           System.out.println("remaining search nodes length" +nodesLength);

           //Search(nodes, frontierCosts.peek().getEndNode() ,nodesLength); // RECURSIVE CALL?
        } 
    }

} 

以下是存储文件信息的Node1类

class Node1 {
   String start, end;
   double cost;

   public Node1(String start, String end, double cost){
       this.start = start;
       this.end = end;
       this.cost = cost;
   }

   public Node1(double cost) { // alternate constructor to store cost in   priority queue
      this.cost = cost;
   }

   public String getStartNode(){
      return start;
   }

   public String getEndNode(){
      return end;
   }

   public double getCost(){
      return cost;
   }
}

以下是文件格式(startNode endNode cost)

A B 10 
A C 12
B D 6
....

[编辑]:

我想实现分支定界搜索,程序要求用户输入startNodegoalNode,然后从Node1类访问数据(其中来自文件的数据是存储)然后程序进入search方法(上面的方法),传递所有nodesstartNodelength of nodes(size)。 如果startNode与任何node1 []。getStartNode匹配,则它将frontierNodes中相应的扩展节点及其frontierCosts中的相应成本存储在优先级队列中(以选择最低成本)。 然后程序偷看()优先级队列&amp;选择最低成本节点(队列前面),然后再次搜索(递归调用上面的搜索方法?),将该特定节点作为startNode,继续搜索。
当程序到达新节点时,每个新节点的成本应该获得到目前为止所访问的路径的累积成本,并且程序应该输出路径和成本。

1 个答案:

答案 0 :(得分:2)

PriorityQueue需要实现Comparable接口的数据结构,除非您将Comparator作为constructor传入。

改变应该非常简单。

class Node1 implements Comparable<Node1> {
    String start, end;
    double cost;

    public Node1(String start, String end, double cost){
        this.start = start;
        this.end = end;
        this.cost = cost;
    }

    public Node1(double cost) { // alternate constructor to store cost in   priority queue
        this.cost = cost;
    }

    public String getStartNode(){
        return start;
    }

    public String getEndNode(){
        return end;
    }

    public double getCost(){
        return cost;
    }

    @Override
    public int compareTo(Node1 o) {
        return Double.compare(this.cost, o.cost);
    }
}

请注意,如果您希望结果具有确定性并且成本不是唯一的,那么您可能还需要使用开始/结束节点作为比较的一部分。

对于你的主要逻辑,有几件事情不太正确。

  1. 您的函数参数应包含目标节点。
  2. 您的函数参数应该包括您目前使用的成本,以便在想要使用递归函数时可以跟踪它。
  3. 您的函数应返回到达节点的最低成本。
  4. 如果您的图表可以有一个循环,请考虑一种机制,以确保它不会重新访问之前访问过的节点。
  5. 一旦你有一个可接受成本的可能解决方案,你需要使用该成本作为上限,以确保如果成本高于目前为止的最佳解决方案,你将不会继续访问更多节点。
相关问题