Documentation

CslibFrontier.Algorithms.Lean.BinPacking.Online

Online bin packing #

An online bin-packing algorithm receives the items one at a time. It puts each item into a bin that has room for it or opens a new bin, without seeing the items still to come, and it never moves an item again. This file models deterministic online algorithms with capacity C as OnlinePacker C. The rule place picks a bin for the next item from the items seen so far and the current bins. If the chosen bin has no room, or no bin is chosen, the item opens a new last bin (onlineInsert), so every rule yields a valid packing (OnlinePacker.isPacking_run). Every deterministic online algorithm is such a rule, since its state after a prefix of the input is a function of that prefix.

Two facts make lower bounds against all online algorithms possible. The packing of l₁ ++ l₂ continues the packing of l₁ (OnlinePacker.run_append). And items that fail a predicate do not change the number of bins holding an item that satisfies it (OnlinePacker.countP_any_runFrom): such bins all exist before the items arrive. First Fit is an online packer (run_firstFitPacker).

Main definitions #

Main results #

Put item a into bin i (for o = some i) if it fits there, and otherwise, or for o = none, into a new last bin.

theorem Cslib.Algorithms.Lean.BinPacking.sum_le_of_mem_onlineInsert {C a : ℕ} (ha : a ≤ C) {o : Option ℕ} {bins : List (List ℕ)} :
(∀ b ∈ bins, b.sum ≤ C) → ∀ b ∈ onlineInsert C a o bins, b.sum ≤ C
theorem Cslib.Algorithms.Lean.BinPacking.countP_any_onlineInsert {C a : ℕ} {p : ℕ → Bool} (hp : p a = false) (o : Option ℕ) (bins : List (List ℕ)) :
List.countP (fun (b : List ℕ) => b.any p) (onlineInsert C a o bins) = List.countP (fun (b : List ℕ) => b.any p) bins

Inserting an item that fails p does not change the number of bins holding an item that satisfies p.

A deterministic online bin-packing algorithm with bin capacity C.

  • place : List ℕ → List (List ℕ) → ℕ → Option ℕ

    The bin chosen for the next item a, from the items seen so far and the current bins; none, or a bin without room, opens a new bin.

The packing that A builds from the items hist seen so far and the bins bins on further items.

The packing of l that A builds online.

theorem Cslib.Algorithms.Lean.BinPacking.OnlinePacker.runFrom_append {C : ℕ} (A : OnlinePacker C) (hist : List ℕ) (bins : List (List ℕ)) (l₁ l₂ : List ℕ) :
A.runFrom hist bins (l₁ ++ l₂) = A.runFrom (hist ++ l₁) (A.runFrom hist bins l₁) l₂
theorem Cslib.Algorithms.Lean.BinPacking.OnlinePacker.run_append {C : ℕ} (A : OnlinePacker C) (l₁ l₂ : List ℕ) :
A.run (l₁ ++ l₂) = A.runFrom l₁ (A.run l₁) l₂

Online. The packing of l₁ ++ l₂ continues the packing of l₁.

theorem Cslib.Algorithms.Lean.BinPacking.OnlinePacker.sum_le_of_mem_runFrom {C : ℕ} (A : OnlinePacker C) {hist : List ℕ} {bins : List (List ℕ)} {l : List ℕ} :
(∀ a ∈ l, a ≤ C) → (∀ b ∈ bins, b.sum ≤ C) → ∀ b ∈ A.runFrom hist bins l, b.sum ≤ C
theorem Cslib.Algorithms.Lean.BinPacking.OnlinePacker.isPacking_run {C : ℕ} (A : OnlinePacker C) {l : List ℕ} (hl : ∀ a ∈ l, a ≤ C) :
IsPacking C l (A.run l)

An online packer packs every list of items that fit into a bin.

theorem Cslib.Algorithms.Lean.BinPacking.OnlinePacker.countP_any_runFrom {C : ℕ} (A : OnlinePacker C) {p : ℕ → Bool} {hist : List ℕ} {bins : List (List ℕ)} {l : List ℕ} :
(∀ a ∈ l, p a = false) → List.countP (fun (b : List ℕ) => b.any p) (A.runFrom hist bins l) = List.countP (fun (b : List ℕ) => b.any p) bins

Items that fail p leave the number of bins holding an item satisfying p unchanged.

First Fit as an online packer: the first bin with room.