Quantcast
Channel: Yet Another Math Programming Consultant
Viewing all articles
Browse latest Browse all 809

2d Bin Packing

$
0
0

 In [1], a question was posed about a particular type of 2d bin-packing. This is a good reason to play with some models.


Problem statement and example data


We consider \(n=50\) rectangular items of different sizes that have to be placed into bins. There are five different categories, each with a different size (height and width). Al the bins have the same size. The goal is to use as few bins as possible.


----     35 PARAMETER itemSize  sizes of bin and items

w h

cat1 7.00012.000
cat2 9.0003.000
cat3 5.00014.000
cat4 13.00011.000
cat5 6.0008.000
bin 40.00060.000


---- 35 SET itemMap mapping between items and categories

item1 .cat1, item2 .cat1, item3 .cat1, item4 .cat1, item5 .cat1, item6 .cat1, item7 .cat1
item8 .cat1, item9 .cat1, item10.cat1, item11.cat2, item12.cat2, item13.cat2, item14.cat2
item15.cat2, item16.cat2, item17.cat2, item18.cat2, item19.cat2, item20.cat2, item21.cat3
item22.cat3, item23.cat3, item24.cat3, item25.cat3, item26.cat3, item27.cat3, item28.cat3
item29.cat3, item30.cat3, item31.cat4, item32.cat4, item33.cat4, item34.cat4, item35.cat4
item36.cat4, item37.cat4, item38.cat4, item39.cat4, item40.cat4, item41.cat5, item42.cat5
item43.cat5, item44.cat5, item45.cat5, item46.cat5, item47.cat5, item48.cat5, item49.cat5
item50.cat5


Furthermore, we assume items cannot be rotated.


Model A


In this formulation, we will not assume that our sizes are integer-valued. The rectangles are allowed to have any positive width and height.
 
The model consists of three parts:
  • Assignment of items to bins.
  • We position the items inside a bin. No-overlap constraints are used for the items placed in the same bin.
  • Some ordering constraints. This can make the solution look better and reduce symmetry in the problem.
In the following, \(i\) and \(j\) indicate items, and \(k\) refers to a bin. The number of items is known. The number of bins is large enough such that we know all items can fit. Here I just use 10 bins. A better approach would be to have some heuristic that finds a good initial configuration. 

Assignment
To model the assignment, we introduce binary variables \[\color{darkred}a_{i,k} = \begin{cases}1 & \text{if item $i$ is placed in bin $k$} \\ 0 & \text{otherwise}\end{cases}\] Each item should go to exactly one bin, so we have \[ \sum_k \color{darkred} a_{i,k} = 1 \>\>\forall i\] 

Bounds
The items should be placed inside a bin: \[\begin{align} &\color{darkred}x_i \in [0,\color{darkblue}w_{\mathit{bin}} - \color{darkblue}w_i]\\ & \color{darkred}y_i \in [0,\color{darkblue}h_{\mathit{bin}} - \color{darkblue}h_i] \end{align}\] 

No-overlap
This is a bit more involved. We need to make sure when comparing items \(i\) and \(j\) assigned to the same bin \(k\) that one of the following conditions hold: item \(i\) is to the left, or to the right, or above, or below item \(j\).  First we need to establish if two items \(i\) and \(j\) are in the same bin. This can be done with \[\begin{align}&\color{darkred}s_{i,j} \ge \color{darkred} a_{i,k} + \color{darkred} a_{j,k} - 1\>\>\forall k, i\lt j \\ &\color{darkred}s_{i,j}\in [0,1]\end{align}\] This implements the implication \[\color{darkred}a_{i,k} = \color{darkred}a_{j,k} = 1 \Rightarrow \color{darkred}s_{i,j}=1\] To prevent double work, we only do this for \(i\lt j\). With this we can implement our no-overlap constraints: \[\begin{align} & \color{darkred} x_i +\color{darkblue} w_i  \le \color{darkred} x_j + \color{darkblue} M (1-\color{darkred}\delta_{i,j,1}) + \color{darkblue} M (1-\color{darkred}s_{i,j}) \\ & \color{darkred} x_i  \ge \color{darkred} x_j +\color{darkblue} w_j  - \color{darkblue} M (1-\color{darkred}\delta_{i,j,2}) - \color{darkblue} M (1-\color{darkred}s_{i,j}) \\ & \color{darkred} y_i +\color{darkblue} h_i  \le \color{darkred} y_j + \color{darkblue} M (1-\color{darkred}\delta_{i,j,3}) + \color{darkblue} M (1-\color{darkred}s_{i,j}) \\ & \color{darkred} y_i  \ge \color{darkred} y_j +\color{darkblue} h_j  - \color{darkblue} M (1-\color{darkred}\delta_{i,j,4}) - \color{darkblue} M (1-\color{darkred}s_{i,j}) \\  & \sum_p \color{darkred}\delta_{i,j,p}\ge 1 \\ & \color{darkred}\delta_{i,j,p} \in \{0,1\} \end{align}\]
A conservative value for \(\color{darkblue}M\) would be to set it equal to the bin width (for the \(x\) constraints) or height (for the \(y\) constraints).

