introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
3.18 kB
import Mathlib
import OAI.Combinatorics.MatchingCount.Sampling.TreeDPProgram
namespace OAI
namespace MatchingFPRAS.ExecutableDP
open TreeTyped
def initial (x : β„•Γ—β„•) : Table :=
tabulate (fun ar : β„•Γ—(β„•Γ—β„•)=>UnitRefresh.pairingCount ar.2.2) (0,x)
def initialRealizer : Realizer initial :=
composition (pair (constant 0) identity)
(tabulateRealizer (composition second.snd pairingNumber))
lemma initial_eq (m B : β„•) : initial (m,B)=UnitRefresh.Materialized.initial m B := by
exact tabulate_materialized (fun x : β„•Γ—(β„•Γ—β„•)=>UnitRefresh.pairingCount x.2.2) 0 m B
def finish (x : TableΓ—β„•) : β„• := ((List.range x.2).map fun a=>read (x.1,(a,0))).prod
def finishRealizer : Realizer finish :=
composition (composition (pair first (composition second range))
(mapWith (composition (pair first (pair second (constant 0))) readRealizer))) listProd
lemma map_range_ofFn {A : Type} (f : β„• β†’ A) (n : β„•) :
(List.range n).map f=List.ofFn (fun i : Fin n=>f i.val) := by
apply List.ext_getElem
Β· simp
Β· intro i hi hi'; simp only [List.getElem_map,List.getElem_range,List.getElem_ofFn]
lemma finish_eq (t : Table) (m : β„•) :
finish (t,m)=∏ a : Fin m,UnitRefresh.Materialized.read t a.val 0 := by
rw [finish,map_range_ofFn,List.prod_ofFn]
rfl
abbrev CountInput := (BagsΓ—Parents)Γ—List β„•
def count (x : CountInput) : β„• :=
let n:=x.1.1.length
let m:=x.1.2.length
finish (run ((n,(m,x.1.2)),(x.2,(x.1.1,initial (m,n*m)))),m)
def countRealizer : Realizer count := by
let bs : Realizer (fun x : CountInput=>x.1.1) := first.fst
let ps : Realizer (fun x : CountInput=>x.1.2) := first.snd
let ls : Realizer (fun x : CountInput=>x.2) := second
let n := composition bs listLength
let m := composition ps listLength
let B := composition (pair n m) multiply
let t := composition (pair m B) initialRealizer
let p := pair n (pair m ps)
let table := composition (pair p (pair ls (pair bs t))) runRealizer
exact (composition (pair table m) finishRealizer).congr (fun _=>rfl)
lemma count_eq {V : Type} [Fintype V] [DecidableEq V] {m : β„•}
(bags : V β†’ Finset (Fin m)) (parent : Fin m β†’ Option (Fin m)) (ls : List (Fin m)) :
count ((activeRep bags Finset.univ,parentRep parent),ls.map Fin.val)=
UnitRefresh.Materialized.count bags parent ls := by
unfold count
rw [show (activeRep bags Finset.univ).length=Fintype.card V by simp [activeRep]]
rw [parentRep_length]
dsimp only
rw [initial_eq]
unfold run
rw [run_eq bags parent ls Finset.univ _ _ (List.Perm.refl _),finish_eq]
rfl
theorem count_coloredPairings {V : Type} [Fintype V] [DecidableEq V] {m : β„•}
(bags : V β†’ Finset (Fin m)) (rank : Fin m β†’ β„•) (parent : Fin m β†’ Option (Fin m))
(hb : UnitRefresh.RankedBags bags rank parent) (hne : βˆ€ x,(bags x).Nonempty)
(hp : βˆ€ l p,parent l=some p β†’ rank p<rank l) :
count ((activeRep bags Finset.univ,parentRep parent),(UnitRefresh.rankOrder rank).map Fin.val)=
Fintype.card (UnitRefresh.ColoredPairing bags) := by
rw [count_eq]
exact UnitRefresh.Materialized.count_eq bags rank parent hb hne hp
end MatchingFPRAS.ExecutableDP
end OAI