A Genetic Algorithm for Bin Packing

My CSC 742 assignment on a genetic algorithm that assigns items to bins, rerun and extended. What the submitted GA found and why it stopped at about four times the optimum, the lower bound and First Fit Decreasing that put those numbers in context, and a permutation encoding with a first-fit decoder that reaches the optimum on the same instances, with first fit traced on a small example and a crossover that breaks two optimal parents.

The encoding my CSC 742 assignment specified, a bin label per item, keeps a genetic algorithm far from the optimum. On four instances of 10 to 100 items (0 to 2 kg each, 10 kg boxes; population 20, 50 generations, 30 seeds), it used a median of 2, 8, 17.5 and 42 bins against optima of 2, 3, 6 and 11, fixed by the bound $L_1 = \lceil \sum_i w_i / C \rceil$ and First Fit Decreasing. Labels carry no meaning, so crossover cannot pass on a bin, and the bin count rewards no partial progress. A permutation encoding with a first-fit decoder reached the optimum in every run, as did the best of 1,000 random orders. On harder triplet instances it reached a median of 21 and 42 bins against optima of 20 and 40.

The third assignment in CSC 742 set a bin packing problem in the language of an online shop. Orders weighing between 0 and 2 kg each are to be packed into boxes that hold at most 10 kg, using as few boxes as possible. In the submitted baseline run, the genetic algorithm's best packing of 100 items used 42 boxes, and the report observed that its packings used "nearly 4x more bins than necessary". The operators were standard and the constraint simple. The gap came from the encoding and the objective that the assignment specified, not from the instances, as the sections below show.

The assignment fixed most of the design:

  • An individual is a list of integers, one per item, giving the bin that item goes into, with bin labels from 0 to $N - 1$ for $N$ items.
  • Two quantities are computed for each individual: $f$, the number of distinct bins used, and $g$, the number of bins whose load exceeds the capacity.
  • Parents are chosen by binary tournaments with feasibility-first rules.
  • One- or two-point crossover follows, and a mutation that reassigns a gene to a random bin.
  • The GA was to run with a population of 20 for 50 generations on 10, 25, 50 and 100 items, with variations in population size and run length.

This article reports what I submitted, explains why it stopped far from the optimum, and compares it with a classical heuristic, a lower bound, and a different encoding. The route has five steps:

  1. The submitted algorithm and its results.
  2. A lower bound and First Fit Decreasing, which show how far from optimal those results were, traced on a small example.
  3. Thirty seeds on fixed instances, to separate the parameters from the luck of the draw.
  4. The reasons the assignment encoding struggles, each shown on the small example or a stalled run.
  5. A permutation encoding with a first-fit decoder, and harder instances that tell methods apart.

The feasibility-first tournament rules are Deb's, described in constraint handling in evolutionary algorithms, and the operators are those of crossover and mutation operators. The experiment script is tools/experiments/evolution/bin_packing.py in the site repository; the figures and the small worked examples come from tools/figures/genetic-algorithm-bin-packing.py.

The submitted algorithm

The program (bpp_ga.py) followed the specification closely. Initial genes were uniform on $0, \dots, N - 1$. The tournament compared two random individuals by three rules:

  • if both are feasible ($g = 0$), the one with fewer bins wins;
  • if only one is feasible, it wins;
  • if neither is, the one with fewer overfull bins wins.

These are the tournament rules Deb (2000) proposed for constrained optimization, with the number of violated bins standing in for his total amount of violation. One tournament per member of the population filled a mating pool, from which pairs were drawn at random. Two-point crossover was applied with probability 0.9, and each gene was reset to a uniformly random bin label with probability $1/N$. Offspring replaced the population, with no elitism, and the best individual ever evaluated was reported.

The program seeded Python's random number generator once per pass (seeds 42, 773 and 2025). It then generated a fresh set of item weights for every combination of configuration and size, drawing from the same stream. Rerunning the same call sequence with seed 42 reproduces every number the report gave:

Configuration 10 items 25 items 50 items 100 items
Baseline (pop 20, 50 generations) 3 7 18 42
Small population (pop 10) 2 8 20 43
Large population (pop 40) 2 7 18 37
Short run (25 generations) 3 9 22 52
Long run (100 generations) 2 8 17 37

The report observed that every run had feasible individuals from generation 0, and concluded that the constraint rules, though implemented, were never needed. The rerun confirms that they had almost nothing to do: of the 24,000 individuals evaluated at seed 42, 4 had an overfull bin. The report drew further conclusions:

  • larger populations and longer runs both helped, most of all on 100 items;
  • the search settled in local optima made of many partly filled bins.

