2014-12-04 60 views
-1

我嘗試了和算法,我發現谷歌圖書http://books.google.ee/books?id=DAorddWEgl0C&printsec=frontcover&source=gbs_ge_summary_r&cad=0#v=onepage&q&f=false解決揹包問題。我爲自己的項目構建了一個類似的程序,但程序不會返回揹包整數。我的揹包算法一直運行,沒有回答一個答案

換句話說,我應該得到90在System.out.println(knapsack(n,maxW));,但我的代碼不會返回任何結果。

這裏是我寫的代碼:

public class Algoritmid3 { 

public static int[] weight; 
public static int[] value; 
public static int maxW = 16; 
public static int n; 

public static class Node { 
    int level; 
    int weight; 
    int profit; 
    double bound; 
} 

public static class NodeComparator implements Comparator { 
    @Override 
    public int compare(Object o1, Object o2) { 
     Node n1 = (Node) o1; 
     Node n2 = (Node) o2; 
     if (n1.bound < n2.bound) { 
      return 1; 
     } 
     if (n1.bound > n2.bound) { 
      return -1; 
     } 
     else { 
      return 0; 
     } 
    } 
} 

public static double bound(Node a) { 
    int j, k; 
    int totalWeight; 
    double result; 
    if (a.weight >= maxW) { 
     return 0; 
    } 
    else { 
     result = a.profit; 
     j = a.level + 1; 
     totalWeight = a.weight; 
     while (j <= n && totalWeight + weight[j] <= maxW) { 
      totalWeight += weight[j]; 
      result += value[j]; 
      j++; 
     } 
     k = j; 
     if (k <= n) { 
      result += ((maxW - totalWeight) * (value[k]/weight[k])); 
     } 
     return result; 
    } 

} 


public static void main(String[] args) { 
    maxW = 16; 
    n = 5; 
    weight = new int[5]; 
    value = new int[5]; 
    weight[0] = 2; 
    value[0] = 40; 
    weight[1] = 5; 
    value[1] = 30; 
    weight[2] = 10; 
    value[2] = 50; 
    weight[3] = 15; 
    value[3] = 60; 
    weight[4] = 5; 
    value[4] = 5; 

    System.out.println(knapsack(n,maxW)); 
} 

public static int knapsack(int n, int maxWeight) { 
    NodeComparator nc = new NodeComparator(); 
    PriorityQueue pq = new PriorityQueue(n, nc); 
    Node u, v; 
    int maxprofit = 0; 
    v = new Node(); 
    u = new Node(); 
    v.level = 0; 
    v.profit = 0; 
    v.weight = 0; 
    v.bound = bound(v); 
    pq.add(v); 
    while (!(pq.isEmpty())) { 
     pq.remove(v); 
     if (v.bound > maxprofit) { 
      u.level = v.level + 1; 
      u.weight = v.weight + weight[u.level]; 
      u.profit = v.profit + value[u.level]; 

      if (u.weight <= maxW && u.profit > maxprofit) { 
       maxprofit = u.profit; 
      } 
      u.bound = bound(u); 
      if (u.bound > maxprofit) { 
       pq.add(u); 
      } 
      u.weight = v.weight; 
      u.profit = v.profit; 
      u.bound = bound(u); 
      if (u.bound > maxprofit) { 
       pq.add(u); 
      } 
     } 
    } 
    return maxprofit; 
} 
} 
+0

我有它運行53分鐘以前,它仍然沒有結果。它似乎在無盡的循環中,但它不應該是。 – Evald 2014-12-04 00:33:53

+0

很確定這不是你的主要問題,因爲如果是你會碰到segfaults,但是在'bound(..)'中的'else'內,如果'j = n'那麼'weight [j]'未定義。你需要'while(j chrisb2244 2014-12-04 00:49:20

+0

應用普通的調試技巧。在這種情況下:打印操作的每一步,手工跟蹤,觀察它什麼時候出錯,修正錯誤的東西 – 2014-12-04 02:22:50

