Lean / Reader
Lean reader

ExamQueue.lean

236 lines21 declarationsno sorryimports Mathlib
  1. import Mathlib
  2. /-! The finite-horizon mean of the fair, simultaneous exam queue and a terminating
  3. finite recursion for its exact mean. Cells are indexed (person, exam), starting at zero. -/
  4. noncomputable section
  5. open Finset Filter Set
  6. open scoped BigOperators Topology
  7. namespace ExamQueue
  8. abbrev Cell (n : ℕ) := Fin n × Fin n
  9. /-- An outstanding exam is available when all its prerequisites have been completed.
  10. The two indices give the queue order and the exam order. -/
  11. def available {n : ℕ} (s : Finset (Cell n)) : Finset (Cell n) :=
  12. s.filter (fun x ↦ ∀ y ∈ s, y.1 ≤ x.1 → y.2 ≤ x.2 → y = x)
  13. /-- Nonempty sets of simultaneous successful exams. -/
  14. def moves {n : ℕ} (s : Finset (Cell n)) : Finset (Finset (Cell n)) :=
  15. (available s).powerset.erase ∅
  16. def numberChoices {n : ℕ} (s : Finset (Cell n)) : ℝ := 2^(available s).card
  17. /-- Mean rounds consumed up to a finite horizon. Every currently available
  18. exam passes independently with probability one half. -/
  19. def horizon {n : ℕ} : ℕ → Finset (Cell n) → ℝ
  20. | 0, _ => 0
  21. | t+1, s => if s.Nonempty then
  22. 1 + (horizon t s + ∑ b ∈ moves s, horizon t (s \ b)) / numberChoices s
  23. else 0
  24. /-- Removing empty rounds leaves at most one level per outstanding exam. -/
  25. def finiteSum {n : ℕ} : ℕ → Finset (Cell n) → ℝ
  26. | 0, _ => 0
  27. | t+1, s => if s.Nonempty then
  28. (numberChoices s + ∑ b ∈ moves s, finiteSum t (s \ b)) / (numberChoices s - 1)
  29. else 0
  30. /-- The finite answer for the current outstanding set. -/
  31. def finiteMean {n : ℕ} (s : Finset (Cell n)) : ℝ := finiteSum s.card s
  32. /-- Probability of still having a required exam after t simultaneous rounds.
  33. All subsets of the current set of eligible exams are equally likely, including
  34. the empty subset. -/
  35. def survival {n : ℕ} : ℕ → Finset (Cell n) → ℝ
  36. | 0, s => if s.Nonempty then 1 else 0
  37. | t+1, s => if s.Nonempty then
  38. (∑ b ∈ (available s).powerset, survival t (s \ b)) / numberChoices s
  39. else 0
  40. -- Queue lemmas
  41. private theorem available_nonempty {n : ℕ} {s : Finset (Cell n)} (hs : s.Nonempty) :
  42. (available s).Nonempty := by
  43. obtain ⟨x,hx,hmin⟩ := s.exists_min_image (fun x ↦ x.1.val+x.2.val) hs
  44. refine ⟨x, mem_filter.mpr ⟨hx, ?_⟩⟩
  45. intro y hy h₁ h₂
  46. have h := hmin y hy
  47. apply Prod.ext <;> apply Fin.ext
  48. · change y.1.val ≤ x.1.val at h₁
  49. change y.2.val ≤ x.2.val at h₂
  50. omega
  51. · change y.1.val ≤ x.1.val at h₁
  52. change y.2.val ≤ x.2.val at h₂
  53. omega
  54. private theorem choices_gt_one {n : ℕ} {s : Finset (Cell n)} (hs : s.Nonempty) :
  55. 1 < numberChoices s :=
  56. one_lt_pow₀ (by norm_num) (Finset.card_pos.mpr (available_nonempty hs)).ne'
  57. private theorem smaller {n : ℕ} (s : Finset (Cell n)) (b : Finset (Cell n))
  58. (hb : b ∈ moves s) : (s \ b).card < s.card := by
  59. have H := mem_erase.mp hb
  60. have sub : b ⊆ s := (mem_powerset.mp H.2).trans (filter_subset _ _)
  61. have hpos : 0 < b.card := Finset.card_pos.mpr (Finset.nonempty_iff_ne_empty.mpr H.1)
  62. have heq := card_sdiff_add_card_eq_card sub
  63. omega
  64. private theorem stable {n : ℕ} (s : Finset (Cell n)) (j k : ℕ)
  65. (hj : s.card ≤ j) (hk : s.card ≤ k) : finiteSum j s = finiteSum k s := by
  66. induction s using (measure (fun s : Finset (Cell n) ↦ s.card)).wf.induction generalizing j k with
  67. | h s ih =>
  68. by_cases hs : s.Nonempty
  69. · have hpos := Finset.card_pos.mpr hs
  70. cases j with
  71. | zero => omega
  72. | succ j =>
  73. cases k with
  74. | zero => omega
  75. | succ k =>
  76. simp only [finiteSum, if_pos hs]
  77. congr 2
  78. apply sum_congr rfl
  79. intro b hb
  80. have hlt := smaller s b hb
  81. exact ih _ hlt j k (by omega) (by omega)
  82. · cases j <;> cases k <;> simp [finiteSum, hs]
  83. private theorem mean_rule {n : ℕ} (s : Finset (Cell n)) :
  84. finiteMean s = if s.Nonempty then
  85. (numberChoices s + ∑ b ∈ moves s, finiteMean (s \ b)) / (numberChoices s-1) else 0 := by
  86. have hstable := stable s s.card (s.card+1) le_rfl (by omega)
  87. rw [finiteMean, hstable, finiteSum]
  88. split_ifs with hs
  89. · congr 2
  90. apply sum_congr rfl
  91. intro b hb
  92. exact stable _ _ _ (smaller s b hb).le le_rfl
  93. · rfl
  94. private theorem mean_nonneg {n : ℕ} (s : Finset (Cell n)) : 0 ≤ finiteMean s := by
  95. induction s using (measure (fun s : Finset (Cell n) ↦ s.card)).wf.induction with
  96. | h s ih =>
  97. rw [mean_rule]
  98. split_ifs with hs
  99. · have hc := choices_gt_one hs
  100. apply div_nonneg _ (by linarith)
  101. apply add_nonneg (by linarith)
  102. exact Finset.sum_nonneg fun b hb ↦ ih _ (smaller s b hb)
  103. · exact le_rfl
  104. private theorem bellman {n : ℕ} (s : Finset (Cell n)) (hs : s.Nonempty) :
  105. finiteMean s = 1 + (finiteMean s + ∑ b ∈ moves s, finiteMean (s \ b)) / numberChoices s := by
  106. have hm := mean_rule s
  107. rw [if_pos hs] at hm
  108. have hc := choices_gt_one hs
  109. have eq := (eq_div_iff (by linarith : numberChoices s - 1 ≠ 0)).mp hm
  110. field_simp
  111. nlinarith
  112. private theorem horizon_le_mean {n : ℕ} (t : ℕ) (s : Finset (Cell n)) :
  113. horizon t s ≤ finiteMean s := by
  114. induction t generalizing s with
  115. | zero => exact mean_nonneg s
  116. | succ t ih =>
  117. rw [horizon]
  118. split_ifs with hs
  119. · conv_rhs => rw [bellman s hs]
  120. gcongr
  121. · exact (choices_gt_one hs).le.trans' zero_le_one
  122. · exact ih s
  123. · exact ih _
  124. · exact mean_nonneg s
  125. private theorem horizon_mono {n : ℕ} (s : Finset (Cell n)) : Monotone (fun t ↦ horizon t s) := by
  126. apply monotone_nat_of_le_succ
  127. intro t
  128. induction t generalizing s with
  129. | zero =>
  130. rw [horizon, horizon]
  131. split_ifs <;> simp [horizon]
  132. | succ t ih =>
  133. conv_lhs => rw [horizon]
  134. conv_rhs => rw [horizon]
  135. split_ifs with hs
  136. · gcongr
  137. · exact (choices_gt_one hs).le.trans' zero_le_one
  138. · exact ih s
  139. · exact ih _
  140. · exact le_rfl
  141. /-- The actual expected number of rounds (the increasing limit of the expected
  142. truncations) equals a finite expression that uses strictly smaller states. -/
  143. theorem finite_expected_rounds {n : ℕ} (s : Finset (Cell n)) :
  144. Tendsto (fun t ↦ horizon t s) atTop (𝓝 (finiteMean s)) := by
  145. let L (a : Finset (Cell n)) := ⨆ t, horizon t a
  146. have lim (a : Finset (Cell n)) : Tendsto (fun t ↦ horizon t a) atTop (𝓝 (L a)) :=
  147. tendsto_atTop_ciSup (horizon_mono a) ⟨finiteMean a, by
  148. rintro _ ⟨t,rfl⟩
  149. exact horizon_le_mean t a⟩
  150. have eqall (a : Finset (Cell n)) : L a = finiteMean a := by
  151. induction a using (measure (fun s : Finset (Cell n) ↦ s.card)).wf.induction with
  152. | h a ih =>
  153. by_cases ha : a.Nonempty
  154. · have recLimit : L a = 1 + (L a + ∑ b ∈ moves a, L (a \ b)) / numberChoices a := by
  155. apply tendsto_nhds_unique ((lim a).comp (tendsto_add_atTop_nat 1))
  156. have S := tendsto_finsetSum (moves a) (fun b _ ↦ lim (a \ b))
  157. simpa only [Function.comp_def, horizon, if_pos ha] using
  158. (tendsto_const_nhds.add (((lim a).add S).div_const (numberChoices a)))
  159. have S : (∑ b ∈ moves a, L (a \ b)) = ∑ b ∈ moves a, finiteMean (a \ b) :=
  160. sum_congr rfl (fun b hb ↦ ih _ (smaller a b hb))
  161. rw [S] at recLimit
  162. rw [mean_rule, if_pos ha]
  163. have hc := choices_gt_one ha
  164. apply (eq_div_iff (by linarith)).2
  165. field_simp at recLimit
  166. nlinarith
  167. · have hz : ∀ t, horizon t a = 0 := by intro t; cases t <;> simp [horizon, ha]
  168. rw [mean_rule, if_neg ha]
  169. apply tendsto_nhds_unique (lim a)
  170. simpa only [hz] using (tendsto_const_nhds : Tendsto (fun _ : ℕ ↦ (0 : ℝ)) atTop (𝓝 0))
  171. rw [← eqall s]
  172. exact lim s
  173. private theorem survival_nonneg {n : ℕ} (t : ℕ) (s : Finset (Cell n)) : 0 ≤ survival t s := by
  174. induction t generalizing s with
  175. | zero => simp [survival]; split_ifs <;> norm_num
  176. | succ t ih =>
  177. rw [survival]
  178. split_ifs with hs
  179. · exact div_nonneg (sum_nonneg fun _ _ ↦ ih _) ((choices_gt_one hs).le.trans' zero_le_one)
  180. · exact le_rfl
  181. private theorem horizon_succ_eq {n : ℕ} (t : ℕ) (s : Finset (Cell n)) :
  182. horizon (t+1) s = horizon t s + survival t s := by
  183. induction t generalizing s with
  184. | zero => by_cases hs : s.Nonempty <;> simp [horizon, survival, hs]
  185. | succ t ih =>
  186. conv_lhs => rw [horizon]
  187. rw [survival]
  188. by_cases hs : s.Nonempty
  189. · rw [if_pos hs]
  190. have E : survival t s + ∑ b ∈ moves s, survival t (s \ b) =
  191. ∑ b ∈ (available s).powerset, survival t (s \ b) := by
  192. simp [moves]
  193. rw [← E, if_pos hs]
  194. change 1 + (horizon (t+1) s + ∑ b ∈ moves s, horizon (t+1) (s \ b)) / numberChoices s =
  195. horizon (t+1) s + (survival t s + ∑ b ∈ moves s, survival t (s \ b)) / numberChoices s
  196. conv_lhs => simp only [ih, sum_add_distrib]
  197. conv_rhs => rw [horizon, if_pos hs]
  198. ring
  199. · simp [horizon, hs]
  200. /-- Summing the original probabilities of noncompletion gives the finite answer. -/
  201. theorem expected_rounds {n : ℕ} (s : Finset (Cell n)) :
  202. HasSum (fun t ↦ survival t s) (finiteMean s) := by
  203. apply (hasSum_iff_tendsto_nat_of_nonneg (fun t ↦ survival_nonneg t s) _).2
  204. have e (m : ℕ) : (∑ t ∈ range m, survival t s) = horizon m s := by
  205. induction m with
  206. | zero => simp [horizon]
  207. | succ m ih => rw [sum_range_succ, ih, horizon_succ_eq]
  208. simpa only [e] using finite_expected_rounds s
  209. #print axioms expected_rounds
  210. end ExamQueue