-
Notifications
You must be signed in to change notification settings - Fork 3
[Model] IntegerKnapsack #532
Description
Important
Build as optimization, not decision. Value = Max<i64>.
Objective: Maximize total value Σ v(u)·c(u) subject to Σ s(u)·c(u) ≤ B.
Do not add a bound/threshold field — let the solver find the optimum directly. See #765.
Motivation
INTEGER KNAPSACK (P216) from Garey & Johnson, A6 MP10. A classical NP-complete problem that generalizes the standard 0-1 Knapsack by allowing each item to be used with any non-negative integer multiplicity c(u), rather than just 0 or 1. This is also known as the "unbounded knapsack problem" in much of the literature. It remains NP-complete even in the special case where s(u) = v(u) for all items, which connects it directly to SUBSET SUM. Solvable in pseudo-polynomial time by dynamic programming, and polynomial time when |U| = 2 (Hirschberg and Wong, 1976).
Associated rules:
- R160: SUBSET SUM -> INTEGER KNAPSACK (establishes NP-completeness via Lueker 1975)
Reduction Rule Crossref:
- [Rule] SUBSET SUM to INTEGER KNAPSACK #521:
[Rule] SUBSET SUM to INTEGER KNAPSACK - [Rule] IntegerKnapsack to ILP #781:
[Rule] IntegerKnapsack to ILP(direct ILP formulation)
Definition
Name: IntegerKnapsack
Reference: Garey & Johnson, Computers and Intractability, A6 MP10
Mathematical definition (GJ decision form):
INSTANCE: Finite set U, for each u ∈ U a size s(u) ∈ Z⁺ and a value v(u) ∈ Z⁺, and positive integers B and K.
QUESTION: Is there an assignment of a non-negative integer c(u) to each u ∈ U such that Σᵤ∈U c(u)·s(u) ≤ B and such that Σᵤ∈U c(u)·v(u) ≥ K?
Optimization formulation (used in implementation):
Given a finite set U with sizes s(u) ∈ Z⁺ and values v(u) ∈ Z⁺ for each u ∈ U, and a capacity B ∈ Z⁺, find an assignment of non-negative integers c(u) to each u ∈ U that maximizes Σᵤ∈U c(u)·v(u) subject to Σᵤ∈U c(u)·s(u) ≤ B.
Variables
- Count: n = |U| (one variable per item in the set U)
- Per-variable domain: {0, 1, 2, ..., floor(B / s(u))} — non-negative integers, bounded above by floor(B / min_size) in the worst case. For the codebase representation, we use a discretized domain where each variable c(u) ranges from 0 to floor(B / s(u)).
- Meaning: c(u) is the non-negative integer multiplicity assigned to item u. The assignment is feasible if the total size Σ c(u)·s(u) ≤ B, and the objective is to maximize total value Σ c(u)·v(u).
Schema (data type)
Type name: IntegerKnapsack
Variants: none (operates on a generic item set with sizes and values)
| Field | Type | Description |
|---|---|---|
sizes |
Vec<i64> |
Size s(u) for each item u ∈ U |
values |
Vec<i64> |
Value v(u) for each item u ∈ U |
capacity |
i64 |
Knapsack capacity B (total size budget) |
Notes:
- Implemented as an optimization problem with
Value = Max<i64>, maximizing total value subject to capacity. Follows the same pattern as the existingKnapsackmodel. - Key difference from the existing
Knapsack(0-1 Knapsack): each item can be used with integer multiplicity c(u) ≥ 0, not just 0 or 1. The configuration space per variable is larger: dims() returns[floor(B/s(0))+1, floor(B/s(1))+1, ...]instead of[2, 2, ..., 2]. - Key getter methods needed:
num_items()(= |U|),capacity()(= B).
Complexity
- Decision complexity: Weakly NP-complete (Lueker, 1975; transformation from SUBSET SUM). Remains NP-complete even when s(u) = v(u) for all u. Not strongly NP-complete — solvable in pseudo-polynomial time.
- Best known exact algorithm: Dynamic programming in O(n · B) time and O(B) space, where n = |U| and B is the capacity. This is pseudo-polynomial time. The DP recurrence is: dp[w] = max over all u of (dp[w - s(u)] + v(u)) for w = 1, ..., B, with dp[0] = 0. Each item can be used multiple times naturally in this formulation.
declare_variants!complexity string:"(capacity + 1)^num_items"— worst-case brute-force bound where each item can be used 0 to capacity times.- Special case |U| = 2: Solvable in polynomial time (Hirschberg and Wong, 1976) via a number-theoretic algorithm exploiting the structure of linear Diophantine equations.
- Approximation: Admits an FPTAS. For any ε > 0, a (1-ε)-approximation can be computed in O(n / ε) time using LP relaxation and rounding techniques.
- References:
- G. S. Lueker (1975). "Two NP-complete problems in nonnegative integer programming." Computer Science Laboratory, Princeton University. Original NP-completeness proof.
- D. S. Hirschberg and C. K. Wong (1976). "A polynomial-time algorithm for the knapsack problem with two variables." JACM 23, pp. 147–154.
Extra Remark
Full book text:
INSTANCE: Finite set U, for each u ∈ U a size s(u) ∈ Z⁺ and a value v(u) ∈ Z⁺, and positive integers B and K.
QUESTION: Is there an assignment of a non-negative integer c(u) to each u ∈ U such that Σᵤ∈U c(u)·s(u) ≤ B and such that Σᵤ∈U c(u)·v(u) ≥ K?
Reference: [Lueker, 1975]. Transformation from SUBSET SUM.
Comment: Remains NP-complete if s(u) = v(u) for all u ∈ U. Solvable in pseudo-polynomial time by dynamic programming. Solvable in polynomial time if |U| = 2 [Hirschberg and Wong, 1976].
How to solve
- It can be solved by (existing) bruteforce. (Enumerate all multiplicity vectors (c(u_1), ..., c(u_n)) with 0 ≤ c(u_i) ≤ floor(B/s(u_i)); check feasibility and compute total value.)
- It can be solved by reducing to integer programming. (ILP with integer variables c_i ≥ 0 for each item; constraint Σ c_i · s_i ≤ B; objective maximize Σ c_i · v_i. See companion issue.)
- Other: Dynamic programming in O(n·B) pseudo-polynomial time.
Example Instance
Input:
U = {u₁, u₂, u₃, u₄, u₅} (n = 5 items)
Sizes: s(u₁) = 3, s(u₂) = 4, s(u₃) = 5, s(u₄) = 2, s(u₅) = 7
Values: v(u₁) = 4, v(u₂) = 5, v(u₃) = 7, v(u₄) = 3, v(u₅) = 9
Capacity B = 15
Optimal solution: c(u₁) = 0, c(u₂) = 0, c(u₃) = 1, c(u₄) = 5, c(u₅) = 0
- Total size: 0·3 + 0·4 + 1·5 + 5·2 + 0·7 = 15 ≤ 15 ✓
- Total value: 0·4 + 0·5 + 1·7 + 5·3 + 0·9 = 22 (optimal)
Alternative optimal: c(u₁) = 1, c(u₂) = 0, c(u₃) = 0, c(u₄) = 6, c(u₅) = 0
- Total size: 1·3 + 0·4 + 0·5 + 6·2 + 0·7 = 15 ≤ 15 ✓
- Total value: 1·4 + 0·5 + 0·7 + 6·3 + 0·9 = 22 (optimal)
Suboptimal feasible solutions (examples):
- c = (0, 0, 3, 0, 0): size = 15, value = 21
- c = (0, 0, 0, 4, 1): size = 15, value = 21
- c = (5, 0, 0, 0, 0): size = 15, value = 20
- c = (0, 0, 0, 7, 0): size = 14, value = 21
Note how the same item u₃ can be used 3 times — this is impossible in 0-1 Knapsack. The instance has 103 feasible solutions across 21 distinct objective values (verified by brute-force enumeration), making it suitable for round-trip testing.
Negative instance:
Same items but B = 5. Best achievable: c(u₃) = 1 gives value 7. No combination with total size ≤ 5 can achieve value ≥ 20.
References
- [Lueker, 1975]: G. S. Lueker (1975). "Two NP-complete problems in nonnegative integer programming." Computer Science Laboratory, Princeton University. Original NP-completeness proof.
- [Hirschberg and Wong, 1976]: D. S. Hirschberg and C. K. Wong (1976). "A polynomial-time algorithm for the knapsack problem with two variables." JACM 23, pp. 147–154.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status