π§© Constraint Solving POTD:Knapsack Problem β Packing & Cutting #24733
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 #24883. |
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
You have a knapsack with a weight capacity
Wand a set ofnitems. Each itemihas a weightw_iand a profitp_i. The goal is to select a subset of items to maximise total profit without exceeding the capacity.Small concrete instance (
n = 6,W = 15):Optimal selection: items {1, 3, 4, 5} β total weight 14 β€ 15, total profit 25.
Input:
nitems with weightsw[]and profitsp[], capacityW.Output: a 0/1 selection vector
x[]maximisingβ p_i Β· x_isubject toβ w_i Β· x_i β€ W.Why It Matters
Modeling Approaches
Approach 1 β Constraint Programming (CP)
Decision variables:
x_i β {0, 1}for each itemi.Model:
Runs in
O(nW)(pseudo-polynomial). Yields exact optimal and reconstructs the solution by backtracking.Trade-offs: Optimal and simple; pseudo-polynomial complexity (poor when
Wis huge or non-integer); doesn't generalise easily to side constraints.Example Model β MiniZinc (CP with knapsack global)
Without the
knapsackglobal, the equivalent manual model is:Key Techniques
1. LP Relaxation Bound (Upper Bounding)
The fractional knapsack relaxation (greedy by profit density) provides a tight upper bound. In branch-and-bound, this bound prunes branches where even the best fractional completion cannot beat the current incumbent. This is why MIP solvers are so effective here.
2. Knapsack Global Constraint (Propagation)
The
knapsackglobal constraint propagates both ways: it infers which items must be included (their combined profit is needed to reach a lower bound) and which cannot be included (their weight would bust the capacity even without other mandatory items). This dual inference is far stronger than applying a weight-sum constraint and a profit-sum constraint separately.3. Symmetry Breaking & Dominance
If two items have
w_i β€ w_jandp_i β₯ p_j, itemjis dominated and can be pruned before solving. Similarly, cover inequalities β cuts that say "you can't take all items in a minimal infeasible set" β are used by MIP solvers to dramatically tighten the LP relaxation.Challenge Corner
References
knapsack_solverand CP-SAT APIs side by side.Beta Was this translation helpful? Give feedback.
All reactions