import OAI.Optimization.LinearProgramming.Optimization /-! # My bridge file Small statements that I derive from the two theorems of the claimant. The kernel of Lean checks each proof. The file is my own work. -/ set_option maxHeartbeats 400000 set_option format.width 1000 namespace MobiusOtter open Matrix open OAI.Optimization.LinearProgramming /-! ## 1. The certificate of infeasibility is correct -/ /-- A Farkas certificate excludes a feasible point. -/ theorem certificate_excludes_feasible_point {m d : ℕ} (A : Matrix (Fin m) (Fin d) ℚ) (b : Fin m → ℚ) {y : Fin m → ℚ} (hy : Certificate A b y) : ¬ ∃ x, Feasible A b x := by unfold Certificate at hy rintro ⟨x, hx⟩ obtain ⟨hy0, hyA, hyb, -⟩ := hy unfold Feasible at hx have hle : y ⬝ᵥ (A *ᵥ x) ≤ y ⬝ᵥ b := by unfold dotProduct exact Finset.sum_le_sum fun i _ => mul_le_mul_of_nonneg_left (hx i) (hy0 i) have hzero : y ⬝ᵥ (A *ᵥ x) = 0 := by rw [dotProduct_mulVec, ← Matrix.mulVec_transpose, hyA] simp rw [dotProduct_comm y b] at hle linarith /-! ## 2. Feasibility -/ /-- For each rational instance with at least one constraint and one variable, a feasible point exists or a Farkas certificate exists. -/ theorem feasible_point_or_certificate (m d : ℕ) (hm : 0 < m) (hd : 0 < d) (A : Matrix (Fin m) (Fin d) ℚ) (b : Fin m → ℚ) : (∃ x, Feasible A b x) ∨ (∃ y, Certificate A b y) := by obtain ⟨p, c, e, bC, bE, h⟩ := strongly_polynomial_feasibility obtain ⟨k, t, -, -, -, hout⟩ := h m d hm hd A b unfold FeasibilityOutput at hout rcases hout with ⟨-, h⟩ | ⟨-, h⟩ · exact Or.inl ⟨_, h⟩ · exact Or.inr ⟨_, h⟩ /-! ## 3. Optimization -/ /-- The answer of the optimization program has one of three forms: a certificate of infeasibility, a primal point with a dual point of the same objective value, or a feasible point with a ray. -/ theorem optimization_three_answers (m d : ℕ) (hm : 0 < m) (hd : 0 < d) (A : Matrix (Fin m) (Fin d) ℚ) (b : Fin m → ℚ) (c : Fin d → ℚ) : (∃ y, Certificate A b y) ∨ (∃ x y value, Feasible A b x ∧ 0 ≤ y ∧ A.transpose *ᵥ y = c ∧ c ⬝ᵥ x = value ∧ b ⬝ᵥ y = value) ∨ (∃ x v, Feasible A b x ∧ A *ᵥ v ≤ 0 ∧ c ⬝ᵥ v = 1) := by obtain ⟨p, c₀, e, bC, bE, h⟩ := strongly_polynomial_optimization have h3 : ∃ (k : ℕ) (t : Fin (k + 1) → RAM.State ℚ), RAM.Trace p (RAM.start [m, d] (lpInput A b ++ List.ofFn c)) k t ∧ k ≤ RAM.polyBound c₀ e (m + d) ∧ (∀ i, RAM.scalarBound (RAM.polyBound bC bE (RAM.totalBits (lpInput A b ++ List.ofFn c))) (t i)) ∧ OptimizationOutput A b c (t (Fin.last k)) := h m d hm hd A b c obtain ⟨k, t, -, -, -, hout⟩ := h3 unfold OptimizationOutput at hout rcases hout with ⟨-, hy⟩ | ⟨-, hx, hy0, hyA, hcv, hbv, -⟩ | ⟨-, hx, hv, hcv⟩ · exact Or.inl ⟨_, hy⟩ · exact Or.inr (Or.inl ⟨_, _, _, hx, hy0, hyA, hcv, hbv⟩) · exact Or.inr (Or.inr ⟨_, _, hx, hv, hcv⟩) /-- A feasible point and a ray with `A *ᵥ v ≤ 0` and `c ⬝ᵥ v = 1` give a feasible point for each nonnegative step length `r`. The objective value is larger by `r`. -/ theorem ray_gives_large_values {m d : ℕ} (A : Matrix (Fin m) (Fin d) ℚ) (b : Fin m → ℚ) (c : Fin d → ℚ) {x v : Fin d → ℚ} (hx : Feasible A b x) (hv : A *ᵥ v ≤ 0) (hcv : c ⬝ᵥ v = 1) {r : ℚ} (hr : 0 ≤ r) : Feasible A b (x + r • v) ∧ c ⬝ᵥ (x + r • v) = c ⬝ᵥ x + r := by unfold Feasible at hx constructor · unfold Feasible have hA : A *ᵥ (x + r • v) = A *ᵥ x + r • A *ᵥ v := by rw [Matrix.mulVec_add, Matrix.mulVec_smul] rw [hA] intro i have hvi : (A *ᵥ v) i ≤ (0 : ℚ) := hv i have h2 := mul_le_mul_of_nonneg_left hvi hr rw [mul_zero] at h2 show (A *ᵥ x) i + r * (A *ᵥ v) i ≤ b i linarith [hx i] · simp only [dotProduct_add, dotProduct_smul, hcv, smul_eq_mul, mul_one] /-- The duality theorem for rational linear programs, as a consequence. If the problem is feasible and bounded, then a primal optimum and a dual optimum of the same value exist. -/ theorem duality_of_feasible_bounded (m d : ℕ) (hm : 0 < m) (hd : 0 < d) (A : Matrix (Fin m) (Fin d) ℚ) (b : Fin m → ℚ) (c : Fin d → ℚ) (hfeas : ∃ x, Feasible A b x) (hbounded : ∃ M, ∀ x, Feasible A b x → c ⬝ᵥ x ≤ M) : ∃ x y value, Feasible A b x ∧ 0 ≤ y ∧ A.transpose *ᵥ y = c ∧ c ⬝ᵥ x = value ∧ b ⬝ᵥ y = value ∧ ∀ z, Feasible A b z → c ⬝ᵥ z ≤ value := by rcases optimization_three_answers m d hm hd A b c with hy | h | h · obtain ⟨y, hy⟩ := hy exact absurd hfeas (certificate_excludes_feasible_point A b hy) · obtain ⟨x, y, value, hx, hy0, hyA, hcv, hbv⟩ := h refine ⟨x, y, value, hx, hy0, hyA, hcv, hbv, fun z hz => ?_⟩ have h1 : c ⬝ᵥ z = (A *ᵥ z) ⬝ᵥ y := by rw [← hyA, dotProduct_comm, dotProduct_mulVec, Matrix.vecMul_transpose] have h2 : (A *ᵥ z) ⬝ᵥ y ≤ b ⬝ᵥ y := by unfold dotProduct unfold Feasible at hz exact Finset.sum_le_sum fun i _ => mul_le_mul_of_nonneg_right (hz i) (hy0 i) linarith · obtain ⟨x, v, hx, hv, hcv⟩ := h obtain ⟨M, hM⟩ := hbounded have key : ∀ r : ℚ, 0 ≤ r → Feasible A b (x + r • v) ∧ c ⬝ᵥ (x + r • v) = c ⬝ᵥ x + r := fun r hr => ray_gives_large_values A b c hx hv hcv hr obtain ⟨hfeas2, hval⟩ := key (max 0 (M - c ⬝ᵥ x) + 1) (by positivity) have hle := hM _ hfeas2 rw [hval] at hle have hmax : M - c ⬝ᵥ x ≤ max 0 (M - c ⬝ᵥ x) := le_max_right _ _ linarith /-! ## 4. The address registers stay small The statement of the claimant bounds the bit length of the data registers. It does not bound the address registers. The results in this section show that each address register stays below `B + (the number of steps so far)` when the registers start at most `B`. -/ /-- The largest constant that one instruction can write into an address register. -/ def instrAddressConst : RAM.Instr → ℕ | .addrConst _ n => n | _ => 0 /-- The largest constant that a program can write into an address register. -/ def programAddressConst : RAM.Program → ℕ | [] => 0 | i :: p => max (instrAddressConst i) (programAddressConst p) theorem instrAddressConst_le_mem {i : RAM.Instr} : ∀ {p : RAM.Program}, i ∈ p → instrAddressConst i ≤ programAddressConst p := by intro p induction p with | nil => intro h; cases h | cons a p ih => intro h rcases List.mem_cons.mp h with h | h · subst h exact le_max_left _ _ · exact le_trans (ih h) (le_max_right _ _) /-- One step increases no address register beyond `B + 1`, if all address registers are at most `B` and `B` is at least each constant of the program. -/ theorem step_address_bound {p : RAM.Program} {B : ℕ} (hB : programAddressConst p ≤ B) {s s' : RAM.State ℚ} (h : RAM.step p s = some s') (hs : ∀ j, s.index j ≤ B) : ∀ j, s'.index j ≤ B + 1 := by cases h0 : s.output with | none => cases hi : p[s.pc]? with | none => simp only [RAM.step, h0, hi] at h exact absurd h (by simp) | some i => simp only [RAM.step, h0, hi] at h have hiB : instrAddressConst i ≤ B := le_trans (instrAddressConst_le_mem (List.mem_of_getElem? hi)) hB cases i with | const d q => simp only [RAM.execInstr] at h cases h intro j exact (hs j).trans (Nat.le_succ B) | copy d a => simp only [RAM.execInstr] at h cases h intro j exact (hs j).trans (Nat.le_succ B) | add d a b => simp only [RAM.execInstr] at h cases h intro j exact (hs j).trans (Nat.le_succ B) | sub d a b => simp only [RAM.execInstr] at h cases h intro j exact (hs j).trans (Nat.le_succ B) | mul d a b => simp only [RAM.execInstr] at h cases h intro j exact (hs j).trans (Nat.le_succ B) | div d a b => simp only [RAM.execInstr] at h split at h · exact absurd h (by simp) · cases h intro j exact (hs j).trans (Nat.le_succ B) | load d a => simp only [RAM.execInstr] at h cases h intro j exact (hs j).trans (Nat.le_succ B) | store a d => simp only [RAM.execInstr] at h cases h intro j exact (hs j).trans (Nat.le_succ B) | addrConst d n => simp only [RAM.execInstr] at h cases h simp only [RAM.State.putIndex, Function.update_apply] simp only [instrAddressConst] at hiB intro j split · omega · have hj := hs j omega | addrCopy d a => simp only [RAM.execInstr] at h cases h simp only [RAM.State.putIndex, Function.update_apply] intro j split · have ha := hs a omega · have hj := hs j omega | incr d a => simp only [RAM.execInstr] at h cases h simp only [RAM.State.putIndex, Function.update_apply] intro j split · have ha := hs a omega · have hj := hs j omega | decr d a => simp only [RAM.execInstr] at h cases h simp only [RAM.State.putIndex, Function.update_apply] intro j split · have ha := hs a omega · have hj := hs j omega | addrLoad d a => simp only [RAM.execInstr] at h cases h simp only [RAM.State.putIndex, Function.update_apply] intro j split · have ha := hs (s.index a) omega · have hj := hs j omega | addrStore a d => simp only [RAM.execInstr] at h cases h simp only [RAM.State.putIndex, Function.update_apply] intro j split · have ha := hs d omega · have hj := hs j omega | branchLT a b yes no => simp only [RAM.execInstr] at h cases h intro j exact (hs j).trans (Nat.le_succ B) | branchAddrLT a b yes no => simp only [RAM.execInstr] at h cases h intro j exact (hs j).trans (Nat.le_succ B) | jump target => simp only [RAM.execInstr] at h cases h intro j exact (hs j).trans (Nat.le_succ B) | halt tag => simp only [RAM.execInstr] at h cases h intro j exact (hs j).trans (Nat.le_succ B) | some x => simp only [RAM.step, h0] at h exact absurd h (by simp) /-- Along the steps of a trace, each address register stays below `B + (the number of steps so far)`. -/ theorem address_bound_of_steps {p : RAM.Program} {k : ℕ} {t : Fin (k + 1) → RAM.State ℚ} (hstep : ∀ i : Fin k, RAM.step p (t i.castSucc) = some (t i.succ)) {B : ℕ} (hB : programAddressConst p ≤ B) (h0 : ∀ j, (t 0).index j ≤ B) (i : Fin (k + 1)) (j : ℕ) : (t i).index j ≤ B + i := by have key : ∀ (n : ℕ) (hn : n < k + 1) (j : ℕ), (t ⟨n, hn⟩).index j ≤ B + n := by intro n induction n with | zero => intro hn j; simpa using h0 j | succ n ih => intro hn j have hnk : n < k := Nat.lt_of_succ_lt_succ hn have hs : RAM.step p (t ⟨n, Nat.lt_succ_of_lt hnk⟩) = some (t ⟨n + 1, hn⟩) := hstep ⟨n, hnk⟩ have hprev : ∀ j', (t ⟨n, Nat.lt_succ_of_lt hnk⟩).index j' ≤ B + n := fun j' => ih (Nat.lt_succ_of_lt hnk) j' have hBn : programAddressConst p ≤ B + n := le_trans hB (Nat.le_add_right _ _) exact (step_address_bound hBn hs hprev j).trans (by omega) exact key i i.isLt j /-- The address registers of the start state hold `m`, `d` and zero. -/ theorem start_address_le (m d B : ℕ) (hm : m ≤ B) (hd : d ≤ B) (input : List ℚ) (j : ℕ) : (RAM.start [m, d] input).index j ≤ B := by show ([m, d][j]?.getD 0 : ℕ) ≤ B cases j with | zero => exact hm | succ j => cases j with | zero => exact hd | succ j => simp /-- In the run of a program from the start state, each address register is at most `B + (the number of steps so far)`. Here `B` is at least `m`, `d` and each constant of the program. -/ theorem address_registers_stay_small {m d : ℕ} {input : List ℚ} {p : RAM.Program} {k : ℕ} {t : Fin (k + 1) → RAM.State ℚ} (htr : RAM.Trace p (RAM.start [m, d] input) k t) {B : ℕ} (hB : programAddressConst p ≤ B) (hm : m ≤ B) (hd : d ≤ B) (i : Fin (k + 1)) (j : ℕ) : (t i).index j ≤ B + i := by unfold RAM.Trace at htr obtain ⟨h0eq, hstep⟩ := htr refine address_bound_of_steps hstep hB (fun j => ?_) i j rw [h0eq] exact start_address_le m d B hm hd input j /-- The number of bits of each address register is less than `log2 (B + k) + 1`. -/ theorem address_register_bits {m d : ℕ} {input : List ℚ} {p : RAM.Program} {k : ℕ} {t : Fin (k + 1) → RAM.State ℚ} (htr : RAM.Trace p (RAM.start [m, d] input) k t) {B : ℕ} (hB : programAddressConst p ≤ B) (hm : m ≤ B) (hd : d ≤ B) (i : Fin (k + 1)) (j : ℕ) : Nat.log2 ((t i).index j) < Nat.log2 (B + k) + 1 := by have h1 : (t i).index j ≤ B + k := by have h := address_registers_stay_small htr hB hm hd i j omega rcases Nat.eq_zero_or_pos ((t i).index j) with hn | hn · simp [hn] · rw [Nat.log2_lt hn.ne'] have hmpos : 0 < B + k := lt_of_lt_of_le hn h1 exact lt_of_le_of_lt h1 ((Nat.log2_lt hmpos.ne').mp (Nat.lt_succ_self _)) /-- The feasibility theorem without the abbreviations of the claimant. The number of steps is at most `c * (m + d + 1) ^ e`. Each data register has at most `bC * (totalBits + 1) ^ bE` bits. Each address register is at most `max (max m d) (programAddressConst p) + (the number of steps so far)`. -/ theorem feasibility_explicit (m d : ℕ) (hm : 0 < m) (hd : 0 < d) (A : Matrix (Fin m) (Fin d) ℚ) (b : Fin m → ℚ) : ∃ (p : RAM.Program) (c e bC bE : ℕ), ∃ (k : ℕ) (t : Fin (k + 1) → RAM.State ℚ), RAM.Trace p (RAM.start [m, d] (lpInput A b)) k t ∧ k ≤ c * (m + d + 1) ^ e ∧ (∀ i j, RAM.ratBits ((t i).data j) ≤ bC * (RAM.totalBits (lpInput A b) + 1) ^ bE) ∧ (∀ i j, (t i).index j ≤ max (max m d) (programAddressConst p) + i) ∧ FeasibilityOutput A b (t (Fin.last k)) := by obtain ⟨p, c, e, bC, bE, h⟩ := strongly_polynomial_feasibility obtain ⟨k, t, htr, hk, hb, hout⟩ := h m d hm hd A b refine ⟨p, c, e, bC, bE, k, t, htr, hk, fun i j => hb i j, ?_, hout⟩ exact address_registers_stay_small htr (le_max_right _ _) (le_trans (le_max_left m d) (le_max_left _ _)) (le_trans (le_max_right m d) (le_max_left _ _)) /-! ## 5. The two statements in plain words Each restatement in this section does not use the definitions `StronglyPolynomialFeasibility`, `StronglyPolynomialOptimization`, `Trace`, `polyBound`, `scalarBound`, `ratBits`, `start`, `readVector`, `Feasible`, `Certificate`, `FeasibilityOutput` and `OptimizationOutput`. Each proof is the theorem of the claimant itself. Lean accepts a proof only if the restatement and the statement of the claimant are the same by definition. -/ /-- The input tape holds the rows of `A`, then `b`. -/ theorem lpInput_is {m d : ℕ} (A : Matrix (Fin m) (Fin d) ℚ) (b : Fin m → ℚ) : lpInput A b = (List.ofFn fun i => List.ofFn (A i)).flatten ++ List.ofFn b := rfl /-- The bit length of a rational number is 3, plus the binary length of its numerator, plus the binary length of its denominator. -/ theorem ratBits_is (q : ℚ) : RAM.ratBits q = 3 + q.num.natAbs.log2 + q.den.log2 := rfl /-- Feasibility in plain words. One list of instructions `p` and four constants `c`, `e`, `bC`, `bE` come before all inputs. For each rational instance with `m ≥ 1` constraints and `d ≥ 1` variables, there is a run of the machine that starts on the input and follows the program at each step. The run has at most `c * (m + d + 1) ^ e` steps. Each data register at each step has at most `bC * (totalBits + 1) ^ bE` bits. The last state holds either (tag 0) a feasible point, or (tag 1) a Farkas certificate with at most `rank A + 1` nonzero entries. -/ theorem feasibility_in_plain_words : ∃ (p : List RAM.Instr) (c e bC bE : ℕ), ∀ (m d : ℕ), 0 < m → 0 < d → ∀ (A : Matrix (Fin m) (Fin d) ℚ) (b : Fin m → ℚ), ∃ (k : ℕ) (t : Fin (k + 1) → RAM.State ℚ), ((t 0 = ⟨0, fun i => (lpInput A b)[i]?.getD 0, fun i => [m, d][i]?.getD 0, none⟩ ∧ (∀ i : Fin k, RAM.step p (t i.castSucc) = some (t i.succ))) ∧ k ≤ c * (m + d + 1) ^ e ∧ (∀ (i : Fin (k + 1)) (j : ℕ), 3 + ((t i).data j).num.natAbs.log2 + ((t i).data j).den.log2 ≤ bC * (RAM.totalBits (lpInput A b) + 1) ^ bE) ∧ (((t (Fin.last k)).output = some 0 ∧ A *ᵥ (fun i => (t (Fin.last k)).data (0 + ↑i)) ≤ b) ∨ ((t (Fin.last k)).output = some 1 ∧ 0 ≤ (fun i : Fin m => (t (Fin.last k)).data (0 + ↑i)) ∧ Aᵀ *ᵥ (fun i : Fin m => (t (Fin.last k)).data (0 + ↑i)) = 0 ∧ b ⬝ᵥ (fun i : Fin m => (t (Fin.last k)).data (0 + ↑i)) < 0 ∧ (Finset.univ.filter fun i : Fin m => (t (Fin.last k)).data (0 + ↑i) ≠ 0).card ≤ A.rank + 1))) := strongly_polynomial_feasibility /-- Optimization in plain words. The form is the same. The input tape holds the rows of `A`, then `b`, then the objective `obj`. The last state holds one of three answers. (Tag 0) A Farkas certificate of infeasibility. (Tag 1) A primal point `x`, a dual point `y` and a common value: `x` is feasible, `y` is nonnegative, `Aᵀ y = obj`, the objective value of `x` is the common value, `b ⬝ᵥ y` is the common value, and no feasible point has a larger objective value. (Tag 2) A feasible point and a ray `v` with `A v ≤ 0` and `obj ⬝ᵥ v = 1`. -/ theorem optimization_in_plain_words : ∃ (p : List RAM.Instr) (c e bC bE : ℕ), ∀ (m d : ℕ), 0 < m → 0 < d → ∀ (A : Matrix (Fin m) (Fin d) ℚ) (b : Fin m → ℚ) (obj : Fin d → ℚ), ∃ (k : ℕ) (t : Fin (k + 1) → RAM.State ℚ), ((t 0 = ⟨0, fun i => (lpInput A b ++ List.ofFn obj)[i]?.getD 0, fun i => [m, d][i]?.getD 0, none⟩ ∧ (∀ i : Fin k, RAM.step p (t i.castSucc) = some (t i.succ))) ∧ k ≤ c * (m + d + 1) ^ e ∧ (∀ (i : Fin (k + 1)) (j : ℕ), 3 + ((t i).data j).num.natAbs.log2 + ((t i).data j).den.log2 ≤ bC * (RAM.totalBits (lpInput A b ++ List.ofFn obj) + 1) ^ bE) ∧ (((t (Fin.last k)).output = some 0 ∧ 0 ≤ (fun i : Fin m => (t (Fin.last k)).data (0 + ↑i)) ∧ Aᵀ *ᵥ (fun i : Fin m => (t (Fin.last k)).data (0 + ↑i)) = 0 ∧ b ⬝ᵥ (fun i : Fin m => (t (Fin.last k)).data (0 + ↑i)) < 0 ∧ (Finset.univ.filter fun i : Fin m => (t (Fin.last k)).data (0 + ↑i) ≠ 0).card ≤ A.rank + 1) ∨ ((t (Fin.last k)).output = some 1 ∧ A *ᵥ (fun i : Fin d => (t (Fin.last k)).data (0 + ↑i)) ≤ b ∧ 0 ≤ (fun i : Fin m => (t (Fin.last k)).data (d + ↑i)) ∧ Aᵀ *ᵥ (fun i : Fin m => (t (Fin.last k)).data (d + ↑i)) = obj ∧ obj ⬝ᵥ (fun i : Fin d => (t (Fin.last k)).data (0 + ↑i)) = (t (Fin.last k)).data (d + m) ∧ b ⬝ᵥ (fun i : Fin m => (t (Fin.last k)).data (d + ↑i)) = (t (Fin.last k)).data (d + m) ∧ ∀ z : Fin d → ℚ, A *ᵥ z ≤ b → obj ⬝ᵥ z ≤ (t (Fin.last k)).data (d + m)) ∨ ((t (Fin.last k)).output = some 2 ∧ A *ᵥ (fun i : Fin d => (t (Fin.last k)).data (0 + ↑i)) ≤ b ∧ A *ᵥ (fun i : Fin d => (t (Fin.last k)).data (d + ↑i)) ≤ 0 ∧ obj ⬝ᵥ (fun i : Fin d => (t (Fin.last k)).data (d + ↑i)) = 1))) := strongly_polynomial_optimization end MobiusOtter -- Readable form of my two restatements. #check @MobiusOtter.feasibility_in_plain_words #check @MobiusOtter.optimization_in_plain_words -- Axioms of my statements and of the two theorems of the claimant. #print axioms OAI.Optimization.LinearProgramming.strongly_polynomial_feasibility #print axioms OAI.Optimization.LinearProgramming.strongly_polynomial_optimization #print axioms MobiusOtter.certificate_excludes_feasible_point #print axioms MobiusOtter.feasible_point_or_certificate #print axioms MobiusOtter.optimization_three_answers #print axioms MobiusOtter.ray_gives_large_values #print axioms MobiusOtter.duality_of_feasible_bounded #print axioms MobiusOtter.step_address_bound #print axioms MobiusOtter.address_bound_of_steps #print axioms MobiusOtter.start_address_le #print axioms MobiusOtter.address_registers_stay_small #print axioms MobiusOtter.address_register_bits #print axioms MobiusOtter.feasibility_explicit #print axioms MobiusOtter.lpInput_is #print axioms MobiusOtter.ratBits_is #print axioms MobiusOtter.feasibility_in_plain_words #print axioms MobiusOtter.optimization_in_plain_words