An Asymptotic PTAS for Bin Packing
Bin packing has no approximation ratio below unless P = NP, because of instances with OPT = 2 (Bin Packing: First Fit and First-Fit Decreasing). For large instances the picture is completely different. Fernandez de la Vega and Lueker (1981) showed that for every there is a polynomial algorithm using at most bins: an asymptotic PTAS (APTAS). The construction combines three ideas that recur in approximation schemes everywhere: set aside small items and add them greedily at the end, round the large items to few distinct sizes, and solve the rounded instance exactly because it has only a constant number of "types".
Step 1: small items do not matter
Call an item small if its size is at most , and large otherwise.
Lemma. Suppose the large items are packed into bins. Adding the small items with First Fit (opening new bins only when needed) gives at most bins.
Proof. If no new bin is opened, there are bins. Otherwise, when the last bin was opened, every other bin had less than free space, so with bins in total, , and for .
So it suffices to pack the large items into at most bins.
Step 2: linear grouping
Sort the large items by decreasing size and cut the list into consecutive groups of items each (the last group may be smaller). Round every item up to the largest size in its group. Then:
- Groups of the rounded instance are dominated by groups of the original: each rounded item of group is no larger than any original item of group . So the rounded instance without its first group fits in OPT bins: take an optimal packing and put each rounded item of group where an item of group was.
- The first group is packed separately, one item per bin: extra bins.
Choose , where is the total size of the large items. (If , then and there are fewer than large items, a constant number that brute force packs optimally.) Then and the number of groups is , since every large item has size above , so .
The program performs the grouping on 200 random large items and checks the domination property group by group. It also counts the distinct sizes left after rounding, which is what the exact step has to cope with.
import random
rng = random.Random(17)
items = sorted((rng.randint(11, 70) for _ in range(200)), reverse=True) # sizes in % of a bin
for eps in [0.5, 0.3, 0.2]:
large = [s for s in items if s > eps * 100 / 2]
g = max(1, int(eps * sum(large) / 100))
groups = [large[i:i + g] for i in range(0, len(large), g)]
rounded = [[grp[0]] * len(grp) for grp in groups] # round up to the group's largest
dominated = all(max(rounded[j + 1]) <= min(groups[j]) for j in range(len(groups) - 1))
print(f"eps = {eps}: {len(large)} large items, group size {g}, {len(groups)} groups -> "
f"{len({r[0] for r in rounded[1:]})} distinct sizes, group j+1 rounded <= group j original: {dominated}")eps = 0.5: 153 large items, group size 36, 5 groups -> 4 distinct sizes, group j+1 rounded <= group j original: True
eps = 0.3: 181 large items, group size 23, 8 groups -> 7 distinct sizes, group j+1 rounded <= group j original: True
eps = 0.2: 200 large items, group size 16, 13 groups -> 12 distinct sizes, group j+1 rounded <= group j original: True
The number of distinct sizes grows like , independent of the number of items: that is what makes the exact step polynomial for fixed .
Step 3: few sizes means exact solvability
The rounded instance has distinct sizes, all above , so a bin holds fewer than items. A configuration is a multiset of sizes that fits in one bin; there are at most of them, a constant for fixed . A packing is determined by how many bins use each configuration, at most each, so trying all of them, or a dynamic program over the vector of remaining counts per size, takes time : polynomial for fixed , though with a horrible constant.
Theorem (Fernandez de la Vega and Lueker, 1981). For every fixed , the algorithm uses at most bins and runs in polynomial time.
Proof. The large items use at most bins (step 2, with an exact packing in step 3), and step 1 adds the small ones.
import random
from functools import lru_cache
CAP = 100
def first_fit_into(rooms, items):
rooms = list(rooms)
for s in items:
for i, r in enumerate(rooms):
if s <= r:
rooms[i] -= s
break
else:
rooms.append(CAP - s)
return rooms
def exact_rounded(sizes, counts):
"""Fewest bins for counts[i] items of size sizes[i]; returns the free room of each bin."""
configs = []
def gen(i, room, cfg):
if i == len(sizes):
if any(cfg):
configs.append((tuple(cfg), room))
return
for c in range(room // sizes[i] + 1):
gen(i + 1, room - c * sizes[i], cfg + [c])
gen(0, CAP, [])
@lru_cache(None)
def best(state):
if not any(state):
return 0, ()
result = None
for cfg, room in configs:
if all(c <= s for c, s in zip(cfg, state)):
rest = tuple(s - c for c, s in zip(cfg, state))
k, rooms = best(rest)
if result is None or k + 1 < result[0]:
result = (k + 1, rooms + (room,))
return result
return best(tuple(counts))[1], len(configs)
def aptas(items, eps):
small = [s for s in items if s <= eps * CAP / 2]
large = sorted((s for s in items if s > eps * CAP / 2), reverse=True)
g = max(1, int(eps * sum(large) / CAP))
groups = [large[i:i + g] for i in range(0, len(large), g)]
rooms = [CAP - s for s in groups[0]] # first group: one item per bin
sizes = [grp[0] for grp in groups[1:]] # round up to the group's largest
counts = [len(grp) for grp in groups[1:]]
packed, n_configs = exact_rounded(sizes, counts) if sizes else ((), 0)
rooms += list(packed)
rooms = first_fit_into(rooms, sorted(small, reverse=True))
return len(rooms), len(groups), n_configs
def ffd(items):
return len(first_fit_into([], sorted(items, reverse=True)))
rng = random.Random(23)
items = [rng.choice([rng.randint(3, 12), rng.randint(26, 60)]) for _ in range(40)]
lb = -(-sum(items) // CAP)
print(f"{len(items)} items, total size {sum(items)}, lower bound {lb} bins, FFD {ffd(items)} bins, so OPT = {lb}")
for eps in [0.8, 0.6, 0.4]:
bins, groups, n_configs = aptas(items, eps)
print(f"eps = {eps}: {groups} groups, {n_configs:3} configurations, {bins} bins <= (1 + eps) OPT + 1 = {(1 + eps) * lb + 1:.1f}")40 items, total size 1009, lower bound 11 bins, FFD 11 bins, so OPT = 11
eps = 0.8: 4 groups, 8 configurations, 11 bins <= (1 + eps) OPT + 1 = 20.8
eps = 0.6: 5 groups, 16 configurations, 11 bins <= (1 + eps) OPT + 1 = 18.6
eps = 0.4: 7 groups, 29 configurations, 12 bins <= (1 + eps) OPT + 1 = 16.4
On this instance FFD already meets the lower bound, so the scheme cannot beat it, and with it even uses one bin more, which its guarantee allows: rounding up wastes space that FFD uses. What the program shows is the mechanism. A smaller means more groups and more configurations, and the cost of the exact step grows quickly. Asymptotic schemes are theorems about what is possible, and practical solvers use the configuration idea through linear programming instead.
In C++
The exact step on its own: a dynamic program over the vector of remaining counts, for three rounded sizes.
#include <cstdio>
#include <map>
#include <vector>
std::vector<int> sizes = {45, 34, 27};
std::vector<std::vector<int>> configs;
std::map<std::vector<int>, int> memo;
void gen(size_t i, int room, std::vector<int>& cfg) {
if (i == sizes.size()) {
for (int c : cfg) if (c) { configs.push_back(cfg); break; }
return;
}
for (int c = 0; c * sizes[i] <= room; c++) {
cfg.push_back(c);
gen(i + 1, room - c * sizes[i], cfg);
cfg.pop_back();
}
}
int best(const std::vector<int>& state) {
bool empty = true;
for (int s : state) if (s) empty = false;
if (empty) return 0;
auto it = memo.find(state);
if (it != memo.end()) return it->second;
int result = 1 << 30;
for (auto& cfg : configs) {
std::vector<int> rest = state;
bool ok = true;
for (size_t i = 0; i < rest.size(); i++) if ((rest[i] -= cfg[i]) < 0) ok = false;
if (ok) result = std::min(result, 1 + best(rest));
}
return memo[state] = result;
}
int main() {
std::vector<int> cfg;
gen(0, 100, cfg);
std::printf("%zu configurations fit in a bin of size 100\n", configs.size());
std::vector<int> counts = {7, 9, 11};
std::printf("7 x 45, 9 x 34, 11 x 27: %d bins (total size %d)\n", best(counts), 7 * 45 + 9 * 34 + 11 * 27);
std::printf("states explored: %zu\n", memo.size());
}13 configurations fit in a bin of size 100
7 x 45, 9 x 34, 11 x 27: 10 bins (total size 918)
states explored: 959
The configuration LP and additive guarantees
Instead of enumerating packings, write one variable per configuration and require that every size is covered: subject to for each size . This LP, of Gilmore and Gomory (1961) for cutting stock, has exponentially many variables but can be solved approximately with a knapsack separation oracle for its dual (Separation Oracles and Exponentially Many Constraints). A basic solution has at most fractional variables; rounding them up costs at most extra bins.
- Karmarkar and Karp (1982) combined the configuration LP with a smarter geometric grouping and obtained bins, an asymptotic FPTAS.
- Hoberg and Rothvoß (2017) improved this to .
- Whether is achievable in polynomial time is open; the configuration LP is conjectured to have an additive integrality gap of at most 1 (the modified integer round-up property).
Test yourself
Why can small items (size at most ) be added greedily at the end?
- Because they always fit in one bin
- If First Fit opens a new bin for them, all other bins are more than full, so the total is at most about
- Because they are packed optimally by the DP
- Because there are few of them
In linear grouping, why does the rounded instance without its first group fit into OPT bins?
- Because rounding makes items smaller
- Each rounded item of group is no larger than every original item of group , so it can take that item's place in an optimal packing
- Because the first group is the smallest
- Because of First Fit
Why is the rounded instance solvable exactly in polynomial time for fixed ?
- Bin packing is in P
- It has sizes and fewer than items per bin, so the number of configurations is a constant and packings are count vectors over them
- Because all items are equal
- Because the LP is integral