Online makespan on three machines
DefinitionMakespan3Jobs with release times and sizes run without preemption on three identical machines. An online scheduler maps each input to a feasible schedule whose starts up to time depend only on the jobs released by ; it is -competitive if it always finishes by .
A locking scheduler starts a longest released job (least index on ties) whenever a machine is free and the lock has expired; dispatching a size- job locks further starts for . The fraction is constant (SLEEPY), a function of the dispatch count and size (adaptive), or a two-rate rule: if the short-lock guard holds, else . With the earlier dispatches of the current busy component, the guard asks that , that every job of has , and that some job of with started at least after the component began. The residual guard checks the bound only for jobs with at least work left.
For a locking rule, StrictBound bounds every run by for every feasible offline deadline ; ForcedLower says every factor below is beaten on some input; ExactRatio is both.
import Mathlib.Data.Real.Basic
import Mathlib.Data.List.FinRange
import Mathlib.Order.Monotone.Basic
import Mathlib.Logic.Equiv.Defs
/-! Online makespan minimization on three identical machines: inputs, offline
feasibility, general deterministic online schedulers, longest-job-first schedulers
with a dispatch lock, and the target propositions of the mission. -/
namespace Makespan3
abbrev Machine := Fin 3
/-- `n` jobs, each with a release time and a size. Jobs are distinct even when equal. -/
structure Instance (n : ℕ) where
release : Fin n → ℝ
size : Fin n → ℝ
def Valid {n : ℕ} (I : Instance n) : Prop :=
0 < n ∧ ∀ j, 0 ≤ I.release j ∧ 0 < I.size j
/-! ### Schedules -/
structure Schedule (n : ℕ) where
start : Fin n → ℝ
machine : Fin n → Machine
/-- Every job starts after its release, and jobs on one machine do not overlap. -/
def Feasible {n : ℕ} (I : Instance n) (σ : Schedule n) : Prop :=
(∀ i, I.release i ≤ σ.start i) ∧
∀ i j, i ≠ j → σ.machine i = σ.machine j →
σ.start i + I.size i ≤ σ.start j ∨ σ.start j + I.size j ≤ σ.start i
def DoneBy {n : ℕ} (I : Instance n) (σ : Schedule n) (T : ℝ) : Prop :=
∀ i, σ.start i + I.size i ≤ T
/-- Some feasible schedule completes every job by `T`. -/
def OfflineBy {n : ℕ} (I : Instance n) (T : ℝ) : Prop :=
∃ σ, Feasible I σ ∧ DoneBy I σ T
/-! ### General deterministic online schedulers -/
/-- `e` matches the jobs of `I` and `I'` released by time `t`, preserving index order,
release times and sizes. -/
def SamePast {n n' : ℕ} (I : Instance n) (I' : Instance n') (t : ℝ)
(e : {j // I.release j ≤ t} ≃ {j // I'.release j ≤ t}) : Prop :=
StrictMono ⇑e ∧ ∀ j, I'.release (e j).1 = I.release j.1 ∧ I'.size (e j).1 = I.size j.1
/-- A deterministic online scheduler: a feasible schedule for every valid input, where
the starts made by time `t` depend only on the jobs released by time `t`. -/
structure OnlineScheduler where
run : ∀ {n : ℕ}, Instance n → Schedule n
feasible : ∀ {n : ℕ} (I : Instance n), Valid I → Feasible I (run I)
online : ∀ {n n' : ℕ} (I : Instance n) (I' : Instance n') (t : ℝ)
(e : {j // I.release j ≤ t} ≃ {j // I'.release j ≤ t}),
Valid I → Valid I' → SamePast I I' t e →
∀ j : {j // I.release j ≤ t}, (run I).start j.1 ≤ t →
(run I').start (e j).1 = (run I).start j.1 ∧
(run I').machine (e j).1 = (run I).machine j.1
/-- Strict competitive ratio at most `ρ`, with no additive constant. -/
def Competitive (S : OnlineScheduler) (ρ : ℝ) : Prop :=
∀ (n : ℕ) (I : Instance n), Valid I → ∀ T, OfflineBy I T → DoneBy I (S.run I) (ρ * T)
/-! ### Longest-job-first scheduling with a dispatch lock -/
structure Event (n : ℕ) where
job : Fin n
time : ℝ
machine : Machine
def finish {n : ℕ} (I : Instance n) (e : Event n) : ℝ :=
e.time + I.size e.job
/-- The lock fraction for dispatching job `j` at time `t`, given the number of earlier
dispatches and the earlier dispatches of the current busy component, most recent first. -/
abbrev LockRule : Type :=
∀ n : ℕ, Instance n → ℕ → List (Event n) → ℝ → Fin n → ℝ
structure State (n : ℕ) where
pending : List (Fin n)
ready : Machine → ℝ
lockEnd : ℝ
count : ℕ
/-- Dispatches of the current busy component, most recent first. -/
history : List (Event n)
def initial (n : ℕ) : State n :=
⟨List.finRange n, fun _ => 0, 0, 0, []⟩
/-- The busy component seen at time `t`. A positive idle interval clears it; a completion
at exactly `t` keeps it. -/
noncomputable def component {n : ℕ} (s : State n) (t : ℝ) : List (Event n) := by
classical
exact if ∀ m, s.ready m < t then [] else s.history
def componentStart {n : ℕ} (h : List (Event n)) (t : ℝ) : ℝ :=
(h.getLast?.map Event.time).getD t
/-- A dispatch happens at the earliest time any pending job can use any machine after the
lock; it starts a longest released job, least index first, on any free machine. -/
structure LegalChoice {n : ℕ} (I : Instance n) (s : State n) (e : Event n) : Prop where
pending : e.job ∈ s.pending
released : I.release e.job ≤ e.time
machineReady : s.ready e.machine ≤ e.time
lockExpired : s.lockEnd ≤ e.time
earliest : ∀ j ∈ s.pending, ∀ m : Machine,
e.time ≤ max (I.release j) (max (s.ready m) s.lockEnd)
longest : ∀ j ∈ s.pending, I.release j ≤ e.time → I.size j ≤ I.size e.job
tieOrder : ∀ j ∈ s.pending, I.release j ≤ e.time → I.size j = I.size e.job → e.job ≤ j
noncomputable def afterDispatch {n : ℕ} (r : LockRule) (I : Instance n) (s : State n)
(e : Event n) : State n :=
let h := component s e.time
{ pending := s.pending.erase e.job
ready := fun m => if m = e.machine then finish I e else s.ready m
lockEnd := e.time + r n I s.count h e.time e.job * I.size e.job
count := s.count + 1
history := e :: h }
/-- A complete execution, in chronological order, ending when no job is pending. -/
inductive Execution {n : ℕ} (r : LockRule) (I : Instance n) :
State n → List (Event n) → Prop where
| done {s} (empty : s.pending = []) : Execution r I s []
| step {s e es} (choose : LegalChoice I s e)
(rest : Execution r I (afterDispatch r I s e) es) : Execution r I s (e :: es)
def Runs {n : ℕ} (r : LockRule) (I : Instance n) (es : List (Event n)) : Prop :=
Execution r I (initial n) es
/-- Reachable states, with all past dispatches, most recent first. -/
inductive Reachable {n : ℕ} (r : LockRule) (I : Instance n) :
State n → List (Event n) → Prop where
| init : Reachable r I (initial n) []
| step {s e past} (prior : Reachable r I s past) (choose : LegalChoice I s e) :
Reachable r I (afterDispatch r I s e) (e :: past)
/-- Machine ties go to the least free machine. -/
def LeastMachine {n : ℕ} (s : State n) (e : Event n) : Prop :=
∀ m : Machine, s.ready m ≤ e.time → e.machine ≤ m
noncomputable def LeastMachineTrace {n : ℕ} (r : LockRule) (I : Instance n) :
State n → List (Event n) → Prop
| _, [] => True
| s, e :: es => LeastMachine s e ∧ LeastMachineTrace r I (afterDispatch r I s e) es
def DeterministicRuns {n : ℕ} (r : LockRule) (I : Instance n) (es : List (Event n)) : Prop :=
Runs r I es ∧ LeastMachineTrace r I (initial n) es
/-- The schedule `σ` starts each dispatched job at its dispatch time and machine. -/
def Realizes {n : ℕ} (es : List (Event n)) (σ : Schedule n) : Prop :=
∀ e ∈ es, σ.start e.job = e.time ∧ σ.machine e.job = e.machine
def Total (r : LockRule) : Prop :=
∀ (n : ℕ) (I : Instance n), Valid I → ∃ es, Runs r I es
/-- Every run on every valid input finishes by `ρ` times any feasible offline deadline. -/
def StrictBound (r : LockRule) (ρ : ℝ) : Prop :=
Total r ∧ ∀ (n : ℕ) (I : Instance n), Valid I → ∀ T, OfflineBy I T →
∀ es, Runs r I es → ∀ e ∈ es, finish I e ≤ ρ * T
/-- Every factor below `q` is defeated by one input, whatever the machine choices. -/
def ForcedLower (r : LockRule) (q : ℝ) : Prop :=
∀ ρ : ℝ, ρ < q → ∃ (n : ℕ) (I : Instance n) (T : ℝ),
Valid I ∧ OfflineBy I T ∧ (∃ es, Runs r I es) ∧
∀ es, Runs r I es → ∃ e ∈ es, ρ * T < finish I e
def ExactRatio (r : LockRule) (q : ℝ) : Prop :=
StrictBound r q ∧ ForcedLower r q
/-! ### The lock rules -/
/-- Generalized SLEEPY: constant lock fraction `α`. -/
def sleepy (α : ℝ) : LockRule := fun _ _ _ _ _ _ => α
/-- Fraction `β k p` at dispatch number `k` for a job of size `p`. -/
def adaptive (β : ℕ → ℝ → ℝ) : LockRule := fun _ I k _ _ j => β k (I.size j)
inductive Guard where
| history
| residual
/-- The short-lock test for a size-`x` job at time `t`. The residual guard checks the
size bound only for earlier jobs with at least `L x` work left at `t`. -/
def ShortGuard {n : ℕ} (g : Guard) (I : Instance n) (L : ℝ) (h : List (Event n)) (t : ℝ)
(j : Fin n) : Prop :=
let x := I.size j
3*x/4 ≤ (h.map fun e => I.size e.job).sum ∧
(∀ e ∈ h, (g = .history ∨ L*x ≤ finish I e - t) → I.size e.job ≤ 7*x/10) ∧
∃ e ∈ h, I.size e.job ≤ 3*x/5 ∧ componentStart h t + x/10 ≤ e.time ∧ e.time < t
/-- Fraction `L` when the guard holds, `A` otherwise. -/
noncomputable def twoRate (g : Guard) (A L : ℝ) : LockRule := by
classical
exact fun _ I _ h t j => if ShortGuard g I L h t j then L else A
def Admissible (A L : ℝ) : Prop := 0 < L ∧ L ≤ A ∧ A < 1
/-! ### Constants -/
def RootV (v : ℝ) : Prop := 445/1000 < v ∧ v < 446/1000 ∧ v^3 - v^2 - 2*v + 1 = 0
noncomputable def Lstar (v : ℝ) : ℝ := (2 + 17*v - 8*v^2)/41
def RootW (w : ℝ) : Prop := 2*w^3 - w^2 + 2*w - 2 = 0
def RootA (a : ℝ) : Prop := 36/100 < a ∧ a < 37/100 ∧ a^3 + a^2 - 6*a + 2 = 0
/-! ### Targets -/
def RunsExist : Prop := ∀ r : LockRule, Total r
def BusyBeforeUnlocked : Prop :=
∀ r : LockRule, (∀ (n : ℕ) (I : Instance n) k h t j, 0 ≤ r n I k h t j) →
∀ (n : ℕ) (I : Instance n), Valid I →
∀ s past, Reachable r I s past → ∀ e, LegalChoice I s e → s.lockEnd < e.time →
(∃ w ∈ s.pending, I.release w < e.time) →
∃ occ : Machine → Event n, Function.Injective (fun m => (occ m).job) ∧
∀ m, occ m ∈ past ∧ (occ m).machine = m ∧
(occ m).time < e.time ∧ e.time ≤ finish I (occ m)
def SleepyOptimal : Prop :=
(∃! w : ℝ, RootW w) ∧ ∀ w, RootW w →
let H := 1 + w/(1+w)
7*H^3 - 32*H^2 + 51*H - 28 = 0 ∧
(∀ α, 0 ≤ α → α ≤ 1 → ForcedLower (sleepy α) H) ∧
ExactRatio (sleepy (1-w)) H ∧
∀ α, 0 ≤ α → α ≤ 1 → StrictBound (sleepy α) H → α = 1-w
def AdaptiveNoGain : Prop :=
∀ w, RootW w → ∀ β : ℕ → ℝ → ℝ, (∀ k p, 0 < p → 0 ≤ β k p ∧ β k p ≤ 1) →
∀ ρ, StrictBound (adaptive β) ρ → 1 + w/(1+w) ≤ ρ
def HistoryLower : Prop :=
∀ A L : ℝ, Admissible A L →
ForcedLower (twoRate .history A L) (1 + (1-A)/(2-A)) ∧
(19/100 ≤ A → A ≤ 9/40 → ForcedLower (twoRate .history A L) (1 + A + (1-A)/(2-A)^2))
def UpperBounds : Prop :=
(∀ v L : ℝ, RootV v → 194640/1000000 ≤ L → L ≤ Lstar v →
StrictBound (twoRate .history (v^2) L) (1+v)) ∧
StrictBound (twoRate .residual (1981/10000) (973/5000)) (26038/18019)
def HistoryOptimal : Prop :=
(∃! v : ℝ, RootV v) ∧ ∀ v, RootV v →
(194640/1000000 ≤ Lstar v ∧ Lstar v ≤ v^2) ∧
(∀ A L, Admissible A L → ForcedLower (twoRate .history A L) (1+v)) ∧
(∀ A L, Admissible A L → StrictBound (twoRate .history A L) (1+v) →
A = v^2 ∧ L ≤ Lstar v) ∧
∀ L, 194640/1000000 ≤ L → L ≤ Lstar v → ExactRatio (twoRate .history (v^2) L) (1+v)
def ResidualExact : Prop :=
ExactRatio (twoRate .residual (1981/10000) (973/5000)) (26038/18019) ∧
(∀ v, RootV v → 26038/18019 < 1+v) ∧
∀ A L ρ, Admissible A L → StrictBound (twoRate .history A L) ρ → 26038/18019 < ρ
def ResidualOnline : Prop :=
∃ S : OnlineScheduler, Competitive S (26038/18019) ∧
∀ (n : ℕ) (I : Instance n), Valid I → ∃ es,
DeterministicRuns (twoRate .residual (1981/10000) (973/5000)) I es ∧
Realizes es (S.run I)
def OnlineLower : Prop :=
∀ a, RootA a → ∀ (S : OnlineScheduler) (ρ : ℝ), Competitive S ρ → 1 + a ≤ ρ
def OnlineBounds : Prop :=
(∃! a : ℝ, RootA a) ∧ OnlineLower ∧ ∃ S : OnlineScheduler, Competitive S (26038/18019)
end Makespan3
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
Setting. Three identical machines, indexed (ordered as numbers). An instance with jobs consists of arbitrary functions (release times, sizes); jobs are identified by index only. is valid iff and , for all .
Offline. A schedule assigns each job a real start and a machine . It is feasible iff for all , and for with : or . : for all . : some feasible is done by .
General online schedulers. For instances ( jobs), ( jobs), time and a bijection from to , means is strictly increasing in index and preserves release and size. An online scheduler is a map (any ) such that (i) is feasible whenever is valid (no requirement on invalid ), and (ii) for valid , any , any such with SamePast: every job of with and -start has, in , the same start and machine for . is -competitive iff for every valid and every with , is done by (no additive constant; any real).
Lock-based list scheduling. An event is (job, time, machine); . A lock rule is any function giving a real number for an instance (any ), a natural , a list of events, a time and a job — no sign or range restriction. A state has: pending job list, ready time per machine, lock end , counter , history list . Initial state: all jobs pending, , , , .
- if for all three , else .
- = time of the last (oldest) event of , or if is empty.
A legal choice in state : pending; ; ; ; for every pending and every (so ); every pending with has , and if equal then . Only the machine is not determined (any machine with ).
After dispatch (): remove from pending; ; ; ; . The lock is global (all machines). An execution from is a list of successive legal choices ending in a state with nothing pending; : is an execution from the initial state (each job dispatched exactly once, chronologically). Reachable : obtained from the initial state by legal choices, past = all events, newest first. : for every with ; deterministic run = run where every step satisfies it. : starts each event's job at its time and machine.
- : every valid has a run.
- : Total, and for valid , with , every run and every event: .
- : for every real there are , valid , with , a run exists, and every run has an event with .
- = StrictBound ForcedLower.
Rules. . with the number of earlier dispatches. For guard , with : (1) ; (2) every with [=history, or ] has ; (3) some has , . returns if holds, else (ignores ). : .
Constants. : , . . : (no interval). : , . Numerically , , , , , .