• Stars
    star
    391
  • Rank 110,003 (Top 3 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created over 8 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A variant of the Largest Area Fit First (LAFF) algorithm + brute force algorithm

3d-bin-container-packing

This library does 3D rectangular bin packing; it attempts to match a set of 3D items to one or more in a set of 3D containers. The result can be constrained to a maximum number of containers.

Projects using this library will benefit from:

  • short and predictable calculation time,
  • fairly good use of container space,
  • brute-force support for low number of boxes (ideal for small orders)

Bugs, feature suggestions and help requests can be filed with the issue-tracker.

Obtain

The project is implemented in Java and built using Maven. The project is available on the central Maven repository.

Maven coordinates

Add

<3d-bin-container-packing.version>3.0.7</3d-bin-container-packing.version>

and

<dependency>
    <groupId>com.github.skjolber.3d-bin-container-packing</groupId>
    <artifactId>core</artifactId>
    <version>${3d-bin-container-packing.version}</version>
</dependency>

or

Gradle coordinates

For

ext {
  containerBinPackingVersion = '3.0.7'
}

add

api("com.github.skjolber.3d-bin-container-packing:core:${containerBinPackingVersion}")

Java 11+ projects please use module com.github.skjolber.packing.core.

Usage

The units of measure is out-of-scope, be they cm, mm or inches.

Obtain a Packager instance, then then compose your container and product list:

List<StackableItem> products = new ArrayList<StackableItem>();

products.add(new StackableItem(Box.newBuilder().withId("Foot").withSize(6, 10, 2).withRotate3D().withWeight(25).build(), 1));
products.add(new StackableItem(Box.newBuilder().withId("Leg").withSize(4, 10, 1).withRotate3D().withWeight(25).build(), 1));
products.add(new StackableItem(Box.newBuilder().withId("Arm").withSize(4, 10, 2).withRotate3D().withWeight(50).build(), 1));

// add a single container type
Container container = Container.newBuilder()
    .withDescription("1")
    .withSize(10, 10, 3)
    .withEmptyWeight(1)
    .withMaxLoadWeight(100)
    .build();
    
// with unlimited number of containers available
List<ContainerItem> containerItems = ContainerItem
    .newListBuilder()
    .withContainer(container)
    .build();

Pack all in a single container:

PackagerResult result = packager
    .newResultBuilder()
    .withContainers(containerItems)
    .withStackables(products)
    .build();

if(result.isSuccess()) {
    Container match = result.get(0);
    
    // ...
}

Pack all in a maximum number of containers:

int maxContainers = ...; // maximum number of containers which can be used

PackagerResult result = packager
    .newResultBuilder()
    .withContainers(containerItems)
    .withStackables(products)
    .withMaxContainerCount(maxContainers)
    .build();

Note that all packager instances are thread-safe.

Plain packager

A simple packager

PlainPackager packager = PlainPackager
    .newBuilder()
    .build();

Largest Area Fit First (LAFF) packager

A packager using the LAFF algorithm

LargestAreaFitFirstPackager packager = LargestAreaFitFirstPackager
    .newBuilder()
    .build();

Brute-force packager

For a low number of packages (like <= 6) the brute force packager might be a good fit.

Packager packager = BruteForcePackager
    .newBuilder()
    .build();

Using a deadline is recommended whenever brute-forcing in a real-time application.

Algorithm details

Largest Area Fit First algorithm

The implementation is based on this paper, and is not a traditional bin packing problem solver.

The box which covers the largest ground area of the container is placed first; its height becomes the level height. Boxes which fill the full remaining height take priority. Subsequent boxes are stacked in the remaining space in at the same level, the boxes with the greatest volume first. If box height is lower than level height, the algorithm attempts to place some there as well.

When no more boxes fit in a level, the level is incremented and the process repeated. Boxes are rotated, containers not.

  • LargestAreaFitFirstPackager stacks in 3D within each level
  • FastLargestAreaFitFirstPackager stacks in 2D within each level

The algorithm runs reasonably fast, usually in milliseconds. Some customization is possible.

Plain algorithm

This algorithm selects the box with the biggest volume, fitting it where it is best supported.

Brute-force algorithm

This algorithm has no logic for selecting the best box or rotation; running through all permutations, for each permutation all rotations:

  • BruteForcePackager attempts all box orders, rotations and placement positions.
  • FastLargestAreaFitFirstPackager selects all box orders and rotations, selecting the most appropriate placement position.

The complexity of this approach is exponential, and thus there is a limit to the feasible number of boxes which can be packaged within a reasonable time. However, for real-life applications, a healthy part of for example online shopping orders are within its grasp.

The worst case complexity can be estimated using the DefaultPermutationRotationIterator before packaging is attempted.

The algorithm tries to skip combinations which will obviously not yield a (better) result:

  • permutations
    • two or more boxes have the same dimensions
    • permutations which mutated at a previously unreachable index
  • fewer rotations
    • two or more sides have the same length
    • rotations which mutated at a previously unreachable index

There is also a parallel version ParallelBruteForcePackager of the brute-force packager, for those wishing to use it on a multi-core system.

Note that the algorithm is recursive on the number of boxes, so do not attempt this with many boxes (it will likely not complete in time anyhow).

Visualizer

There is a simple output visualizer included in this project, based of three.js. This visualizer is currently intended as a tool for developing better algorithms (not as stacking instructions).

Alt text

To use the visualizer during development, make your unit tests write directly to a file in the project (see VisualizationTest example).

Customization

The code has been structured so it is possible to extend and adapt to specialized needs. See AbstractPackager class, the extreme-points and test artifacts.

Get involved

If you have any questions, comments or improvement suggestions, please file an issue or submit a pull-request.

Note on bugs: Please follow shuairan's example and file a test case with a visualization.

Feel free to connect with me on LinkedIn, see also my Github page.

License

Apache 2.0. Social media preview by pch.vector on www.freepik.com.

History

  • 3.0.4-3.0.6: Fix issue #689
  • 3.0.3: Fix module info
  • 3.0.2: Make Plain Packager prefer low z coordinate over supported area.
  • 3.0.1: Various performance improvements.
  • 3.0.0: Support max number of containers (i.e. per container type). Use builders from now on. Various optimizations.
  • 2.1.4: Fix issue #574
  • 2.1.3: Fix nullpointer
  • 2.1.2: Tidy up, i.e. remove warnings, nuke some dependencies.
  • 2.1.1: Improve free space calculation performance
  • 2.1.0: Improve brute force iterators, respect deadlines in brute for packagers.

More Repositories

1

external-nfc-api

Interaction with external NFC readers in Android
Java
171
star
2

ndef-tools-for-android

NDEF Tools for Android
Java
139
star
3

desfire-tools-for-android

Open source MIFARE DESFire EV1 NFC library for Android
Java
75
star
4

mockito-soap-cxf

Test SOAP services using JUnit and Mockito
Java
49
star
5

xswi

The simple, standalone XML Stream Writer for iOS
Objective-C
30
star
6

Fagmote

Java
28
star
7

aotc-gradle-plugin

Ahead-of-Time Compilation in Gradle projects.
Java
16
star
8

nfc-tags

15
star
9

nfc-eclipse-plugin

Eclipse plugin for working with NDEF in NFC tags
Java
14
star
10

java-jwt-benchmark

Project for benchmarking popular Json Web Token (JWT) frameworks for Java using JMH.
Java
10
star
11

maven-cache-github-action

Github Action for caching of the Maven repository (at ~/.m2)
TypeScript
10
star
12

sesseltjonna-csv

World's fastest CSV parser / databinding for Java
Java
10
star
13

json-log-filter

World's fastest JSON filter for the JVM
Java
7
star
14

mockito-rest-spring

REST web-service mocking utility for Spring
Java
6
star
15

android-nfc-lifecycle-wrapper

Library for NFC on Android using androidx lifecycle extensions and functional interfaces.
Java
6
star
16

json-log-domain

Library supporting JSON-logging with Logback and Stackdriver
Java
6
star
17

jackson-syntax-highlight

Syntax highlighting (via ANSI) for JSON output using Jackson
Java
6
star
18

logback-logstash-syntax-highlighting-decorators

Syntax highlighting JSON decorator for logstash-logback-encoder
Java
6
star
19

google

This is my working repository for Google APIs.
HTML
4
star
20

async-stax-utils

Asynchronous StAX-utils
Java
4
star
21

xml-log-filter

High-performance filtering of to-be-logged XML
Java
4
star
22

mule-module-dxpath

Dynamic XPath module for Mule ESB
Java
3
star
23

gtfs-neo4j-import

GTFS Neo4j import
Java
3
star
24

gtfs-databinding

High-performance reading of zipped GTFS files
Java
3
star
25

gradle-jfrog-artifactory-multimodule-library-example

Pattern for multimodule open-source library using gradle and JFrog Artifactory
Java
2
star
26

MIFAREClassicRefactor

Java
2
star
27

csv-benchmark

Benchmarking high-performance open source CSV parsers using JMH
Java
2
star
28

unzip-csv

High-performance (i.e. multi-threaded) unpacking and processing of CSV files directly from ZIP archives.
Java
1
star
29

inline-histogram

Inline text histogram
Java
1
star
30

spring-boot-maven3-jdk8-centos

Openshift docker image with s2i file and descriptor.
Shell
1
star
31

gradle-cache-cleaner

Removed unused dependencies from Gradle cache
Java
1
star
32

datasets-json-benchmark

Json parser benchmarks for specific datasets
Java
1
star
33

skjolber.github.io

Thomas
JavaScript
1
star