import Mathlib /-! The finite-horizon mean of the fair, simultaneous exam queue and a terminating finite recursion for its exact mean. Cells are indexed (person, exam), starting at zero. -/ noncomputable section open Finset Filter Set open scoped BigOperators Topology namespace ExamQueue abbrev 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 available exam passes independently with probability one half. -/ def horizon {n : ℕ} : ℕ → Finset (Cell n) → ℝ | 0, _ => 0 | t+1, s => if s.Nonempty then 1 + (horizon t s + ∑ b ∈ moves s, horizon t (s \ b)) / numberChoices s else 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, including the 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 s else 0 -- Queue lemmas private theorem available_nonempty {n : ℕ} {s : Finset (Cell n)} (hs : s.Nonempty) : (available s).Nonempty := by obtain ⟨x,hx,hmin⟩ := s.exists_min_image (fun x ↦ x.1.val+x.2.val) hs refine ⟨x, mem_filter.mpr ⟨hx, ?_⟩⟩ intro y hy h₁ h₂ have h := hmin y hy apply 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₂ omega private 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 := by have H := mem_erase.mp hb have 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 sub omega private theorem stable {n : ℕ} (s : Finset (Cell n)) (j k : ℕ) (hj : s.card ≤ j) (hk : s.card ≤ k) : finiteSum j s = finiteSum k s := by induction 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 hs cases j with | zero => omega | succ j => cases k with | zero => omega | succ k => simp only [finiteSum, if_pos hs] congr 2 apply sum_congr rfl intro b hb have hlt := smaller s b hb exact 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 := by have hstable := stable s s.card (s.card+1) le_rfl (by omega) rw [finiteMean, hstable, finiteSum] split_ifs with hs · congr 2 apply sum_congr rfl intro b hb exact stable _ _ _ (smaller s b hb).le le_rfl · rfl private theorem mean_nonneg {n : ℕ} (s : Finset (Cell n)) : 0 ≤ finiteMean s := by induction 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 hs apply div_nonneg _ (by linarith) apply add_nonneg (by linarith) exact Finset.sum_nonneg fun b hb ↦ ih _ (smaller s b hb) · exact le_rfl private theorem bellman {n : ℕ} (s : Finset (Cell n)) (hs : s.Nonempty) : finiteMean s = 1 + (finiteMean s + ∑ b ∈ moves s, finiteMean (s \ b)) / numberChoices s := by have hm := mean_rule s rw [if_pos hs] at hm have hc := choices_gt_one hs have eq := (eq_div_iff (by linarith : numberChoices s - 1 ≠ 0)).mp hm field_simp nlinarith private theorem horizon_le_mean {n : ℕ} (t : ℕ) (s : Finset (Cell n)) : horizon t s ≤ finiteMean s := by induction 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 s private theorem horizon_mono {n : ℕ} (s : Finset (Cell n)) : Monotone (fun t ↦ horizon t s) := by apply monotone_nat_of_le_succ intro t induction 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 expected truncations) 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)) := by let L (a : Finset (Cell n)) := ⨆ t, horizon t a have lim (a : Finset (Cell n)) : Tendsto (fun t ↦ horizon t a) atTop (𝓝 (L a)) := tendsto_atTop_ciSup (horizon_mono a) ⟨finiteMean a, by rintro _ ⟨t,rfl⟩ exact horizon_le_mean t a⟩ have eqall (a : Finset (Cell n)) : L a = finiteMean a := by induction 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 := by apply 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 recLimit rw [mean_rule, if_pos ha] have hc := choices_gt_one ha apply (eq_div_iff (by linarith)).2 field_simp at recLimit nlinarith · 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 s private theorem survival_nonneg {n : ℕ} (t : ℕ) (s : Finset (Cell n)) : 0 ≤ survival t s := by induction 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_rfl private theorem horizon_succ_eq {n : ℕ} (t : ℕ) (s : Finset (Cell n)) : horizon (t+1) s = horizon t s + survival t s := by induction 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) := by simp [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 s conv_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) := by apply (hasSum_iff_tendsto_nat_of_nonneg (fun t ↦ survival_nonneg t s) _).2 have e (m : ℕ) : (∑ t ∈ range m, survival t s) = horizon m s := by induction 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_rounds end ExamQueue