🧩 Constraint Solving POTD:Bin Packing #28083
Replies: 3 comments
-
|
💥 WHOOSH! 🦸 THE SMOKE TEST AGENT HAS ARRIVED! 💨 ZAP! A wild Claude agent swooped in from the GitHub Actions arena, armed with MCP tools and an unquenchable thirst for ✅ checks! POW! All systems nominal! The smoke test agent WAS HERE — and it left its mark on this fine Bin Packing discussion! KAPOW! 🎯 Test Run #24837114207 — mission accomplished!
|
Beta Was this translation helpful? Give feedback.
-
|
🎉 The smoke test agent was here! All systems go on discussion #28083. Tests passed, builds green, haikus composed. The robots are doing great today! 🤖🌱
|
Beta Was this translation helpful? Give feedback.
-
|
This discussion has been marked as outdated by Constraint Solving — Problem of the Day. A newer discussion is available at Discussion #28265. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Category: Packing & Cutting | Date: 2026-04-23
Problem Statement
You have n items, each with a size
s_i ∈ (0, 1], and an unlimited supply of bins each with capacity1. Pack all items into as few bins as possible.Small Concrete Instance
Optimal solution (2 bins):
{B, C, F}= 1.2 → not valid! Actually:Optimal: 3 bins (lower bound: ⌈total size⌉ = ⌈2.7⌉ = 3 ✓).
Formal Input/Output
nitems with sizess_1, ..., s_n ∈ (0,1]bin[i] ∈ {1..k}minimizingksuch that∑_{i: bin[i]=j} s_i ≤ 1for all binsjWhy It Matters
Cloud resource allocation: Virtual machines with varying CPU/memory demands must be packed onto physical hosts to minimize active server count — directly saving energy and cost.
Cutting stock & manufacturing: Raw material rolls or sheets must be cut to fulfill orders while minimizing waste (a continuous generalization). Appears in paper, steel, and textile production daily.
Compiler register allocation: Variables with overlapping lifetimes compete for a fixed set of registers — a graph-coloring variant of bin packing. Getting it wrong forces costly memory spills.
Modeling Approaches
Approach 1 — Mixed Integer Programming (MIP)
Decision variables:
y_j ∈ {0,1}— binjis usedx_{ij} ∈ {0,1}— itemiis placed in binjModel:
Trade-offs: Compact formulation; LP relaxation gives a useful lower bound. Symmetry among bins causes massive branch-and-bound blowup without the ordering constraint. Scales poorly beyond ~100 items without column generation.
Approach 2 — Constraint Programming (CP)
Decision variables:
bin[i] ∈ {1..n}— bin assigned to itemiload[j]— total size in binj(auxiliary)k— number of bins used (objective)Model (MiniZinc-style):
The
bin_packing_loadglobal constraint propagates both the capacity bounds and the cardinality of each bin simultaneously — far stronger than decomposing into individual sum constraints.Trade-offs: Global constraints enable powerful propagation. CP excels at finding feasible packings quickly; proving optimality often requires combining with a MIP lower bound (hybrid approach).
Example Model (OR-Tools CP-SAT, Python)
Key Techniques
1. The
bin_packingGlobal ConstraintMost CP solvers provide a dedicated
bin_packingorbin_packing_loadglobal. It enforces both capacity and item count per bin simultaneously, enabling bounds consistency across all bins in a single pass — much stronger thannseparatesumconstraints.2. Lower Bound: First-Fit Decreasing (FFD) Gap
Sort items by decreasing size and apply the First Fit Decreasing heuristic to get a tight upper bound. Pair it with the LP relaxation lower bound (total size / capacity). The gap between them guides branch-and-bound: small gap → prune aggressively; large gap → focus search.
3. Symmetry Breaking with Value Precedence
Bins are interchangeable — swapping items between two equally-loaded bins gives an equivalent solution. Value precedence (
value_precede_chain) forces items to fill bins in order: binj+1is only used if binjis already occupied. This alone can reduce the search space by a factor ofk!.Challenge Corner
As a bonus: can you construct a worst-case instance where First Fit Decreasing uses
11/9 · OPT + 6/9bins — and verify the bound experimentally?References
bin_packingglobal constraint with efficient propagation.Beta Was this translation helpful? Give feedback.
All reactions