【问题标题】:Using a queue to solve TSP (Branch and Bound)使用队列解决 TSP(分支和绑定)
【发布时间】:2011-08-23 08:43:44
【问题描述】:

我正在研究旅行商问题的分支定界算法,但遇到了一点麻烦。我正在使用一个非常标准的队列,其中的节点代表顶点(路径)的子集。我很确定我已经解决了整个问题,但目前我有公共类 Queue,在它下面有私有类 Node,它的所有属性:当前路径、下限等。

但是,在我的主程序中,我初始化了节点队列并创建了两个起始节点,但出现错误“节点无法解析为类型”。我认为这是因为它在 Queue 类中并且主程序无法访问,但是当我将其移出时,在连接到节点的项目上的其他任何地方都会出现错误。

我真的希望这是有道理的,我不知道如何解释它,但其他一切似乎都很好。这是我的代码的 sn-p 以进行澄清:

`public class Queue<Item> implements Iterable<Item> {
    private int N;         // number of elements on queue
    private Node first;    // beginning of queue
    private Node last;     // end of queue

    // helper linked list class
    public class Node {
        public int level;       //level on the tree
        public int[] path;      //current path
        public int bound;       //lower bound
        public Item item;       
        public Node next;       //the next in the path
        public boolean inPath;  //checks to see if the vertex is in the current path
        public int missingV;    //the vertex that is missing from the path

    }`

这是我声明 Node 类的地方,也是我在主程序中实际使用它的地方:

`public static void main(String args[]) {
    int n = 4;
    int[][] W = new int[][] {{0,2,4,7},{2,0,7,3},{4,7,0,5},{6,3,5,0}};
    int[] opttour;
    Queue<Node> PQ = new Queue<Node>();
    Node u = new Node();
    Node v = new Node();`

【问题讨论】:

    标签: java algorithm traveling-salesman branch-and-bound


    【解决方案1】:

    在您的主目录中,将 Node 更改为 Queue.Node

        Queue<Queue.Node> PQ = new Queue<Queue.Node>();
        Queue.Node u = PQ.new Node();
        Queue.Node v = PQ.new Node();
    

    我建议将Node 类放在与Queue 分开的文件中,因为您显然需要从Queue 类之外操作它的实例。

    【讨论】:

      【解决方案2】:

      我认为您的 Node 类应该是静态的,而不是 Queue 的内部类。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-06-14
        • 1970-01-01
        • 2011-06-10
        • 2023-03-31
        • 2016-05-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多