diff --git a/api/src/main/java/com/github/skjolber/packing/api/Packager.java b/api/src/main/java/com/github/skjolber/packing/api/Packager.java index beb029c6..1c79e32e 100644 --- a/api/src/main/java/com/github/skjolber/packing/api/Packager.java +++ b/api/src/main/java/com/github/skjolber/packing/api/Packager.java @@ -1,12 +1,14 @@ package com.github.skjolber.packing.api; +import java.io.Closeable; + /** * Fit boxes into container, i.e. perform bin packing to a single container. * * Thread-safe implementation. */ -public interface Packager> { +public interface Packager> extends Closeable { B newResultBuilder(); diff --git a/core/src/main/java/com/github/skjolber/packing/deadline/DeadlineCheckPackagerInterruptSupplier.java b/core/src/main/java/com/github/skjolber/packing/deadline/DeadlineCheckPackagerInterruptSupplier.java index 43d7b900..9d7bf7c6 100644 --- a/core/src/main/java/com/github/skjolber/packing/deadline/DeadlineCheckPackagerInterruptSupplier.java +++ b/core/src/main/java/com/github/skjolber/packing/deadline/DeadlineCheckPackagerInterruptSupplier.java @@ -1,17 +1,34 @@ package com.github.skjolber.packing.deadline; -public class DeadlineCheckPackagerInterruptSupplier implements PackagerInterruptSupplier { +import java.io.Closeable; +import java.util.concurrent.ScheduledFuture; - protected final long deadline; +public class DeadlineCheckPackagerInterruptSupplier implements PackagerInterruptSupplier, Runnable, Closeable { - public DeadlineCheckPackagerInterruptSupplier(long deadline) { - super(); - this.deadline = deadline; + // this is not entirely accurate for multi-threading, but close enough + // (should have been volatile) + protected boolean expired = false; + protected ScheduledFuture future; + + public DeadlineCheckPackagerInterruptSupplier() { } @Override public boolean getAsBoolean() { - return System.currentTimeMillis() > deadline; + return expired; + } + + @Override + public void run() { + this.expired = true; + } + + public void close() { + future.cancel(true); + } + + public void setFuture(ScheduledFuture future) { + this.future = future; } } \ No newline at end of file diff --git a/core/src/main/java/com/github/skjolber/packing/deadline/DelegateDeadlineCheckPackagerInterruptSupplier.java b/core/src/main/java/com/github/skjolber/packing/deadline/DelegateDeadlineCheckPackagerInterruptSupplier.java index ca5d19f5..5120a6fa 100644 --- a/core/src/main/java/com/github/skjolber/packing/deadline/DelegateDeadlineCheckPackagerInterruptSupplier.java +++ b/core/src/main/java/com/github/skjolber/packing/deadline/DelegateDeadlineCheckPackagerInterruptSupplier.java @@ -1,21 +1,38 @@ package com.github.skjolber.packing.deadline; +import java.io.Closeable; +import java.util.concurrent.ScheduledFuture; import java.util.function.BooleanSupplier; -public class DelegateDeadlineCheckPackagerInterruptSupplier implements PackagerInterruptSupplier { +public class DelegateDeadlineCheckPackagerInterruptSupplier implements PackagerInterruptSupplier, Runnable, Closeable { + // this is not entirely accurate for multi-threading, but close enough + // (should have been volatile) + protected boolean expired = false; + protected ScheduledFuture future; protected final BooleanSupplier delegate; - protected final long deadline; - - public DelegateDeadlineCheckPackagerInterruptSupplier(long deadline, BooleanSupplier delegate) { + + public DelegateDeadlineCheckPackagerInterruptSupplier(BooleanSupplier delegate) { super(); - this.deadline = deadline; this.delegate = delegate; } @Override public boolean getAsBoolean() { - return delegate.getAsBoolean() || System.currentTimeMillis() > deadline; + return expired || delegate.getAsBoolean(); + } + + @Override + public void run() { + this.expired = true; + } + + public void close() { + future.cancel(true); + } + + public void setFuture(ScheduledFuture future) { + this.future = future; } } \ No newline at end of file diff --git a/core/src/main/java/com/github/skjolber/packing/deadline/DelegateNthDeadlineCheckPackagerInterruptSupplier.java b/core/src/main/java/com/github/skjolber/packing/deadline/DelegateNthDeadlineCheckPackagerInterruptSupplier.java deleted file mode 100644 index 69de9877..00000000 --- a/core/src/main/java/com/github/skjolber/packing/deadline/DelegateNthDeadlineCheckPackagerInterruptSupplier.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.github.skjolber.packing.deadline; - -import java.util.function.BooleanSupplier; - -public class DelegateNthDeadlineCheckPackagerInterruptSupplier implements ClonablePackagerInterruptSupplier { - - protected final BooleanSupplier delegate; - protected final int checkpointsPerDeadlineCheck; - protected final long deadline; - protected int count = 0; - public long t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16 = 1L; - - public DelegateNthDeadlineCheckPackagerInterruptSupplier(long deadline, int checkpointsPerDeadlineCheck, BooleanSupplier delegate) { - super(); - this.deadline = deadline; - this.checkpointsPerDeadlineCheck = checkpointsPerDeadlineCheck; - this.delegate = delegate; - } - - @Override - public boolean getAsBoolean() { - return delegate.getAsBoolean() || (--count % checkpointsPerDeadlineCheck == 0 && System.currentTimeMillis() > deadline); - } - - @Override - public ClonablePackagerInterruptSupplier clone() { - return new DelegateNthDeadlineCheckPackagerInterruptSupplier(deadline, checkpointsPerDeadlineCheck, delegate); - } - - public long preventOptmisation() { - return t1 + t2 + t3 + t4 + t5 + t6 + t7 + t8 + t9 + t10 + t11 + t12 + t13 + t14 + t15 + t16; - } - -} \ No newline at end of file diff --git a/core/src/main/java/com/github/skjolber/packing/deadline/NthDeadlineCheckPackagerInterruptSupplier.java b/core/src/main/java/com/github/skjolber/packing/deadline/NthDeadlineCheckPackagerInterruptSupplier.java deleted file mode 100644 index 70224424..00000000 --- a/core/src/main/java/com/github/skjolber/packing/deadline/NthDeadlineCheckPackagerInterruptSupplier.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.github.skjolber.packing.deadline; - -public class NthDeadlineCheckPackagerInterruptSupplier implements ClonablePackagerInterruptSupplier { - - protected final int checkpointsPerDeadlineCheck; - protected final long deadline; - protected int count = 0; - public long t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16 = 1L; - - public NthDeadlineCheckPackagerInterruptSupplier(long deadline, int checkpointsPerDeadlineCheck) { - super(); - this.deadline = deadline; - this.checkpointsPerDeadlineCheck = checkpointsPerDeadlineCheck; - } - - @Override - public boolean getAsBoolean() { - return --count % checkpointsPerDeadlineCheck == 0 && System.currentTimeMillis() > deadline; - } - - @Override - public ClonablePackagerInterruptSupplier clone() { - return new NthDeadlineCheckPackagerInterruptSupplier(deadline, checkpointsPerDeadlineCheck); - } - - public long preventOptmisation() { - return t1 + t2 + t3 + t4 + t5 + t6 + t7 + t8 + t9 + t10 + t11 + t12 + t13 + t14 + t15 + t16; - } -} \ No newline at end of file diff --git a/core/src/main/java/com/github/skjolber/packing/deadline/NthPackagerInterruptSupplier.java b/core/src/main/java/com/github/skjolber/packing/deadline/NthPackagerInterruptSupplier.java deleted file mode 100644 index 59d4ba36..00000000 --- a/core/src/main/java/com/github/skjolber/packing/deadline/NthPackagerInterruptSupplier.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.github.skjolber.packing.deadline; - -import java.util.concurrent.atomic.AtomicBoolean; - -public class NthPackagerInterruptSupplier implements ClonablePackagerInterruptSupplier { - - protected final int checkpointsPerDeadlineCheck; - protected int count = 0; - public long t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16 = 1L; - protected AtomicBoolean atomicBoolean; - - public NthPackagerInterruptSupplier(AtomicBoolean atomicBoolean, int checkpointsPerDeadlineCheck) { - super(); - this.atomicBoolean = atomicBoolean; - this.checkpointsPerDeadlineCheck = checkpointsPerDeadlineCheck; - } - - @Override - public boolean getAsBoolean() { - return --count % checkpointsPerDeadlineCheck == 0 && atomicBoolean.get(); - } - - @Override - public ClonablePackagerInterruptSupplier clone() { - return new NthPackagerInterruptSupplier(atomicBoolean, checkpointsPerDeadlineCheck); - } - - public long preventOptmisation() { - return t1 + t2 + t3 + t4 + t5 + t6 + t7 + t8 + t9 + t10 + t11 + t12 + t13 + t14 + t15 + t16; - } -} \ No newline at end of file diff --git a/core/src/main/java/com/github/skjolber/packing/deadline/PackagerInterruptSupplier.java b/core/src/main/java/com/github/skjolber/packing/deadline/PackagerInterruptSupplier.java index 94fa1623..145de14c 100644 --- a/core/src/main/java/com/github/skjolber/packing/deadline/PackagerInterruptSupplier.java +++ b/core/src/main/java/com/github/skjolber/packing/deadline/PackagerInterruptSupplier.java @@ -1,7 +1,9 @@ package com.github.skjolber.packing.deadline; +import java.io.Closeable; + @FunctionalInterface -public interface PackagerInterruptSupplier { +public interface PackagerInterruptSupplier extends Closeable { /** * Gets a result. @@ -9,4 +11,7 @@ public interface PackagerInterruptSupplier { * @return a result */ boolean getAsBoolean(); + + default void close() { + } } diff --git a/core/src/main/java/com/github/skjolber/packing/deadline/PackagerInterruptSupplierBuilder.java b/core/src/main/java/com/github/skjolber/packing/deadline/PackagerInterruptSupplierBuilder.java index 9d95cc2a..887f2cd8 100644 --- a/core/src/main/java/com/github/skjolber/packing/deadline/PackagerInterruptSupplierBuilder.java +++ b/core/src/main/java/com/github/skjolber/packing/deadline/PackagerInterruptSupplierBuilder.java @@ -1,5 +1,8 @@ package com.github.skjolber.packing.deadline; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.TimeUnit; import java.util.function.BooleanSupplier; public class PackagerInterruptSupplierBuilder { @@ -7,16 +10,15 @@ public class PackagerInterruptSupplierBuilder { public static final NegativePackagerInterruptSupplier NOOP = new NegativePackagerInterruptSupplier(); private long deadline = Long.MAX_VALUE; - private int checkpointsPerDeadlineCheck = 1; private BooleanSupplier interrupt = null; + private ScheduledThreadPoolExecutor scheduledThreadPoolExecutor; public static PackagerInterruptSupplierBuilder builder() { return new PackagerInterruptSupplierBuilder(); } - public PackagerInterruptSupplierBuilder withDeadline(long deadline, int checkpointsPerDeadlineCheck) { + public PackagerInterruptSupplierBuilder withDeadline(long deadline) { this.deadline = deadline; - this.checkpointsPerDeadlineCheck = checkpointsPerDeadlineCheck; return this; } @@ -24,26 +26,36 @@ public PackagerInterruptSupplierBuilder withInterrupt(BooleanSupplier interrupt) this.interrupt = interrupt; return this; } + + public PackagerInterruptSupplierBuilder withScheduledThreadPoolExecutor(ScheduledThreadPoolExecutor executor) { + this.scheduledThreadPoolExecutor = executor; + return this; + } public PackagerInterruptSupplier build() { - if(checkpointsPerDeadlineCheck == Integer.MAX_VALUE || checkpointsPerDeadlineCheck == -1 || deadline == Long.MAX_VALUE || deadline == -1L) { + if(deadline == Long.MAX_VALUE || deadline == -1L) { // no deadline if(interrupt != null) { return new DefaultPackagerInterrupt(interrupt); } return NOOP; } - - if(checkpointsPerDeadlineCheck == 1) { - if(interrupt == null) { - return new DeadlineCheckPackagerInterruptSupplier(deadline); - } - return new DelegateDeadlineCheckPackagerInterruptSupplier(deadline, interrupt); + + if(scheduledThreadPoolExecutor == null) { + throw new IllegalStateException("Expected scheduler"); } + if(interrupt == null) { - return new NthDeadlineCheckPackagerInterruptSupplier(deadline, checkpointsPerDeadlineCheck); + DeadlineCheckPackagerInterruptSupplier supplier = new DeadlineCheckPackagerInterruptSupplier(); + ScheduledFuture schedule = scheduledThreadPoolExecutor.schedule(supplier, deadline - System.currentTimeMillis(), TimeUnit.MILLISECONDS); + supplier.setFuture(schedule); + return supplier; } - return new DelegateNthDeadlineCheckPackagerInterruptSupplier(deadline, checkpointsPerDeadlineCheck, interrupt); + + DelegateDeadlineCheckPackagerInterruptSupplier supplier = new DelegateDeadlineCheckPackagerInterruptSupplier(interrupt); + ScheduledFuture schedule = scheduledThreadPoolExecutor.schedule(supplier, deadline - System.currentTimeMillis(), TimeUnit.MILLISECONDS); + supplier.setFuture(schedule); + return supplier; } } diff --git a/core/src/main/java/com/github/skjolber/packing/iterator/PermutationRotationIterator.java b/core/src/main/java/com/github/skjolber/packing/iterator/PermutationRotationIterator.java index ea05bc07..2a6948b6 100644 --- a/core/src/main/java/com/github/skjolber/packing/iterator/PermutationRotationIterator.java +++ b/core/src/main/java/com/github/skjolber/packing/iterator/PermutationRotationIterator.java @@ -40,7 +40,7 @@ public interface PermutationRotationIterator { /** * Get current permutations * - * @return + * @return current permutations array */ int[] getPermutations(); @@ -49,7 +49,7 @@ public interface PermutationRotationIterator { * * Get current length * - * @return + * @return current length of permutations array */ int length(); @@ -112,7 +112,7 @@ public interface PermutationRotationIterator { * * @param state previously saved state * @param length number of items - * @return + * @return current permutations + rotations */ List get(PermutationRotationState state, int length); diff --git a/core/src/main/java/com/github/skjolber/packing/packer/AbstractPackager.java b/core/src/main/java/com/github/skjolber/packing/packer/AbstractPackager.java index 536844df..21bb044c 100644 --- a/core/src/main/java/com/github/skjolber/packing/packer/AbstractPackager.java +++ b/core/src/main/java/com/github/skjolber/packing/packer/AbstractPackager.java @@ -3,6 +3,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.concurrent.ScheduledThreadPoolExecutor; import com.github.skjolber.packing.api.Container; import com.github.skjolber.packing.api.ContainerItem; @@ -27,18 +28,16 @@ public abstract class AbstractPackager

packList(List products, List stackables) { } return minArea; } - + + public void close() { + scheduledThreadPoolExecutor.shutdownNow(); + } + } diff --git a/core/src/main/java/com/github/skjolber/packing/packer/AbstractPackagerBuilder.java b/core/src/main/java/com/github/skjolber/packing/packer/AbstractPackagerBuilder.java index 5cf4b1a8..ea01b6a3 100644 --- a/core/src/main/java/com/github/skjolber/packing/packer/AbstractPackagerBuilder.java +++ b/core/src/main/java/com/github/skjolber/packing/packer/AbstractPackagerBuilder.java @@ -11,7 +11,6 @@ @SuppressWarnings({ "rawtypes", "unchecked" }) public abstract class AbstractPackagerBuilder

> { - protected int checkpointsPerDeadlineCheck = 1; protected PackResultComparator packResultComparator; public B withPackResultComparator(PackResultComparator packResultComparator) { @@ -20,11 +19,6 @@ public B withPackResultComparator(PackResultComparator packResultComparator) { return (B)this; } - public B withCheckpointsPerDeadlineCheck(int n) { - this.checkpointsPerDeadlineCheck = n; - return (B)this; - } - public abstract P build(); } diff --git a/core/src/main/java/com/github/skjolber/packing/packer/bruteforce/AbstractBruteForcePackager.java b/core/src/main/java/com/github/skjolber/packing/packer/bruteforce/AbstractBruteForcePackager.java index 03289860..853c50b4 100644 --- a/core/src/main/java/com/github/skjolber/packing/packer/bruteforce/AbstractBruteForcePackager.java +++ b/core/src/main/java/com/github/skjolber/packing/packer/bruteforce/AbstractBruteForcePackager.java @@ -3,6 +3,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.logging.Logger; import com.github.skjolber.packing.api.Container; @@ -36,13 +37,13 @@ public abstract class AbstractBruteForcePackager extends AbstractPackager getPlacements(int size) { @@ -250,4 +251,8 @@ private List> packStackPlacement(ExtremePoints3DStack ex return best; } + protected ScheduledThreadPoolExecutor getScheduledThreadPoolExecutor() { + return scheduledThreadPoolExecutor; + } + } diff --git a/core/src/main/java/com/github/skjolber/packing/packer/bruteforce/BruteForcePackager.java b/core/src/main/java/com/github/skjolber/packing/packer/bruteforce/BruteForcePackager.java index 07039bab..a0e7bfaa 100644 --- a/core/src/main/java/com/github/skjolber/packing/packer/bruteforce/BruteForcePackager.java +++ b/core/src/main/java/com/github/skjolber/packing/packer/bruteforce/BruteForcePackager.java @@ -44,7 +44,7 @@ public BruteForcePackager build() { if(packResultComparator == null) { packResultComparator = new DefaultPackResultComparator(); } - return new BruteForcePackager(checkpointsPerDeadlineCheck, packResultComparator); + return new BruteForcePackager(packResultComparator); } } @@ -125,8 +125,8 @@ public Container accept(BruteForcePackagerResult bruteForceResult) { } - public BruteForcePackager(int checkpointsPerDeadlineCheck, PackResultComparator packResultComparator) { - super(checkpointsPerDeadlineCheck, packResultComparator); + public BruteForcePackager(PackResultComparator packResultComparator) { + super(packResultComparator); } @Override diff --git a/core/src/main/java/com/github/skjolber/packing/packer/bruteforce/BruteForcePackagerResultBuilder.java b/core/src/main/java/com/github/skjolber/packing/packer/bruteforce/BruteForcePackagerResultBuilder.java index 8c0e82c1..4c70fb53 100644 --- a/core/src/main/java/com/github/skjolber/packing/packer/bruteforce/BruteForcePackagerResultBuilder.java +++ b/core/src/main/java/com/github/skjolber/packing/packer/bruteforce/BruteForcePackagerResultBuilder.java @@ -13,18 +13,11 @@ public class BruteForcePackagerResultBuilder extends PackagerResultBuilder packList = packager.pack(items, containers, maxContainerCount, build); - long duration = System.currentTimeMillis() - start; - if(packList == null) { - return new PackagerResult(Collections.emptyList(), duration, true); + try { + List packList = packager.pack(items, containers, maxContainerCount, build); + long duration = System.currentTimeMillis() - start; + if(packList == null) { + return new PackagerResult(Collections.emptyList(), duration, true); + } + return new PackagerResult(packList, duration, false); + } finally { + build.close(); } - return new PackagerResult(packList, duration, false); } } diff --git a/core/src/main/java/com/github/skjolber/packing/packer/bruteforce/FastBruteForcePackager.java b/core/src/main/java/com/github/skjolber/packing/packer/bruteforce/FastBruteForcePackager.java index 6ced168c..9d577a7f 100644 --- a/core/src/main/java/com/github/skjolber/packing/packer/bruteforce/FastBruteForcePackager.java +++ b/core/src/main/java/com/github/skjolber/packing/packer/bruteforce/FastBruteForcePackager.java @@ -3,6 +3,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.concurrent.ScheduledThreadPoolExecutor; import com.github.skjolber.packing.api.Container; import com.github.skjolber.packing.api.ContainerItem; @@ -50,7 +51,7 @@ public FastBruteForcePackager build() { if(packResultComparator == null) { packResultComparator = new DefaultPackResultComparator(); } - return new FastBruteForcePackager(checkpointsPerDeadlineCheck, packResultComparator); + return new FastBruteForcePackager(packResultComparator); } } @@ -140,13 +141,13 @@ public Container accept(BruteForcePackagerResult bruteForceResult) { } - public FastBruteForcePackager(int checkpointsPerDeadlineCheck, PackResultComparator packResultComparator) { - super(checkpointsPerDeadlineCheck, packResultComparator); + public FastBruteForcePackager(PackResultComparator packResultComparator) { + super(packResultComparator); } @Override public FastBruteForcePackagerResultBuilder newResultBuilder() { - return new FastBruteForcePackagerResultBuilder().withCheckpointsPerDeadlineCheck(checkpointsPerDeadlineCheck).withPackager(this); + return new FastBruteForcePackagerResultBuilder().withPackager(this); } @Override @@ -398,5 +399,9 @@ public int packStackPlacement(FastExtremePoints3DStack extremePoints3D, List packList = packager.pack(items, containers, maxContainerCount, build); - long duration = System.currentTimeMillis() - start; - if(packList == null) { - return new PackagerResult(Collections.emptyList(), duration, true); + try { + List packList = packager.pack(items, containers, maxContainerCount, build); + long duration = System.currentTimeMillis() - start; + if(packList == null) { + return new PackagerResult(Collections.emptyList(), duration, true); + } + return new PackagerResult(packList, duration, false); + } finally { + build.close(); } - return new PackagerResult(packList, duration, false); } - + + } diff --git a/core/src/main/java/com/github/skjolber/packing/packer/bruteforce/ParallelBruteForcePackager.java b/core/src/main/java/com/github/skjolber/packing/packer/bruteforce/ParallelBruteForcePackager.java index 123f6069..6509a02d 100644 --- a/core/src/main/java/com/github/skjolber/packing/packer/bruteforce/ParallelBruteForcePackager.java +++ b/core/src/main/java/com/github/skjolber/packing/packer/bruteforce/ParallelBruteForcePackager.java @@ -113,7 +113,7 @@ public ParallelBruteForcePackager build() { } } - return new ParallelBruteForcePackager(executorService, parallelizationCount, checkpointsPerDeadlineCheck, packResultComparator); + return new ParallelBruteForcePackager(executorService, parallelizationCount, packResultComparator); } } @@ -121,9 +121,9 @@ public ParallelBruteForcePackager build() { private final int parallelizationCount; private final ExecutorService executorService; - public ParallelBruteForcePackager(ExecutorService executorService, int parallelizationCount, int checkpointsPerDeadlineCheck, + public ParallelBruteForcePackager(ExecutorService executorService, int parallelizationCount, PackResultComparator packResultComparator) { - super(checkpointsPerDeadlineCheck, packResultComparator); + super(packResultComparator); this.parallelizationCount = parallelizationCount; this.executorService = executorService; diff --git a/core/src/main/java/com/github/skjolber/packing/packer/laff/AbstractLargestAreaFitFirstPackager.java b/core/src/main/java/com/github/skjolber/packing/packer/laff/AbstractLargestAreaFitFirstPackager.java index eef199d3..54268a87 100644 --- a/core/src/main/java/com/github/skjolber/packing/packer/laff/AbstractLargestAreaFitFirstPackager.java +++ b/core/src/main/java/com/github/skjolber/packing/packer/laff/AbstractLargestAreaFitFirstPackager.java @@ -2,6 +2,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.stream.Collectors; import com.github.skjolber.packing.api.Container; @@ -68,8 +69,8 @@ public abstract class AbstractLargestAreaFitFirstPackager

stackables, Container targetContainer, int index, PackagerInterruptSupplier interrupt); @@ -127,4 +128,8 @@ protected LAFFAdapter adapter(List boxes, List con return new LAFFAdapter(boxes, containers, interrupt); } + protected ScheduledThreadPoolExecutor getScheduledThreadPoolExecutor() { + return scheduledThreadPoolExecutor; + } + } diff --git a/core/src/main/java/com/github/skjolber/packing/packer/laff/FastLargestAreaFitFirstPackager.java b/core/src/main/java/com/github/skjolber/packing/packer/laff/FastLargestAreaFitFirstPackager.java index ffc44ba8..e36635c3 100644 --- a/core/src/main/java/com/github/skjolber/packing/packer/laff/FastLargestAreaFitFirstPackager.java +++ b/core/src/main/java/com/github/skjolber/packing/packer/laff/FastLargestAreaFitFirstPackager.java @@ -41,12 +41,12 @@ public FastLargestAreaFitFirstPackager build() { if(packResultComparator == null) { packResultComparator = new DefaultPackResultComparator(); } - return new FastLargestAreaFitFirstPackager(checkpointsPerDeadlineCheck, packResultComparator); + return new FastLargestAreaFitFirstPackager(packResultComparator); } } - public FastLargestAreaFitFirstPackager(int checkpointsPerDeadlineCheck, PackResultComparator packResultComparator) { - super(checkpointsPerDeadlineCheck, packResultComparator); + public FastLargestAreaFitFirstPackager(PackResultComparator packResultComparator) { + super(packResultComparator); } public DefaultPackResult pack(List stackables, Container targetContainer, int containerIndex, PackagerInterruptSupplier interrupt) { @@ -244,7 +244,7 @@ public DefaultPackResult pack(List stackables, Container targetContai @Override public LargestAreaFitFirstPackagerResultBuilder newResultBuilder() { - return new LargestAreaFitFirstPackagerResultBuilder().withCheckpointsPerDeadlineCheck(checkpointsPerDeadlineCheck).withPackager(this); + return new LargestAreaFitFirstPackagerResultBuilder().withPackager(this); } } diff --git a/core/src/main/java/com/github/skjolber/packing/packer/laff/LargestAreaFitFirstPackager.java b/core/src/main/java/com/github/skjolber/packing/packer/laff/LargestAreaFitFirstPackager.java index f99b1aa5..fa39ec3d 100644 --- a/core/src/main/java/com/github/skjolber/packing/packer/laff/LargestAreaFitFirstPackager.java +++ b/core/src/main/java/com/github/skjolber/packing/packer/laff/LargestAreaFitFirstPackager.java @@ -41,12 +41,12 @@ public LargestAreaFitFirstPackager build() { if(packResultComparator == null) { packResultComparator = new DefaultPackResultComparator(); } - return new LargestAreaFitFirstPackager(checkpointsPerDeadlineCheck, packResultComparator); + return new LargestAreaFitFirstPackager(packResultComparator); } } - public LargestAreaFitFirstPackager(int checkpointsPerDeadlineCheck, PackResultComparator packResultComparator) { - super(checkpointsPerDeadlineCheck, packResultComparator); + public LargestAreaFitFirstPackager(PackResultComparator packResultComparator) { + super(packResultComparator); } public DefaultPackResult pack(List stackables, Container targetContainer, int index, PackagerInterruptSupplier interrupt) { @@ -234,6 +234,6 @@ public DefaultPackResult pack(List stackables, Container targetContai @Override public LargestAreaFitFirstPackagerResultBuilder newResultBuilder() { - return new LargestAreaFitFirstPackagerResultBuilder().withCheckpointsPerDeadlineCheck(checkpointsPerDeadlineCheck).withPackager(this); + return new LargestAreaFitFirstPackagerResultBuilder().withPackager(this); } } diff --git a/core/src/main/java/com/github/skjolber/packing/packer/laff/LargestAreaFitFirstPackagerResultBuilder.java b/core/src/main/java/com/github/skjolber/packing/packer/laff/LargestAreaFitFirstPackagerResultBuilder.java index 951cb980..b1a34559 100644 --- a/core/src/main/java/com/github/skjolber/packing/packer/laff/LargestAreaFitFirstPackagerResultBuilder.java +++ b/core/src/main/java/com/github/skjolber/packing/packer/laff/LargestAreaFitFirstPackagerResultBuilder.java @@ -13,18 +13,11 @@ public class LargestAreaFitFirstPackagerResultBuilder extends PackagerResultBuil private AbstractLargestAreaFitFirstPackager packager; - private int checkpointsPerDeadlineCheck = 1; - public LargestAreaFitFirstPackagerResultBuilder withPackager(AbstractLargestAreaFitFirstPackager packager) { this.packager = packager; return this; } - public LargestAreaFitFirstPackagerResultBuilder withCheckpointsPerDeadlineCheck(int n) { - this.checkpointsPerDeadlineCheck = n; - return this; - } - public PackagerResult build() { if(maxContainerCount <= 0) { throw new IllegalStateException(); @@ -39,20 +32,25 @@ public PackagerResult build() { PackagerInterruptSupplierBuilder booleanSupplierBuilder = PackagerInterruptSupplierBuilder.builder(); if(deadline != -1L) { - booleanSupplierBuilder.withDeadline(deadline, checkpointsPerDeadlineCheck); + booleanSupplierBuilder.withDeadline(deadline); } if(interrupt != null) { booleanSupplierBuilder.withInterrupt(interrupt); } - PackagerInterruptSupplier build = booleanSupplierBuilder.build(); + booleanSupplierBuilder.withScheduledThreadPoolExecutor(packager.getScheduledThreadPoolExecutor()); - List packList = packager.pack(items, containers, maxContainerCount, build); - long duration = System.currentTimeMillis() - start; - if(packList == null) { - return new PackagerResult(Collections.emptyList(), duration, true); + PackagerInterruptSupplier build = booleanSupplierBuilder.build(); + try { + List packList = packager.pack(items, containers, maxContainerCount, build); + long duration = System.currentTimeMillis() - start; + if(packList == null) { + return new PackagerResult(Collections.emptyList(), duration, true); + } + return new PackagerResult(packList, duration, false); + } finally { + build.close(); } - return new PackagerResult(packList, duration, false); } } diff --git a/core/src/main/java/com/github/skjolber/packing/packer/plain/AbstractPlainPackager.java b/core/src/main/java/com/github/skjolber/packing/packer/plain/AbstractPlainPackager.java index 65d04471..e5cc5a43 100644 --- a/core/src/main/java/com/github/skjolber/packing/packer/plain/AbstractPlainPackager.java +++ b/core/src/main/java/com/github/skjolber/packing/packer/plain/AbstractPlainPackager.java @@ -2,6 +2,7 @@ import java.util.LinkedList; import java.util.List; +import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.stream.Collectors; import com.github.skjolber.packing.api.Container; @@ -25,8 +26,8 @@ */ public abstract class AbstractPlainPackager

> extends AbstractPackager { - public AbstractPlainPackager(int checkpointsPerDeadlineCheck, PackResultComparator packResultComparator) { - super(checkpointsPerDeadlineCheck, packResultComparator); + public AbstractPlainPackager(PackResultComparator packResultComparator) { + super(packResultComparator); } public abstract DefaultPackResult pack(List stackables, Container targetContainer, int containerIndex, PackagerInterruptSupplier interrupt); @@ -84,4 +85,8 @@ protected PlainAdapter adapter(List boxes, List co return new PlainAdapter(boxes, containers, interrupt); } + protected ScheduledThreadPoolExecutor getScheduledThreadPoolExecutor() { + return scheduledThreadPoolExecutor; + } + } diff --git a/core/src/main/java/com/github/skjolber/packing/packer/plain/PlainPackager.java b/core/src/main/java/com/github/skjolber/packing/packer/plain/PlainPackager.java index cb783747..2479b893 100644 --- a/core/src/main/java/com/github/skjolber/packing/packer/plain/PlainPackager.java +++ b/core/src/main/java/com/github/skjolber/packing/packer/plain/PlainPackager.java @@ -40,12 +40,12 @@ public PlainPackager build() { if(packResultComparator == null) { packResultComparator = new DefaultPackResultComparator(); } - return new PlainPackager(checkpointsPerDeadlineCheck, packResultComparator); + return new PlainPackager(packResultComparator); } } - public PlainPackager(int checkpointsPerDeadlineCheck, PackResultComparator packResultComparator) { - super(checkpointsPerDeadlineCheck, packResultComparator); + public PlainPackager(PackResultComparator packResultComparator) { + super(packResultComparator); } public DefaultPackResult pack(List stackables, Container targetContainer, int index, PackagerInterruptSupplier interrupt) { @@ -271,6 +271,6 @@ protected boolean isBetter(Stackable referenceStackable, Stackable potentiallyBe @Override public PlainPackagerResultBuilder newResultBuilder() { - return new PlainPackagerResultBuilder().withCheckpointsPerDeadlineCheck(checkpointsPerDeadlineCheck).withPackager(this); + return new PlainPackagerResultBuilder().withPackager(this); } } diff --git a/core/src/main/java/com/github/skjolber/packing/packer/plain/PlainPackagerResultBuilder.java b/core/src/main/java/com/github/skjolber/packing/packer/plain/PlainPackagerResultBuilder.java index 164fb542..0dd89a2b 100644 --- a/core/src/main/java/com/github/skjolber/packing/packer/plain/PlainPackagerResultBuilder.java +++ b/core/src/main/java/com/github/skjolber/packing/packer/plain/PlainPackagerResultBuilder.java @@ -13,18 +13,11 @@ public class PlainPackagerResultBuilder extends PackagerResultBuilder packager; - private int checkpointsPerDeadlineCheck = 1; - public PlainPackagerResultBuilder withPackager(AbstractPlainPackager packager) { this.packager = packager; return this; } - public PlainPackagerResultBuilder withCheckpointsPerDeadlineCheck(int n) { - this.checkpointsPerDeadlineCheck = n; - return this; - } - public PackagerResult build() { if(maxContainerCount <= 0) { throw new IllegalStateException(); @@ -39,19 +32,25 @@ public PackagerResult build() { PackagerInterruptSupplierBuilder booleanSupplierBuilder = PackagerInterruptSupplierBuilder.builder(); if(deadline != -1L) { - booleanSupplierBuilder.withDeadline(deadline, checkpointsPerDeadlineCheck); + booleanSupplierBuilder.withDeadline(deadline); } if(interrupt != null) { booleanSupplierBuilder.withInterrupt(interrupt); } + booleanSupplierBuilder.withScheduledThreadPoolExecutor(packager.getScheduledThreadPoolExecutor()); + PackagerInterruptSupplier build = booleanSupplierBuilder.build(); - List packList = packager.pack(items, containers, maxContainerCount, build); - long duration = System.currentTimeMillis() - start; - if(packList == null) { - return new PackagerResult(Collections.emptyList(), duration, true); + try { + List packList = packager.pack(items, containers, maxContainerCount, build); + long duration = System.currentTimeMillis() - start; + if(packList == null) { + return new PackagerResult(Collections.emptyList(), duration, true); + } + return new PackagerResult(packList, duration, false); + } finally { + build.close(); } - return new PackagerResult(packList, duration, false); } } diff --git a/core/src/test/java/com/github/skjolber/packing/packer/bruteforce/AbstractBruteForcePackagerTest.java b/core/src/test/java/com/github/skjolber/packing/packer/bruteforce/AbstractBruteForcePackagerTest.java index 82de0b54..87376d06 100644 --- a/core/src/test/java/com/github/skjolber/packing/packer/bruteforce/AbstractBruteForcePackagerTest.java +++ b/core/src/test/java/com/github/skjolber/packing/packer/bruteforce/AbstractBruteForcePackagerTest.java @@ -41,15 +41,18 @@ public void testImpossible1() throws Exception { ); AbstractPackager packager = createPackager(); - - PackagerResult build = packager.newResultBuilder() - .withContainers(ContainerItem.newListBuilder() - .withContainer(container) - .build()) - .withStackables(b1) - .build(); - - assertFalse(build.isSuccess()); + try { + PackagerResult build = packager.newResultBuilder() + .withContainers(ContainerItem.newListBuilder() + .withContainer(container) + .build()) + .withStackables(b1) + .build(); + + assertFalse(build.isSuccess()); + } finally { + packager.close(); + } } @Test @@ -62,17 +65,20 @@ public void testImpossible2() { .build(); AbstractPackager packager = createPackager(); - - List products = Arrays.asList( - box(3,7,35,1)); - - PackagerResult build = packager - .newResultBuilder() - .withContainers(containerItems) - .withStackables(products) - .build(); - - assertFalse(build.isSuccess()); + try { + List products = Arrays.asList( + box(3,7,35,1)); + + PackagerResult build = packager + .newResultBuilder() + .withContainers(containerItems) + .withStackables(products) + .build(); + + assertFalse(build.isSuccess()); + } finally { + packager.close(); + } } @Test @@ -84,18 +90,21 @@ public void testImpossible3() { .build(); AbstractPackager packager = createPackager(); - - List products = Arrays.asList( - box(1,1,1,1), box(3,7,35,1)); - - PackagerResult build = packager - .newResultBuilder() - .withContainers(containerItems) - .withStackables(products) - .withMaxContainerCount(3) - .build(); - - assertFalse(build.isSuccess()); + try { + List products = Arrays.asList( + box(1,1,1,1), box(3,7,35,1)); + + PackagerResult build = packager + .newResultBuilder() + .withContainers(containerItems) + .withStackables(products) + .withMaxContainerCount(3) + .build(); + + assertFalse(build.isSuccess()); + } finally { + packager.close(); + } } @@ -138,16 +147,19 @@ public void testMutuallyExclusiveBoxesAndContainersForMultiContainerResult() thr ); AbstractPackager packager = createPackager(); - - PackagerResult build = packager.newResultBuilder() - .withContainers(ContainerItem.newListBuilder() - .withContainers(thin, thick) - .build()) - .withStackables(thinBox, thickBox) - .withMaxContainerCount(2) - .build(); - - assertTrue(build.isSuccess()); + try { + PackagerResult build = packager.newResultBuilder() + .withContainers(ContainerItem.newListBuilder() + .withContainers(thin, thick) + .build()) + .withStackables(thinBox, thickBox) + .withMaxContainerCount(2) + .build(); + + assertTrue(build.isSuccess()); + } finally { + packager.close(); + } } protected abstract AbstractPackager createPackager(); diff --git a/core/src/test/java/com/github/skjolber/packing/packer/bruteforce/BruteForcePackagerTest.java b/core/src/test/java/com/github/skjolber/packing/packer/bruteforce/BruteForcePackagerTest.java index 01ca1f21..4c374006 100644 --- a/core/src/test/java/com/github/skjolber/packing/packer/bruteforce/BruteForcePackagerTest.java +++ b/core/src/test/java/com/github/skjolber/packing/packer/bruteforce/BruteForcePackagerTest.java @@ -47,26 +47,29 @@ void testStackingSquaresOnSquare() { .build(); BruteForcePackager packager = BruteForcePackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); - - PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - List containers = build.getContainers(); - assertValid(containers); - - List placements = containers.get(0).getStack().getPlacements(); - - assertThat(placements.get(0)).isAt(0, 0, 0).hasStackableName("A"); - assertThat(placements.get(1)).isAt(1, 0, 0).hasStackableName("B"); - assertThat(placements.get(2)).isAt(2, 0, 0).hasStackableName("C"); - - assertThat(placements.get(0)).isAlongsideX(placements.get(1)); - assertThat(placements.get(2)).followsAlongsideX(placements.get(1)); - assertThat(placements.get(1)).preceedsAlongsideX(placements.get(2)); + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); + + PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + List containers = build.getContainers(); + assertValid(containers); + + List placements = containers.get(0).getStack().getPlacements(); + + assertThat(placements.get(0)).isAt(0, 0, 0).hasStackableName("A"); + assertThat(placements.get(1)).isAt(1, 0, 0).hasStackableName("B"); + assertThat(placements.get(2)).isAt(2, 0, 0).hasStackableName("C"); + + assertThat(placements.get(0)).isAlongsideX(placements.get(1)); + assertThat(placements.get(2)).followsAlongsideX(placements.get(1)); + assertThat(placements.get(1)).preceedsAlongsideX(placements.get(2)); + } finally { + packager.close(); + } } @Test @@ -78,31 +81,34 @@ void testStackMultipleContainers() { .build(); BruteForcePackager packager = BruteForcePackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 2)); - products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 2)); - products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 2)); - - PackagerResult build = packager.newResultBuilder().withContainers(containers).withStackables(products).withMaxContainerCount(5).build(); - - List packList = build.getContainers(); - - assertValid(packList); - assertThat(packList).hasSize(2); - - Container fits = packList.get(0); - - List placements = fits.getStack().getPlacements(); - - assertThat(placements.get(0)).isAt(0, 0, 0).hasStackableName("A"); - assertThat(placements.get(1)).isAt(1, 0, 0).hasStackableName("A"); - assertThat(placements.get(2)).isAt(2, 0, 0).hasStackableName("B"); - - assertThat(placements.get(0)).isAlongsideX(placements.get(1)); - assertThat(placements.get(2)).followsAlongsideX(placements.get(1)); - assertThat(placements.get(1)).preceedsAlongsideX(placements.get(2)); + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 2)); + products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 2)); + products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 2)); + + PackagerResult build = packager.newResultBuilder().withContainers(containers).withStackables(products).withMaxContainerCount(5).build(); + + List packList = build.getContainers(); + + assertValid(packList); + assertThat(packList).hasSize(2); + + Container fits = packList.get(0); + + List placements = fits.getStack().getPlacements(); + + assertThat(placements.get(0)).isAt(0, 0, 0).hasStackableName("A"); + assertThat(placements.get(1)).isAt(1, 0, 0).hasStackableName("A"); + assertThat(placements.get(2)).isAt(2, 0, 0).hasStackableName("B"); + + assertThat(placements.get(0)).isAlongsideX(placements.get(1)); + assertThat(placements.get(2)).followsAlongsideX(placements.get(1)); + assertThat(placements.get(1)).preceedsAlongsideX(placements.get(2)); + } finally { + packager.close(); + } } @Test @@ -114,22 +120,25 @@ void testStackingBinary1() { .build(); BruteForcePackager packager = BruteForcePackager.newBuilder().build(); - - List products = new ArrayList<>(); - products.add(new StackableItem(Box.newBuilder().withDescription("J").withRotate3D().withSize(4, 4, 1).withWeight(1).build(), 1)); - - for (int i = 0; i < 4; i++) { - products.add(new StackableItem(Box.newBuilder().withDescription("K").withRotate3D().withSize(2, 2, 1).withWeight(1).build(), 1)); - } - for (int i = 0; i < 16; i++) { - products.add(new StackableItem(Box.newBuilder().withDescription("K").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); + try { + List products = new ArrayList<>(); + products.add(new StackableItem(Box.newBuilder().withDescription("J").withRotate3D().withSize(4, 4, 1).withWeight(1).build(), 1)); + + for (int i = 0; i < 4; i++) { + products.add(new StackableItem(Box.newBuilder().withDescription("K").withRotate3D().withSize(2, 2, 1).withWeight(1).build(), 1)); + } + for (int i = 0; i < 16; i++) { + products.add(new StackableItem(Box.newBuilder().withDescription("K").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); + } + + PackagerResult build = packager.newResultBuilder().withContainers(containers).withStackables(products).build(); + + Container fits = build.getContainers().get(0); + assertValid(fits); + assertEquals(products.size(), fits.getStack().getPlacements().size()); + } finally { + packager.close(); } - - PackagerResult build = packager.newResultBuilder().withContainers(containers).withStackables(products).build(); - - Container fits = build.getContainers().get(0); - assertValid(fits); - assertEquals(products.size(), fits.getStack().getPlacements().size()); } @Test @@ -141,18 +150,21 @@ public void testStackingRectanglesOnSquareRectangleVolumeFirst() { .build(); BruteForcePackager packager = BruteForcePackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("J").withRotate3D().withSize(5, 10, 4).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("L").withRotate3D().withSize(5, 10, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("J").withRotate3D().withSize(5, 10, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("M").withRotate3D().withSize(5, 10, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("N").withRotate3D().withSize(5, 10, 1).withWeight(1).build(), 1)); - - PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - - assertValid(build); + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("J").withRotate3D().withSize(5, 10, 4).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("L").withRotate3D().withSize(5, 10, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("J").withRotate3D().withSize(5, 10, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("M").withRotate3D().withSize(5, 10, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("N").withRotate3D().withSize(5, 10, 1).withWeight(1).build(), 1)); + + PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + + assertValid(build); + } finally { + packager.close(); + } } @Test @@ -164,16 +176,19 @@ public void testStackingBox() { .build(); BruteForcePackager packager = BruteForcePackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("A").withSize(3, 2, 1).withRotate3D().withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("B").withSize(3, 2, 1).withRotate3D().withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("C").withSize(3, 2, 1).withRotate3D().withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("D").withSize(3, 2, 1).withRotate3D().withWeight(1).build(), 1)); - - PackagerResult build = packager.newResultBuilder().withContainers(containers).withStackables(products).build(); - assertValid(build); + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("A").withSize(3, 2, 1).withRotate3D().withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("B").withSize(3, 2, 1).withRotate3D().withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("C").withSize(3, 2, 1).withRotate3D().withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("D").withSize(3, 2, 1).withRotate3D().withWeight(1).build(), 1)); + + PackagerResult build = packager.newResultBuilder().withContainers(containers).withStackables(products).build(); + assertValid(build); + } finally { + packager.close(); + } } @Test @@ -217,32 +232,35 @@ protected void pack(BouwkampCode bouwkampCode) { .build(); BruteForcePackager packager = BruteForcePackager.newBuilder().build(); - - List products = new ArrayList<>(); - - List squares = new ArrayList<>(); - for (BouwkampCodeLine bouwkampCodeLine : bouwkampCode.getLines()) { - squares.addAll(bouwkampCodeLine.getSquares()); - } - - // map similar items to the same stack item - this actually helps a lot - Map frequencyMap = new TreeMap<>(); - squares.forEach(word -> frequencyMap.merge(word, 1, (v, newV) -> v + newV)); - - for (Entry entry : frequencyMap.entrySet()) { - int square = entry.getKey(); - int count = entry.getValue(); - products.add(new StackableItem(Box.newBuilder().withDescription(Integer.toString(square)).withSize(square, square, 1).withRotate3D().withWeight(1).build(), count)); + try { + List products = new ArrayList<>(); + + List squares = new ArrayList<>(); + for (BouwkampCodeLine bouwkampCodeLine : bouwkampCode.getLines()) { + squares.addAll(bouwkampCodeLine.getSquares()); + } + + // map similar items to the same stack item - this actually helps a lot + Map frequencyMap = new TreeMap<>(); + squares.forEach(word -> frequencyMap.merge(word, 1, (v, newV) -> v + newV)); + + for (Entry entry : frequencyMap.entrySet()) { + int square = entry.getKey(); + int count = entry.getValue(); + products.add(new StackableItem(Box.newBuilder().withDescription(Integer.toString(square)).withSize(square, square, 1).withRotate3D().withWeight(1).build(), count)); + } + + Collections.shuffle(products); + + PackagerResult build = packager.newResultBuilder().withContainers(containers).withStackables(products).build(); + + Container fits = build.getContainers().get(0); + assertNotNull(bouwkampCode.getName(), fits); + assertValid(fits); + assertEquals(bouwkampCode.getName(), fits.getStack().getSize(), squares.size()); + } finally { + packager.close(); } - - Collections.shuffle(products); - - PackagerResult build = packager.newResultBuilder().withContainers(containers).withStackables(products).build(); - - Container fits = build.getContainers().get(0); - assertNotNull(bouwkampCode.getName(), fits); - assertValid(fits); - assertEquals(bouwkampCode.getName(), fits.getStack().getSize(), squares.size()); } @Test @@ -264,20 +282,24 @@ public void issueNew() { .newBuilder() .build(); - List products = Arrays.asList( - new StackableItem(Box.newBuilder().withId("1").withSize(200, 2, 50).withRotate3D().withWeight(0).build(), 4), - new StackableItem(Box.newBuilder().withId("2").withSize(1, 1, 1).withRotate3D().withWeight(0).build(), 1), - new StackableItem(Box.newBuilder().withId("3").withSize(53, 11, 21).withRotate3D().withWeight(0).build(), 1), - new StackableItem(Box.newBuilder().withId("4").withSize(38, 7, 19).withRotate3D().withWeight(0).build(), 1), - new StackableItem(Box.newBuilder().withId("5").withSize(15, 3, 7).withRotate3D().withWeight(0).build(), 1), - new StackableItem(Box.newBuilder().withId("6").withSize(95, 5, 3).withRotate3D().withWeight(0).build(), 1), - new StackableItem(Box.newBuilder().withId("7").withSize(48, 15, 42).withRotate3D().withWeight(0).build(), 1), - new StackableItem(Box.newBuilder().withId("8").withSize(140, 10, 10).withRotate3D().withWeight(0).build(), 2), - new StackableItem(Box.newBuilder().withId("9").withSize(150, 4, 65).withRotate3D().withWeight(0).build(), 2), - new StackableItem(Box.newBuilder().withId("10").withSize(75, 17, 60).withRotate3D().withWeight(0).build(), 1)); - - PackagerResult build = packager.newResultBuilder().withContainers(containers).withStackables(products).build(); - assertValid(build); + try { + List products = Arrays.asList( + new StackableItem(Box.newBuilder().withId("1").withSize(200, 2, 50).withRotate3D().withWeight(0).build(), 4), + new StackableItem(Box.newBuilder().withId("2").withSize(1, 1, 1).withRotate3D().withWeight(0).build(), 1), + new StackableItem(Box.newBuilder().withId("3").withSize(53, 11, 21).withRotate3D().withWeight(0).build(), 1), + new StackableItem(Box.newBuilder().withId("4").withSize(38, 7, 19).withRotate3D().withWeight(0).build(), 1), + new StackableItem(Box.newBuilder().withId("5").withSize(15, 3, 7).withRotate3D().withWeight(0).build(), 1), + new StackableItem(Box.newBuilder().withId("6").withSize(95, 5, 3).withRotate3D().withWeight(0).build(), 1), + new StackableItem(Box.newBuilder().withId("7").withSize(48, 15, 42).withRotate3D().withWeight(0).build(), 1), + new StackableItem(Box.newBuilder().withId("8").withSize(140, 10, 10).withRotate3D().withWeight(0).build(), 2), + new StackableItem(Box.newBuilder().withId("9").withSize(150, 4, 65).withRotate3D().withWeight(0).build(), 2), + new StackableItem(Box.newBuilder().withId("10").withSize(75, 17, 60).withRotate3D().withWeight(0).build(), 1)); + + PackagerResult build = packager.newResultBuilder().withContainers(containers).withStackables(products).build(); + assertValid(build); + } finally { + packager.close(); + } } @Test @@ -307,15 +329,18 @@ public void testImpossible1() throws Exception { ); Packager packager = BruteForcePackager.newBuilder().build(); - - PackagerResult build = packager.newResultBuilder() - .withContainers(ContainerItem.newListBuilder() - .withContainer(container) - .build()) - .withStackables(b1) - .build(); - - assertFalse(build.isSuccess()); + try { + PackagerResult build = packager.newResultBuilder() + .withContainers(ContainerItem.newListBuilder() + .withContainer(container) + .build()) + .withStackables(b1) + .build(); + + assertFalse(build.isSuccess()); + } finally { + packager.close(); + } } @Test @@ -327,18 +352,21 @@ public void testImpossible2() { .withMaxLoadWeight(100).withStack(new ValidatingStack()).build(), 1) .build(); - Packager packager = BruteForcePackager.newBuilder().build(); - - List products = Arrays.asList( - box(3,7,35,1)); - - PackagerResult build = packager - .newResultBuilder() - .withContainers(containerItems) - .withStackables(products) - .build(); - - assertFalse(build.isSuccess()); + BruteForcePackager packager = BruteForcePackager.newBuilder().build(); + try { + List products = Arrays.asList( + box(3,7,35,1)); + + PackagerResult build = packager + .newResultBuilder() + .withContainers(containerItems) + .withStackables(products) + .build(); + + assertFalse(build.isSuccess()); + } finally { + packager.close(); + } } @Test @@ -349,19 +377,22 @@ public void testImpossible3() { .withMaxLoadWeight(100).withStack(new ValidatingStack()).build(), 1) .build(); - Packager packager = BruteForcePackager.newBuilder().build(); - - List products = Arrays.asList( - box(1,1,1,1), box(3,7,35,1)); - - PackagerResult build = packager - .newResultBuilder() - .withContainers(containerItems) - .withStackables(products) - .withMaxContainerCount(3) - .build(); - - assertFalse(build.isSuccess()); + BruteForcePackager packager = BruteForcePackager.newBuilder().build(); + try { + List products = Arrays.asList( + box(1,1,1,1), box(3,7,35,1)); + + PackagerResult build = packager + .newResultBuilder() + .withContainers(containerItems) + .withStackables(products) + .withMaxContainerCount(3) + .build(); + + assertFalse(build.isSuccess()); + } finally { + packager.close(); + } } @@ -404,16 +435,19 @@ public void testMutuallyExclusiveBoxesAndContainersForMultiContainerResult() thr ); BruteForcePackager packager = BruteForcePackager.newBuilder().build(); - - PackagerResult build = packager.newResultBuilder() - .withContainers(ContainerItem.newListBuilder() - .withContainers(thin, thick) - .build()) - .withStackables(thinBox, thickBox) - .withMaxContainerCount(2) - .build(); - - assertTrue(build.isSuccess()); + try { + PackagerResult build = packager.newResultBuilder() + .withContainers(ContainerItem.newListBuilder() + .withContainers(thin, thick) + .build()) + .withStackables(thinBox, thickBox) + .withMaxContainerCount(2) + .build(); + + assertTrue(build.isSuccess()); + } finally { + packager.close(); + } } @Test @@ -466,16 +500,19 @@ public void testMutuallyExclusiveBoxesAndContainersForMultiContainerResult2() th ); BruteForcePackager packager = BruteForcePackager.newBuilder().build(); - - PackagerResult build = packager.newResultBuilder() - .withContainers(ContainerItem.newListBuilder() - .withContainers(thin, thick) - .build()) - .withStackables(thinBox1, thickBox, thinBox2) - .withMaxContainerCount(2) - .build(); - - assertTrue(build.isSuccess()); + try { + PackagerResult build = packager.newResultBuilder() + .withContainers(ContainerItem.newListBuilder() + .withContainers(thin, thick) + .build()) + .withStackables(thinBox1, thickBox, thinBox2) + .withMaxContainerCount(2) + .build(); + + assertTrue(build.isSuccess()); + } finally { + packager.close(); + } } @Override @@ -493,25 +530,28 @@ public void test752() throws Exception { .build(); Packager packager = BruteForcePackager.newBuilder().build(); - - List products = Arrays.asList( - new StackableItem(Box.newBuilder().withRotate3D().withSize(3350, 510, 3350).withWeight(250).build(), 1), - new StackableItem(Box.newBuilder().withRotate3D().withSize(2600, 20500, 3600).withWeight(1200).build(), 1), - new StackableItem(Box.newBuilder().withRotate3D().withSize(2600, 25600, 4200).withWeight(1520).build(), 1), - new StackableItem(Box.newBuilder().withRotate3D().withSize(2600, 25600, 4200).withWeight(1900).build(), 1), - new StackableItem(Box.newBuilder().withRotate3D().withSize(2600, 25600, 4200).withWeight(1500).build(), 1), - new StackableItem(Box.newBuilder().withRotate3D().withSize(2600, 25600, 4200).withWeight(1420).build(), 1) - ); - - PackagerResult result = packager - .newResultBuilder() - .withContainers(containerItems) - .withStackables(products) - .withMaxContainerCount(1) - .build(); - - List packList = result.getContainers(); - assertThat(packList).hasSize(0); + try { + List products = Arrays.asList( + new StackableItem(Box.newBuilder().withRotate3D().withSize(3350, 510, 3350).withWeight(250).build(), 1), + new StackableItem(Box.newBuilder().withRotate3D().withSize(2600, 20500, 3600).withWeight(1200).build(), 1), + new StackableItem(Box.newBuilder().withRotate3D().withSize(2600, 25600, 4200).withWeight(1520).build(), 1), + new StackableItem(Box.newBuilder().withRotate3D().withSize(2600, 25600, 4200).withWeight(1900).build(), 1), + new StackableItem(Box.newBuilder().withRotate3D().withSize(2600, 25600, 4200).withWeight(1500).build(), 1), + new StackableItem(Box.newBuilder().withRotate3D().withSize(2600, 25600, 4200).withWeight(1420).build(), 1) + ); + + PackagerResult result = packager + .newResultBuilder() + .withContainers(containerItems) + .withStackables(products) + .withMaxContainerCount(1) + .build(); + + List packList = result.getContainers(); + assertThat(packList).hasSize(0); + } finally { + packager.close(); + } } diff --git a/core/src/test/java/com/github/skjolber/packing/packer/bruteforce/FastBruteForcePackagerTest.java b/core/src/test/java/com/github/skjolber/packing/packer/bruteforce/FastBruteForcePackagerTest.java index 6ef98db9..94fc7e8e 100644 --- a/core/src/test/java/com/github/skjolber/packing/packer/bruteforce/FastBruteForcePackagerTest.java +++ b/core/src/test/java/com/github/skjolber/packing/packer/bruteforce/FastBruteForcePackagerTest.java @@ -41,27 +41,30 @@ void testStackingSquaresOnSquare() { .build(); FastBruteForcePackager packager = FastBruteForcePackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); - - PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - assertValid(build); - - Container fits = build.getContainers().get(0); - - List placements = fits.getStack().getPlacements(); - - assertThat(placements.get(0)).isAt(0, 0, 0).hasStackableName("A"); - assertThat(placements.get(1)).isAt(1, 0, 0).hasStackableName("B"); - assertThat(placements.get(2)).isAt(2, 0, 0).hasStackableName("C"); - - assertThat(placements.get(0)).isAlongsideX(placements.get(1)); - assertThat(placements.get(2)).followsAlongsideX(placements.get(1)); - assertThat(placements.get(1)).preceedsAlongsideX(placements.get(2)); + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); + + PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + assertValid(build); + + Container fits = build.getContainers().get(0); + + List placements = fits.getStack().getPlacements(); + + assertThat(placements.get(0)).isAt(0, 0, 0).hasStackableName("A"); + assertThat(placements.get(1)).isAt(1, 0, 0).hasStackableName("B"); + assertThat(placements.get(2)).isAt(2, 0, 0).hasStackableName("C"); + + assertThat(placements.get(0)).isAlongsideX(placements.get(1)); + assertThat(placements.get(2)).followsAlongsideX(placements.get(1)); + assertThat(placements.get(1)).preceedsAlongsideX(placements.get(2)); + } finally { + packager.close(); + } } @Test @@ -77,31 +80,34 @@ void testStackMultipleContainers() { .build(); FastBruteForcePackager packager = FastBruteForcePackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 2)); - products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 2)); - products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 2)); - - PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).withMaxContainerCount(5).build(); - assertValid(build); - - List packList = build.getContainers(); - - assertThat(packList).hasSize(2); - - Container fits = packList.get(0); - - List placements = fits.getStack().getPlacements(); - - assertThat(placements.get(0)).isAt(0, 0, 0).hasStackableName("A"); - assertThat(placements.get(1)).isAt(1, 0, 0).hasStackableName("A"); - assertThat(placements.get(2)).isAt(2, 0, 0).hasStackableName("B"); - - assertThat(placements.get(0)).isAlongsideX(placements.get(1)); - assertThat(placements.get(2)).followsAlongsideX(placements.get(1)); - assertThat(placements.get(1)).preceedsAlongsideX(placements.get(2)); + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 2)); + products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 2)); + products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 2)); + + PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).withMaxContainerCount(5).build(); + assertValid(build); + + List packList = build.getContainers(); + + assertThat(packList).hasSize(2); + + Container fits = packList.get(0); + + List placements = fits.getStack().getPlacements(); + + assertThat(placements.get(0)).isAt(0, 0, 0).hasStackableName("A"); + assertThat(placements.get(1)).isAt(1, 0, 0).hasStackableName("A"); + assertThat(placements.get(2)).isAt(2, 0, 0).hasStackableName("B"); + + assertThat(placements.get(0)).isAlongsideX(placements.get(1)); + assertThat(placements.get(2)).followsAlongsideX(placements.get(1)); + assertThat(placements.get(1)).preceedsAlongsideX(placements.get(2)); + } finally { + packager.close(); + } } @Test @@ -145,36 +151,39 @@ protected void pack(BouwkampCode bouwkampCode) { .build(); FastBruteForcePackager packager = FastBruteForcePackager.newBuilder().build(); - - List products = new ArrayList<>(); - - List squares = new ArrayList<>(); - for (BouwkampCodeLine bouwkampCodeLine : bouwkampCode.getLines()) { - squares.addAll(bouwkampCodeLine.getSquares()); - } - - // map similar items to the same stack item - this actually helps a lot - Map frequencyMap = new HashMap<>(); - squares.forEach(word -> frequencyMap.merge(word, 1, (v, newV) -> v + newV)); - - for (Entry entry : frequencyMap.entrySet()) { - int square = entry.getKey(); - int count = entry.getValue(); - products.add(new StackableItem(Box.newBuilder().withDescription(Integer.toString(square)).withSize(square, square, 1).withRotate3D().withWeight(1).build(), count)); + try { + List products = new ArrayList<>(); + + List squares = new ArrayList<>(); + for (BouwkampCodeLine bouwkampCodeLine : bouwkampCode.getLines()) { + squares.addAll(bouwkampCodeLine.getSquares()); + } + + // map similar items to the same stack item - this actually helps a lot + Map frequencyMap = new HashMap<>(); + squares.forEach(word -> frequencyMap.merge(word, 1, (v, newV) -> v + newV)); + + for (Entry entry : frequencyMap.entrySet()) { + int square = entry.getKey(); + int count = entry.getValue(); + products.add(new StackableItem(Box.newBuilder().withDescription(Integer.toString(square)).withSize(square, square, 1).withRotate3D().withWeight(1).build(), count)); + } + + Collections.shuffle(products); + + PackagerResult build = packager + .newResultBuilder() + .withContainers(containerItems) + .withStackables(products).build(); + + Container fits = build.get(0); + + assertNotNull(bouwkampCode.getName(), fits); + assertValid(fits); + assertEquals(bouwkampCode.getName(), fits.getStack().getSize(), squares.size()); + } finally { + packager.close(); } - - Collections.shuffle(products); - - PackagerResult build = packager - .newResultBuilder() - .withContainers(containerItems) - .withStackables(products).build(); - - Container fits = build.get(0); - - assertNotNull(bouwkampCode.getName(), fits); - assertValid(fits); - assertEquals(bouwkampCode.getName(), fits.getStack().getSize(), squares.size()); } @Test @@ -189,39 +198,44 @@ void testAnotherLargeProblemShouldRespectDeadline() { FastBruteForcePackager packager = FastBruteForcePackager.newBuilder().build(); - List products = Arrays.asList( - box(1000, 1000, 1000, 1), - box(1000, 1000, 1000, 4), - box(100, 1050, 750, 1), - box(100, 650, 750, 1), - box(16, 2500, 11, 1), - box(250, 150, 80, 1), - box(280, 800, 480, 1), - box(30, 620, 10, 1), - box(40, 1000, 1000, 1), - box(40, 100, 165, 1), - box(44, 575, 534, 1), - box(475, 530, 150, 1), - box(47, 3160, 660, 1), - box(530, 120, 570, 1), - box(55, 500, 745, 1), - box(670, 25, 15, 1), - box(700, 300, 30, 1), - box(700, 400, 30, 1), - box(75, 400, 720, 1), - box(77, 360, 750, 1), - box(80, 450, 760, 1), - box(90, 210, 680, 1)); - - PackagerResult build = packager - .newResultBuilder() - .withContainers(containerItems) - .withStackables(products) - .build(); - - // strangely when the timeout is set to now + 200ms it properly returns null - Container fits = build.get(0); - assertNull(fits); + try { + List products = Arrays.asList( + box(1000, 1000, 1000, 1), + box(1000, 1000, 1000, 4), + box(100, 1050, 750, 1), + box(100, 650, 750, 1), + box(16, 2500, 11, 1), + box(250, 150, 80, 1), + box(280, 800, 480, 1), + box(30, 620, 10, 1), + box(40, 1000, 1000, 1), + box(40, 100, 165, 1), + box(44, 575, 534, 1), + box(475, 530, 150, 1), + box(47, 3160, 660, 1), + box(530, 120, 570, 1), + box(55, 500, 745, 1), + box(670, 25, 15, 1), + box(700, 300, 30, 1), + box(700, 400, 30, 1), + box(75, 400, 720, 1), + box(77, 360, 750, 1), + box(80, 450, 760, 1), + box(90, 210, 680, 1)); + + PackagerResult build = packager + .newResultBuilder() + .withContainers(containerItems) + .withStackables(products) + .build(); + + // strangely when the timeout is set to now + 200ms it properly returns null + Container fits = build.get(0); + assertNull(fits); + } finally { + packager.close(); + } + } @Test diff --git a/core/src/test/java/com/github/skjolber/packing/packer/bruteforce/ParallelBruteForcePackagerTest.java b/core/src/test/java/com/github/skjolber/packing/packer/bruteforce/ParallelBruteForcePackagerTest.java index fd816302..38c78ed8 100644 --- a/core/src/test/java/com/github/skjolber/packing/packer/bruteforce/ParallelBruteForcePackagerTest.java +++ b/core/src/test/java/com/github/skjolber/packing/packer/bruteforce/ParallelBruteForcePackagerTest.java @@ -43,29 +43,32 @@ void testStackingSquaresOnSquare() { .build(); ParallelBruteForcePackager packager = ParallelBruteForcePackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); - - PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - assertValid(build); - - Container fits = build.get(0); - assertValid(fits); - assertEquals(fits.getStack().getSize(), products.size()); - - List placements = fits.getStack().getPlacements(); - - assertThat(placements.get(0)).isAt(0, 0, 0).hasStackableName("A"); - assertThat(placements.get(1)).isAt(1, 0, 0).hasStackableName("B"); - assertThat(placements.get(2)).isAt(2, 0, 0).hasStackableName("C"); - - assertThat(placements.get(0)).isAlongsideX(placements.get(1)); - assertThat(placements.get(2)).followsAlongsideX(placements.get(1)); - assertThat(placements.get(1)).preceedsAlongsideX(placements.get(2)); + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); + + PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + assertValid(build); + + Container fits = build.get(0); + assertValid(fits); + assertEquals(fits.getStack().getSize(), products.size()); + + List placements = fits.getStack().getPlacements(); + + assertThat(placements.get(0)).isAt(0, 0, 0).hasStackableName("A"); + assertThat(placements.get(1)).isAt(1, 0, 0).hasStackableName("B"); + assertThat(placements.get(2)).isAt(2, 0, 0).hasStackableName("C"); + + assertThat(placements.get(0)).isAlongsideX(placements.get(1)); + assertThat(placements.get(2)).followsAlongsideX(placements.get(1)); + assertThat(placements.get(1)).preceedsAlongsideX(placements.get(2)); + } finally { + packager.close(); + } } @Test @@ -77,31 +80,34 @@ void testStackMultipleContainers() { .build(); ParallelBruteForcePackager packager = ParallelBruteForcePackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 2)); - products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 2)); - products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 2)); - - PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).withMaxContainerCount(5).build(); - assertValid(build); - - List packList = build.getContainers(); - assertValid(packList); - assertThat(packList).hasSize(2); - - Container fits = packList.get(0); - - List placements = fits.getStack().getPlacements(); - - assertThat(placements.get(0)).isAt(0, 0, 0).hasStackableName("A"); - assertThat(placements.get(1)).isAt(1, 0, 0).hasStackableName("A"); - assertThat(placements.get(2)).isAt(2, 0, 0).hasStackableName("B"); - - assertThat(placements.get(0)).isAlongsideX(placements.get(1)); - assertThat(placements.get(2)).followsAlongsideX(placements.get(1)); - assertThat(placements.get(1)).preceedsAlongsideX(placements.get(2)); + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 2)); + products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 2)); + products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 2)); + + PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).withMaxContainerCount(5).build(); + assertValid(build); + + List packList = build.getContainers(); + assertValid(packList); + assertThat(packList).hasSize(2); + + Container fits = packList.get(0); + + List placements = fits.getStack().getPlacements(); + + assertThat(placements.get(0)).isAt(0, 0, 0).hasStackableName("A"); + assertThat(placements.get(1)).isAt(1, 0, 0).hasStackableName("A"); + assertThat(placements.get(2)).isAt(2, 0, 0).hasStackableName("B"); + + assertThat(placements.get(0)).isAlongsideX(placements.get(1)); + assertThat(placements.get(2)).followsAlongsideX(placements.get(1)); + assertThat(placements.get(1)).preceedsAlongsideX(placements.get(2)); + } finally { + packager.close(); + } } @Test @@ -114,17 +120,21 @@ void testStackingBinary1() { ParallelBruteForcePackager packager = ParallelBruteForcePackager.newBuilder().build(); - List products = new ArrayList<>(); - products.add(new StackableItem(Box.newBuilder().withDescription("J").withSize(4, 4, 1).withRotate3D().withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("K").withRotate3D().withSize(2, 2, 1).withWeight(1).build(), 4)); - products.add(new StackableItem(Box.newBuilder().withDescription("K").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 16)); - - PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - assertValid(build); - - Container fits = build.get(0); - assertValid(fits); - assertEquals(21, fits.getStack().getPlacements().size()); + try { + List products = new ArrayList<>(); + products.add(new StackableItem(Box.newBuilder().withDescription("J").withSize(4, 4, 1).withRotate3D().withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("K").withRotate3D().withSize(2, 2, 1).withWeight(1).build(), 4)); + products.add(new StackableItem(Box.newBuilder().withDescription("K").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 16)); + + PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + assertValid(build); + + Container fits = build.get(0); + assertValid(fits); + assertEquals(21, fits.getStack().getPlacements().size()); + } finally { + packager.close(); + } } @Test @@ -136,20 +146,23 @@ public void testStackingRectanglesOnSquareRectangleVolumeFirst() { .build(); ParallelBruteForcePackager packager = ParallelBruteForcePackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("J").withRotate3D().withSize(5, 10, 4).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("L").withRotate3D().withSize(5, 10, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("J").withRotate3D().withSize(5, 10, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("M").withRotate3D().withSize(5, 10, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("N").withRotate3D().withSize(5, 10, 1).withWeight(1).build(), 1)); - - PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - assertValid(build); - - Container fits = build.get(0); - assertEquals(fits.getStack().getSize(), products.size()); + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("J").withRotate3D().withSize(5, 10, 4).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("L").withRotate3D().withSize(5, 10, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("J").withRotate3D().withSize(5, 10, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("M").withRotate3D().withSize(5, 10, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("N").withRotate3D().withSize(5, 10, 1).withWeight(1).build(), 1)); + + PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + assertValid(build); + + Container fits = build.get(0); + assertEquals(fits.getStack().getSize(), products.size()); + } finally { + packager.close(); + } } @Test @@ -161,19 +174,22 @@ public void testStackingBox() { .build(); ParallelBruteForcePackager packager = ParallelBruteForcePackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(3, 2, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(3, 2, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(3, 2, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("D").withRotate3D().withSize(3, 2, 1).withWeight(1).build(), 1)); - - PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - assertValid(build); - Container fits = build.get(0); - - assertEquals(fits.getStack().getSize(), products.size()); + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(3, 2, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(3, 2, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(3, 2, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("D").withRotate3D().withSize(3, 2, 1).withWeight(1).build(), 1)); + + PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + assertValid(build); + Container fits = build.get(0); + + assertEquals(fits.getStack().getSize(), products.size()); + } finally { + packager.close(); + } } @Test @@ -214,34 +230,38 @@ protected void pack(BouwkampCode bouwkampCode) { .withStack(new ValidatingStack()).build(), 1) .build(); - ParallelBruteForcePackager packager = ParallelBruteForcePackager.newBuilder().withExecutorService(executorService).withParallelizationCount(256).withCheckpointsPerDeadlineCheck(1024).build(); + ParallelBruteForcePackager packager = ParallelBruteForcePackager.newBuilder().withExecutorService(executorService).withParallelizationCount(256).build(); - List products = new ArrayList<>(); - - List squares = new ArrayList<>(); - for (BouwkampCodeLine bouwkampCodeLine : bouwkampCode.getLines()) { - squares.addAll(bouwkampCodeLine.getSquares()); - } - - // map similar items to the same stack item - this actually helps a lot - Map frequencyMap = new HashMap<>(); - squares.forEach(word -> frequencyMap.merge(word, 1, (v, newV) -> v + newV)); - - for (Entry entry : frequencyMap.entrySet()) { - int square = entry.getKey(); - int count = entry.getValue(); - products.add(new StackableItem(Box.newBuilder().withDescription(Integer.toString(square)).withRotate3D().withSize(square, square, 1).withWeight(1).build(), count)); + try { + List products = new ArrayList<>(); + + List squares = new ArrayList<>(); + for (BouwkampCodeLine bouwkampCodeLine : bouwkampCode.getLines()) { + squares.addAll(bouwkampCodeLine.getSquares()); + } + + // map similar items to the same stack item - this actually helps a lot + Map frequencyMap = new HashMap<>(); + squares.forEach(word -> frequencyMap.merge(word, 1, (v, newV) -> v + newV)); + + for (Entry entry : frequencyMap.entrySet()) { + int square = entry.getKey(); + int count = entry.getValue(); + products.add(new StackableItem(Box.newBuilder().withDescription(Integer.toString(square)).withRotate3D().withSize(square, square, 1).withWeight(1).build(), count)); + } + + Collections.shuffle(products); + + PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + assertValid(build); + Container fits = build.get(0); + + assertNotNull(bouwkampCode.getName(), fits); + assertValid(fits); + assertEquals(bouwkampCode.getName(), fits.getStack().getSize(), squares.size()); + } finally { + packager.close(); } - - Collections.shuffle(products); - - PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - assertValid(build); - Container fits = build.get(0); - - assertNotNull(bouwkampCode.getName(), fits); - assertValid(fits); - assertEquals(bouwkampCode.getName(), fits.getStack().getSize(), squares.size()); } @Disabled // TODO @@ -252,6 +272,6 @@ public void testAHugeProblemShouldRespectDeadline() { @Override protected AbstractPackager createPackager() { - return ParallelBruteForcePackager.newBuilder().withExecutorService(executorService).withParallelizationCount(256).withCheckpointsPerDeadlineCheck(1024).build(); + return ParallelBruteForcePackager.newBuilder().withExecutorService(executorService).withParallelizationCount(256).build(); } } diff --git a/core/src/test/java/com/github/skjolber/packing/packer/laff/FastLargestAreaFitFirstPackagerTest.java b/core/src/test/java/com/github/skjolber/packing/packer/laff/FastLargestAreaFitFirstPackagerTest.java index aa5aae08..ef937b69 100644 --- a/core/src/test/java/com/github/skjolber/packing/packer/laff/FastLargestAreaFitFirstPackagerTest.java +++ b/core/src/test/java/com/github/skjolber/packing/packer/laff/FastLargestAreaFitFirstPackagerTest.java @@ -29,16 +29,19 @@ void testStackingSquaresOnSquare() { .build(); FastLargestAreaFitFirstPackager packager = FastLargestAreaFitFirstPackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); - - PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - List containers = build.getContainers(); - assertValid(containers); + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); + + PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + List containers = build.getContainers(); + assertValid(containers); + } finally { + packager.close(); + } } @Test @@ -50,16 +53,19 @@ void testStackingRectangles() { .build(); FastLargestAreaFitFirstPackager packager = FastLargestAreaFitFirstPackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 1)); - - PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - List containers = build.getContainers(); - assertValid(containers); + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 1)); + + PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + List containers = build.getContainers(); + assertValid(containers); + } finally { + packager.close(); + } } @Test @@ -71,16 +77,19 @@ void testStackingSquaresAndRectangle() { .build(); FastLargestAreaFitFirstPackager packager = FastLargestAreaFitFirstPackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(5, 10, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(5, 5, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(5, 5, 1).withWeight(1).build(), 1)); - - PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - List containers = build.getContainers(); - assertValid(containers); + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(5, 10, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(5, 5, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(5, 5, 1).withWeight(1).build(), 1)); + + PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + List containers = build.getContainers(); + assertValid(containers); + } finally { + packager.close(); + } } @Test @@ -92,16 +101,19 @@ void testStackingDecreasingRectangles() { .build(); FastLargestAreaFitFirstPackager packager = FastLargestAreaFitFirstPackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(3, 1, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); - - PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - List containers = build.getContainers(); - assertValid(containers); + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(3, 1, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); + + PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + List containers = build.getContainers(); + assertValid(containers); + } finally { + packager.close(); + } } @Test @@ -113,21 +125,24 @@ void testStackingRectanglesTwoLevels() { .build(); FastLargestAreaFitFirstPackager packager = FastLargestAreaFitFirstPackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 2)); - products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 2)); - products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 2)); - - PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - List containers = build.getContainers(); - assertValid(containers); - - Container fits = containers.get(0); - - LevelStack levelStack = (LevelStack)fits.getStack(); - assertEquals(2, levelStack.getLevels().size()); + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 2)); + products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 2)); + products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 2)); + + PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + List containers = build.getContainers(); + assertValid(containers); + + Container fits = containers.get(0); + + LevelStack levelStack = (LevelStack)fits.getStack(); + assertEquals(2, levelStack.getLevels().size()); + } finally { + packager.close(); + } } @Test @@ -139,19 +154,22 @@ void testStackingRectanglesThreeLevels() { .build(); FastLargestAreaFitFirstPackager packager = FastLargestAreaFitFirstPackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(2, 2, 1).withWeight(1).build(), 3)); - - PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - List containers = build.getContainers(); - assertValid(containers); - - Container fits = containers.get(0); - - LevelStack levelStack = (LevelStack)fits.getStack(); - assertEquals(3, levelStack.getLevels().size()); + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(2, 2, 1).withWeight(1).build(), 3)); + + PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + List containers = build.getContainers(); + assertValid(containers); + + Container fits = containers.get(0); + + LevelStack levelStack = (LevelStack)fits.getStack(); + assertEquals(3, levelStack.getLevels().size()); + } finally { + packager.close(); + } } @Test @@ -164,15 +182,18 @@ void testStackingNotPossible() { .build(); FastLargestAreaFitFirstPackager packager = FastLargestAreaFitFirstPackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(1, 2, 1).withWeight(1).build(), 18)); // 12 - products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); // 1 - - PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - List containers = build.getContainers(); - assertThat(containers).isEmpty(); + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(1, 2, 1).withWeight(1).build(), 18)); // 12 + products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); // 1 + + PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + List containers = build.getContainers(); + assertThat(containers).isEmpty(); + } finally { + packager.close(); + } } @Test @@ -194,17 +215,20 @@ void issue453BoxesShouldNotFit() { FastLargestAreaFitFirstPackager packager = FastLargestAreaFitFirstPackager .newBuilder() .build(); - - List products = Arrays.asList( - new StackableItem(Box.newBuilder().withId("1").withSize(32, 19, 24).withRotate3D().withWeight(0).build(), 1), - new StackableItem(Box.newBuilder().withId("2").withSize(32, 21, 27).withRotate3D().withWeight(0).build(), 1), - new StackableItem(Box.newBuilder().withId("3").withSize(34, 21, 24).withRotate3D().withWeight(0).build(), 1), - new StackableItem(Box.newBuilder().withId("4").withSize(30, 19, 23).withRotate3D().withWeight(0).build(), 1), - new StackableItem(Box.newBuilder().withId("5").withSize(30, 21, 25).withRotate3D().withWeight(0).build(), 1)); - - PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - - assertEquals(0, build.size()); + try { + List products = Arrays.asList( + new StackableItem(Box.newBuilder().withId("1").withSize(32, 19, 24).withRotate3D().withWeight(0).build(), 1), + new StackableItem(Box.newBuilder().withId("2").withSize(32, 21, 27).withRotate3D().withWeight(0).build(), 1), + new StackableItem(Box.newBuilder().withId("3").withSize(34, 21, 24).withRotate3D().withWeight(0).build(), 1), + new StackableItem(Box.newBuilder().withId("4").withSize(30, 19, 23).withRotate3D().withWeight(0).build(), 1), + new StackableItem(Box.newBuilder().withId("5").withSize(30, 21, 25).withRotate3D().withWeight(0).build(), 1)); + + PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + + assertEquals(0, build.size()); + } finally { + packager.close(); + } } @Test diff --git a/core/src/test/java/com/github/skjolber/packing/packer/laff/LargestAreaFitFirstPackagerTest.java b/core/src/test/java/com/github/skjolber/packing/packer/laff/LargestAreaFitFirstPackagerTest.java index b4a15a9b..c2e386ee 100644 --- a/core/src/test/java/com/github/skjolber/packing/packer/laff/LargestAreaFitFirstPackagerTest.java +++ b/core/src/test/java/com/github/skjolber/packing/packer/laff/LargestAreaFitFirstPackagerTest.java @@ -38,28 +38,31 @@ void testStackingSquaresOnSquare() { .build(); LargestAreaFitFirstPackager packager = LargestAreaFitFirstPackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); - - PackagerResult result = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - Container fits = result.get(0); - - assertNotNull(fits); - validate(fits); - - List placements = fits.getStack().getPlacements(); - - assertThat(placements.get(0)).isAt(0, 0, 0).hasStackableName("A"); - assertThat(placements.get(1)).isAt(1, 0, 0).hasStackableName("B"); - assertThat(placements.get(2)).isAt(2, 0, 0).hasStackableName("C"); - - assertThat(placements.get(0)).isAlongsideX(placements.get(1)); - assertThat(placements.get(2)).followsAlongsideX(placements.get(1)); - assertThat(placements.get(1)).preceedsAlongsideX(placements.get(2)); + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); + + PackagerResult result = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + Container fits = result.get(0); + + assertNotNull(fits); + validate(fits); + + List placements = fits.getStack().getPlacements(); + + assertThat(placements.get(0)).isAt(0, 0, 0).hasStackableName("A"); + assertThat(placements.get(1)).isAt(1, 0, 0).hasStackableName("B"); + assertThat(placements.get(2)).isAt(2, 0, 0).hasStackableName("C"); + + assertThat(placements.get(0)).isAlongsideX(placements.get(1)); + assertThat(placements.get(2)).followsAlongsideX(placements.get(1)); + assertThat(placements.get(1)).preceedsAlongsideX(placements.get(2)); + } finally { + packager.close(); + } } @Test @@ -71,24 +74,27 @@ void testStackingRectangles() { .build(); LargestAreaFitFirstPackager packager = LargestAreaFitFirstPackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 1)); - - PackagerResult result = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - Container fits = result.get(0); - - assertNotNull(fits); - validate(fits); - - List placements = fits.getStack().getPlacements(); - - assertThat(placements.get(0)).isAt(0, 0, 0).hasStackableName("A"); - assertThat(placements.get(1)).isAt(1, 0, 0).hasStackableName("B"); - assertThat(placements.get(2)).isAt(1, 1, 0).hasStackableName("C"); + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 1)); + + PackagerResult result = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + Container fits = result.get(0); + + assertNotNull(fits); + validate(fits); + + List placements = fits.getStack().getPlacements(); + + assertThat(placements.get(0)).isAt(0, 0, 0).hasStackableName("A"); + assertThat(placements.get(1)).isAt(1, 0, 0).hasStackableName("B"); + assertThat(placements.get(2)).isAt(1, 1, 0).hasStackableName("C"); + } finally { + packager.close(); + } } @@ -101,18 +107,21 @@ void testStackingSquaresAndRectangle() { .build(); LargestAreaFitFirstPackager packager = LargestAreaFitFirstPackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(5, 10, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(5, 5, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(5, 5, 1).withWeight(1).build(), 1)); - - PackagerResult result = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - Container fits = result.get(0); - - assertNotNull(fits); - validate(fits); + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(5, 10, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(5, 5, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(5, 5, 1).withWeight(1).build(), 1)); + + PackagerResult result = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + Container fits = result.get(0); + + assertNotNull(fits); + validate(fits); + } finally { + packager.close(); + } } @Test @@ -124,24 +133,27 @@ void testStackingDecreasingRectangles() { .build(); LargestAreaFitFirstPackager packager = LargestAreaFitFirstPackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(3, 1, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); - - PackagerResult result = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - Container fits = result.get(0); - - assertNotNull(fits); - validate(fits); - - List placements = fits.getStack().getPlacements(); - - assertThat(placements.get(0)).isAt(0, 0, 0).hasStackableName("A"); - assertThat(placements.get(1)).isAt(3, 0, 0).hasStackableName("B"); // point with lowest x is selected first - assertThat(placements.get(2)).isAt(5, 0, 0).hasStackableName("C"); + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(3, 1, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); + + PackagerResult result = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + Container fits = result.get(0); + + assertNotNull(fits); + validate(fits); + + List placements = fits.getStack().getPlacements(); + + assertThat(placements.get(0)).isAt(0, 0, 0).hasStackableName("A"); + assertThat(placements.get(1)).isAt(3, 0, 0).hasStackableName("B"); // point with lowest x is selected first + assertThat(placements.get(2)).isAt(5, 0, 0).hasStackableName("C"); + } finally { + packager.close(); + } } @@ -154,31 +166,34 @@ void testStackingRectanglesTwoLevels() { .build(); LargestAreaFitFirstPackager packager = LargestAreaFitFirstPackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 2)); - products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 2)); - products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 2)); - - PackagerResult result = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - Container fits = result.get(0); - - assertNotNull(fits); - validate(fits); - - LevelStack levelStack = (LevelStack)fits.getStack(); - assertEquals(2, levelStack.getLevels().size()); - - List placements = fits.getStack().getPlacements(); - - assertThat(placements.get(0)).isAt(0, 0, 0).hasStackableName("A"); - assertThat(placements.get(1)).isAt(1, 0, 0).hasStackableName("A"); - assertThat(placements.get(2)).isAt(1, 1, 0).hasStackableName("B"); - - assertThat(placements.get(3)).isAt(0, 0, 1).hasStackableName("B"); - assertThat(placements.get(4)).isAt(1, 0, 1).hasStackableName("C"); - assertThat(placements.get(5)).isAt(1, 1, 1).hasStackableName("C"); + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 2)); + products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 2)); + products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 2)); + + PackagerResult result = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + Container fits = result.get(0); + + assertNotNull(fits); + validate(fits); + + LevelStack levelStack = (LevelStack)fits.getStack(); + assertEquals(2, levelStack.getLevels().size()); + + List placements = fits.getStack().getPlacements(); + + assertThat(placements.get(0)).isAt(0, 0, 0).hasStackableName("A"); + assertThat(placements.get(1)).isAt(1, 0, 0).hasStackableName("A"); + assertThat(placements.get(2)).isAt(1, 1, 0).hasStackableName("B"); + + assertThat(placements.get(3)).isAt(0, 0, 1).hasStackableName("B"); + assertThat(placements.get(4)).isAt(1, 0, 1).hasStackableName("C"); + assertThat(placements.get(5)).isAt(1, 1, 1).hasStackableName("C"); + } finally { + packager.close(); + } } @Test @@ -190,22 +205,25 @@ void testStackingRectanglesThreeLevels() { .build(); LargestAreaFitFirstPackager packager = LargestAreaFitFirstPackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(2, 2, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(2, 2, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(2, 2, 1).withWeight(1).build(), 1)); - - PackagerResult result = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - - Container fits = result.get(0); - - assertNotNull(fits); - validate(fits); - - LevelStack levelStack = (LevelStack)fits.getStack(); - assertEquals(3, levelStack.getLevels().size()); + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(2, 2, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(2, 2, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(2, 2, 1).withWeight(1).build(), 1)); + + PackagerResult result = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + + Container fits = result.get(0); + + assertNotNull(fits); + validate(fits); + + LevelStack levelStack = (LevelStack)fits.getStack(); + assertEquals(3, levelStack.getLevels().size()); + } finally { + packager.close(); + } } @Test @@ -222,14 +240,17 @@ void testStackingNotPossible() { containers.add(Container.newBuilder().withDescription("1").withEmptyWeight(1).withSize(3, 2, 3).withMaxLoadWeight(100).withStack(new ValidatingStack()).build()); LargestAreaFitFirstPackager packager = LargestAreaFitFirstPackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(1, 2, 1).withWeight(1).build(), 18)); // 12 - products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); // 1 - - PackagerResult result = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - assertEquals(result.size(), 0); + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(1, 2, 1).withWeight(1).build(), 18)); // 12 + products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); // 1 + + PackagerResult result = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + assertEquals(result.size(), 0); + } finally { + packager.close(); + } } @Test @@ -251,12 +272,16 @@ void issue433() { .newBuilder() .build(); - List products = Arrays.asList( - new StackableItem(Box.newBuilder().withId("Foot").withSize(7, 37, 39).withRotate3D().withWeight(0).build(), 20)); - - PackagerResult result = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - Container pack = result.get(0); - assertNotNull(pack); + try { + List products = Arrays.asList( + new StackableItem(Box.newBuilder().withId("Foot").withSize(7, 37, 39).withRotate3D().withWeight(0).build(), 20)); + + PackagerResult result = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + Container pack = result.get(0); + assertNotNull(pack); + } finally { + packager.close(); + } } @@ -277,27 +302,31 @@ void issue440() { LargestAreaFitFirstPackager packager = LargestAreaFitFirstPackager.newBuilder() .build(); - for (int i = 1; i <= 10; i++) { - int boxCountPerStackableItem = i; - - List products = Arrays.asList( - createStackableItem("1", 1200, 750, 2280, 285, boxCountPerStackableItem), - createStackableItem("2", 1200, 450, 2280, 155, boxCountPerStackableItem), - createStackableItem("3", 360, 360, 570, 20, boxCountPerStackableItem), - createStackableItem("4", 2250, 1200, 2250, 900, boxCountPerStackableItem), - createStackableItem("5", 1140, 750, 1450, 395, boxCountPerStackableItem), - createStackableItem("6", 1130, 1500, 3100, 800, boxCountPerStackableItem), - createStackableItem("7", 800, 490, 1140, 156, boxCountPerStackableItem), - createStackableItem("8", 800, 2100, 1200, 135, boxCountPerStackableItem), - createStackableItem("9", 1120, 1700, 2120, 160, boxCountPerStackableItem), - createStackableItem("10", 1200, 1050, 2280, 390, boxCountPerStackableItem)); - - PackagerResult result = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - List packList = result.getContainers(); - - assertNotNull(packList); - assertTrue(i >= packList.size()); - validate(packList); + try { + for (int i = 1; i <= 10; i++) { + int boxCountPerStackableItem = i; + + List products = Arrays.asList( + createStackableItem("1", 1200, 750, 2280, 285, boxCountPerStackableItem), + createStackableItem("2", 1200, 450, 2280, 155, boxCountPerStackableItem), + createStackableItem("3", 360, 360, 570, 20, boxCountPerStackableItem), + createStackableItem("4", 2250, 1200, 2250, 900, boxCountPerStackableItem), + createStackableItem("5", 1140, 750, 1450, 395, boxCountPerStackableItem), + createStackableItem("6", 1130, 1500, 3100, 800, boxCountPerStackableItem), + createStackableItem("7", 800, 490, 1140, 156, boxCountPerStackableItem), + createStackableItem("8", 800, 2100, 1200, 135, boxCountPerStackableItem), + createStackableItem("9", 1120, 1700, 2120, 160, boxCountPerStackableItem), + createStackableItem("10", 1200, 1050, 2280, 390, boxCountPerStackableItem)); + + PackagerResult result = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + List packList = result.getContainers(); + + assertNotNull(packList); + assertTrue(i >= packList.size()); + validate(packList); + } + } finally { + packager.close(); } } @@ -319,20 +348,24 @@ void testCorrectLevelZOffsetAdjustments() { // issue 450 LargestAreaFitFirstPackager packager = LargestAreaFitFirstPackager.newBuilder() .build(); - int boxCountPerStackableItem = 1; - - List products = Arrays.asList( - createStackableItem("1", 1200, 750, 2280, 285, boxCountPerStackableItem), - createStackableItem("2", 1200, 450, 2280, 155, boxCountPerStackableItem), - createStackableItem("3", 360, 360, 570, 20, boxCountPerStackableItem), - createStackableItem("4", 2250, 1200, 2250, 900, boxCountPerStackableItem), - createStackableItem("5", 1140, 750, 1450, 395, boxCountPerStackableItem), - createStackableItem("6", 1130, 1500, 3100, 800, boxCountPerStackableItem), - createStackableItem("7", 800, 490, 1140, 156, boxCountPerStackableItem)); - - PackagerResult result = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - List packList = result.getContainers(); - validate(packList); + try { + int boxCountPerStackableItem = 1; + + List products = Arrays.asList( + createStackableItem("1", 1200, 750, 2280, 285, boxCountPerStackableItem), + createStackableItem("2", 1200, 450, 2280, 155, boxCountPerStackableItem), + createStackableItem("3", 360, 360, 570, 20, boxCountPerStackableItem), + createStackableItem("4", 2250, 1200, 2250, 900, boxCountPerStackableItem), + createStackableItem("5", 1140, 750, 1450, 395, boxCountPerStackableItem), + createStackableItem("6", 1130, 1500, 3100, 800, boxCountPerStackableItem), + createStackableItem("7", 800, 490, 1140, 156, boxCountPerStackableItem)); + + PackagerResult result = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + List packList = result.getContainers(); + validate(packList); + } finally { + packager.close(); + } } private StackableItem createStackableItem(String id, int width, int height, int depth, int weight, int boxCountPerStackableItem) { @@ -447,14 +480,18 @@ public void testIssue698() { FastLargestAreaFitFirstPackager packager = FastLargestAreaFitFirstPackager .newBuilder() .build(); - PackagerResult result = packager - .newResultBuilder() - .withMaxContainerCount(20) - .withContainers(containerItems) - .withStackables(stackableItems) - .build(); - - assertEquals(true, result.isSuccess()); + try { + PackagerResult result = packager + .newResultBuilder() + .withMaxContainerCount(20) + .withContainers(containerItems) + .withStackables(stackableItems) + .build(); + + assertEquals(true, result.isSuccess()); + } finally { + packager.close(); + } } diff --git a/core/src/test/java/com/github/skjolber/packing/packer/plain/PlainPackagerTest.java b/core/src/test/java/com/github/skjolber/packing/packer/plain/PlainPackagerTest.java index 298d283b..a55a5364 100644 --- a/core/src/test/java/com/github/skjolber/packing/packer/plain/PlainPackagerTest.java +++ b/core/src/test/java/com/github/skjolber/packing/packer/plain/PlainPackagerTest.java @@ -29,15 +29,18 @@ void testStackingSquaresOnSquare() { .build(); PlainPackager packager = PlainPackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); - - PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - assertValid(build); + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); + + PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + assertValid(build); + } finally { + packager.close(); + } } @@ -49,15 +52,18 @@ void testStackingRectangles() { .build(); PlainPackager packager = PlainPackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 1)); - - PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - assertValid(build); + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 1)); + + PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + assertValid(build); + } finally { + packager.close(); + } } @@ -69,16 +75,18 @@ void testStackingSquaresAndRectangle() { .build(); PlainPackager packager = PlainPackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(5, 10, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(5, 5, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(5, 5, 1).withWeight(1).build(), 1)); - - PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - assertValid(build); - + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(5, 10, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(5, 5, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(5, 5, 1).withWeight(1).build(), 1)); + + PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + assertValid(build); + } finally { + packager.close(); + } } @Test @@ -89,16 +97,18 @@ void testStackingDecreasingRectangles() { .build(); PlainPackager packager = PlainPackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(3, 1, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); - - PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - assertValid(build); - + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(3, 1, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); + + PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + assertValid(build); + } finally { + packager.close(); + } } @Test @@ -110,15 +120,18 @@ void testStackingRectanglesTwoLevels() { .build(); PlainPackager packager = PlainPackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 2)); - products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 2)); - products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 2)); - - PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - assertValid(build); + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 2)); + products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 2)); + products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(2, 1, 1).withWeight(1).build(), 2)); + + PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + assertValid(build); + } finally { + packager.close(); + } } @@ -131,13 +144,16 @@ void testStackingRectanglesThreeLevels() { .build(); PlainPackager packager = PlainPackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(2, 2, 1).withWeight(1).build(), 3)); - - PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - assertValid(build); + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(2, 2, 1).withWeight(1).build(), 3)); + + PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + assertValid(build); + } finally { + packager.close(); + } } @Test @@ -150,14 +166,17 @@ void testStackingNotPossible() { .build(); PlainPackager packager = PlainPackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(1, 2, 1).withWeight(1).build(), 18)); // 12 - products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); // 1 - - PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - assertThat(build.getContainers()).isEmpty(); + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(1, 2, 1).withWeight(1).build(), 18)); // 12 + products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); // 1 + + PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + assertThat(build.getContainers()).isEmpty(); + } finally { + packager.close(); + } } @Test @@ -175,17 +194,21 @@ void testStackingMultipleContainersSingleContainerResult() { PlainPackager packager = PlainPackager.newBuilder().build(); - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); - - PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); - assertValid(build); - - Container fits = build.get(0); - assertEquals(fits.getVolume(), containers.get(2).getVolume()); + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); + + PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withStackables(products).build(); + assertValid(build); + + Container fits = build.get(0); + assertEquals(fits.getVolume(), containers.get(2).getVolume()); + } finally { + packager.close(); + } } @Test @@ -205,20 +228,23 @@ void testStackingMultipleContainersMultiContainerResult() { .build(); PlainPackager packager = PlainPackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 2)); - products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 3)); - products.add(new StackableItem(Box.newBuilder().withDescription("D").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 4)); - - PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withMaxContainerCount(5).withStackables(products).build(); - assertValid(build); - - assertEquals(build.size(), 2); - assertEquals(build.get(0).getVolume(), 7); - assertEquals(build.get(1).getVolume(), 3); + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 2)); + products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 3)); + products.add(new StackableItem(Box.newBuilder().withDescription("D").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 4)); + + PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withMaxContainerCount(5).withStackables(products).build(); + assertValid(build); + + assertEquals(build.size(), 2); + assertEquals(build.get(0).getVolume(), 7); + assertEquals(build.get(1).getVolume(), 3); + } finally { + packager.close(); + } } @Test @@ -233,31 +259,35 @@ void issue440() { PlainPackager packager = PlainPackager.newBuilder() .build(); - for (int i = 1; i <= 10; i++) { - int boxCountPerStackableItem = i; - - List containerItems = ContainerItem - .newListBuilder() - .withContainer(build, i + 2) - .build(); - - List stackableItems = Arrays.asList( - createStackableItem("1", 1200, 750, 2280, 285, boxCountPerStackableItem), - createStackableItem("2", 1200, 450, 2280, 155, boxCountPerStackableItem), - createStackableItem("3", 360, 360, 570, 20, boxCountPerStackableItem), - createStackableItem("4", 2250, 1200, 2250, 900, boxCountPerStackableItem), - createStackableItem("5", 1140, 750, 1450, 395, boxCountPerStackableItem), - createStackableItem("6", 1130, 1500, 3100, 800, boxCountPerStackableItem), - createStackableItem("7", 800, 490, 1140, 156, boxCountPerStackableItem), - createStackableItem("8", 800, 2100, 1200, 135, boxCountPerStackableItem), - createStackableItem("9", 1120, 1700, 2120, 160, boxCountPerStackableItem), - createStackableItem("10", 1200, 1050, 2280, 390, boxCountPerStackableItem)); - - PackagerResult result = packager.newResultBuilder().withContainers(containerItems).withStackables(stackableItems).build(); - List packList = result.getContainers(); - - assertNotNull(packList); - assertTrue(i >= packList.size()); + try { + for (int i = 1; i <= 10; i++) { + int boxCountPerStackableItem = i; + + List containerItems = ContainerItem + .newListBuilder() + .withContainer(build, i + 2) + .build(); + + List stackableItems = Arrays.asList( + createStackableItem("1", 1200, 750, 2280, 285, boxCountPerStackableItem), + createStackableItem("2", 1200, 450, 2280, 155, boxCountPerStackableItem), + createStackableItem("3", 360, 360, 570, 20, boxCountPerStackableItem), + createStackableItem("4", 2250, 1200, 2250, 900, boxCountPerStackableItem), + createStackableItem("5", 1140, 750, 1450, 395, boxCountPerStackableItem), + createStackableItem("6", 1130, 1500, 3100, 800, boxCountPerStackableItem), + createStackableItem("7", 800, 490, 1140, 156, boxCountPerStackableItem), + createStackableItem("8", 800, 2100, 1200, 135, boxCountPerStackableItem), + createStackableItem("9", 1120, 1700, 2120, 160, boxCountPerStackableItem), + createStackableItem("10", 1200, 1050, 2280, 390, boxCountPerStackableItem)); + + PackagerResult result = packager.newResultBuilder().withContainers(containerItems).withStackables(stackableItems).build(); + List packList = result.getContainers(); + + assertNotNull(packList); + assertTrue(i >= packList.size()); + } + } finally { + packager.close(); } } @@ -288,19 +318,22 @@ void testStackingSpecificMultipleContainers() { .build(); PlainPackager packager = PlainPackager.newBuilder().build(); - - List products = new ArrayList<>(); - - products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 4)); - products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); - products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); - - PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withMaxContainerCount(5).withStackables(products).build(); - assertValid(build); - - assertEquals(build.get(0).getDescription(), "big"); - assertEquals(build.get(1).getDescription(), "small"); - assertEquals(build.get(2).getDescription(), "small"); + try { + List products = new ArrayList<>(); + + products.add(new StackableItem(Box.newBuilder().withDescription("A").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 4)); + products.add(new StackableItem(Box.newBuilder().withDescription("B").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); + products.add(new StackableItem(Box.newBuilder().withDescription("C").withRotate3D().withSize(1, 1, 1).withWeight(1).build(), 1)); + + PackagerResult build = packager.newResultBuilder().withContainers(containerItems).withMaxContainerCount(5).withStackables(products).build(); + assertValid(build); + + assertEquals(build.get(0).getDescription(), "big"); + assertEquals(build.get(1).getDescription(), "small"); + assertEquals(build.get(2).getDescription(), "small"); + } finally { + packager.close(); + } } } diff --git a/jmh/src/main/java/com/github/skjolber/packing/jmh/BouwkampCodeBruteForcePackagerState.java b/jmh/src/main/java/com/github/skjolber/packing/jmh/BouwkampCodeBruteForcePackagerState.java index 9941aae0..47a2997b 100644 --- a/jmh/src/main/java/com/github/skjolber/packing/jmh/BouwkampCodeBruteForcePackagerState.java +++ b/jmh/src/main/java/com/github/skjolber/packing/jmh/BouwkampCodeBruteForcePackagerState.java @@ -32,31 +32,26 @@ public class BouwkampCodeBruteForcePackagerState { private final int threadPoolSize; - private final int nth; private ExecutorService pool1; private ExecutorService pool2; private List parallelBruteForcePackager = new ArrayList<>(); - private List parallelBruteForcePackagerNth = new ArrayList<>(); private List bruteForcePackager = new ArrayList<>(); - private List bruteForcePackagerNth = new ArrayList<>(); private List plainPackager = new ArrayList<>(); - private List plainPackagerNth = new ArrayList<>(); private List fastBruteForcePackager = new ArrayList<>(); public BouwkampCodeBruteForcePackagerState() { - this(8, 20000); + this(8); } - public BouwkampCodeBruteForcePackagerState(int threadPoolSize, int nth) { + public BouwkampCodeBruteForcePackagerState(int threadPoolSize) { this.threadPoolSize = threadPoolSize; - this.nth = nth; this.pool1 = Executors.newFixedThreadPool(threadPoolSize, new DefaultThreadFactory()); this.pool2 = Executors.newFixedThreadPool(threadPoolSize, new DefaultThreadFactory()); } - + @Setup(Level.Trial) public void init() { // these does not really result in successful stacking, but still should run as expected @@ -78,29 +73,22 @@ public void init() { List stackableItems3D = BouwkampConverter.getStackableItems3D(bkpLine); ParallelBruteForcePackager parallelPackager = ParallelBruteForcePackager.newBuilder().withExecutorService(pool2).withParallelizationCount(threadPoolSize * 16).build(); - ParallelBruteForcePackager parallelPackagerNth = ParallelBruteForcePackager.newBuilder().withExecutorService(pool1).withParallelizationCount(threadPoolSize * 16) - .withCheckpointsPerDeadlineCheck(nth).build(); BruteForcePackager packager = BruteForcePackager.newBuilder().build(); - BruteForcePackager packagerNth = BruteForcePackager.newBuilder().withCheckpointsPerDeadlineCheck(nth).build(); PlainPackager plainPackager = PlainPackager.newBuilder().build(); - PlainPackager plainPackagerNth = PlainPackager.newBuilder().withCheckpointsPerDeadlineCheck(nth).build(); FastBruteForcePackager fastPackager = FastBruteForcePackager.newBuilder().build(); // single-threaded this.bruteForcePackager.add(new BenchmarkSet(packager, stackableItems3D, containers)); - this.bruteForcePackagerNth.add(new BenchmarkSet(packagerNth, stackableItems3D, containers)); this.plainPackager.add(new BenchmarkSet(plainPackager, stackableItems3D, containers)); - this.plainPackagerNth.add(new BenchmarkSet(plainPackagerNth, stackableItems3D, containers)); this.fastBruteForcePackager.add(new BenchmarkSet(fastPackager, stackableItems3D, containers)); // multi-threaded this.parallelBruteForcePackager.add(new BenchmarkSet(parallelPackager, stackableItems3D, containers)); - this.parallelBruteForcePackagerNth.add(new BenchmarkSet(parallelPackagerNth, stackableItems3D, containers)); } } } @@ -110,6 +98,20 @@ public void shutdown() throws InterruptedException { pool1.shutdown(); pool2.shutdown(); + + for (BenchmarkSet benchmarkSet : parallelBruteForcePackager) { + benchmarkSet.getPackager().close(); + } + for (BenchmarkSet benchmarkSet : bruteForcePackager) { + benchmarkSet.getPackager().close(); + } + for (BenchmarkSet benchmarkSet : plainPackager) { + benchmarkSet.getPackager().close(); + } + for (BenchmarkSet benchmarkSet : fastBruteForcePackager) { + benchmarkSet.getPackager().close(); + } + try { Thread.sleep(500); } catch (Exception e) { @@ -121,26 +123,14 @@ public List getBruteForcePackager() { return bruteForcePackager; } - public List getBruteForcePackagerNth() { - return bruteForcePackagerNth; - } - public List getParallelBruteForcePackager() { return parallelBruteForcePackager; } - public List getParallelBruteForcePackagerNth() { - return parallelBruteForcePackagerNth; - } - public List getPlainPackager() { return plainPackager; } - public List getPlainPackagerNth() { - return plainPackagerNth; - } - public List getFastBruteForcePackager() { return fastBruteForcePackager; } diff --git a/jmh/src/main/java/com/github/skjolber/packing/jmh/DeadlineBenchmark.java b/jmh/src/main/java/com/github/skjolber/packing/jmh/DeadlineBenchmark.java index d54515a6..040164d3 100644 --- a/jmh/src/main/java/com/github/skjolber/packing/jmh/DeadlineBenchmark.java +++ b/jmh/src/main/java/com/github/skjolber/packing/jmh/DeadlineBenchmark.java @@ -44,11 +44,6 @@ public Object parallelPackagerDeadline(BouwkampCodeBruteForcePackagerState state return process(state.getParallelBruteForcePackager(), System.currentTimeMillis() + 10000); } - @Benchmark - public Object parallelPackagerDeadlineNth(BouwkampCodeBruteForcePackagerState state) throws Exception { - return process(state.getParallelBruteForcePackagerNth(), System.currentTimeMillis() + 10000); - } - @Benchmark public Object packagerNoDeadline(BouwkampCodeBruteForcePackagerState state) throws Exception { return process(state.getBruteForcePackager(), -1L); @@ -59,11 +54,6 @@ public Object packagerDeadline(BouwkampCodeBruteForcePackagerState state) throws return process(state.getBruteForcePackager(), System.currentTimeMillis() + 30000); } - @Benchmark - public Object packagerDeadlineNth(BouwkampCodeBruteForcePackagerState state) throws Exception { - return process(state.getBruteForcePackagerNth(), System.currentTimeMillis() + 30000); - } - public int process(List sets, long deadline) { int i = 0; for (BenchmarkSet set : sets) { diff --git a/jmh/src/main/java/com/github/skjolber/packing/jmh/EgyPackagerState.java b/jmh/src/main/java/com/github/skjolber/packing/jmh/EgyPackagerState.java index 126d3c59..27be007a 100644 --- a/jmh/src/main/java/com/github/skjolber/packing/jmh/EgyPackagerState.java +++ b/jmh/src/main/java/com/github/skjolber/packing/jmh/EgyPackagerState.java @@ -39,17 +39,13 @@ public class EgyPackagerState { private final int threadPoolSize; - private final int nth; private ExecutorService pool1; private ExecutorService pool2; private List parallelBruteForcePackager = new ArrayList<>(); - private List parallelBruteForcePackagerNth = new ArrayList<>(); private List bruteForcePackager = new ArrayList<>(); - private List bruteForcePackagerNth = new ArrayList<>(); private List plainPackager = new ArrayList<>(); - private List plainPackagerNth = new ArrayList<>(); private List fastBruteForcePackager = new ArrayList<>(); private static List stackableItems3D; @@ -74,12 +70,11 @@ public class EgyPackagerState { } public EgyPackagerState() { - this(8, 20000); + this(8); } - public EgyPackagerState(int threadPoolSize, int nth) { + public EgyPackagerState(int threadPoolSize) { this.threadPoolSize = threadPoolSize; - this.nth = nth; this.pool1 = Executors.newFixedThreadPool(threadPoolSize, new DefaultThreadFactory()); this.pool2 = Executors.newFixedThreadPool(threadPoolSize, new DefaultThreadFactory()); @@ -89,29 +84,22 @@ public EgyPackagerState(int threadPoolSize, int nth) { public void init() { ParallelBruteForcePackager parallelPackager = ParallelBruteForcePackager.newBuilder().withExecutorService(pool2).withParallelizationCount(threadPoolSize * 16) .build(); - ParallelBruteForcePackager parallelPackagerNth = ParallelBruteForcePackager.newBuilder().withExecutorService(pool1).withParallelizationCount(threadPoolSize * 16) - .withCheckpointsPerDeadlineCheck(nth).build(); BruteForcePackager packager = BruteForcePackager.newBuilder().build(); - BruteForcePackager packagerNth = BruteForcePackager.newBuilder().withCheckpointsPerDeadlineCheck(nth).build(); PlainPackager plainPackager = PlainPackager.newBuilder().build(); - PlainPackager plainPackagerNth = PlainPackager.newBuilder().withCheckpointsPerDeadlineCheck(nth).build(); FastBruteForcePackager fastPackager = FastBruteForcePackager.newBuilder().build(); // single-threaded this.bruteForcePackager.add(new BenchmarkSet(packager, stackableItems3D, containers)); - this.bruteForcePackagerNth.add(new BenchmarkSet(packagerNth, stackableItems3D, containers)); this.plainPackager.add(new BenchmarkSet(plainPackager, stackableItems3D, containers)); - this.plainPackagerNth.add(new BenchmarkSet(plainPackagerNth, stackableItems3D, containers)); this.fastBruteForcePackager.add(new BenchmarkSet(fastPackager, stackableItems3D, containers)); // multi-threaded this.parallelBruteForcePackager.add(new BenchmarkSet(parallelPackager, stackableItems3D, containers)); - this.parallelBruteForcePackagerNth.add(new BenchmarkSet(parallelPackagerNth, stackableItems3D, containers)); } public static Container getContainer(List items) { @@ -171,6 +159,19 @@ public void shutdown() throws InterruptedException { pool1.shutdown(); pool2.shutdown(); + for (BenchmarkSet benchmarkSet : parallelBruteForcePackager) { + benchmarkSet.getPackager().close(); + } + for (BenchmarkSet benchmarkSet : bruteForcePackager) { + benchmarkSet.getPackager().close(); + } + for (BenchmarkSet benchmarkSet : plainPackager) { + benchmarkSet.getPackager().close(); + } + for (BenchmarkSet benchmarkSet : fastBruteForcePackager) { + benchmarkSet.getPackager().close(); + } + try { Thread.sleep(500); } catch (Exception e) { @@ -182,26 +183,14 @@ public List getBruteForcePackager() { return bruteForcePackager; } - public List getBruteForcePackagerNth() { - return bruteForcePackagerNth; - } - public List getParallelBruteForcePackager() { return parallelBruteForcePackager; } - public List getParallelBruteForcePackagerNth() { - return parallelBruteForcePackagerNth; - } - public List getPlainPackager() { return plainPackager; } - public List getPlainPackagerNth() { - return plainPackagerNth; - } - public List getFastBruteForcePackager() { return fastBruteForcePackager; } diff --git a/jmh/src/main/java/com/github/skjolber/packing/jmh/TychoPackagerState.java b/jmh/src/main/java/com/github/skjolber/packing/jmh/TychoPackagerState.java index 30796ded..e016549d 100644 --- a/jmh/src/main/java/com/github/skjolber/packing/jmh/TychoPackagerState.java +++ b/jmh/src/main/java/com/github/skjolber/packing/jmh/TychoPackagerState.java @@ -30,17 +30,13 @@ public class TychoPackagerState { private final int threadPoolSize; - private final int nth; private ExecutorService pool1; private ExecutorService pool2; private List parallelBruteForcePackager = new ArrayList<>(); - private List parallelBruteForcePackagerNth = new ArrayList<>(); private List bruteForcePackager = new ArrayList<>(); - private List bruteForcePackagerNth = new ArrayList<>(); private List plainPackager = new ArrayList<>(); - private List plainPackagerNth = new ArrayList<>(); private List fastBruteForcePackager = new ArrayList<>(); private List stackableItems3D; @@ -49,12 +45,11 @@ public class TychoPackagerState { .withContainer(Container.newBuilder().withDescription("1").withEmptyWeight(1).withSize(1500, 1900, 4000).withMaxLoadWeight(100).build()).build(); public TychoPackagerState() { - this(8, 20000); + this(8); } - public TychoPackagerState(int threadPoolSize, int nth) { + public TychoPackagerState(int threadPoolSize) { this.threadPoolSize = threadPoolSize; - this.nth = nth; this.pool1 = Executors.newFixedThreadPool(threadPoolSize, new DefaultThreadFactory()); this.pool2 = Executors.newFixedThreadPool(threadPoolSize, new DefaultThreadFactory()); @@ -64,29 +59,21 @@ public TychoPackagerState(int threadPoolSize, int nth) { public void init() { ParallelBruteForcePackager parallelPackager = ParallelBruteForcePackager.newBuilder().withExecutorService(pool2).withParallelizationCount(threadPoolSize * 16) .build(); - ParallelBruteForcePackager parallelPackagerNth = ParallelBruteForcePackager.newBuilder().withExecutorService(pool1).withParallelizationCount(threadPoolSize * 16) - .withCheckpointsPerDeadlineCheck(nth).build(); BruteForcePackager packager = BruteForcePackager.newBuilder().build(); - BruteForcePackager packagerNth = BruteForcePackager.newBuilder().withCheckpointsPerDeadlineCheck(nth).build(); PlainPackager plainPackager = PlainPackager.newBuilder().build(); - PlainPackager plainPackagerNth = PlainPackager.newBuilder().withCheckpointsPerDeadlineCheck(nth).build(); FastBruteForcePackager fastPackager = FastBruteForcePackager.newBuilder().build(); // single-threaded this.bruteForcePackager.add(new BenchmarkSet(packager, stackableItems3D, containers)); - this.bruteForcePackagerNth.add(new BenchmarkSet(packagerNth, stackableItems3D, containers)); this.plainPackager.add(new BenchmarkSet(plainPackager, stackableItems3D, containers)); - this.plainPackagerNth.add(new BenchmarkSet(plainPackagerNth, stackableItems3D, containers)); - this.fastBruteForcePackager.add(new BenchmarkSet(fastPackager, stackableItems3D, containers)); // multi-threaded this.parallelBruteForcePackager.add(new BenchmarkSet(parallelPackager, stackableItems3D, containers)); - this.parallelBruteForcePackagerNth.add(new BenchmarkSet(parallelPackagerNth, stackableItems3D, containers)); } @TearDown(Level.Trial) @@ -94,6 +81,19 @@ public void shutdown() throws InterruptedException { pool1.shutdown(); pool2.shutdown(); + for (BenchmarkSet benchmarkSet : parallelBruteForcePackager) { + benchmarkSet.getPackager().close(); + } + for (BenchmarkSet benchmarkSet : bruteForcePackager) { + benchmarkSet.getPackager().close(); + } + for (BenchmarkSet benchmarkSet : plainPackager) { + benchmarkSet.getPackager().close(); + } + for (BenchmarkSet benchmarkSet : fastBruteForcePackager) { + benchmarkSet.getPackager().close(); + } + try { Thread.sleep(500); } catch (Exception e) { @@ -105,25 +105,14 @@ public List getBruteForcePackager() { return bruteForcePackager; } - public List getBruteForcePackagerNth() { - return bruteForcePackagerNth; - } - public List getParallelBruteForcePackager() { return parallelBruteForcePackager; } - public List getParallelBruteForcePackagerNth() { - return parallelBruteForcePackagerNth; - } - public List getPlainPackager() { return plainPackager; } - public List getPlainPackagerNth() { - return plainPackagerNth; - } public List getFastBruteForcePackager() { return fastBruteForcePackager; diff --git a/visualizer/packaging/src/test/java/com/github/skjolber/packing/visualizer/packaging/BruteForcePackagerTest.java b/visualizer/packaging/src/test/java/com/github/skjolber/packing/visualizer/packaging/BruteForcePackagerTest.java index 5ff9b959..5218f5b2 100644 --- a/visualizer/packaging/src/test/java/com/github/skjolber/packing/visualizer/packaging/BruteForcePackagerTest.java +++ b/visualizer/packaging/src/test/java/com/github/skjolber/packing/visualizer/packaging/BruteForcePackagerTest.java @@ -24,7 +24,7 @@ public class BruteForcePackagerTest extends AbstractPackagerTest { public void testSimpleImperfectSquaredRectangles() throws Exception { // this will take quite some time - BruteForcePackager packager = BruteForcePackager.newBuilder().withCheckpointsPerDeadlineCheck(1024).build(); + BruteForcePackager packager = BruteForcePackager.newBuilder().build(); BouwkampCodeDirectory directory = BouwkampCodeDirectory.getInstance(); diff --git a/visualizer/packaging/src/test/java/com/github/skjolber/packing/visualizer/packaging/ParallelBruteForcePackagerTest.java b/visualizer/packaging/src/test/java/com/github/skjolber/packing/visualizer/packaging/ParallelBruteForcePackagerTest.java index d24cb6f1..c2dbca8d 100644 --- a/visualizer/packaging/src/test/java/com/github/skjolber/packing/visualizer/packaging/ParallelBruteForcePackagerTest.java +++ b/visualizer/packaging/src/test/java/com/github/skjolber/packing/visualizer/packaging/ParallelBruteForcePackagerTest.java @@ -26,7 +26,7 @@ public class ParallelBruteForcePackagerTest extends AbstractPackagerTest { public void testSimpleImperfectSquaredRectangles() throws Exception { // if you do not have a lot of CPU cores, this will take quite some time - ParallelBruteForcePackager packager = ParallelBruteForcePackager.newBuilder().withExecutorService(executorService).withParallelizationCount(256).withCheckpointsPerDeadlineCheck(1024).build(); + ParallelBruteForcePackager packager = ParallelBruteForcePackager.newBuilder().withExecutorService(executorService).withParallelizationCount(256).build(); BouwkampCodeDirectory directory = BouwkampCodeDirectory.getInstance();