Objective
The objective is to minimize the number of used bins. A bin is used if there at least one item in it. So we can write: \[\begin{align} \min&\sum_k \color{darkred}u_k \\ &\color{darkred}u_k  \ge \color{darkred}a_{i,k} &&\forall i,k \\ & \color{darkred}u_k \in [0,1] \end{align}\]
 
Order used bins
It may be preferable to have the unused bins be the higher-numbered ones and the used bins the lower-numbered ones. We can easily enforce that bin \(k\) is used before bin \(k+1\): \[ \color{darkred}u_k  \ge \color{darkred}u_{k+1} \] 

Lower bound
We can calculate a lower bound on the objective by just looking at the area of the bin and the items. The total area of all the items does not fit in just one bin. So we know we need at least two bins. This also means that as soon as we have found a solution with two bins we can stop. 

Does it work?
This can be a very difficult model to solve. In this case, the performance is not bad at all. Here we see the MIP solver in action:


Summary of the model

MIP Model A
\[\begin{align} \min&\sum_k \color{darkred}u_k \\ & \color{darkred}u_k  \ge \color{darkred}a_{i,k} &&\forall i,k \\ & \sum_k \color{darkred} a_{i,k} = 1 && \forall i \\ & \color{darkred}s_{i,j} \ge \color{darkred} a_{i,k} + \color{darkred} a_{j,k} - 1 && \forall k, i\lt j \\ & \color{darkred} x_i +\color{darkblue} w_i  \le \color{darkred} x_j + \color{darkblue} M (1-\color{darkred}\delta_{i,j,1}) + \color{darkblue} M (1-\color{darkred}s_{i,j}) && \forall i \lt j\\ & \color{darkred} x_i  \ge \color{darkred} x_j +\color{darkblue} w_j  - \color{darkblue} M (1-\color{darkred}\delta_{i,j,2}) - \color{darkblue} M (1-\color{darkred}s_{i,j}) && \forall i \lt j\\ & \color{darkred} y_i +\color{darkblue} h_i  \le \color{darkred} y_j + \color{darkblue} M (1-\color{darkred}\delta_{i,j,3}) + \color{darkblue} M (1-\color{darkred}s_{i,j})&& \forall i \lt j \\ & \color{darkred} y_i  \ge \color{darkred} y_j +\color{darkblue} h_j  - \color{darkblue} M (1-\color{darkred}\delta_{i,j,4}) - \color{darkblue} M (1-\color{darkred}s_{i,j}) && \forall i \lt j\\  & \sum_p \color{darkred}\delta_{i,j,p}\ge 1 && \forall i \lt j \\ & \color{darkred}u_k  \ge \color{darkred}u_{k+1} && \forall k \> \text{except last}\\ & \color{darkred}u_k \in [0,1] \\ & \color{darkred} a_{i,k} \in \{0,1\} \\ & \color{darkred}s_{i,j}\in [0,1] \\ & \color{darkred}\delta_{i,j,p} \in \{0,1\} \end{align} \]


The variables with domain \([0,1]\) (i.e. continuous between 0 and 1) can also be declared binary. In my test run, I used binary variables.

Alternative model B


In [2] a somewhat different MIP model is proposed. Let's have a look:


I don't immediately recognize much of my model in this version.

The first thing we have is a single set: \(Q =\{1,\dots,n\}\) for both the items and the bins. That is interesting. In my model, I would dimension the set \(k\) (the bins) as small as possible (e.g. by using some heuristic). Here we have \(n=50\) items that can fit in 2 bins, and this model assumes we dimension for 50 bins.

