2017-03-19 6 views
-4

Ich habe die Implementierung von Branch and Bound Ich bin in dieser Funktion immer Fehler gibt es Prioritätswarteschlange, die ihre IllegalArgumentExceptionjava.lang.IllegalArgumentException

immer verwendet wird weiter // hier ist, dass Linie, auf der ich bin immer diese Fehlermeldung

PriorityQueue<Node> pq = new PriorityQueue<>(0, comp); 

    import java.util.Comparator; 
    import java.util.PriorityQueue; 

    public class Node { 
     private int N = 3; 
     Node parent; 
     int[][] mat = new int[N][N]; 
     int x, y; 
     int cost; 
     int level; 

     public void printMatrix(int[][] mat) { 
      for (int i = 0; i < mat.length; i++) { 
       for (int j = 0; j < mat[i].length; j++) { 
        System.out.print(mat[i][j]); 
       } 
       System.out.println(); 
      } 
     } 

     public void newNode(int[][] mat, int x, int y, int newX, int newY, int level, Node parent) { 
      Node node = new Node(); 
      node.parent = parent; 
      node.mat = mat; 
      node.mat[x][y] = node.mat[newX][newY]; 
      node.cost = Integer.MAX_VALUE; 
      node.level = level; 
      node.x = newX; 
      node.y = newY; 
     } 

     int[] row = {1, 0, -1, 0}; 
     int[] col = {0, -1, 0, 1}; 

     public int calculateCost(int[][] initial, int[][] fin) { 
      int count = 0; 
      for (int i = 0; i < fin.length; i++) { 
       for (int j = 0; j < fin.length; j++) { 
        if(initial[i][j] != fin[i][j]) 
         count++; 
       } 
      } 
      return count; 
     } 

     public int isSafe(int x, int y) { 
      if ((x >= 0 && x < N) && (y >= 0 && y < N)) 
       return 1; 
      return 0; 
     } 

     public void printPath(Node root) { 
      if(root == null) 
       return; 
      printPath(root.parent); 
      printMatrix(root.mat); 
      System.out.println(); 
     } 

** // hier ich bin Fehler immer in dieser Funktion gibt es Prioritätswarteschlange, die seine immer Illegal verwendet wird weiter **

public void solve(int[][] initial, int x, int y, int[][] fin) { 
      Comparator<Node> comp = new Comparator<Node>() { 

       @Override 
       public int compare(Node lhs, Node rhs) { 
        if((lhs.cost + lhs.level) > (rhs.cost + rhs.level)) 
         return 1; 
        return 0; 
       } 
      }; 
//here is that line on which im getting this error 
      PriorityQueue<Node> pq = new PriorityQueue<>(0, comp); 
      Node root = new Node(); 
      root.newNode(initial, x, y, x, y, 0, null); 
      root.cost = calculateCost(initial, fin); 
      pq.add(root); 

      while(!pq.isEmpty()) { 
       Node min = pq.peek(); 
       pq.remove(); 
       if(min.cost == 0) { 
        printPath(min); 
        return; 
       } 

       for (int i = 0; i < 4; i++) { 
        if(isSafe(min.x + row[i], min.y + col[i]) == 1) { 
         Node child = new Node(); 
         child.newNode(min.mat, min.x, min.y, min.x + row[i], min.y + col[i], min.level + 1, min); 
         child.cost = calculateCost(child.mat, fin); 
         pq.add(child); 
        } 
       } 
      } 
     } 

     public static void main(String[] args) { 
      int[][] initial = 
       { 
        {1, 2, 3}, 
        {5, 6, 0}, 
        {7, 8, 4} 
       }; 

      int[][] fin = 
       { 
        {1, 2, 3}, 
        {5, 0, 6}, 
        {8, 7, 4} 
       }; 

      int x = 1, y = 2; 

      Node newNode = new Node(); 
      newNode.solve(initial, x, y, fin); 

     } 

    } 
+0

Sie benötigen die COMPLETE-Stack-Trace zu schreiben und die Anweisung im Code zu identifizieren, die die Ausnahme auslöst . –

+0

// Hier ist die Zeile, in der der Fehler auftritt PriorityQueue pq = new PriorityQueue <> (0, comp); –

Antwort

2

Vom javadoc (Schwerpunkt liegt mir):

public PriorityQueue(int initialCapacity, Comparator<? super E> comparator)

Erstellt eine PriorityQueue mit der Anfangskapazität festgelegt, dass seine Elemente entsprechend der angegebenen Komparator befiehlt.

[...]

Wirft: Illegal - wenn initial weniger als 1

+0

Wer hat das abgelehnt? Das ist die richtige Antwort. Danke RC –

Verwandte Themen