BruteForcePackager extends Packager { public BruteForceResult pack(List<Placement> placements, Container container, PermutationRotationIterator rotator, long deadline, int checkpointsPerDeadlineCheck) { return pack(placements, container, rotator, BooleanSupplierBuilder.builder().withDeadline(deadline, checkpointsPerDeadlineCheck).build()); } BruteForcePackager(List<Container> containers, boolean rotate3D, boolean binarySearch, int checkpointsPerDeadlineCheck);  BruteForcePackager(List<Container> containers); static BruteForcePackagerBuilder newBuilder(); BruteForceResult pack(List<Placement> placements, Container container, PermutationRotationIterator rotator, long deadline, int checkpointsPerDeadlineCheck); BruteForceResult pack(List<Placement> placements, Container container, PermutationRotationIterator rotator, long deadline, int checkpointsPerDeadlineCheck, BooleanSupplier interrupt); BruteForceResult pack(List<Placement> placements, Container container, PermutationRotationIterator rotator, BooleanSupplier interrupt); static int pack(List<Placement> placements, Dimension container, PermutationRotationIterator rotator, long deadline, int checkpointsPerDeadlineCheck, Container holder, int index); static int pack(List<Placement> placements, Dimension container, PermutationRotationIterator rotator, long deadline, int checkpointsPerDeadlineCheck, Container holder, int index, BooleanSupplier interrupt); static int pack(List<Placement> placements, Dimension remainingSpace, PermutationRotationIterator rotator, Container holder, int index, BooleanSupplier interrupt);  }
@Test void testStackingRectanglesOnSquare() { List<Container> containers = new ArrayList<>(); containers.add(new ValidatingContainer("container1", 10, 10, 1, 0)); BruteForcePackager packager = new BruteForcePackager(containers); List<BoxItem> products = new ArrayList<>(); products.add(new BoxItem(new Box("E", 5, 10, 1, 0), 1)); products.add(new BoxItem(new Box("F", 5, 10, 1, 0), 1)); Container fits = packager.pack(products); assertNotNull(fits); assertEquals(fits.getLevels().size(), 1); }
@Test void testStackingRectanglesOnSquareRectangle() { List<Container> containers = new ArrayList<>(); containers.add(new ValidatingContainer("container1", 10, 10, 1, 0)); BruteForcePackager packager = new BruteForcePackager(containers); List<BoxItem> products = new ArrayList<>(); products.add(new BoxItem(new Box("J", 5, 10, 1, 0), 1)); products.add(new BoxItem(new Box("K", 5, 5, 1, 0), 1)); products.add(new BoxItem(new Box("L", 5, 5, 1, 0), 1)); Container fits = packager.pack(products); assertNotNull(fits); assertEquals(fits.getLevels().size(), 1); }
@Test void testStackingRectanglesOnSquareRectangleVolumeFirst() { List<Container> containers = new ArrayList<>(); containers.add(new ValidatingContainer("container1", 10, 10, 4, 0)); BruteForcePackager packager = new BruteForcePackager(containers); List<BoxItem> products = new ArrayList<>(); products.add(new BoxItem(new Box("J", 5, 10, 4, 0), 1)); products.add(new BoxItem(new Box("L", 5, 10, 1, 0), 1)); products.add(new BoxItem(new Box("K", 5, 10, 1, 0), 1)); products.add(new BoxItem(new Box("M", 5, 10, 1, 0), 1)); products.add(new BoxItem(new Box("N", 5, 10, 1, 0), 1)); Container fits = packager.pack(products); assertNotNull(fits); assertEquals(fits.getLevels().size(), 1); assertEquals(fits.getLevels().get(0).get(0).getSpace().getZ(), 0); assertEquals(fits.getLevels().get(0).get(1).getSpace().getZ(), 0); assertEquals(fits.getLevels().get(0).get(2).getSpace().getZ(), 1); assertEquals(fits.getLevels().get(0).get(3).getSpace().getZ(), 2); assertEquals(fits.getLevels().get(0).get(4).getSpace().getZ(), 3); }
@Test void testStackingBinary1() { List<Container> containers = new ArrayList<>(); containers.add(new ValidatingContainer("container1", 2, 2, 1, 0)); BruteForcePackager packager = new BruteForcePackager(containers); List<BoxItem> products = new ArrayList<>(); for(int i = 0; i < 4; i++) { products.add(new BoxItem(new Box("K", 1, 1, 1, 0), 1)); } Container fits = packager.pack(products); assertNotNull(fits); assertEquals(fits.getLevels().size(), 1); }
@Test void testStackingBinary2() { List<Container> containers = new ArrayList<>(); containers.add(new ValidatingContainer("container1", 8, 8, 1, 0)); BruteForcePackager packager = new BruteForcePackager(containers); List<BoxItem> products = new ArrayList<>(); products.add(new BoxItem(new Box("J", 4, 4, 1, 0), 1)); for(int i = 0; i < 4; i++) { products.add(new BoxItem(new Box("K", 2, 2, 1, 0), 1)); } for(int i = 0; i < 16; i++) { products.add(new BoxItem(new Box("K", 1, 1, 1, 0), 1)); } Container fits = packager.pack(products); assertNotNull(fits); assertEquals(fits.getLevels().size(), 1); assertEquals(1, fits.getLevels().size()); for (Level level : fits.getLevels()) { for(Placement p : level) { assertEquals(0, p.getSpace().getZ()); } } }
@Test void testStackingTooHigh() { List<Container> containers = new ArrayList<>(); containers.add(new ValidatingContainer("container1", 10, 10, 5, 0)); BruteForcePackager packager = new BruteForcePackager(containers); List<BoxItem> products = new ArrayList<>(); products.add(new BoxItem(new Box("J", 10, 10, 6, 0), 1)); Container fits = packager.pack(products); assertNull(fits); }
@Test void testStackingTooHighLevel() { List<Container> containers = new ArrayList<>(); containers.add(new ValidatingContainer("container1", 10, 10, 5, 0)); BruteForcePackager packager = new BruteForcePackager(containers); List<BoxItem> products = new ArrayList<>(); products.add(new BoxItem(new Box("J", 10, 10, 5, 0), 1)); products.add(new BoxItem(new Box("J", 5, 10, 1, 0), 1)); products.add(new BoxItem(new Box("K", 5, 5, 1, 0), 1)); products.add(new BoxItem(new Box("L", 5, 5, 1, 0), 1)); Container fits = packager.pack(products); assertNull(fits); }
@Test void testStacking3xLP() { List<Container> containers = new ArrayList<>(); containers.add(new ValidatingContainer("container1", 350, 150, 400, 0)); BruteForcePackager packager = new BruteForcePackager(containers); List<BoxItem> products1 = new ArrayList<>(); products1.add(new BoxItem(new Box("A", 400, 50, 350, 0), 1)); products1.add(new BoxItem(new Box("B", 400, 50, 350, 0), 1)); products1.add(new BoxItem(new Box("C", 400, 50, 350, 0), 1)); Container fits1 = packager.pack(products1); assertNotNull(fits1); List<BoxItem> products2 = new ArrayList<>(); products2.add(new BoxItem(new Box("A", 350, 50, 400, 0), 1)); products2.add(new BoxItem(new Box("B", 350, 50, 400, 0), 1)); products2.add(new BoxItem(new Box("C", 350, 50, 400, 0), 1)); Container fits2 = packager.pack(products2); assertNotNull(fits2); }
@Test void testLargestAreaFitFirstDoesNotWork() { List<Container> containers = new ArrayList<>(); containers.add(new ValidatingContainer("container1", 15, 10, 10, 0)); Packager bruteForcePackager = new BruteForcePackager(containers, true, true, 1); LargestAreaFitFirstPackager packager = new LargestAreaFitFirstPackager(containers, true, true, true, 1); List<BoxItem> products1 = new ArrayList<>(); products1.add(new BoxItem(new Box("01", 5, 10, 10, 0), 1)); products1.add(new BoxItem(new Box("02", 5, 10, 10, 0).rotate3D(), 1)); products1.add(new BoxItem(new Box("03", 5, 10, 10, 0).rotate3D().rotate3D(), 1)); long time = System.currentTimeMillis(); Container fits1 = bruteForcePackager.pack(products1); System.out.println(products1.size() + " boxes in " + (System.currentTimeMillis() - time)); assertNotNull(fits1); assertEquals(products1.size(), fits1.getBoxCount()); print(fits1); assertNull(packager.pack(products1)); }
@Test void testPackagingExposesVolumeUsed() { List<Container> containers = new ArrayList<>(); containers.add(new ValidatingContainer("container1", 60, 25, 20, 0)); Packager bruteForcePackager = new BruteForcePackager(containers, true, true, 1); List<BoxItem> products1 = Arrays.asList( new BoxItem(new Box("1", 7, 24, 58, 0), 1), new BoxItem(new Box("2", 7, 25, 56, 0), 1), new BoxItem(new Box("3", 5, 25, 58, 0), 1) ); Container fits1 = bruteForcePackager.pack(products1); assertEquals(new Dimension(58, 25, 19), fits1.getUsedSpace()); }
@Test @Disabled void testRunsPerformanceGraphLinearStacking() { long duration = 60 * 10; System.out.println("Run for " + duration + " seconds"); long deadline = System.currentTimeMillis() + duration * 1000; int n = 1; while(deadline > System.currentTimeMillis()) { List<Container> containers = new ArrayList<>(); containers.add(new ValidatingContainer(5 * n, 10, 10, 0)); Packager bruteForcePackager = new BruteForcePackager(containers, true, true, 1); List<BoxItem> products1 = new ArrayList<>(); for(int i = 0; i < n; i++) { Box box = new Box(Integer.toString(i), 5, 10, 10, 0); for(int k = 0; k < i % 2; k++) { box.rotate3D(); } products1.add(new BoxItem(box, 1)); } long time = System.currentTimeMillis(); Container container = bruteForcePackager.pack(products1, deadline); if(container != null) { System.out.println(n + " in " + (System.currentTimeMillis() - time)); } else { System.out.println(n + " discarded in " + (System.currentTimeMillis() - time)); } n++; } }
@Test void testIssue11ArrayOutOfBounds() { List<Container> containers = Arrays.asList( new ValidatingContainer("2", 330, 222, 121, 0), new ValidatingContainer("4", 330, 235, 225, 0) ); List<BoxItem> items = Arrays.asList( new BoxItem(new Box(105, 105, 293, 0), 1), new BoxItem(new Box(92, 94, 255, 0), 1), new BoxItem(new Box(105, 70, 60, 0), 2) ); BruteForcePackager packer = new BruteForcePackager(containers); packer.pack(items); }
@Test void test2DBruteForceFor6PacketsIssue28() { List<Container> containers = new ArrayList<>(); containers.add(new ValidatingContainer("container1", 152, 252, 1, 0)); Packager bruteForcePackager = new BruteForcePackager(containers, false, true, 1); List<BoxItem> products1 = Arrays.asList( new BoxItem(new Box("1", 73, 82, 1, 0), 1), new BoxItem(new Box("2", 72, 80, 1, 0), 1), new BoxItem(new Box("3", 73, 83, 1, 0), 1), new BoxItem(new Box("4", 71, 83, 1, 0), 1), new BoxItem(new Box("5", 74, 83, 1, 0), 1), new BoxItem(new Box("6", 74, 82, 1, 0), 1) ); Container fits1 = bruteForcePackager.pack(products1); assertNotNull(fits1); }
@Test void test3DBruteForceFor6PacketsIssue28() { List<Container> containers = new ArrayList<>(); containers.add(new ValidatingContainer("container1", 152, 252, 1, 0)); Packager bruteForcePackager = new BruteForcePackager(containers, true, true, 1); List<BoxItem> products1 = Arrays.asList( new BoxItem(new Box("1", 73, 82, 1, 0), 1), new BoxItem(new Box("2", 72, 80, 1, 0), 1), new BoxItem(new Box("3", 73, 83, 1, 0), 1), new BoxItem(new Box("4", 71, 83, 1, 0), 1), new BoxItem(new Box("5", 74, 83, 1, 0), 1), new BoxItem(new Box("6", 74, 82, 1, 0), 1) ); Container fits1 = bruteForcePackager.pack(products1); assertNotNull(fits1); }
@Test void testBruteForceFor6PacketsIssue28() { List<Container> containers = new ArrayList<>(); containers.add(new ValidatingContainer("container1", 152, 252, 58, 0)); Packager bruteForcePackager = new BruteForcePackager(containers, false, true, 1); List<BoxItem> products1 = Arrays.asList( new BoxItem(new Box("1", 73, 82, 54, 0), 1), new BoxItem(new Box("2", 72, 80, 57, 0), 1), new BoxItem(new Box("3", 73, 83, 53, 0), 1), new BoxItem(new Box("4", 71, 83, 53, 0), 1), new BoxItem(new Box("5", 74, 83, 53, 0), 1), new BoxItem(new Box("6", 74, 82, 54, 0), 1) ); Container fits1 = bruteForcePackager.pack(products1); assertNotNull(fits1); }
@Test void testPackagerWith3ContainersAnd1BoxItemUsingSingleConstraint() { final List<Container> containers = Arrays.asList( new ValidatingContainer(1,1,4417,1), new ValidatingContainer(4417,1,1,1), new ValidatingContainer(1,4417,1,1)); final List<BoxItem> packets = Collections.singletonList(new BoxItem(new Box(1, 1, 4417,1), 1)); final long deadline = System.currentTimeMillis() + 300; final Container pack = new BruteForcePackager(containers).pack(packets, deadline); assertThat(pack).isNotNull(); }
@Test void testExpansionOfRemainderSpace() { List<Container> containers = new ArrayList<>(); Container container = new ValidatingContainer("X",100, 36, 1, 1000); containers.add(container); List<BoxItem> products = new ArrayList<>(); products.add(new BoxItem(new Box("1", 1, 18, 90, 1))); products.add(new BoxItem(new Box("2", 1, 18, 90, 1))); products.add(new BoxItem(new Box("3", 1, 36, 3, 1))); products.add(new BoxItem(new Box("4", 1, 36, 3, 1))); BruteForcePackager packager = new BruteForcePackager(containers, true, true, 1); Container pack = packager.pack(products, Long.MAX_VALUE); print(pack); assertEquals(pack.getLevels().size(), 1); }
@Test void testBouwcampCodes33x32A() { BouwkampCodeDirectory directory = BouwkampCodeDirectory.getInstance(); BouwkampCode bkpLine = directory.codesForCount(9, "33x32A"); List<Container> containers = new ArrayList<>(); containers.add(toContainer(bkpLine)); BruteForcePackager packager = new BruteForcePackager(containers); List<BoxItem> products = new ArrayList<>(); List<Box> boxes = toBoxes(bkpLine); for(Box box : boxes) { products.add(new BoxItem(box, 1)); } Container fits = packager.pack(products); assertNull(fits); }
@Test void testBouwcampCodes() { BouwkampCodeDirectory directory = BouwkampCodeDirectory.getInstance(); List<BouwkampCodes> codesForCount = directory.codesForCount(10); for(BouwkampCodes c : codesForCount) { for(BouwkampCode bkpLine : c.getCodes()) { List<Container> containers = new ArrayList<>(); containers.add(toContainer(bkpLine)); BruteForcePackager packager = new BruteForcePackager(containers); List<BoxItem> products = new ArrayList<>(); List<Box> boxes = toBoxes(bkpLine); for(Box box : boxes) { products.add(new BoxItem(box, 1)); } Container fits = packager.pack(products); assertNull(fits); } } }
@Test void testStackingRectanglesOnSquareWithReuse() { int n = 12; int boxesPerLevel = 4; List<Container> containers = new ArrayList<>(); int levels = n / boxesPerLevel + (n % boxesPerLevel > 0 ? 1 : 0); containers.add(new ValidatingContainer(2 * boxesPerLevel / 2, 1 * (boxesPerLevel / 2), 10 * levels, 0)); List<BoxItem> identialProducts = new ArrayList<>(); for(int i = 0; i < n - boxesPerLevel; i++) { Box box = new Box(Integer.toString(i), 1, 2, 10, 0); identialProducts.add(new BoxItem(box, 1)); } Box box = new Box("a", 3, 1, 10, 0); box.rotate3D(); identialProducts.add(new BoxItem(box, 1)); box = new Box("b", 3, 1, 10, 0); box.rotate3D(); identialProducts.add(new BoxItem(box, 1)); box = new Box("c", 2, 1, 10, 0); box.rotate3D(); identialProducts.add(new BoxItem(box, 1)); BruteForcePackager packager = new BruteForcePackager(containers); Container fits = packager.pack(identialProducts); assertNotNull(fits); assertEquals(fits.getLevels().size(), levels); }