It noted that the best packings used "nearly 4x more bins than necessary", with most bins holding only 2 to 4 kg. It traced the gap to operators that make incremental improvements but "struggle to discover the coordinated item rearrangements needed for dense packing". The sections below take that observation further.

How far from optimal

Measuring how far a packing is from the best requires the best, or at least bounds on it. The simplest lower bound on the number of bins is the total weight divided by the capacity, rounded up (Martello and Toth, 1990, §8.3.1):

$$L_1 = \left\lceil \frac{\sum_{i=1}^{N} w_i}{C} \right\rceil,$$

where $w_i$ is the weight of item $i$ and $C$ the capacity. An upper bound comes from First Fit Decreasing (FFD). Sort the items from heaviest to lightest. Put each into the first bin, in order of opening, that still has room, and open a new bin when none does. Johnson (1973) proved that FFD never uses more than $\frac{11}{9}\mathrm{OPT} + 4$ bins, where OPT is the optimum. Dósa (2007) lowered the additive constant to the best possible value:

$$\mathrm{FFD}(I) \le \tfrac{11}{9}\,\mathrm{OPT}(I) + \tfrac{6}{9}.$$

A small instance shows how the bound and first fit work, and why the order of the items matters. Take eight items A to H weighing 6, 5, 5, 4, 4, 3, 2 and 1 kg, and boxes of 10 kg. The total is 30 kg, so $L_1 = \lceil 30 / 10 \rceil = 3$. No packing can use fewer than three bins, and a three-bin packing must fill every bin exactly. First fit in decreasing order places the items like this:

Item Weight Placed in Bin loads afterwards
A 6 bin 1 (new) 6
B 5 bin 2 (new; does not fit in bin 1) 6, 5
C 5 bin 2 6, 10
D 4 bin 1 10, 10
E 4 bin 3 (new) 10, 10, 4
F 3 bin 3 10, 10, 7
G 2 bin 3 10, 10, 9
H 1 bin 3 10, 10, 10

Three bins, equal to $L_1$, so this packing is optimal.

Example (first fit in increasing order). The same first-fit rule applied to the eight items in increasing order, H first, uses four bins. Bin 1 takes H, G, F and E (1 + 2 + 3 + 4 = 10), and D and C go into bin 2 (9 kg). Then there is no room for B or A anywhere: B opens bin 3, and A, at 6 kg, does not fit with B and opens bin 4.

First fit on the eight-item example in two orders. Notice that taking the items from heaviest to lightest fills three bins exactly, which matches the lower bound and is optimal. Taking them from lightest to heaviest packs the small items together early, leaving the 5 and 6 kg items to open bins of their own: four bins.
First fit on the eight-item example in two orders. Notice that taking the items from heaviest to lightest fills three bins exactly, which matches the lower bound and is optimal. Taking them from lightest to heaviest packs the small items together early, leaving the 5 and 6 kg items to open bins of their own: four bins.

Large items placed first leave the small ones to fill the gaps. Small items placed first use up the space that the large ones needed. That is the reasoning behind sorting before packing. It is also why an encoding that chooses the order, as the one later in this article does, can reach good packings with a simple decoder.

Back to the submission's instances. When FFD and $L_1$ agree, both equal the optimum. On the twenty instances of the submission, they agree on nineteen. For the baseline instances, the optimum is 2, 3, 6 and 11 bins, against the GA's 3, 7, 18 and 42. The report estimated the optimum for 100 items at "approximately 10"; the bound and FFD together fix it at 11. The ratio to the optimum grows with the problem: 1.5, 2.3, 3.0 and 3.8.

The single exception is the short run's 100-item instance. Its weights total 99.98 kg, so that $L_1 = 10$ while FFD uses 11. Ten bins would have only 0.024 kg of free space among them, and neither bound settles whether such a packing exists.

The same check shows something that a table of single runs cannot. Because every configuration drew its own weights, each row of the table above was run on different instances. The large-population and long-run configurations both reached 37 bins on 100 items, but against optima of 10 and 12. Differences between rows mix the effect of the parameters with the luck of the instance, and with one seed per row the two cannot be separated.

Thirty seeds on fixed instances

To separate them, the script fixes the four baseline instances and reruns the submitted GA with seeds 0 to 29. The budget is the same, 20 individuals for 50 generations, 1,000 evaluations in all:

