🧩 Constraint Solving POTD:Problem of the Day: The 0/1 Knapsack Problem #38355
Closed
Replies: 1 comment
-
|
This discussion has been marked as outdated by Constraint Solving — Problem of the Day. A newer discussion is available at Discussion #38606. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Problem Statement
The 0/1 Knapsack Problem is one of the most iconic optimization problems in computer science and operations research. It asks: given a set of items, each with a weight and a value, select a subset of items that maximizes total value without exceeding a weight capacity.
Concrete Instance:
Imagine you're a thief with a knapsack of capacity 15 kg. You have access to these items:
Question: Which items should you take to maximize value while staying within 15 kg?
Input/Output:
Why It Matters
Financial Portfolio Optimization: Investors select assets (stocks, bonds, projects) to maximize expected return within a budget or risk constraint. Each asset has an expected value and a capital requirement.
Manufacturing & Cutting: Factories must select production orders or material cutting patterns that maximize profit while respecting machine capacity or raw material limits.
Cloud Resource Provisioning: Allocating VMs or containers to meet service demand while minimizing cost—each service has value (revenue) and cost (compute/memory footprint).
Inventory Management: Retailers select which products to stock given shelf space constraints and varying profit margins.
Modeling Approaches
Approach 1: Integer Linear Programming (ILP)
Paradigm: Mathematical Programming
Decision variables:
x_i ∈ {0, 1}for each item i (1 = take, 0 = leave)Model:
Trade-offs:
Approach 2: Constraint Programming (CP)
Paradigm: Declarative Logic with Propagation
Decision variables:
x_i ∈ {0, 1}for each itemConstraints:
Trade-offs:
Approach 3: Dynamic Programming
Paradigm: Tabulation / Memoization
Recurrence:
Trade-offs:
Example: MiniZinc Model
Run with:
minizinc knapsack.mznKey Techniques
1. Dynamic Programming with Space Optimization
For instances where capacity is moderate (≤ 10^6), DP is the go-to approach. The 1D recurrence can be optimized to O(C) space by processing items in reverse order, avoiding a 2D table.
2. Branch & Bound with LP Relaxation
Relax 0/1 variables to continuous [0, 1]. The resulting linear program is solved in O(n log n) via a greedy algorithm: sort by value-to-weight ratio, fill greedily. This LP bound prunes many branches during search.
3. Symmetry Breaking & Preprocessing
Challenge Corner
Question 1: The standard 0/1 knapsack has one constraint. How would you model multi-dimensional knapsack (e.g., weight AND volume limits)? Would DP still be efficient?
Question 2: Real thieves care about uncertainty—item weights and values aren't exactly known. How would you reformulate this as a robust optimization problem? What constraints would encode "worst-case" scenarios?
Question 3: In a CP solver, global constraints like
knapsack()sometimes achieve better propagation than decomposing into arithmetic constraints. Can you think of a domain reduction that theknapsack()constraint can perform that a simplesum()constraint cannot?References
Martello, S., & Toth, P. (1990). Knapsack Problems: Algorithms and Computer Implementations. Wiley. — The definitive reference; covers DP, branch & bound, and approximation.
Pisinger, D. (2005). "Where are the hard knapsack problems?" Computers & Operations Research, 32(9), 2271–2284. — Explores phase transitions and problem difficulty.
SICStus Prolog Knapsack Constraint. (sicstus.sics.se/redacted) — Example of a native global constraint in a CP system.
Hooker, J. N. (2012). Integrated Methods for Optimization. Springer. — Chapter on hybrid CP/integer programming approaches to knapsack variants.
Did this problem intrigue you? Next time, we'll explore a problem that combines scheduling and routing—the Pickup & Delivery Problem. See you then! 🎯
Beta Was this translation helpful? Give feedback.
All reactions