We also immediately notice problems in the indexing. The set \(Q\) starts with element 1, but the model uses indices 0. I assume we start the summations at 1.

The matrix \(\alpha_{i,k}\in \{0,1\}\) indicates if item \(i\) is placed in bin \(k\). This looks like my assignment variable \(\color{darkred}a_{i,k}\in \{0,1\}\). Well, that is only superficially so. Here \(\alpha_{i,k}\) has the following properties:
  • Only \(\alpha_{i,k}\) for \(i \ge k\) is used. (This essentially means \(\alpha_{i,k}=0\) for \(i \lt k\)). 
  • The diagonal \(\alpha_{k,k}\)  indicates if bin \(k\) is used. This explains the formulation of the objective. 
  • If the diagonal element is zero, the whole column should be zero, This is stated in constraint (3). We can skip equation (3) for the case \(i=k\). I.e. we only need to apply it for \(i\gt k\) instead of  \(i\ge k\).
The variable \(ul, ur, uu, ua\) are similar to my variables \(\color{darkred}\delta\). The term \(W\cdot (3-ul_{ij}-\alpha_{ik}-\alpha_{jk})\) says that the corresponding constraint should be  binding only if all \(ul_{ij}=\alpha_{ik}=\alpha_{jk}\). Similar for the other no-overlap constraints.  In my model, I don't need as many of these big-M constraints as I calculated \(\color{darkred}s_{i,j}\) separately.


Alternative model C


In [3,4] the following model is shown:



The integer variable \(m_i\ge 1\) is the bin number where item \(i\) is placed.
 
This model has binary variables:
  • \(l_{i,j}\) indicating that item \(i\) is to the left of item \(j\), 
  • \(b_{i,j}=1\) means: item \(i\) is below item \(j\),
  • \(p_{i,j}=1\) indicates \(m_i \lt m_j\).
Constraint (1) says: items \(i\) and \(j\) should not overlap (i.e. below or left of one another), or they should be assigned to a different bin. Equations (2) and (3) are the familiar no-overlap constraints (somewhat rearranged).  Note that there we apply them for all \(i\ne j\). Equation (5) implements the implication: \(p_{i,j}=1 \Rightarrow m_i \le m_j-1\). 


Performance


A quick test on our small data set.

Model AModel BModel C
Equations18,94585,8018,625
Variables6,7366,2767,501
Discrete variables6,6356,1757,400
Nonzeros63,279425,17629,500
Objective222
Time1204,38060
Nodes1,89332,3149,062
Iterations296,176617,121 287,226


In all cases, I added the lowerbound \[\color{darkred}{\mathit{obj}} \ge \left\lceil\frac{\sum_i\color{darkblue}{\mathit{area}}_i}{\color{darkblue}{\mathit{area}}_{\mathit{bin}}} \right\rceil\] This really helps. Without this bound the solver just spends a lot a time proving that 2 is the optimal objective. An area constraint (sum of area of assigned items to a bin cannot exceed bin area) can also help with this.

Model C is the winner for this data set. It needs more nodes than model A, but still finishes earlier.

References


  1. https://stackoverflow.com/questions/66156154/how-to-model-fixed-width-columns-for-2d-bin-strip-packing-problem
  2. Christian Blum, Verena Schmid and Lukas Baumgartner, On Solving the Oriented Two-Dimensional under Free Guillotine Cutting: Exploiting the Power of Probabilistic Solution Construction, sept. 2012,. https://www.researchgate.net/publication/230795080_On_Solving_the_Oriented_Two-Dimensional_Bin_Packing_Problem_under_FreeGuillotine_Cutting_Exploiting_the_Power_of_Probabilistic_SolutionConstruction
  3. Christian Blum, Verena Schmid, Solving the 2D bin packing problem by means of a hybrid evolutionary algorithm, Procedia Computer Science 18 ( 2013 ) 899 – 908, https://www.sciencedirect.com/science/article/pii/S1877050913003980
  4. David Pisinger, Mikkel Mühldorff Sigurd, Using Decomposition Techniques and Constraint Programming for Solving the Two-Dimensional Bin-Packing Problem, January 2007, Informs Journal on Computing 19(1):36-51

Viewing all articles
Browse latest Browse all 809

Trending Articles