Items Total weight $L_1$ = FFD = OPT Assignment GA, median [IQR] Range
10 10.47 kg 2 2 [2, 3] 2 to 3
25 27.49 kg 3 8 [7, 8] 6 to 9
50 55.10 kg 6 17.5 [17, 19] 15 to 20
100 101.24 kg 11 42 [41, 43] 38 to 47

The baseline's single results (3, 7, 18, 42) are typical of the distribution. More computation helps, slowly.

Best bin count found so far per generation, median over 30 runs with the band from the first to the third quartile. Left, the 100-item baseline instance: notice that the assignment GA starts at a median of 58 bins and is still falling steadily at 42 when the 50 generations end, while the permutation GA described below is at the optimum of 11 from its first generation. Right, the harder 120-item triplet instance discussed further down: the permutation GA starts below First Fit Decreasing's 47 bins, improves to 42, and never reaches the optimum of 40.
Best bin count found so far per generation, median over 30 runs with the band from the first to the third quartile. Left, the 100-item baseline instance: notice that the assignment GA starts at a median of 58 bins and is still falling steadily at 42 when the 50 generations end, while the permutation GA described below is at the optimum of 11 from its first generation. Right, the harder 120-item triplet instance discussed further down: the permutation GA starts below First Fit Decreasing's 47 bins, improves to 42, and never reaches the optimum of 40.

The left panel shows the shape of the assignment GA's progress on 100 items. The medians are 58 bins after the first generation, 53 after 10, 48 after 25 and 42 after 50. That is about one bin every three generations, with no sign of leveling off. The runs were stopped, not converged. On the 100-item instance the median falls to 33 bins with a population of 40 for 100 generations. With a population of 100 for 500 generations, fifty times the original budget, it falls to 20 bins, still nearly twice the optimum.

Why the assignment encoding struggles

More computation helps slowly because of the encoding and the objective, whose effects this section takes one at a time. What a stalled run looks like is clearest on the 25-item instance. The run with seed 0 ended with 7 bins, against an optimum of 3:

Two packings of the same 25 items into boxes of 10 kg, with each item drawn as a block of its weight; blue blocks weigh at least 1 kg, green ones less. Left, the best packing found by the assignment GA with seed 0: notice that only one of its seven bins is more than half full, and two hold a single small item. Right, First Fit Decreasing: three bins, two of them essentially full.
Two packings of the same 25 items into boxes of 10 kg, with each item drawn as a block of its weight; blue blocks weigh at least 1 kg, green ones less. Left, the best packing found by the assignment GA with seed 0: notice that only one of its seven bins is more than half full, and two hold a single small item. Right, First Fit Decreasing: three bins, two of them essentially full.

The GA's bins hold 8.10, 4.98, 4.95, 4.81, 3.81, 0.50 and 0.35 kg. The last two each contain one light item, and emptying either would save a bin. But nothing in the search rewards moving a 0.35 kg item into a bin that has room for it until the move is made. A random reset of that one gene is also far more likely to open a new bin than to pick one of the six labels in use. The paragraphs below take these effects one at a time.

The encoding is highly redundant. Bin labels carry no meaning, so any packing can be written under many relabelings. A packing of 100 items into 11 bins, with labels drawn from 0 to 99, has $100 \times 99 \times \dots \times 90 \approx 5.7 \times 10^{21}$ encodings. Two parents that contain the same good bin under different labels cannot pass it on together. Two-point crossover copies a stretch of labels from one parent into the other, and the copied labels mean different bins in the receiving parent.

What crossover transmits is "item 17 is in bin 4", a fact whose meaning depends on where the rest of bin 4 sits in the same chromosome. It does not transmit "items 17, 23 and 61 belong together", which is the information a good packing contains. This is the motivation for representations in which the bins themselves are the units of inheritance.

The eight-item example makes this concrete. Both parents below describe the same optimal packing, {A, D}, {B, C} and {E, F, G, H}, each bin holding exactly 10 kg, under different labels. Gene $i$ is the label of item $i$'s bin, in the order A to H.

A B C D E F G H
Parent 1 0 1 1 0 2 2 2 2
Parent 2 5 0 0 5 3 3 3 3

Example (crossing two encodings of one optimal packing). Two-point crossover with cuts after positions 2 and 5 swaps the genes of C, D and E. Neither child is the optimal packing both parents describe:

A B C D E F G H
Child 1 0 1 0 5 3 2 2 2
Child 2 5 0 1 0 2 3 3 3

Grouping the items by label gives the bins, written as label: items (load in kg):

  • Parent 1: 0: AD (10), 1: BC (10), 2: EFGH (10).
  • Parent 2: 5: AD (10), 0: BC (10), 3: EFGH (10).
  • Child 1: 0: AC (11, overfull), 1: B (5), 5: D (4), 3: E (4), 2: FGH (6).
  • Child 2: 5: A (6), 0: BD (9), 1: C (5), 2: E (4), 3: FGH (6).

