en.wikipedia.org/wiki/Knapsack_problem
3 corrections found
Besides, here we assume that x i > 0
This is incorrect for the unbounded knapsack problem: item counts may be zero, not strictly positive. Requiring x_i > 0 would force every item type to be chosen at least once.
Full reasoning
In the standard unbounded knapsack problem, each decision variable counts how many copies of an item type are selected, and that count is allowed to be any non-negative integer. In other words, an item type may be omitted entirely, so (x_i=0) must be allowed.
Saying "(x_i > 0)" changes the problem into a different one that requires at least one copy of every item type in every feasible solution. That is not the usual UKP definition and would wrongly exclude many valid solutions.
Credible references state the UKP with non-negative counts:
- Fabio Furini's combinatorial optimization lecture notes define a feasible solution as one where "(x_j\in \mathbb{Z}_{\ge 0})" and explicitly say that "each variable (x_j) can take any non-negative integer value".
- Jansen and Kraft's paper on the unbounded knapsack problem states that "an arbitrary number of copies of every item can be taken in UKP," which includes taking zero copies of some item types.
So the post's statement "Besides, here we assume that x i > 0" is false as written; it should say that (x_i) is a non-negative integer (equivalently, (x_i \ge 0) and integral).
2 sources
- DP for the unbounded knapsack problem - Combinatorial Optimization · Fabio Furini
A feasible solution is described by the integer vector (x1,x2,…,xn), where xj∈Z≥0 is the number of copies of item type j selected... In the UKP, each variable xj can take any non-negative integer value, provided that the capacity constraint is respected.
- A Faster FPTAS for the Unbounded Knapsack Problem
In contrast to 0-1 KP, an arbitrary number of copies of every item can be taken in UKP.
H = − ∑ i = 1 n v i x i + P ( ∑ i = 1 n w i x i − W ) 2 ,
This Hamiltonian does not correctly encode the knapsack constraint ≤ W in general. The squared term penalizes being under capacity as well as over capacity, so it enforces equality to W rather than the original inequality.
Full reasoning
For the knapsack problem, the constraint is
[
\sum_i w_i x_i \le W,
]
not equality. The formula in the post uses the penalty
[
P\left(\sum_i w_i x_i - W\right)^2,
]
which penalizes both overweight and underweight solutions. That means it is really pushing the solution toward exactly filling the knapsack, which is not equivalent to the original problem when the optimal feasible solution leaves unused capacity.
Standard QUBO/QAOA formulations handle this by introducing slack variables first to convert the inequality into an equality, and only then squaring the equality constraint. Both OpenQAOA and Qiskit document this explicitly.
A concrete counterexample shows the post's Hamiltonian is wrong as a general encoding. Take capacity (W=10) and three candidate single-item choices:
- item A: weight 9, value 100 → this is the true knapsack optimum;
- item B: weight 10, value 99;
- item C: weight 11, value 102 → infeasible.
Under the post's Hamiltonian, their energies are:
- (E_A = -100 + P)
- (E_B = -99)
- (E_C = -102 + P)
For every penalty value (P), (E_C < E_A), so the Hamiltonian prefers the infeasible 11-weight item over the true optimum. Therefore this formula does not correctly represent the knapsack problem in general.
The standard fix is to rewrite the inequality with slack variables, e.g. (\sum_i w_i x_i + \sum_k 2^k y_k = W), and then penalize that equality.
2 sources
- Knapsack - OpenQAOA
In order to turn such a formulation into a QUBO one, we must first make the inequality constraint an equality. This is done by introducing extra variable (sometimes called "slack variables") ... so that one can rewrite the inequality constraint as ∑ w_i x_i + ∑ 2^i y_i = W.
- Converters for Quadratic Programs - Qiskit Optimization 0.7.0
InequalityToEquality converts inequality constraints into equality constraints with additional slack variables... As we can see, the inequality constraints are replaced with equality constraints with additional integer slack variables.
subject to ∑ i = 1 n w i x i ≤ w ′ and x i > 0
This constraint is wrong for unbounded knapsack. The item-count variables should be allowed to be 0; requiring x_i > 0 forces every item type to appear at least once.
Full reasoning
In the unbounded knapsack problem, each variable (x_i) counts how many copies of item type (i) are selected, and the standard requirement is that (x_i) be a non-negative integer. That allows valid solutions to omit item types entirely by setting (x_i=0).
Replacing that with (x_i>0) changes the feasible set substantially: it requires at least one copy of every item type, which is not the usual knapsack problem and excludes many legitimate solutions.
Authoritative descriptions of UKP use non-negative counts, not strictly positive counts. One source states that a feasible solution has (x_j\in \mathbb{Z}_{\ge 0}) and that each variable can take any non-negative integer value. Another describes UKP as allowing an arbitrary number of copies of every item, which includes zero copies for some item types.
2 sources
- DP for the unbounded knapsack problem - Combinatorial Optimization · Fabio Furini
A feasible solution is described by the integer vector (x1,x2,…,xn), where xj∈Z≥0 is the number of copies of item type j selected... In the UKP, each variable xj can take any non-negative integer value, provided that the capacity constraint is respected.
- A Faster FPTAS for the Unbounded Knapsack Problem
In contrast to 0-1 KP, an arbitrary number of copies of every item can be taken in UKP.