回答

1

具有相同的代碼類似的問題在StackOverflow

已經提出但這種解決方案並不存儲效率。

在下方找到您更正的代碼。

公共類Algoritmid3 {

public static int[] weight; 
public static int[] value; 
public static int maxW = 16; 
public static int n; 

public static class Node { 
    int level; 
    int weight; 
    int profit; 
    double bound; 
} 

public static class NodeComparator implements Comparator { 
    @Override 
    public int compare(Object o1, Object o2) { 
     Node n1 = (Node) o1; 
     Node n2 = (Node) o2; 
     if (n1.bound < n2.bound) { 
      return 1; 
     } 
     if (n1.bound > n2.bound) { 
      return -1; 
     } else { 
      return 0; 
     } 
    } 
} 

public static double bound(Node a) { 
    int j = 0, k; 
    int totalWeight; 
    double result; 
    if (a.weight >= maxW) { 
     return 0; 
    } else { 
     result = a.profit; 
     // j = a.level + 1; 
     if (a.level < weight.length) { 
      j = a.level + 1; 
     } 
     totalWeight = a.weight; 
     // while (j <= n && totalWeight + weight[j] <= maxW) { 
     while (j < n && totalWeight + weight[j] <= maxW) { 
      totalWeight += weight[j]; 
      result += value[j]; 
      j++; 
     } 
     k = j; 
     // if (k <= n) { 
     if (k < n) { 
      result += ((maxW - totalWeight) * (value[k]/weight[k])); 
     } 
     return result; 
    } 

} 

public static void main(String[] args) { 
    maxW = 16; 
    n = 5; 
    weight = new int[5]; 
    value = new int[5]; 
    weight[0] = 2; 
    value[0] = 40; 
    weight[1] = 5; 
    value[1] = 30; 
    weight[2] = 10; 
    value[2] = 50; 
    weight[3] = 15; 
    value[3] = 60; 
    weight[4] = 5; 
    value[4] = 5; 

    System.out.println(knapsack(n, maxW)); 
} 

public static int knapsack(int n, int maxWeight) { 
    NodeComparator nc = new NodeComparator(); 
    PriorityQueue pq = new PriorityQueue(n, nc); 
    Node u, v; 
    int maxprofit = 0; 
    v = new Node(); 
    u = new Node(); 
    v.level = -1; 
    v.profit = 0; 
    v.weight = 0; 
    v.bound = bound(v); 
    pq.add(v); 
    while (!(pq.isEmpty())) { 
     // pq.remove(v); 
     // Remove head of the queue 
     v = (Node) pq.poll(); 
     u = new Node(); 
     if (v.bound > maxprofit) { 
      u.level = v.level + 1; 
      u.weight = v.weight + weight[u.level]; 
      u.profit = v.profit + value[u.level]; 

      if (u.weight <= maxW && u.profit > maxprofit) { 
       maxprofit = u.profit; 
      } 
      u.bound = bound(u); 
      if (u.bound > maxprofit) { 
       pq.add(u); 
      } 
      // >> IZMO 
      u = new Node(); 
      u.level = v.level + 1; 
      // << IZMO 
      u.weight = v.weight; 
      u.profit = v.profit; 
      u.bound = bound(u); 
      if (u.bound > maxprofit) { 
       pq.add(u); 
      } 
     } 
    } 
    return maxprofit; 
} 

}

+0

Thanks!Now it works很好,唯一的問題是我的揹包算法沒有找到我在主類中得到的最好的解決方案,int揹包返回80,但是最好的值是90(item 1 + item 3),我會試圖找出 – Evald 2014-12-04 10:37:34

+0

嘿,它對我來說工作得很好,我的預期值是90. – 2014-12-05 01:53:24

+0

謝謝,它現在可以正常工作了,我沒有改變任何東西,它在第二天按預期工作(我不確定是否我改變了anythi或者它沒有正確編譯)。 – Evald 2014-12-06 16:33:46

相關問題