import Mathlib/-! The finite-horizon mean of the fair, simultaneous exam queue and a terminatingfinite recursion for its exact mean. Cells are indexed (person, exam), starting at zero. -/noncomputable sectionopen Finset Filter Setopen scoped BigOperators Topologynamespace ExamQueueabbrev Cell (n : ℕ) := Fin n × Fin n/-- An outstanding exam is available when all its prerequisites have been completed.The two indices give the queue order and the exam order. -/def available {n : ℕ} (s : Finset (Cell n)) : Finset (Cell n) :=s.filter (fun x ↦ ∀ y ∈ s, y.1 ≤ x.1 → y.2 ≤ x.2 → y = x)/-- Nonempty sets of simultaneous successful exams. -/def moves {n : ℕ} (s : Finset (Cell n)) : Finset (Finset (Cell n)) :=(available s).powerset.erase ∅def numberChoices {n : ℕ} (s : Finset (Cell n)) : ℝ := 2^(available s).card/-- Mean rounds consumed up to a finite horizon. Every currently availableexam passes independently with probability one half. -/def horizon {n : ℕ} : ℕ → Finset (Cell n) → ℝ| 0, _ => 0| t+1, s => if s.Nonempty then1 + (horizon t s + ∑ b ∈ moves s, horizon t (s \ b)) / numberChoices selse 0/-- Removing empty rounds leaves at most one level per outstanding exam. -/def finiteSum {n : ℕ} : ℕ → Finset (Cell n) → ℝ| 0, _ => 0| t+1, s => if s.Nonempty then(numberChoices s + ∑ b ∈ moves s, finiteSum t (s \ b)) / (numberChoices s - 1)else 0/-- The finite answer for the current outstanding set. -/def finiteMean {n : ℕ} (s : Finset (Cell n)) : ℝ := finiteSum s.card s/-- Probability of still having a required exam after t simultaneous rounds.All subsets of the current set of eligible exams are equally likely, includingthe empty subset. -/def survival {n : ℕ} : ℕ → Finset (Cell n) → ℝ| 0, s => if s.Nonempty then 1 else 0| t+1, s => if s.Nonempty then(∑ b ∈ (available s).powerset, survival t (s \ b)) / numberChoices selse 0-- Queue lemmasprivate theorem available_nonempty {n : ℕ} {s : Finset (Cell n)} (hs : s.Nonempty) :(available s).Nonempty := byobtain ⟨x,hx,hmin⟩ := s.exists_min_image (fun x ↦ x.1.val+x.2.val) hsrefine ⟨x, mem_filter.mpr ⟨hx, ?_⟩⟩intro y hy h₁ h₂have h := hmin y hyapply Prod.ext <;> apply Fin.ext· change y.1.val ≤ x.1.val at h₁change y.2.val ≤ x.2.val at h₂omega· change y.1.val ≤ x.1.val at h₁change y.2.val ≤ x.2.val at h₂omegaprivate theorem choices_gt_one {n : ℕ} {s : Finset (Cell n)} (hs : s.Nonempty) :1 < numberChoices s :=one_lt_pow₀ (by norm_num) (Finset.card_pos.mpr (available_nonempty hs)).ne'private theorem smaller {n : ℕ} (s : Finset (Cell n)) (b : Finset (Cell n))(hb : b ∈ moves s) : (s \ b).card < s.card := byhave H := mem_erase.mp hbhave sub : b ⊆ s := (mem_powerset.mp H.2).trans (filter_subset _ _)have hpos : 0 < b.card := Finset.card_pos.mpr (Finset.nonempty_iff_ne_empty.mpr H.1)have heq := card_sdiff_add_card_eq_card subomegaprivate theorem stable {n : ℕ} (s : Finset (Cell n)) (j k : ℕ)(hj : s.card ≤ j) (hk : s.card ≤ k) : finiteSum j s = finiteSum k s := byinduction s using (measure (fun s : Finset (Cell n) ↦ s.card)).wf.induction generalizing j k with| h s ih =>by_cases hs : s.Nonempty· have hpos := Finset.card_pos.mpr hscases j with| zero => omega| succ j =>cases k with| zero => omega| succ k =>simp only [finiteSum, if_pos hs]congr 2apply sum_congr rflintro b hbhave hlt := smaller s b hbexact ih _ hlt j k (by omega) (by omega)· cases j <;> cases k <;> simp [finiteSum, hs]private theorem mean_rule {n : ℕ} (s : Finset (Cell n)) :finiteMean s = if s.Nonempty then(numberChoices s + ∑ b ∈ moves s, finiteMean (s \ b)) / (numberChoices s-1) else 0 := byhave hstable := stable s s.card (s.card+1) le_rfl (by omega)rw [finiteMean, hstable, finiteSum]split_ifs with hs· congr 2apply sum_congr rflintro b hbexact stable _ _ _ (smaller s b hb).le le_rfl· rflprivate theorem mean_nonneg {n : ℕ} (s : Finset (Cell n)) : 0 ≤ finiteMean s := byinduction s using (measure (fun s : Finset (Cell n) ↦ s.card)).wf.induction with| h s ih =>rw [mean_rule]split_ifs with hs· have hc := choices_gt_one hsapply div_nonneg _ (by linarith)apply add_nonneg (by linarith)exact Finset.sum_nonneg fun b hb ↦ ih _ (smaller s b hb)· exact le_rflprivate theorem bellman {n : ℕ} (s : Finset (Cell n)) (hs : s.Nonempty) :finiteMean s = 1 + (finiteMean s + ∑ b ∈ moves s, finiteMean (s \ b)) / numberChoices s := byhave hm := mean_rule srw [if_pos hs] at hmhave hc := choices_gt_one hshave eq := (eq_div_iff (by linarith : numberChoices s - 1 ≠ 0)).mp hmfield_simpnlinarithprivate theorem horizon_le_mean {n : ℕ} (t : ℕ) (s : Finset (Cell n)) :horizon t s ≤ finiteMean s := byinduction t generalizing s with| zero => exact mean_nonneg s| succ t ih =>rw [horizon]split_ifs with hs· conv_rhs => rw [bellman s hs]gcongr· exact (choices_gt_one hs).le.trans' zero_le_one· exact ih s· exact ih _· exact mean_nonneg sprivate theorem horizon_mono {n : ℕ} (s : Finset (Cell n)) : Monotone (fun t ↦ horizon t s) := byapply monotone_nat_of_le_succintro tinduction t generalizing s with| zero =>rw [horizon, horizon]split_ifs <;> simp [horizon]| succ t ih =>conv_lhs => rw [horizon]conv_rhs => rw [horizon]split_ifs with hs· gcongr· exact (choices_gt_one hs).le.trans' zero_le_one· exact ih s· exact ih _· exact le_rfl/-- The actual expected number of rounds (the increasing limit of the expectedtruncations) equals a finite expression that uses strictly smaller states. -/theorem finite_expected_rounds {n : ℕ} (s : Finset (Cell n)) :Tendsto (fun t ↦ horizon t s) atTop (𝓝 (finiteMean s)) := bylet L (a : Finset (Cell n)) := ⨆ t, horizon t ahave lim (a : Finset (Cell n)) : Tendsto (fun t ↦ horizon t a) atTop (𝓝 (L a)) :=tendsto_atTop_ciSup (horizon_mono a) ⟨finiteMean a, byrintro _ ⟨t,rfl⟩exact horizon_le_mean t a⟩have eqall (a : Finset (Cell n)) : L a = finiteMean a := byinduction a using (measure (fun s : Finset (Cell n) ↦ s.card)).wf.induction with| h a ih =>by_cases ha : a.Nonempty· have recLimit : L a = 1 + (L a + ∑ b ∈ moves a, L (a \ b)) / numberChoices a := byapply tendsto_nhds_unique ((lim a).comp (tendsto_add_atTop_nat 1))have S := tendsto_finsetSum (moves a) (fun b _ ↦ lim (a \ b))simpa only [Function.comp_def, horizon, if_pos ha] using(tendsto_const_nhds.add (((lim a).add S).div_const (numberChoices a)))have S : (∑ b ∈ moves a, L (a \ b)) = ∑ b ∈ moves a, finiteMean (a \ b) :=sum_congr rfl (fun b hb ↦ ih _ (smaller a b hb))rw [S] at recLimitrw [mean_rule, if_pos ha]have hc := choices_gt_one haapply (eq_div_iff (by linarith)).2field_simp at recLimitnlinarith· have hz : ∀ t, horizon t a = 0 := by intro t; cases t <;> simp [horizon, ha]rw [mean_rule, if_neg ha]apply tendsto_nhds_unique (lim a)simpa only [hz] using (tendsto_const_nhds : Tendsto (fun _ : ℕ ↦ (0 : ℝ)) atTop (𝓝 0))rw [← eqall s]exact lim sprivate theorem survival_nonneg {n : ℕ} (t : ℕ) (s : Finset (Cell n)) : 0 ≤ survival t s := byinduction t generalizing s with| zero => simp [survival]; split_ifs <;> norm_num| succ t ih =>rw [survival]split_ifs with hs· exact div_nonneg (sum_nonneg fun _ _ ↦ ih _) ((choices_gt_one hs).le.trans' zero_le_one)· exact le_rflprivate theorem horizon_succ_eq {n : ℕ} (t : ℕ) (s : Finset (Cell n)) :horizon (t+1) s = horizon t s + survival t s := byinduction t generalizing s with| zero => by_cases hs : s.Nonempty <;> simp [horizon, survival, hs]| succ t ih =>conv_lhs => rw [horizon]rw [survival]by_cases hs : s.Nonempty· rw [if_pos hs]have E : survival t s + ∑ b ∈ moves s, survival t (s \ b) =∑ b ∈ (available s).powerset, survival t (s \ b) := bysimp [moves]rw [← E, if_pos hs]change 1 + (horizon (t+1) s + ∑ b ∈ moves s, horizon (t+1) (s \ b)) / numberChoices s =horizon (t+1) s + (survival t s + ∑ b ∈ moves s, survival t (s \ b)) / numberChoices sconv_lhs => simp only [ih, sum_add_distrib]conv_rhs => rw [horizon, if_pos hs]ring· simp [horizon, hs]/-- Summing the original probabilities of noncompletion gives the finite answer. -/theorem expected_rounds {n : ℕ} (s : Finset (Cell n)) :HasSum (fun t ↦ survival t s) (finiteMean s) := byapply (hasSum_iff_tendsto_nat_of_nonneg (fun t ↦ survival_nonneg t s) _).2have e (m : ℕ) : (∑ t ∈ range m, survival t s) = horizon m s := byinduction m with| zero => simp [horizon]| succ m ih => rw [sum_range_succ, ih, horizon_succ_eq]simpa only [e] using finite_expected_rounds s#print axioms expected_roundsend ExamQueue