Assume we have an infinite collection of 3d boxes of different sizes. As an example let's use:
---- 30 PARAMETER boxesavailable box sizes
size-x size-y size-z
box1 467
box2 123
box3 456
box4 101232
We want to build a tower of boxes such that the base of the box on top is strictly smaller than the underlying box. The interesting angle is: we can rotate the boxes. Without loss of generality, we can assume that for all boxes, after rotation, we have: the width (\(x\)) is less than the depth \((y\)). (This may require a bit of thinking). With this, any box can be placed in just three different ways.
![]() |
3d rotations |
The objective is to build a tower that is as tall as possible. In [1], this problem is solved using a Dynamic Programming algorithm. Here I want to see how this looks using a MIP model.
Model development
The first thing to do is to create the rotations. I created a mapping set that maps from one size to another. Then I applied this mapping to all our boxes. The result is:
---- 38 SET rot rotate s1->s2
size-x size-y size-z
rot1.size-x YES
rot1.size-y YES
rot1.size-z YES
rot2.size-x YES
rot2.size-y YES
rot2.size-z YES
rot3.size-x YES
rot3.size-y YES
rot3.size-z YES
---- 44 PARAMETER sizes sizes under rotation
size-x size-y size-z
box1.rot1 467
box1.rot2 476
box1.rot3 674
box2.rot1 123
box2.rot2 132
box2.rot3 231
box3.rot1 456
box3.rot2 465
box3.rot3 564
box4.rot1 101232
box4.rot2 103212
box4.rot3 123210
I used the assumption that the data is ordered by size (i.e. \(x \le y \le z\)). See our data table to verify that this indeed holds.
With this under our belt, the model is not very difficult. We use the following indices:
- \(i\): the level. Just use a set that is larger than the number of levels you expect.
- \(k\): the box type from the inventory.
- \(r\): the rotation
- \(s\): the sizes in each direction.
- \({\mathit{xy}}\): subset of \(s\), only the \(x\) and \(y\) coordinates.
MIP Model |
---|
\[\begin{align}\max\>&\color{darkred}{\mathit{height}}=\sum_i \color{darkred}{\mathit{size}}_{i,z}\\ & \color{darkred}b_i = \sum_{k,r}\color{darkred}{\mathit{assign}}_{i,k,r} \\ & \color{darkred}b_i \le \color{darkred}b_{i-1} && \text{ordering} \\ & \color{darkred}{\mathit{size}}_{i,s} = \sum_{k,r}\color{darkred}{\mathit{assign}}_{i,k,r} \cdot \color{darkblue}{\mathit{sizes}}_{k,r,s} && \text{size at level $i$} \\ & \color{darkred}{\mathit{size}}_{i,xy} \le\color{darkred}{\mathit{size}}_{i-1,xy}-0.5\cdot\color{darkred}b_i && \text{smaller boxes on top} \end{align}\] |
Notes:
- We start at the bottom, i.e. \(\color{darkred}b_1 = 1\). So \(\color{darkred}b_i=0\) only happens only at the top. The constraint \(\color{darkred}b_i \le \color{darkred}b_{i-1}\) is not really needed as the equation that requires smaller boxes to be on top, has the same effect. So, we can make the model even more compact.
- We require that the width and the depth are both 0.5 less than the box of the previous layer. We make an exception when there is no box, in which case all sizes remain zero. If we have continuous sizes, we need to change this minimum shrinkage to something like 0.001 (this number should be large than the feasibility tolerance of the solver).
---- 82 PARAMETER results optimal stack
size-x size-y size-z
level1.box4.rot3 123210
level2.box4.rot1 101232
level3.box1.rot3 674
level4.box3.rot3 564
level5.box3.rot1 456
level6.box2.rot3 231
level7.box2.rot1 123
References
- Box stacking problem, DP-22, https://www.geeksforgeeks.org/box-stacking-problem-dp-22/
Appendix: GAMS model
$ontext |