import Mathlib import OAI.Analysis.HardSphere.Comparison.ParameterTarget import OAI.Analysis.HardSphere.Allocation.CoarsePhysical import OAI.Analysis.HardSphere.Allocation.PhysicalTargets namespace OAI section noncomputable section open MeasureTheory ProbabilityTheory Set open scoped ENNReal NNReal Topology namespace HardSphere.Bridge open Wiener GaussianPacking Comparison lemma exposedPeriodPair_at {E : Type*} [MeasurableSpace E] {L : Length} (Q : Kernel E Point) [IsFiniteKernel Q] {t C : ℝ} (ht : 0 linarith lemma unitTarget_sub (v c U : Point) : U-c∈unitTarget v ↔ U∈unitTarget (v+c) := by simp only [unitTarget,mem_pi,mem_univ,forall_const,mem_Ico,Pi.sub_apply,Pi.add_apply] constructor <;> intro h j <;> specialize h j <;> constructor <;> linarith [h.1,h.2] def routeCellPeriod {K : ℕ} (v w : Fin 3→Fin K) (j : Fin 7) : Point := anchorPoint v-(anchorPoint w+targetStep j) lemma routeCellPeriod_chart {K : ℕ} (v w : Fin 3→Fin K) (j : Fin 7) : anchorPoint w+targetStep j+routeCellPeriod v w j=anchorPoint v := by unfold routeCellPeriod abel def vertexTarget {N K M G : ℕ} {L : Length} (hL : L.val=(K:ℝ)) (g : Fin G) (m : ℕ) (t r R C S : ℝ) (u : Time) (i i' : Fin N) (v w : Fin 3→Fin K) (j : Fin 7) : Set (PhysicalDatum N K M G L×Point) := (univ×ˢunitTarget (anchorPoint v))∩ (datumTarget hL g m t r R C S u i 0 ∩ (fun p ↦ (p.1,p.2-routeCellPeriod v w j)) ⁻¹' datumTarget hL g m t r R C S u i' j) lemma vertexTarget_measurable {N K M G : ℕ} {L : Length} (hL : L.val=(K:ℝ)) (g : Fin G) (m : ℕ) (t r R C S : ℝ) (u : Time) (i i' : Fin N) (v w : Fin 3→Fin K) (j : Fin 7) : MeasurableSet (vertexTarget (M:=M) hL g m t r R C S u i i' v w j) := (MeasurableSet.univ.prod (unitTarget_measurable _)).inter ((datumTarget_measurable hL g m t r R C S u i 0).inter ((datumTarget_measurable hL g m t r R C S u i' j).preimage (measurable_fst.prodMk (measurable_snd.sub measurable_const)))) lemma vertexTarget_subset {N K M G : ℕ} {L : Length} (hL : L.val=(K:ℝ)) (g : Fin G) (m : ℕ) (t r R C S : ℝ) (u : Time) (i i' : Fin N) (v w : Fin 3→Fin K) (j : Fin 7) (d : PhysicalDatum N K M G L) : Prod.mk d ⁻¹' vertexTarget hL g m t r R C S u i i' v w j ⊆ unitTarget (anchorPoint v) := fun _ h ↦ h.1.2 lemma vertexTarget_volume {N K M G : ℕ} {L : Length} (hL : L.val=(K:ℝ)) (g : Fin G) (m : ℕ) (t r R C S : ℝ) (u : Time) (i i' : Fin N) (v w : Fin 3→Fin K) (j : Fin 7) (d : PhysicalDatum N K M G L) (hv : datumHome hL d i=v) (hw : datumHome hL d i'=w) (hi : datumAccessible hL g m t r R C S u i d) (hi' : datumAccessible hL g m t r R C S u i' d) : ENNReal.ofReal (49/50:ℝ)≤volume (Prod.mk d ⁻¹' vertexTarget hL g m t r R C S u i i' v w j) := by let B := unitTarget (anchorPoint v) let A := Prod.mk d ⁻¹' datumTarget hL g m t r R C S u i 0 let T := (fun U : Point ↦ U-routeCellPeriod v w j) ⁻¹' (Prod.mk d ⁻¹' datumTarget hL g m t r R C S u i' j) have hA : MeasurableSet A := (datumTarget_measurable hL g m t r R C S u i 0).preimage measurable_prodMk_left have hT : MeasurableSet T := ((datumTarget_measurable hL g m t r R C S u i' j).preimage measurable_prodMk_left).preimage (measurable_id.sub measurable_const) have hAB : A⊆B := by intro U hU have h := hU.1 simpa [hv,targetStep,B] using h have hTB : T⊆B := by intro U hU have h := hU.1 rw [hw,unitTarget_sub,routeCellPeriod_chart] at h exact h have htvol : volume T=volume (Prod.mk d ⁻¹' datumTarget hL g m t r R C S u i' j) := by exact (measurePreserving_add_right volume (-routeCellPeriod v w j)).measure_preimage ((datumTarget_measurable hL g m t r R C S u i' j).preimage measurable_prodMk_left).nullMeasurableSet let : IsProbabilityMeasure (volume.restrict B) := ⟨by rw [Measure.restrict_apply_univ,unitTarget_volume]⟩ have ha : ENNReal.ofReal (99/100:ℝ)≤(volume.restrict B) A := by rw [Measure.restrict_apply hA,inter_eq_left.mpr hAB] exact hi 0 have ht : ENNReal.ofReal (99/100:ℝ)≤(volume.restrict B) T := by rw [Measure.restrict_apply hT,inter_eq_left.mpr hTB,htvol] exact hi' j have H := probability_inter_98 (volume.restrict B) hA hT ha ht rw [Measure.restrict_apply (hA.inter hT)] at H convert H using 1 congr 1 ext U simp only [vertexTarget,mem_preimage,mem_inter_iff,mem_prod,mem_univ,true_and,A,T,B] tauto def shiftedDatumPermitted {N K M G : ℕ} {L : Length} (hL : L.val=(K:ℝ)) (g : Fin G) (m : ℕ) (r R C S : ℝ) (u : Time) (i : Fin N) (c : Point) : Set (PhysicalDatum N K M G L×C(Time,Point)) := (fun p ↦ (p.1,Bridge.shiftPath (-c) p.2)) ⁻¹' datumPermitted hL g m r R C S u i lemma shiftedDatumPermitted_measurable {N K M G : ℕ} {L : Length} (hL : L.val=(K:ℝ)) (g : Fin G) (m : ℕ) (r R C S : ℝ) (u : Time) (i : Fin N) (c : Point) : MeasurableSet (shiftedDatumPermitted (M:=M) hL g m r R C S u i c) := (datumPermitted_measurable hL g m r R C S u i).preimage (measurable_fst.prodMk ((Bridge.shiftPath_continuous (-c)).measurable.comp measurable_snd)) lemma shiftedDatumPermitted_equiv {N K M G : ℕ} {L : Length} (hL : L.val=(K:ℝ)) (g : Fin G) (m : ℕ) (r R C S : ℝ) (u : Time) (i : Fin N) (c : Point) (d : PhysicalDatum N K M G L) (f : C(Time,Point)) : (d,Bridge.shiftPath c f)∈shiftedDatumPermitted hL g m r R C S u i c ↔ (d,f)∈datumPermitted hL g m r R C S u i := by simp only [shiftedDatumPermitted,mem_preimage,Bridge.shiftPath_cancel] lemma datumSuccess_shift {N K M G : ℕ} {L : Length} (hL : L.val=(K:ℝ)) (g : Fin G) (m : ℕ) (t r R C S : ℝ) (u : Time) (i : Fin N) (c : Point) (d : PhysicalDatum N K M G L) (U : Point) : trialKernel t (datumStart hL d i+c) (datumEnd hL d i+c) U (Prod.mk d ⁻¹' shiftedDatumPermitted hL g m r R C S u i c)= datumSuccess hL g m t r R C S u i (d,U-c) := by have H := trialKernel_shift t (datumStart hL d i) (datumEnd hL d i) (U-c) c rw [sub_add_cancel] at H rw [←H,Measure.map_apply (Bridge.shiftPath_continuous c).measurable ((shiftedDatumPermitted_measurable hL g m r R C S u i c).preimage measurable_prodMk_left)] congr 1 ext f exact shiftedDatumPermitted_equiv hL g m r R C S u i c d f def vertexPair {N K M G : ℕ} {L : Length} (hL : L.val=(K:ℝ)) (g : Fin G) (m : ℕ) (t r R C S : ℝ) (u : Time) (i i' : Fin N) (v w : Fin 3→Fin K) (j : Fin 7) : Kernel (PhysicalDatum N K M G L) (C(Time,Point)×C(Time,Point)) := exposedPeriodPair (parameterTargetKernel (B:=unitTarget (anchorPoint v)) (by rw [unitTarget_volume]; exact ENNReal.one_ne_top) (vertexTarget hL g m t r R C S u i i' v w j) (vertexTarget_measurable hL g m t r R C S u i i' v w j)) t (fun d ↦ datumStart hL d i) (fun d ↦ datumEnd hL d i) (fun d ↦ datumStart hL d i') (fun d ↦ datumEnd hL d i') (datumStart_measurable hL i) (datumEnd_measurable hL i) (datumStart_measurable hL i') (datumEnd_measurable hL i') (routeCellPeriod v w j) (datumPermitted hL g m r R C S u i) (shiftedDatumPermitted hL g m r R C S u i' (routeCellPeriod v w j)) (datumPermitted_measurable hL g m r R C S u i) (shiftedDatumPermitted_measurable hL g m r R C S u i' (routeCellPeriod v w j)) instance vertexPair_finite {N K M G : ℕ} {L : Length} (hL : L.val=(K:ℝ)) (g : Fin G) (m : ℕ) (t r R C S : ℝ) (u : Time) (i i' : Fin N) (v w : Fin 3→Fin K) (j : Fin 7) : IsFiniteKernel (vertexPair (M:=M) hL g m t r R C S u i i' v w j) := by unfold vertexPair exposedPeriodPair exposedPairedTrialKernel parameterPairedKernel infer_instance def vertexDensityBound (t : ℝ) : ℝ≥0∞ := (ENNReal.ofReal (9/10:ℝ))⁻¹*((ENNReal.ofReal (49/50:ℝ))⁻¹*(midpointLower t 13)⁻¹) theorem vertexPair_physical {N K M G : ℕ} {L : Length} (hL : L.val=(K:ℝ)) (g : Fin G) (m : ℕ) (t r R C S : ℝ) (ht : 0