9.2 An ILP model for the 0/1 Knapsack Problem¶
Part 2 · Formulations and their properties · Chapter 9 · Chapter 9: Knapsack problems · Lecture notes, volume 2 (PDF) · Slides · Exercises: Knapsack problems · App: LP relaxation and upper bounds for the 0/1 KP
The 0/1 Knapsack Problem and its reference instance (Example 2.1) are defined in Chapter 2 «Knapsack problems», Section 2.2.
-
We introduce the following \(n\) binary variables:
\[\begin{equation*} \label{BIN_VAR} x_j = \left\{ \begin{array}{ll} 1 & \textrm{if item $j$ is selected in the knapsack}\\[1.5ex] 0 & \textrm{otherwise} \end{array} \qquad \forall j \in \underbrace{\{1,2,\dots,n\}}_{=\mathscr{I}}. \right. \tag{9.1} \end{equation*}\]
Using these variables, an Integer Linear Programming (ILP) model for the 0/1 Knapsack Problem (KP) reads as follows:
This ILP model is referred to as the \(\mathrm{ILP}_{\mathrm{KP}}\) model.
-
Description of the objective function and constraints of the \(\mathrm{ILP}_{\mathrm{KP}}\) model:
-
the linear objective function \(\eqref{obj_KP}\) maximizes the total profit of the items selected in the knapsack;
-
constraint \(\eqref{cons1_KP}\) ensures that the total weight of the selected items does not exceed the knapsack capacity (one linear constraint);
-
finally, constraints \(\eqref{cons2_KP}\) define the variables of the ILP model.
-
Example 9.1: \(\mathrm{ILP}_{\mathrm{KP}}\) model
We consider a knapsack of capacity \(c = 9\) and a set \(\mathscr{I} = \{1,2,3,4,5,6\}\) of \(6\) items (\(n = 6\)), with the following profits and weights:
This is the KP instance of Example 2.1, and we refer to it as Example 9.1. The \(\mathrm{ILP}_{\mathrm{KP}}\) model for this KP instance is:
An optimal solution \(~{\tilde{\vet x}}(\mathrm{ILP}_{\mathrm{KP}})\) is:
The optimal value \(z(\mathrm{ILP}_{\mathrm{KP}})\) is \(15\). The associated optimal solution of the KP is \(\tilde{S} =\{1,4\}\), and it is the unique optimal solution.
9.3 A first Linear Programming relaxation¶
To obtain a Linear Programming (LP) relaxation of the \(\mathrm{ILP}_{\mathrm{KP}}\) model, we relax constraints \(\eqref{cons2_KP}\) with the following ones:
This LP model is referred to as the \(\mathrm{LP}\text{-}0_{\mathrm{KP}}\) model.
Example 9.2: \(\mathrm{LP}\text{-}0_{\mathrm{KP}}\) model
The \(\mathrm{LP}\text{-}0_{\mathrm{KP}}\) model for the KP instance of Example 9.1 is:
An optimal solution \(~{\tilde{\vet x}}(\mathrm{LP}\text{-}0_{\mathrm{KP}})\) is:
The optimal value \(z(\mathrm{LP}\text{-}0_{\mathrm{KP}})\) is \(27\).
Accordingly we have the upper bound:
Using a non-negative dual variable \(\pi\) for the constraint \(\eqref{cons1_KP}\), the dual model of the \(\mathrm{LP}\text{-}0_{\mathrm{KP}}\) model is:
Example 9.3: \(D(\mathrm{LP}\text{-}0_{\mathrm{KP}})\) model
The dual \(D(\mathrm{LP}\text{-}0_{\mathrm{KP}})\) model for the KP instance of Example 9.1 is:
An optimal solution \(~{\tilde{\pi}}(D(\mathrm{LP}\text{-}0_{\mathrm{KP}}))\) is:
The optimal value \(z(D(\mathrm{LP}\text{-}0_{\mathrm{KP}}))\) is \(27\).
- The following proposition provides formulas to compute an optimal solution of the primal \(\mathrm{LP}\text{-}0_{\mathrm{KP}}\) model and of the dual \(D(\mathrm{LP}\text{-}0_{\mathrm{KP}})\) model.
Proposition 9.1
If the first item \(j=1\) has the largest efficiency, an optimal solution of the primal \(\mathrm{LP}\text{-}0_{\mathrm{KP}}\) model is:
and an optimal solution of the dual \(D(\mathrm{LP}\text{-}0_{\mathrm{KP}})\) model is:
Corollary 9.2
The optimal value of the primal \(\mathrm{LP}\text{-}0_{\mathrm{KP}}\) and dual \(D(\mathrm{LP}\text{-}0_{\mathrm{KP}})\) models is:
-
The proof proceeds in three steps:
-
We show that \(\eqref{PP_0}\) is a feasible solution for the primal \(\mathrm{LP}\text{-}0_{\mathrm{KP}}\) model;
-
We show that \(\eqref{DD_0}\) is a feasible solution for the dual \(D(\mathrm{LP}\text{-}0_{\mathrm{KP}})\) model;
-
We show that both feasible solutions have the same objective function value.
Therefore, by LP Strong Duality, \(\eqref{PP_0}\) and \(\eqref{DD_0}\) are a pair of optimal primal and dual solutions.
-
Proof
-
1. We have:
\[ \sum_{j \in \mathscr{I}} w_j \, \tilde{x}_j = w_1 \; \frac{c}{w_1} = c \]thus constraint \(\eqref{cons1_KP}\) is satisfied.
Since \(\tilde{x}_j \ge 0\), for every \(j \in \mathscr{I}\), constraints \(\eqref{cons2_KP_LP}\) are satisfied.
Therefore, \(\eqref{PP_0}\) is a feasible solution for the primal \(\mathrm{LP}\text{-}0_{\mathrm{KP}}\) model.
-
2. For every \(j \in \mathscr{I}\), we have:
\[ w_j \, \tilde{\pi} = w_j \; \frac{p_1}{w_1} \ge p_j \qquad \text{since} \qquad \frac{p_1}{w_1} \ge \frac{p_j}{w_j} \]thus constraints \(\eqref{eq:KP-cap_LP_D_0}\) are satisfied.
We have \(\tilde{\pi} \ge 0\), thus constraint \(\eqref{eq:KP-cont1_LP_D_0}\) is satisfied.
Therefore, \(\eqref{DD_0}\) is a feasible solution for the dual \(D(\mathrm{LP}\text{-}0_{\mathrm{KP}})\) model.
-
3. For the objective functions, we have:
\[ \sum_{j \in \mathscr{I}} p_j \, \tilde{x}_j = p_1 \; \frac{c}{w_1} = c \, \tilde{\pi} \]thus the primal and dual objective function values are equal.
Therefore, by LP Strong Duality, \(\eqref{PP_0}\) and \(\eqref{DD_0}\) are a pair of optimal primal and dual solutions. □
9.4 A second Linear Programming relaxation¶
To obtain an LP relaxation of the \(\mathrm{ILP}_{\mathrm{KP}}\) model, we relax constraints \(\eqref{cons2_KP}\) with the following ones:
This LP model is referred to as the \(\mathrm{LP}_{\mathrm{KP}}\) model.
Example 9.4: \(\mathrm{LP}_{\mathrm{KP}}\) model
The \(\mathrm{LP}_{\mathrm{KP}}\) model for the KP instance of Example 9.1 is:
An optimal solution \(~{\tilde{\vet x}}(\mathrm{LP}_{\mathrm{KP}})\) is:
The optimal value \(z(\mathrm{LP}_{\mathrm{KP}})\) is \(\frac{49}{3}\).
Accordingly we have the upper bound:
Using a non-negative dual variable \(\pi\) for the constraint \(\eqref{cons1_KP}\) and a non-negative dual variable \(\mu_j\) for each constraint \(\eqref{cons2_KP_LP_bis}\), the dual model of the \(\mathrm{LP}_{\mathrm{KP}}\) model is:
Example 9.5: \(D(\mathrm{LP}_{\mathrm{KP}})\) model
The dual \(D(\mathrm{LP}_{\mathrm{KP}})\) model for the KP instance of Example 9.1 is:
An optimal solution \((\tilde{\pi},\tilde{\vet \mu})(D(\mathrm{LP}_{\mathrm{KP}}))\) is:
The optimal value \(z(D(\mathrm{LP}_{\mathrm{KP}}))\) is \(\frac{49}{3}\).
9.4.1 Item efficiency¶
Definition 9.3: efficiency
The efficiency of an item \(j \in \mathscr{I}\) is defined as the ratio between its profit and its weight:
- The efficiency of an item is a measure of its profitability per unit of weight: it indicates how much profit is gained for each unit of weight.
Example 9.6: item efficiencies
Let us consider the KP instance of Example 9.1. We have:
| Items | \(j=1\) | \(j=2\) | \(j=3\) | \(j=4\) | \(j=5\) | \(j=6\) | |
|---|---|---|---|---|---|---|---|
| Profits | \(p_j\) | \(6\) | \(5\) | \(8\) | \(9\) | \(6\) | \(7\) |
| Weights | \(w_j\) | \(2\) | \(3\) | \(6\) | \(7\) | \(5\) | \(9\) |
| Efficiencies | \(\frac{p_j}{w_j}\) | \(3\) | \(\frac{5}{3}\) | \(\frac{4}{3}\) | \(\frac{9}{7}\) | \(\frac{6}{5}\) | \(\frac{7}{9}\) |
The items are sorted by non-increasing efficiency, since:
Sorting the items by non-increasing efficiency requires a sorting algorithm with running time \(O(n \log n)\), which is a polynomial function of the number \(n\) of items.
9.4.2 Split item and residual capacity¶
Definition 9.4: split item and residual capacity
Given a set \(\mathscr{I}=\{1,2,\dots,n\}\) of \(n\) items ordered by non-increasing efficiency, the split item is the item
and the residual capacity is
-
The split item is the first one that does not fit entirely in the knapsack if all the previous ones are selected. It is also called break item or critical item, and it can be written as
\[\begin{equation*} s = \min \left\{ j \in \mathscr{I} : \sum_{k=1}^{j} w_k > c \right\} \end{equation*}\] -
The residual capacity is the knapsack capacity remaining when all the items that precede the split item are selected. It satisfies \(0 \le \mathit{rc} < w_s\).
Example 9.7: split item and residual capacity
For the KP instance of Example 9.1, the split item is \(s=3\) since:
and the residual capacity is:
- The following proposition provides formulas to compute an optimal solution of the primal \(\mathrm{LP}_{\mathrm{KP}}\) model and of the dual \(D(\mathrm{LP}_{\mathrm{KP}})\) model.
Proposition 9.5
If the items are sorted by non-increasing efficiency, an optimal solution of the primal \(\mathrm{LP}_{\mathrm{KP}}\) model is:
and an optimal solution of the dual \(D(\mathrm{LP}_{\mathrm{KP}})\) model is:
Corollary 9.6
The optimal value of the primal \(\mathrm{LP}_{\mathrm{KP}}\) and dual \(D(\mathrm{LP}_{\mathrm{KP}})\) models is:
-
The proof proceeds in three steps:
-
We show that \(\eqref{PP}\) is a feasible solution for the primal \(\mathrm{LP}_{\mathrm{KP}}\) model;
-
We show that \(\eqref{DD}\) is a feasible solution for the dual \(D(\mathrm{LP}_{\mathrm{KP}})\) model;
-
We show that both feasible solutions have the same objective function value.
Therefore, by LP Strong Duality, \(\eqref{PP}\) and \(\eqref{DD}\) are a pair of optimal primal and dual solutions.
-
Proof
-
1. We have:
\[ {\sum_{j \in \mathscr{I}} w_j \, \tilde{x}_j} = \sum_{j=1}^{s-1} w_j + w_s \frac{\mathit{rc}}{w_s} = \sum_{j=1}^{s-1} w_j + c - \sum_{j=1}^{s-1} w_j = c \]thus, constraint \(\eqref{cons1_KP}\) is satisfied.
Since \(\mathit{rc} < w_s\), we have \(0 \le \tilde{x}_j \le 1\), for every \(j \in \mathscr{I}\). Accordingly, constraints \(\eqref{cons2_KP_LP_bis}\) and \(\eqref{cons2_KP_LP_tris}\) are satisfied.
Therefore, \(\eqref{PP}\) is a feasible solution for the primal \(\mathrm{LP}_{\mathrm{KP}}\) model.
□
Proof
-
2. For every \(j \in \{1,2,\dots, s-1\}\), we have:
\[ w_j \, \tilde{\pi} + \tilde{\mu}_j = w_j \; \frac{p_s}{w_s } + \bigg(p_j - w_j \; \frac{p_s}{w_s }\bigg) = p_j \]and, for every \(j \in \{s,s+1\dots, n\}\), we have:
\[ w_j \, \tilde{\pi} + \tilde{\mu}_j = w_j \; \frac{p_s}{w_s } \ge p_j \text{ since } \frac{p_s}{w_s } \ge \frac{p_j}{w_j} \]thus constraints \(\eqref{eq:KP-cap_LP_D}\) are satisfied.
We have \(\tilde{\pi} \ge 0\). For every \(j \in \{1,2,\dots, s - 1\}\), we have:
\[ p_j - w_j \; \frac{p_s}{w_s} \ge 0 \text{ since } \frac{p_j}{w_j} \ge \frac{p_s}{w_s } \]accordingly, \(\tilde{\mu}_j \ge 0\) for every \(j \in \mathscr{I}\). Thus constraint \(\eqref{eq:KP-cont1_LP_D}\) and constraints \(\eqref{eq:KP-cont2_LP_D}\) are satisfied.
Therefore, \(\eqref{DD}\) is a feasible solution for the dual \(D(\mathrm{LP}_{\mathrm{KP}})\) model.
-
3. For the objective functions we have:
\[\begin{align*} {\sum_{j \in \mathscr{I}} p_j \, \tilde{x}_j} & = \sum_{j=1}^{s-1} p_j + p_s \; \frac{\mathit{rc}}{w_s}\\[1.5ex] &= \sum_{j=1}^{s-1} p_j + \frac{p_s}{w_s} \; \left(c - \sum_{j=1}^{s-1} w_j \right)\\[1.5ex] &= c \; \frac{p_s}{w_s} + \sum_{j=1}^{s-1} \left( p_j - w_j \; \frac{p_s}{w_s} \right) \\[1.5ex] &=c \, \tilde{\pi} + \;\;{\sum_{j \in \mathscr{I}} \tilde{\mu}_j} \end{align*}\]thus the primal and dual objective function values are equal.
Therefore, by LP Strong Duality, \(\eqref{PP}\) and \(\eqref{DD}\) are a pair of optimal primal and dual solutions. □
In the majority of KP instances the ratio between \(z(\mathrm{LP}_{\mathrm{KP}})\) and \(z(\mathrm{ILP}_{\mathrm{KP}})\) slightly exceeds one: \(z(\mathrm{LP}_{\mathrm{KP}})\) typically is a tight upper bound for \(z(\mathrm{ILP}_{\mathrm{KP}})\).
-
We report an alternative proof for the formulas \(\eqref{PP}\). We proceed by contradiction, proving that a different solution cannot be optimal.
-
In this proof we assume that items with the same efficiency are merged into a single item. As we are allowed to choose any fraction of an item, this does not change the problem, and we avoid discussing symmetric solutions.
Proof
Assume \(\bar{\vet x}=(\bar{x}_1,\bar{x}_2,\dots,\bar{x}_n)\) is an optimal solution of \(\mathrm{LP}_{\mathrm{KP}}\) different from \(\tilde{\vet x}= (\tilde{x}_1,\tilde{x}_2,\dots,\tilde{x}_n)\) determined with formulas \(\eqref{PP}\). Every optimal solution of \(\mathrm{LP}_{\mathrm{KP}}\) has a total weight equal to \(c\), i.e., constraint \(\eqref{cons1_KP}\) is satisfied at equality (otherwise it can be improved).
Assume that no pair of items \(h<k\) exists with \(\bar{x}_h<1\) and \(\bar{x}_k>0\). Then \(\bar{\vet x}\) selects the items in order of efficiency, filling the knapsack item by item up to the weight \(c\): this is exactly \(\tilde{\vet x}\), a contradiction. Hence such a pair of items \(h<k\) exists (the case \(k=s\) is included).
Let:
Construct a new feasible solution \(\hat{\vet x} = (\hat{x}_1,\hat{x}_2,\dots, \hat{x}_n)\) which is identical to \(\bar{\vet x}\) except that:
By the definition of \(d\), we have \(\hat{x}_k \ge 0\) and \(\hat{x}_h \le 1\), and \(\hat{\vet x}\) is a feasible solution since:
The objective function value of \(\hat{\vet x}\) is:
Then the objective function value of \(\hat{\vet x}\) is larger than the objective function value of \(\bar{\vet x}\), contradicting the fact that \(\bar{\vet x}\) is an optimal solution.
The strict inequality \(p_h/w_h > p_k/w_k\) holds for every \(h<k\), since items with the same efficiency have been merged. □
Example 9.8: Primal and dual LP solutions
For the KP instance of Example 9.1, we have \(s=3\) and \(\mathit{rc} = 4\).
Using formulas \(\eqref{PP}\), we can compute the following optimal solution of the \(\mathrm{LP}_{\mathrm{KP}}\) model:
Using formulas \(\eqref{DD}\), we can compute the following optimal solution of the \(D(\mathrm{LP}_{\mathrm{KP}})\) model:
Using formula \(\eqref{VAL}\), the optimal value of both LP models, primal and dual, is:
9.4.3 Some important theoretical properties¶
- The following two propositions show that the ratio between \(z(\mathrm{LP}_{\mathrm{KP}})\) and \(z(\mathrm{ILP}_{\mathrm{KP}})\) is at most two for all KP instances. Furthermore, there exist instances for which the value two can be asymptotically achieved as the capacity \(c \to +\infty\).
Proposition 9.7
For every KP instance, we have:
Proof
We consider two feasible solutions of the \(\mathrm{ILP}_{\mathrm{KP}}\) model:
-
The solution selecting all items preceding the split item:
\[ \bar{x}_j = 1, \quad \forall j \in \{1,2,\dots, s-1\} \quad \text{and} \quad \bar{x}_j = 0, \quad \forall j \in \{s, s+1, \dots, n\} \]which is feasible by definition of the split item, and provides the valid lower bound:
\[ \sum_{j=1}^{s-1} p_j \le z(\mathrm{ILP}_{\mathrm{KP}}) \] -
The solution selecting only the split item:
\[ \bar{x}_j = 0, \quad \forall j \in \mathscr{I} \setminus \{s\} \quad \text{and} \quad \bar{x}_s = 1 \]which is feasible by assumption that \(w_s \le c\), and provides the valid lower bound:
\[ p_s \le z(\mathrm{ILP}_{\mathrm{KP}}) \]
Adding the two inequalities yields:
and thus:
□
Proposition 9.8
There are KP instances such that:
Proof
Given \(\vartheta \in \Z_{\ge 1}\), consider the KP instance with knapsack capacity \(c = 2\vartheta\) and \(n = 2\) items having the following profits and weights:
Since only one item can be selected, an optimal solution \({\tilde{\vet x}}(\mathrm{ILP}_{\mathrm{KP}})\) of the \(\mathrm{ILP}_{\mathrm{KP}}\) model is:
The split item is \(s = 2\), and the residual capacity is:
Accordingly, an optimal solution \({\tilde{\vet x}}(\mathrm{LP}_{\mathrm{KP}})\) of the \(\mathrm{LP}_{\mathrm{KP}}\) model is:
It follows that:
Accordingly, we conclude that:
□
9.5 0/1 Knapsack Problem upper bounds¶
- The following two propositions require that the items are sorted by non-increasing efficiency.
Proposition 9.9: Dantzig upper bound
For every KP instance, we have:
and \(\UB_{\mathrm{D}}\) is called the Dantzig upper bound for the KP.
Proof
We have:
which is a valid upper bound for \(z(\mathrm{ILP}_{\mathrm{KP}})\) since \(p_j \in \Z_{\ge 1}\), for every \(j \in \mathscr{I}\). □
- The Martello-Toth bound uses the items \(s-1\) and \(s+1\). Since \(w_1 \le c\) and \(\sum_{j} w_j > c\) we have \(s \ge 2\), so the item \(s-1\) always exists. If \(s = n\) the item \(s+1\) does not exist, and we set \(p_{n+1}/w_{n+1} := 0\): in this case \(\UB_0 = \sum_{j=1}^{s-1} p_j\).
Proposition 9.10: Martello-Toth upper bound
For every KP instance, we have:
where
and \(\UB_{\mathrm{MT}}\) is called the Martello-Toth upper bound for the KP.
Proof
Since \(x_s\) cannot take a fractional value, an optimal solution \({\tilde{\vet x}}(\mathrm{ILP}_{\mathrm{KP}})\) can be obtained from an optimal solution \({\tilde{\vet x}}(\mathrm{LP}_{\mathrm{KP}})\) either without the split item \(s\) or with the split item \(s\) and hence removing at least one of the first \(s-1\) items.
-
If the split item is out, we have \(z(\mathrm{ILP}_{\mathrm{KP}}) \le \UB_0\) since the residual capacity \(\mathit{rc}\) is filled with items having the largest possible efficiency \(\frac{p_{s+1}}{w_{s+1}}\)
-
If the split item is in, we have \(z(\mathrm{ILP}_{\mathrm{KP}}) \le \UB_1\) since it is supposed that the item to be removed has exactly the minimum necessary weight \(w_s- \mathit{rc}\) and the smallest possible efficiency \(\frac{p_{s-1}}{w_{s-1}}\) □
Example 9.9: Dantzig and Martello-Toth upper bound
For the KP instance of Example 9.1, using formula \(\eqref{DANTZIG}\), we can compute the Dantzig upper bound:
Using formula \(\eqref{boundMT}\), we can compute the Martello-Toth upper bound:
For this KP instance, the Martello-Toth upper bound is as strong as the Dantzig upper bound.