Two optimal, identical packings produce children with five bins each, one of them infeasible. Label 0 means "the bin of A and D" in parent 1 and "the bin of B and C" in parent 2, so copying C's label from parent 2 into parent 1 sends C to A's bin. With a permutation of the items and a decoder, two parents that are the same packing in the same order produce that packing again.

The objective gives almost no guidance. The fitness is the number of distinct labels. It falls by one only when a bin is emptied completely, and a bin of three items is emptied only if all three move elsewhere. Partial progress, such as moving two of the three, scores exactly the same as no progress, so selection cannot reward it.

Mutation mostly opens new bins. A reset gene takes a uniformly random label from 0 to $N - 1$. When a 100-item individual uses 42 labels, the new label is one of the 58 unused ones with probability 0.58, and the item moves to a bin of its own. Random initialization starts high for the same reason. $N$ items placed independently into $N$ bins occupy $N(1 - (1 - 1/N)^N)$ bins on average, about 63 for $N = 100$. The search starts far from the optimum, and its main operator pushes back toward where it started.

The capacity constraint rarely binds. With items averaging 1 kg, a bin overflows only when it holds at least six items. Random assignments almost never put that many together, and selection pressure pushes toward fewer bins only slowly. So almost every individual the GA evaluated was feasible. The constraint-handling machinery, which the assignment emphasized, played almost no part. That was a property of the instances, which the report recognized, not a sign that the problem was easy for this encoding.

A permutation encoding with a decoder

The small example suggested that the order of the items carries what a packing needs, and a GA can search over orders instead. An alternative representation encodes an order of the items and leaves the packing to a deterministic decoder, here first fit. The decoder takes the items in the encoded order and puts each into the lowest-numbered bin that has room, opening a new bin when none has. Every permutation decodes to a feasible packing, so the constraint disappears from the search. That is the idea behind decoder-based constraint handling (Eiben and Smith, 2015, ch. 13; see constraint handling in evolutionary algorithms).

Nothing is lost by restricting the search to first-fit packings: some order decodes to an optimal packing. Here is the argument. Take any packing into $k$ bins and list its items bin by bin, so that the list is $k$ groups, each with a total weight of at most the capacity. Decoding that order with first fit uses at most $k$ bins, because each group opens at most one new bin. Suppose an item of a group opens a new bin. That bin is then the last one open, and until the group ends it holds only items of this group. A later item of the same group therefore finds room there, if not in an earlier bin, since the items of the group together weigh no more than the capacity. So no second bin is opened during the group, and $k$ groups open at most $k$ bins. Applied to an optimal packing, the argument gives an order that first fit decodes into no more bins than the optimum, and no packing uses fewer. The script checks the argument on 2,000 random packings of random instances.

The permutation GA in the script uses:

  • binary tournaments;
  • order crossover with probability 0.9;
  • swap mutation, in which each position is swapped with a random position with probability $1/N$;
  • one elite individual.

Eiben and Smith (2015, ch. 4) describe these operators for permutations. The fitness is compared lexicographically: first the number of bins, then, among packings with equally many bins, the larger value of $\sum_j (\ell_j / C)^2$, where $\ell_j$ is the load of bin $j$. The second term favors packings that concentrate the load in some bins and leave others nearly empty. That is the partial progress the bare bin count cannot see.

With the same budget of 1,000 evaluations, on the same four instances and the same thirty seeds, the permutation GA reached the optimum in every run, 2, 3, 6 and 11 bins. So did a control with no evolution at all: the best of 1,000 random orders, each decoded by first fit. On these instances the decoder does the work.

Example (no order can do badly on these instances). On the 100-item instance, whose items total 101.24 kg, no order can produce more than thirteen bins. First fit opens a new bin only for an item that fits in no open bin, and every item weighs at most 2 kg, so at that moment every bin already open holds more than 8 kg. When the last bin is opened, that is true of all the others, so in any order all bins but the last end more than 80 percent full. Twelve such bins hold more than 96 kg, and thirteen would hold more than 104 kg, more than the instance's total. So at most twelve bins are more than 8 kg full, and the last makes thirteen. The bound is far from tight: of 10,000 random orders, every one decodes to an optimal packing on each of the four instances. The assignment's instances turn out to be easy; the difficulty the GA met came from the integer encoding.

Harder instances

