Advertisement
Guest User

Untitled

a guest
May 19th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. package ads.set1.knapsack;
  2.  
  3. public class MaxItemsKnapsackSolver {
  4. /**
  5. * Solves the limited-items knapsack problem using a dynamic programming
  6. * algorithm based on the one introduced in the lecture.
  7. *
  8. * @param items
  9. * (possibly empty) list of items. All items have a profit
  10. * {@code > 0.}
  11. * @param capacity
  12. * the maximum weight allowed for the knapsack ({@code >= 0}).
  13. * @param maxItems
  14. * the maximum number of items that may be packed ({@code >= 0}).
  15. * @return the maximum profit achievable by packing at most {@code maxItems}
  16. * with at most {@code capacity} weight.
  17. */
  18. public static int pack(Item[] items, int capacity, int maxItems) {
  19. int itemNumber = items.length;
  20. int res = 0;
  21. //creates two 3d arrays with 0 being the profit table and 1 being the items used table
  22. int[][][] knapsack = new int[itemNumber][capacity + 1][2];
  23. // Fill knapsack with -1.
  24. // Set number of used items to 0.
  25. for (int i = 0; i < itemNumber; i++) {
  26. for (int j = 0; j <= capacity; j++) {
  27. knapsack[i][j][0] = -1;
  28. knapsack[i][j][1] = 0;
  29. }
  30.  
  31. }
  32. // Fill the first row with 0.
  33. // Set number of used items to 0.
  34. for (int i = 0; i < itemNumber; i++) {
  35. knapsack[i][0][0] = 0;
  36. knapsack[i][0][1] = 0;
  37. }
  38. // Put the first item into the knapsack.
  39. if(items[0].getWeight() <= capacity) {
  40. knapsack[0][items[0].getWeight()][0] = items[0].getProfit();
  41. knapsack[0][items[0].getWeight()][1] = 1;
  42. }
  43. // Iterate over all items and over all possible weights.
  44.  
  45. for (int i = 1; i < itemNumber; i++) {
  46.  
  47. int weight = items[i].getWeight();
  48. int profit = items[i].getProfit();
  49. // Iterate over all possible weights and check if there is an entry to the left.
  50. // If so check if the current item would improve that entry.
  51. // If it does, exchange it else leave it.
  52. for (int j = 0; j <= capacity; j++) {
  53. // Checks if there is an entry on the left with less than the maxItems.
  54. if ((knapsack[i - 1][j][0] != -1) && knapsack[i - 1][j][1] <= maxItems) {
  55. // Checks if current weight would end up over the maximum capacity.
  56. if (j + weight <= capacity) {
  57. // Checks if the the current item would improve the profit when added to the
  58. // item constellation at [i-1][j]. If so add the item.
  59. if (knapsack[i - 1][j][0] + profit < knapsack[i - 1][j + weight][0]
  60. || knapsack[i - 1][j + weight][0] == -1) {
  61.  
  62. knapsack[i][j + weight][0] = knapsack[i-1][j][0] + profit;
  63. knapsack[i][j + weight][1] = knapsack[i-1][j][1] + 1;
  64.  
  65. }
  66.  
  67. }
  68. // Transfers entries to the next column.
  69. if (knapsack[i][j][0] == -1) {
  70.  
  71. knapsack[i][j][0] = knapsack[i - 1][j][0];
  72. knapsack[i][j][1] = knapsack[i - 1][j][1];
  73. }
  74. }
  75. System.out.print(knapsack[i][j][0]); System.out.print("/");System.out.print(knapsack[i][j][1] + " ");
  76. }
  77. System.out.println("");
  78. }
  79.  
  80.  
  81. // Finds the highest profit possible.
  82. for (int i = 0; i <= capacity; i++) {
  83. if (res < knapsack[itemNumber - 1][i][0]) {
  84. res = knapsack[itemNumber - 1][i][0];
  85. }
  86. }
  87. return res;
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement