Advertisement
uopspop

Untitled

Sep 7th, 2019
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.59 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public class BranchBound_zerooneknapsack {
  4.     static boolean isDebugMode = false;
  5.     static int curBestSolution = -1;
  6.  
  7.     public static void main(String[] args)
  8.     {
  9.         double capacity_weight = 10;
  10.         Integer[] weight = new Integer[]{2, 3, 2, 5, 3};
  11.         Integer[] val = new Integer[]{40, 50, 100, 95, 30};
  12.  
  13. //        double capacity_weight = 50;
  14. //        Integer[] weight = new Integer[]{10, 20, 30};
  15. //        Integer[] val = new Integer[]{60, 100, 120};
  16.  
  17.  
  18.         System.out.println("v: ");
  19.         printArray(val);
  20.         System.out.println("w: ");
  21.         printArray(weight);
  22.  
  23.         Integer finalVal = backtracking_BranchAndBound(capacity_weight, val, weight);
  24.  
  25.         System.out.println("result: ");
  26.         System.out.println(finalVal);
  27.     }
  28.  
  29.     private static Integer backtracking_BranchAndBound(double capacity_weight, Integer[] vals, Integer[] weights) {
  30.         Integer curWeight = 0;
  31.         Integer curVal = 0;
  32.         Integer curIndex = 0;
  33.         int finalVal = goDownDFS(capacity_weight, vals, weights, curWeight, curVal, curIndex);
  34.         return finalVal;
  35.     }
  36.  
  37.     private static Integer goDownDFS(double capacity_weight, Integer[] vals, Integer[] weights, Integer curWeight, Integer curVal, Integer curIndex) {
  38.         // ending point
  39.         if ( curWeight > capacity_weight) { // backtracking here
  40.             return -1; // 爆了,所以當作最小價值
  41.         }
  42.         if (curIndex >= vals.length) {
  43.             return curVal;
  44.         }
  45.  
  46.         // branch and bound
  47.         int greatestValForCurrentNode = getGreatestValForCurrentNode(curVal, vals, curIndex);
  48.         if (greatestValForCurrentNode <= curBestSolution) return -1;
  49.  
  50.         // if take
  51.         int ifTakeWeight = curWeight + weights[curIndex];
  52.         int ifTakeVal = curVal + vals[curIndex];
  53.         int ifTakeCurrVal = goDownDFS(capacity_weight, vals, weights, ifTakeWeight, ifTakeVal, curIndex+1);
  54.  
  55.         // if not take
  56.         int ifNotTakeWeight = curWeight;
  57.         int ifNotTakeVal = curVal;
  58.         int ifNotTakeCUrrVal = goDownDFS(capacity_weight, vals, weights, ifNotTakeWeight, ifNotTakeVal, curIndex+1);
  59.  
  60.         int max = Math.max(ifTakeCurrVal, ifNotTakeCUrrVal);
  61.         if (max > curBestSolution) curBestSolution = max;
  62.         return max;
  63.  
  64.     }
  65.  
  66.     private static Integer getGreatestValForCurrentNode(Integer curVal, Integer[] vals, Integer curIndex) {
  67.         int result = curVal;
  68.         for (int i = curIndex; i < vals.length; i++) {
  69.             result += vals[i];
  70.         }
  71.         return result;
  72.     }
  73.  
  74.  
  75.     ///// Util Only below /////
  76.  
  77.     public static int[][] generateDeepCopy2dAry(int[][] original) {
  78.         if (original == null) {
  79.             return null;
  80.         }
  81.  
  82.         final int[][] result = new int[original.length][];
  83.         for (int i = 0; i < original.length; i++) {
  84.             result[i] = Arrays.copyOf(original[i], original[i].length);
  85.             // For Java versions prior to Java 6 use the next:
  86.             // System.arraycopy(original[i], 0, result[i], 0, original[i].length);
  87.         }
  88.         return result;
  89.     }
  90.  
  91.     private static void printArray(Object[] ary){
  92.         for (int i = 0; i < ary.length; i++) {
  93.             System.out.printf("%4d" , ary[i]);
  94.         }
  95.         System.out.println();
  96.     }
  97.     private static void print2DArray(int[][] ary){
  98.         for (int i = 0; i < ary.length; i++) {
  99.             for (int j = 0; j < ary[i].length; j++) {
  100.                 System.out.printf("%4d" , ary[i][j]);
  101.             }
  102.             System.out.println();
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement