Advertisement
JackOUT

Untitled

Jan 17th, 2024
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. package com.bcs2024.knapsack.algorithm.dancinglinks;
  2.  
  3. import com.bcs2024.knapsack.model.CargoSpace;
  4. import com.bcs2024.knapsack.model.Parcel;
  5. import com.bcs2024.knapsack.model.ParcelPlacement;
  6. import com.bcs2024.knapsack.renderer.UI;
  7.  
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Stack;
  11.  
  12. public class DancingLinks {
  13.  
  14. public Header root;
  15. public Header[] headers;
  16. public Stack<Integer> answer;
  17. public Stack<Integer> pentIDS;
  18. public int duplicateSolutionsFound;
  19.  
  20. private CargoSpace cargoSpace = new CargoSpace();
  21.  
  22. public static int length = (int) (CargoSpace.length);
  23. public static int height = (int) (CargoSpace.height);
  24. public static int width = (int) (CargoSpace.width);
  25. public static boolean stop = false;
  26. // public static int[][][] field = new int[width][height][length];
  27.  
  28. public DancingLinks(final int columns) {
  29.  
  30.  
  31. answer = new Stack<>();
  32. pentIDS = new Stack<>();
  33.  
  34. duplicateSolutionsFound = 0;
  35.  
  36. root = new Header(-1);
  37. headers = new Header[columns];
  38. for (int j = 0; j < columns; j++) {
  39. headers[j] = new Header(j);
  40. root.InsertLeft(headers[j]);
  41. }
  42. }
  43.  
  44. public void AddRow(final int row, final int pentId, final int[] ones, final int[][][] piece) {
  45. final int last = -1;
  46. Cell first = null;
  47. for (final int x : ones) {
  48. final Cell cell = new Cell(headers[x]);
  49. headers[x].InsertUp(cell);
  50. cell.row = row;
  51. cell.shape = piece;
  52. cell.pentID = pentId;
  53.  
  54. headers[x].size++;
  55.  
  56. if (x <= last) {
  57. throw new IllegalArgumentException("Column indexes must be in increasing order");
  58. }
  59.  
  60. if (first == null) {
  61. first = cell;
  62. } else {
  63. first.InsertLeft(cell);
  64. }
  65. }
  66. }
  67.  
  68. public int countA = 0;
  69. public int countB = 0;
  70. public int countC = 0;
  71.  
  72. public void algorithmX(final int step) {
  73. if (stop) return;
  74. // if (root.R == root) {
  75. // System.out.println("Filled the container");
  76. // stop = true;
  77. // return;
  78. // }
  79. final List<ParcelInfo> parcelInfo = new ArrayList<>();
  80. if (answer.size() >= 10) {
  81. DLSearch.pieceCount = 0;
  82. DLSearch.totalValue = 0;
  83. for (final var ans : answer) {
  84. final ParcelInfo r = DLSearch.parcelInfo.get(ans);
  85. parcelInfo.add(r);
  86. }
  87.  
  88. for (int i = 0; i < cargoSpace.getOccupied().length; i++) {
  89. for (int j = 0; j < cargoSpace.getOccupied()[0].length; j++) {
  90. for (int k = 0; k < cargoSpace.getOccupied()[0][0].length; k++) {
  91. cargoSpace.getOccupied()[i][j][k] = -1;
  92. }
  93. }
  94. }
  95.  
  96. for (final var info : parcelInfo) {
  97. final Parcel parcel = new Parcel(info.parcelID, info.shape);
  98. final ParcelPlacement placement = new ParcelPlacement(parcel, info.x0, info.y0, info.z0);
  99.  
  100. cargoSpace.placeParcel(placement);
  101.  
  102. //cargoSpace.placeParcel(info.shape, info.x0, info.y0, info.z0, cargoSpace.getOccupied());
  103. DLSearch.pieceCount++;
  104. DLSearch.totalValue += info.pieceValue;
  105. switch (info.parcelID) {
  106. case 1 -> countA++;
  107. case 2 -> countB++;
  108. case 3 -> countC++;
  109. }
  110. }
  111. // System.out.println("Solution found!");
  112. System.out.println("Total value: " + DLSearch.totalValue);
  113. // System.out.println("Piece count: " + DLSearch.pieceCount);
  114. System.out.println(countA + " " + countB + " " + countC);
  115. countA = 0;
  116. countB = 0;
  117. countC = 0;
  118. // if (DLSearch.totalValue >= 1190){
  119. if (root.R == root) {
  120. stop = true;
  121. final UI ui = new UI();
  122. ui.show();
  123. return;
  124. }
  125.  
  126. cargoSpace.setOccupied(new int[width][height][length]);
  127. }
  128.  
  129. Header head = (Header) root.R;
  130. int minSize = head.size;
  131. for (Cell xCell = head; xCell != root; xCell = xCell.R) {
  132. if (((Header) xCell).size < minSize) {
  133. minSize = ((Header) xCell).size;
  134. head = (Header) xCell;
  135.  
  136. if (head.C.size == 0) {
  137. return;
  138. }
  139. }
  140. }
  141. cover(head);
  142. for (Cell rCell = head.D; rCell != head; rCell = rCell.D) {
  143. answer.push(rCell.row);
  144. pentIDS.push(rCell.pentID);
  145.  
  146. for (Cell jCell = rCell.R; jCell != rCell; jCell = jCell.R) {
  147. cover(jCell.C);
  148. }
  149. algorithmX(step + 1);
  150. answer.pop();
  151. pentIDS.pop();
  152.  
  153. for (Cell jCell = rCell.L; jCell != rCell; jCell = jCell.L) {
  154. uncover(jCell.C);
  155. }
  156.  
  157. }
  158. uncover(head);
  159. }
  160.  
  161. private void cover(final Header head) {
  162. head.R.L = head.L;
  163. head.L.R = head.R;
  164.  
  165. for (Cell iCell = head.D; iCell != head; iCell = iCell.D) {
  166. for (Cell jCell = iCell.R; iCell != jCell; jCell = jCell.R) {
  167. jCell.D.U = jCell.U;
  168. jCell.U.D = jCell.D;
  169. jCell.C.size--;
  170. }
  171. }
  172. }
  173.  
  174. private void uncover(final Header head) {
  175. for (Cell iCell = head.U; iCell != head; iCell = iCell.U)
  176. for (Cell jCell = iCell.L; jCell != iCell; jCell = jCell.L) {
  177. jCell.D.U = jCell;
  178. jCell.U.D = jCell;
  179. jCell.C.size++;
  180. }
  181. head.R.L = head;
  182. head.L.R = head;
  183. }
  184.  
  185. }
  186.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement