francke.tech
All projects

Case study

Bin Packing

First Fit, FFD, Full Bin, Tabu Search, Simulated Annealing and a Genetic Algorithm, measured on Falkenauer instances. A fast greedy rule wins on uniform data, the GA wins on adversarial data, and benchmarking exposed bugs in my first versions of the metaheuristics.

  • Python
  • NumPy
  • Matplotlib
  • pytest
  • GitHub Actions
Bar chart of the gap to the lower bound for six bin packing methods on u120 and t60 instances.
Gap to the lower bound L1 per method (mean over 10 instances; stochastic methods over 5 seeds).

Problem

Given items of different sizes and bins of fixed capacity, pack everything into as few bins as possible. The problem is NP-hard and shows up in load planning, cloud resource allocation and cutting stock. The question here was practical: when is expensive search worth it compared with a simple greedy rule? Answering it needs a fixed yardstick, so every result is reported as the gap to the lower bound L1 = ceil(sum of sizes / capacity).

What I built

  • Six algorithms behind one solver entry point: First Fit, First Fit Decreasing (FFD), Full Bin, Tabu Search, Simulated Annealing and a Genetic Algorithm that searches over item orderings decoded with First Fit.
  • A benchmark on 10 OR-Library instances (5 Falkenauer uniform u120, 5 Falkenauer triplet t60), 5 seeds for the stochastic methods, with the sources documented and the results regenerated by one script.
  • Six executed topic notebooks (problem and bound, greedy heuristics, Tabu, SA, GA, benchmark comparison), a CLI, a pytest suite checking that every method returns a valid packing, and CI. MIT licensed.

Engineering decisions

  • Measure against a lower bound, not against each other. Gap to L1 is the same yardstick for every method and instance, and for the triplet instances L1 is the true optimum (20 bins).
  • The benchmark exposed bugs in my first versions. Tabu Search and Simulated Annealing searched from a fixed first-fit solution instead of the intended start, and the GA encoding could drop items from a packing. I fixed them, added tests for validity, and re-ran everything; the earlier toy results are gone.
  • The objective (bins + 0.1 x free space) is affine in the bin count, so single-item moves see a flat landscape and only register progress when a bin empties. This explains why Tabu and SA gain little, and it is documented rather than hidden.
  • The GA works on item orders for First Fit, so it keeps FF's packing logic and only has to find a good order. That is exactly what the triplet instances punish FFD for.

Results

  • Uniform u120: FFD and Full Bin are within 1.3% of the lower bound in 0 ms and no metaheuristic beats them (GA 3.0%, Tabu and SA 5.4%, First Fit 7.0%).
  • Triplet t60: the Genetic Algorithm reaches a 7.4% gap versus 16.0% for FFD, Simulated Annealing 9.6%, Tabu Search 13.4% and First Fit 14.0%.
  • Takeaway: sorting big-first is often within one bin of optimal at no cost, so search is worth deploying only on structured hard cases, and you only know which case you are in if you measure against a bound.

Limitations & next

  • The objective gives local search no gradient; a fill-based fitness such as Falkenauer's sum((fill/C)^2)/bins would.
  • Neighbourhoods deep-copy whole packings (fine up to about 200 items), and Tabu and SA use single-item moves only: no swaps, no restarts.
  • Ten small instances and 5 seeds show the ranking, not tight statistics; there is no exact solver beyond L1.