import Mathlib import OAI.Combinatorics.MatchingCount.Graphs.RatWeight namespace OAI noncomputable section namespace MatchingFPRAS.RealStage open Finset variable {n : ℕ} lemma scalingTrace_invariant (hn : 2 ≤ n) (G : Fin n → Fin n → ℝ) (hsym : ∀ i j, G i j=G j i) (hG : ∀ i j, i≠j → alpha ≤ G i j ∧ G i j ≤ 3) (k : ℕ) : ScaleInvariant G (scalingTrace hn G k) ∧ ∀ i, i.val constructor · constructor · intro i; norm_num [scalingTrace,alpha] · intro i; simp [scalingTrace] · intro i j h; have := (hG i j h).2; simp only [scalingTrace]; linarith · intro i hi; omega | succ k ih => by_cases hk : k (hG i j h).1) ih.1 i have htrace : scalingTrace hn G (k+1)=scaleUpdate hn G (scalingTrace hn G k) i := by simp [scalingTrace,hk,i] rw [htrace] refine ⟨hinv,?_⟩ intro a ha by_cases hak : a.val fun h => ?_⟩ · intro k i by_cases hk : k ⟨h i,(htrace n).1.upper i⟩,(htrace n).1.feasible,?_⟩ intro i exact (htrace n).2 i i.isLt def rowPeak {n : ℕ} (hn : 2 ≤ n) (g : Fin n → Fin n → ℝ) (i : Fin n) : ℝ := (univ.erase i).sup' (others_nonempty hn i) (g i) def scaledHoleMatrix {n : ℕ} (g : Fin n → Fin n → ℝ) (v : Fin n → ℝ) (i j : Fin n) : ℝ := g i j/(v i*v j) def BalancedRows {n : ℕ} (hn : 2 ≤ n) (g : Fin n → Fin n → ℝ) : Prop := ∀ i, (1:ℝ)/4 ≤ rowPeak hn g i ∧ rowPeak hn g i ≤ 1 theorem one_pass_restores_balance {n : ℕ} (hn : 2 ≤ n) (g G : Fin n → Fin n → ℝ) (hGsym : ∀ i j, G i j=G j i) (hG : ∀ i j, i≠j → alpha ≤ G i j ∧ G i j ≤ 3) (hpre : ∀ i, (1:ℝ)/8 ≤ rowPeak hn g i) (haccuracy : ∀ i j, i≠j → g i j ≤ G i j ∧ G i j ≤ g i j+2*alpha) : (∀ i, (1:ℝ)/16 ≤ scalingTrace hn G n i) ∧ BalancedRows hn (scaledHoleMatrix g (scalingTrace hn G n)) := by let v := scalingTrace hn G n have hv := (scalingTrace_invariant hn G hGsym hG n).1 have hlow (i : Fin n) : (1:ℝ)/16 ≤ v i := by obtain ⟨j,hj,heq⟩ := Finset.exists_mem_eq_sup' (others_nonempty hn i) (g i) have hji := (mem_erase.mp hj).1 change rowPeak hn g i=g i j at heq have hgij : (1:ℝ)/8 ≤ g i j := by rw [← heq]; exact hpre i have hGi := (haccuracy i j hji.symm).1 have hfeas := hv.feasible i j hji.symm have hup := mul_le_mul_of_nonneg_left (hv.upper j) (hv.pos i).le dsimp [v] linarith refine ⟨hlow,?_⟩ intro i constructor · obtain ⟨j,hji,heq⟩ := (one_pass_scaling hn G hGsym hG).2.2.2 i have hprod : (1:ℝ)/256 ≤ v i*v j := by calc (1:ℝ)/256 = ((1:ℝ)/16)*((1:ℝ)/16) := by norm_num _ ≤ v i*v j := mul_le_mul (hlow i) (hlow j) (by norm_num) (hv.pos i).le have hacc := (haccuracy i j hji.symm).2 have hg : (1:ℝ)/4 ≤ scaledHoleMatrix g v i j := by apply (le_div_iff₀ (mul_pos (hv.pos i) (hv.pos j))).mpr change v i*v j=G i j at heq norm_num [alpha] at hacc nlinarith apply hg.trans exact Finset.le_sup' (scaledHoleMatrix g v i) (mem_erase.mpr ⟨hji,mem_univ _⟩) · apply Finset.sup'_le intro j hj have hji := (mem_erase.mp hj).1 apply (div_le_one (mul_pos (hv.pos i) (hv.pos j))).mpr exact ((haccuracy i j hji.symm).1).trans (hv.feasible i j hji.symm) def sigma (D K : ℕ) (epsilon : ℝ) : ℝ := min (alpha/(100*D)) (epsilon/(1000*K)) def ratioEstimate (abar dbar : ℝ) : ℝ := if 0 Switching.holeRatio w {i,j}) i= rowMax (by simpa using hn) w i := rfl structure StageData (n K T : ℕ) where hn : 2 ≤ n w : Fin n → Fin n → ℝ next : Fin n → Fin n → ℝ positive : ∀ i j,0 < w i j next_positive : ∀ i j,0 < next i j partition_positive : 0 < partition w ∅ lower : ∀ i,(1:ℝ)/4 ≤ rowMax (by simpa using hn) w i upper : ∀ i,rowMax (by simpa using hn) w i ≤ 1 cool_lower : ∀ i j,i≠j → (1+1/(n:ℝ))⁻¹*w i j ≤ next i j cool_upper : ∀ i j,i≠j → next i j ≤ w i j cap : ∀ i j,i≠j → edgeActivity w i j ≤ (4:ℝ)^K stages : 1 ≤ T epsilon : ℝ epsilon_positive : 0 < epsilon epsilon_lt_one : epsilon < 1 variable {K T : ℕ} structure EmpiricalData (c : StageData n K T) where abar : ℝ dbar : ℝ zbar : LogicalEdge (Fin n) → ℝ errorA : |abar-meanA c.w K| ≤ RealStage.sigma (sourceD (V:=Fin n) K) T c.epsilon errorD : |dbar-meanD c.w c.next K| ≤ RealStage.sigma (sourceD (V:=Fin n) K) T c.epsilon errorProbe : ∀ e,|zbar e-meanProbe c.w c.next K e| ≤ RealStage.sigma (sourceD (V:=Fin n) K) T c.epsilon omit [Nontrivial (Fin n)] in lemma StageData.cooling (c : StageData n K T) (R : Finset (Fin n)) : partition c.w R/2 ≤ partition c.next R ∧ partition c.next R ≤ partition c.w R := by apply partition_cooling (by simpa using c.hn) c.w c.next (fun i j => (c.positive i j).le) · simpa using c.cool_lower · exact c.cool_upper lemma StageData.mean_bounds (c : StageData n K T) : (1:ℝ)/2 ≤ meanA c.w K ∧ (1:ℝ)/4 ≤ meanD c.w c.next K ∧ meanD c.w c.next K/meanA c.w K=partition c.next ∅/partition c.w ∅ ∧ (1:ℝ)/2 ≤ meanD c.w c.next K/meanA c.w K ∧ meanD c.w c.next K/meanA c.w K ≤ 1 ∧ (∀ e,(sourceD (V:=Fin n) K:ℝ)*meanProbe c.w c.next K e/meanD c.w c.next K= Switching.holeRatio c.next {source e,target e}) := by have h := source_observable_means (V:=Fin n) (by simpa using c.hn) c.w c.next c.positive c.partition_positive c.lower c.upper (by simpa using c.cool_lower) c.cool_upper K c.cap unfold meanA meanD meanProbe exact h.2.2 omit [Nontrivial (Fin n)] in lemma StageData.hole_upper (c : StageData n K T) (i j : Fin n) (hij : i≠j) : Switching.holeRatio c.next {i,j} ≤ 2 := by have h := (holeRatio_cooling c.w c.next (fun i j => (c.positive i j).le) c.partition_positive c.cooling {i,j}).2 have hr := (holeRatio_le_rowMax (by simpa using c.hn) c.w i j hij).trans (c.upper i) linarith theorem actual_stage_statistics (c : StageData n K T) (b : EmpiricalData c) (i j : Fin n) (hij : i≠j) : let G := estimateMatrix (sourceD (V:=Fin n) K) b.dbar b.zbar 0 < b.abar ∧ 0 < b.dbar ∧ |RealStage.ratioEstimate b.abar b.dbar/(partition c.next ∅/partition c.w ∅)-1| ≤ c.epsilon/(100*T) ∧ RealStage.alpha ≤ G i j ∧ G i j ≤ 3 ∧ Switching.holeRatio c.next {i,j} ≤ G i j ∧ G i j ≤ Switching.holeRatio c.next {i,j}+2*RealStage.alpha := by dsimp only have hm := c.mean_bounds have hem := hm.2.2.2.2.2 (pairEdge i j hij) rw [pairEdge_holes] at hem have hg0 := holeRatio_nonneg c.next (fun x y => (c.next_positive x y).le) {i,j} have h := RealStage.successful_stage_estimates (sourceD (V:=Fin n) K) T (sourceD_one K) c.stages c.epsilon c.epsilon_positive c.epsilon_lt_one (meanA c.w K) (meanD c.w c.next K) (meanProbe c.w c.next K (pairEdge i j hij)) b.abar b.dbar (b.zbar (pairEdge i j hij)) hm.1 hm.2.1 ⟨hm.2.2.2.1,hm.2.2.2.2.1⟩ ⟨by rw [hem]; exact hg0,by rw [hem]; exact c.hole_upper i j hij⟩ b.errorA b.errorD (b.errorProbe _) refine ⟨h.1,h.2.1,?_,?_⟩ · have he := h.2.2.1.trans h.2.2.2.1 rwa [hm.2.2.1] at he · simpa only [estimateMatrix,probeMatrix_off _ _ _ hij,hem] using h.2.2.2.2 theorem actual_successful_stage (c : StageData n K T) (b : EmpiricalData c) : let G := estimateMatrix (sourceD (V:=Fin n) K) b.dbar b.zbar let v := RealStage.scalingTrace c.hn G n (∀ i,(1:ℝ)/16 ≤ v i ∧ v i ≤ 2) ∧ (∀ i,(1:ℝ)/4 ≤ rowMax (by simpa using c.hn) (fun x y => c.next x y*v x*v y) i ∧ rowMax (by simpa using c.hn) (fun x y => c.next x y*v x*v y) i ≤ 1) ∧ |RealStage.ratioEstimate b.abar b.dbar/(partition c.next ∅/partition c.w ∅)-1| ≤ c.epsilon/(100*T) := by dsimp only let G := estimateMatrix (sourceD (V:=Fin n) K) b.dbar b.zbar let g := fun i j => Switching.holeRatio c.next {i,j} have hest := actual_stage_statistics c b have hGs := estimateMatrix_symm (sourceD (V:=Fin n) K) b.dbar b.zbar have hGb : ∀ i j,i≠j → RealStage.alpha ≤ G i j ∧ G i j ≤ 3 := by intro i j hij exact ⟨(hest i j hij).2.2.2.1,(hest i j hij).2.2.2.2.1⟩ have hGa : ∀ i j,i≠j → g i j ≤ G i j ∧ G i j ≤ g i j+2*RealStage.alpha := by intro i j hij exact (hest i j hij).2.2.2.2.2 have hpre : ∀ i,(1:ℝ)/8 ≤ RealStage.rowPeak c.hn g i := by intro i rw [show g=(fun i j => Switching.holeRatio c.next {i,j}) from rfl,rowPeak_holeMatrix] exact rowMax_cooling_lower (by simpa using c.hn) c.w c.next (fun x y => (c.positive x y).le) c.partition_positive c.lower c.cooling i have hb := RealStage.one_pass_restores_balance c.hn g G hGs hGb hpre hGa let v := RealStage.scalingTrace c.hn G n have hv := (RealStage.scalingTrace_invariant c.hn G hGs hGb n).1 have heq (i j : Fin n) (hij : i≠j) : Switching.holeRatio (fun x y => c.next x y*v x*v y) {i,j}=RealStage.scaledHoleMatrix g v i j := by rw [holeRatio_vertex_scaling c.next v (fun x => hv.pos x),Finset.prod_pair hij] rfl have hb' (i : Fin n) : (1:ℝ)/4 ≤ rowMax (by simpa using c.hn) (fun x y => c.next x y*v x*v y) i ∧ rowMax (by simpa using c.hn) (fun x y => c.next x y*v x*v y) i ≤ 1 := by have he : rowMax (by simpa using c.hn) (fun x y => c.next x y*v x*v y) i= RealStage.rowPeak c.hn (RealStage.scaledHoleMatrix g v) i := by unfold rowMax RealStage.rowPeak apply Finset.sup'_congr _ rfl intro j hj exact heq i j (Finset.mem_erase.mp hj).1.symm rw [he] exact hb.2 i refine ⟨fun i => ⟨hb.1 i,hv.upper i⟩,hb',?_⟩ let i : Fin n := ⟨0,by have := c.hn; omega⟩ let j : Fin n := ⟨1,by have := c.hn; omega⟩ have hij : i≠j := by intro h; have := Fin.val_eq_of_eq h; dsimp [i,j] at this; omega exact (hest i j hij).2.2.1 end MatchingFPRAS.Enlargement end OAI