If random orders are already optimal, these instances cannot tell methods apart. The instance class matters more than the parameter settings. Falkenauer contributed a family of hard test instances to OR-Library (Beasley, 1990), the triplets. They are built with a known optimum of $N/3$ bins, three items to a bin. The script generates instances in the same style. For each triplet it draws:

  • a first item of 380 to 490 units;
  • a second of 250 units up to half the remainder;
  • a third that fills the bin of capacity 1,000 exactly.

It then shuffles all the items together. The total weight is then exactly $N/3$ full bins, so any packing that leaves space unused in even one bin needs an extra bin.

Items OPT FFD Permutation GA, 10,000 evaluations, median [IQR] Runs at OPT
60 20 24 21 [21, 21] 0 of 30
120 40 47 42 [42, 42] 0 of 30

Here FFD is far from the optimum, the permutation GA improves on it substantially, and neither reaches the optimum in any run. On the 120-item instance the best of the GA's 50 random initial orders already gives a median of 44 bins, three fewer than FFD. Evolution then gains two more bins over 200 generations, most of the way in two steps (the right panel of the convergence figure above).

Falkenauer's (1996) grouping genetic algorithm was designed for problems of this kind. It is a GA "heavily modified to suit the structure of grouping problems", in which the genes represent groups, here bins, so that the operators can pass on whole bins. He combined it with a local optimization inspired by the dominance criterion of Martello and Toth, and reports that the combination outperformed both of its components. Building that representation is the natural next step for this experiment. Another line of work evolves the packing rule instead of a packing. FunSearch evolved heuristics for online bin packing, where items arrive one at a time, that used fewer bins than first fit and best fit on the OR-Library datasets; LLM-guided evolution describes it.

Lessons

  1. Compare with a baseline and a bound before tuning. A two-line FFD and the bound $L_1$ show at once that the optimum is 11 bins, and so that the GA's 42 reflects the method rather than the problem.
  2. Hold the instance fixed when comparing parameter settings, and run each setting over many seeds.
  3. Choose a representation in which the units of a good solution can be inherited. For bin packing those units are groups of items, which neither the item-to-label encoding nor the bare bin count represents.
  4. Give selection a gradient. A fitness that changes only when a bin empties cannot reward the steps that lead there.
  5. Test on instances that can tell methods apart. On the assignment's instances, random orders with a good decoder were already optimal.

What the case shows

The gap of about four times the optimum came from the encoding and objective the assignment specified, not from hard instances: random orders decoded by first fit were already optimal on them. Bin labels carry no meaning, so crossover cannot pass on a good bin, and the bin count rewards no partial progress. A permutation with a first-fit decoder reached the optimum on the same instances, and only triplet instances in Falkenauer's style kept it from the optimum. The case closes the unit on the point its earlier articles prepared: a genetic algorithm does well when its representation, operators and fitness let selection see and keep the parts of a good solution.

The permutation GA already compared packings on two criteria, the number of bins first and the concentration of load second. That order of priority was a choice. Many problems have objectives that genuinely conflict, such as cost against strength, and no single best answer. The next topic, multi-objective optimization, begins with how to compare candidates on several objectives at once: Pareto dominance.

References

  • Beasley, J. E. (1990). OR-Library: distributing test problems by electronic mail. Journal of the Operational Research Society, 41(11), 1069–1072. record
  • Deb, K. (2000). An efficient constraint handling method for genetic algorithms. Computer Methods in Applied Mechanics and Engineering, 186(2–4), 311–338. record · 2 pages
  • Dósa, G. (2007). The tight bound of First Fit Decreasing bin-packing algorithm is FFD(I) ≤ 11/9 OPT(I) + 6/9. In Combinatorics, Algorithms, Probabilistic and Experimental Methodologies (ESCAPE 2007), Lecture Notes in Computer Science 4614, 1–11. Springer. record
  • Eiben, A. E., and Smith, J. E. (2015). Introduction to Evolutionary Computing, 2nd ed., chapters 4 and 13. Springer. record · 35 pages
  • Falkenauer, E. (1996). A hybrid grouping genetic algorithm for bin packing. Journal of Heuristics, 2(1), 5–30. record
  • Johnson, D. S. (1973). Near-Optimal Bin Packing Algorithms. PhD thesis, Massachusetts Institute of Technology. record
  • Martello, S., and Toth, P. (1990). Knapsack Problems: Algorithms and Computer Implementations, chapter 8. Wiley. record

Reviewed: verified, 16 Nov 2026 · citations checked, 16 Nov 2026 · editorial review, 23 Nov 2026 · 7 revisions