File size: 10,599 Bytes
e5a97c6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | import Mathlib
import OAI.Combinatorics.MatchingCount.Algorithm.OnlineStage
import OAI.Combinatorics.MatchingCount.Algorithm.OnlineScheduleNat
namespace OAI
namespace MatchingFPRAS.OnlineSchedule
open TreeTyped TreeFraction Transport ExecutableSource NumericLadder ExecutableScaling
open TreeTyped.Numbered Enlargement UnitRefresh ExecutableAdaptive
open ExecutableBottleneck ExecutableHierarchy ExecutableRefresh
attribute [local instance] NumberedHierarchy.labelOrder
attribute [local instance] Transport.sym2Order Transport.bagEdgeOrder Transport.bagEdgeFintype
attribute [local instance] UnitRefresh.Computable.allowedDec Transport.prodOrder Transport.sumOrder
def lengthAfter {A B : Type} [Codec A] [Codec B] {f : A→List B} (F : Realizer f) :
Realizer (fun a=>(f a).length) := composition F listLength
lemma polynomial_lengthAfter {A B : Type} [Codec A] [Codec B] {f : A→List B} {F : Realizer f}
(hF : PolynomialTime F) : PolynomialTime (lengthAfter F) :=
polynomial_composition hF polynomial_listLength
def rangeAfter {A : Type} [Codec A] {f : A→ℕ} (F : Realizer f) :
Realizer (fun a=>List.range (f a)) := composition F range
def listIndices {A : Type} [Codec A] : Realizer (fun xs : List A=>List.range xs.length) :=
composition listLength range
lemma polynomial_listIndices {A : Type} [Codec A] : PolynomialTime (listIndices (A:=A)) :=
polynomial_boundedRange polynomial_listLength Polynomial.X (fun xs=>by simpa using list_length_le_weight xs)
def indicesAfter {A B : Type} [Codec A] [Codec B] {f : A→List B} (F : Realizer f) :
Realizer (fun a=>List.range (f a).length) := composition F listIndices
lemma polynomial_indicesAfter {A B : Type} [Codec A] [Codec B] {f : A→List B} {F : Realizer f}
(hF : PolynomialTime F) : PolynomialTime (indicesAfter F) :=
polynomial_composition hF polynomial_listIndices
abbrev PrecisionData := ScanInput×(ℕ×ℕ)
def scanError (x : PrecisionData) : ℕ :=
2*(1+(x.1.1.2.2*(ladderQ x.1.1.1.1 x.1.1.1.2)^2+ladderQ x.1.1.1.1 x.1.1.1.2))+
4*(8*x.2.1^2+4*x.2.1+3)+2*x.1.1.1.1^2*x.2.2
def scanErrorRealizer : Realizer scanError :=
((((constant 2).mulNat ((constant 1).addNat
((first.fst.snd.snd.mulNat (composition (composition first.fst.fst qRealizer) (fixedPower 2))).addNat
(composition first.fst.fst qRealizer)))).addNat
((constant 4).mulNat ((((constant 8).mulNat (composition second.fst (fixedPower 2))).addNat
((constant 4).mulNat second.fst)).addNat (constant 3)))).addNat
(((constant 2).mulNat (composition first.fst.fst.fst (fixedPower 2))).mulNat second.snd)).congr (fun _=>rfl)
lemma polynomial_scanError : PolynomialTime scanErrorRealizer := by
unfold scanErrorRealizer
apply polynomial_congr
dsimp only [Realizer.fst, Realizer.snd]
have := polynomial_q
have := polynomial_fixedPower 2
simp only [Realizer.addNat,Realizer.mulNat]
tree_cost
def precisionPlan (x : PrecisionData) : ((ℕ×ℕ)×ℕ)×ℕ :=
(((Nat.clog 2 (discreteLadderSteps x.1.1.1.1 x.1.1.1.2 x.1.1.2.1 x.1.1.2.2 x.1.2 *scanError x*x.1.2),
x.1.1.1.1),discreteLadderSteps x.1.1.1.1 x.1.1.1.2 x.1.1.2.1 x.1.1.2.2 x.1.2),
Nat.clog 2 (6*x.2.1*x.1.2))
def precisionPlanRealizer : Realizer precisionPlan :=
(pair (pair (pair
(composition (((composition first scansRealizer).mulNat scanErrorRealizer).mulNat first.snd) clogTwo)
first.fst.fst.fst) (composition first scansRealizer))
(composition (((constant 6).mulNat second.fst).mulNat first.snd) clogTwo)).congr (fun _=>rfl)
lemma polynomial_precisionPlan : PolynomialTime precisionPlanRealizer := by
unfold precisionPlanRealizer
apply polynomial_congr
dsimp only [Realizer.fst, Realizer.snd]
have := polynomial_scanError
have := polynomial_scans
have := polynomial_clogTwo
simp only [Realizer.mulNat]
tree_cost
abbrev RequestInput := SparseStageData×ℕ
def sourcePrecision (x : RequestInput) : PrecisionData :=
let b:=sparseBagInput x.1
(((((sourceVertices b).length,x.1.1.2.1.2),(x.1.1.2.1.1.2.length/2,x.1.1.2.2.1.length)),
1000*(x.1.1.2.1.1.2.length/2)*x.2),
((sourceTables b).1.length,(sourceParents b).length))
def requestBagRealizer : Realizer (fun x : RequestInput=>sparseBagInput x.1) :=
composition first sparseBagInputRealizer
def requestKRealizer : Realizer (fun x : RequestInput=>x.1.1.2.1.1.2.length/2) :=
composition (pair (composition first.fst.snd.fst.fst.snd listLength) (constant 2)) quotient
def requestNDRealizer : Realizer (fun x : RequestInput=>((sourceVertices (sparseBagInput x.1)).length,x.1.1.2.1.2)) :=
pair (composition (composition (g:=sourceVertices) requestBagRealizer sourceVerticesRealizer) listLength) first.fst.snd.fst.snd
def requestKTRealizer : Realizer (fun x : RequestInput=>(x.1.1.2.1.1.2.length/2,x.1.1.2.2.1.length)) :=
pair requestKRealizer (composition first.fst.snd.snd.fst listLength)
def requestMRealizer : Realizer (fun x : RequestInput=>1000*(x.1.1.2.1.1.2.length/2)*x.2) :=
((constant 1000).mulNat requestKRealizer).mulNat second
def requestScanRealizer : Realizer (fun x : RequestInput=>
((((sourceVertices (sparseBagInput x.1)).length,x.1.1.2.1.2),
(x.1.1.2.1.1.2.length/2,x.1.1.2.2.1.length)),1000*(x.1.1.2.1.1.2.length/2)*x.2)) :=
pair (pair requestNDRealizer requestKTRealizer) requestMRealizer
def requestTablesRealizer : Realizer (fun x : RequestInput=>sourceTables (sparseBagInput x.1)) :=
composition (f:=fun x : RequestInput=>sparseBagInput x.1) (g:=sourceTables)
requestBagRealizer sourceTablesRealizer
def requestCatalogRealizer : Realizer (fun x : RequestInput=>(sourceTables (sparseBagInput x.1)).1) :=
Realizer.fst (f:=fun x : RequestInput=>sourceTables (sparseBagInput x.1)) requestTablesRealizer
def requestEdgesRealizer : Realizer (fun x : RequestInput=>(sourceTables (sparseBagInput x.1)).1.length) :=
lengthAfter (f:=fun x : RequestInput=>(sourceTables (sparseBagInput x.1)).1) requestCatalogRealizer
def requestLabelsRealizer : Realizer (fun x : RequestInput=>(sourceParents (sparseBagInput x.1)).length) :=
composition (composition (g:=sourceParents) requestBagRealizer sourceParentsRealizer) listLength
def sourcePrecisionRealizer : Realizer sourcePrecision :=
pair requestScanRealizer (pair requestEdgesRealizer requestLabelsRealizer)
lemma polynomial_requestBag : PolynomialTime requestBagRealizer :=
polynomial_composition polynomial_first polynomial_sparseBagInput
lemma polynomial_requestK : PolynomialTime requestKRealizer := by
unfold requestKRealizer
dsimp only [Realizer.fst, Realizer.snd]
have := polynomial_quotient
tree_cost
lemma polynomial_requestND : PolynomialTime requestNDRealizer := by
unfold requestNDRealizer
dsimp only [Realizer.fst, Realizer.snd]
have := polynomial_requestBag
have := polynomial_sourceVertices
tree_cost
lemma polynomial_requestKT : PolynomialTime requestKTRealizer := by
unfold requestKTRealizer
dsimp only [Realizer.fst, Realizer.snd]
have := polynomial_requestK
tree_cost
lemma polynomial_requestM : PolynomialTime requestMRealizer :=
polynomial_mulNat (polynomial_mulNat (polynomial_constant _) polynomial_requestK) polynomial_second
lemma polynomial_requestScan : PolynomialTime requestScanRealizer :=
TreeTyped.polynomial_pair (TreeTyped.polynomial_pair polynomial_requestND polynomial_requestKT) polynomial_requestM
lemma polynomial_requestTables : PolynomialTime requestTablesRealizer :=
polynomial_composition polynomial_requestBag polynomial_sourceTables
lemma polynomial_requestCatalog : PolynomialTime requestCatalogRealizer :=
polynomial_fst polynomial_requestTables
lemma polynomial_requestEdges : PolynomialTime requestEdgesRealizer :=
polynomial_lengthAfter polynomial_requestCatalog
lemma polynomial_requestLabels : PolynomialTime requestLabelsRealizer :=
polynomial_composition (polynomial_composition polynomial_requestBag
polynomial_sourceParents) polynomial_listLength
lemma polynomial_sourcePrecision : PolynomialTime sourcePrecisionRealizer :=
TreeTyped.polynomial_pair polynomial_requestScan (TreeTyped.polynomial_pair polynomial_requestEdges polynomial_requestLabels)
def sourceRequest (x : RequestInput) : OnlineSample.Request :=
let plan:=precisionPlan (sourcePrecision x)
(plan.1,(plan.2,List.range ((sourceTables (sparseBagInput x.1)).1.length)))
def requestPlanRealizer : Realizer (fun x : RequestInput=>precisionPlan (sourcePrecision x)) :=
composition (g:=precisionPlan) sourcePrecisionRealizer precisionPlanRealizer
def requestIndicesRealizer : Realizer (fun x : RequestInput=>List.range (sourceTables (sparseBagInput x.1)).1.length) :=
indicesAfter (f:=fun x : RequestInput=>(sourceTables (sparseBagInput x.1)).1) requestCatalogRealizer
def sourceRequestRealizer : Realizer sourceRequest :=
pair requestPlanRealizer.fst (pair requestPlanRealizer.snd requestIndicesRealizer)
lemma polynomial_requestPlan : PolynomialTime requestPlanRealizer :=
polynomial_composition polynomial_sourcePrecision polynomial_precisionPlan
lemma polynomial_requestIndices : PolynomialTime requestIndicesRealizer :=
polynomial_indicesAfter polynomial_requestCatalog
lemma polynomial_sourceRequest : PolynomialTime sourceRequestRealizer :=
TreeTyped.polynomial_pair (polynomial_fst polynomial_requestPlan)
(TreeTyped.polynomial_pair (polynomial_snd polynomial_requestPlan) polynomial_requestIndices)
lemma sparseBagInput_source {G : GraphInput} {j : ℕ} (s : FiniteTrial.ScaleState G j)
(K D : ℕ) (ft fq : List Bool) :
sparseBagInput (OnlineStage.data s K D ft fq)=
(((matrixRep s.activity,List.replicate (2*K+2) false),List.range G.n),List.replicate (2*K) false) := by
have h:=sparseUpdateData_source s K D ft fq
exact congrArg (fun x=>x.1.1.1.1) h
lemma sourcePrecision_source {G : GraphInput} [Nontrivial (Fin G.n)] (hn : 2≤G.n)
{j : ℕ} (s : FiniteTrial.ScaleState G j) (K D S : ℕ) (ft fq : List Bool) :
sourcePrecision (OnlineStage.data s K D ft fq,S)=
(((((Fintype.card (SourceVertex (Fin G.n) K)),D),(K,ft.length)),1000*K*S),
(Fintype.card (BagEdge (RationalSource.bags s.activity K)),Fintype.card (RationalSource.Label s.activity K))) := by
unfold sourcePrecision
rw [sparseBagInput_source]
dsimp only [OnlineStage.data]
rw [sourceVertices_length,sourceTables_catalog hn s.activity
(fun i j=>(s.activity_pos i j).le) K,sourceParents_source hn s.activity
(fun i j=>(s.activity_pos i j).le) K]
simp only [List.length_replicate,Nat.mul_div_cancel_left _ (by decide : 0<2),sourceCatalog,List.length_ofFn,
ExecutableDP.parentRep_length]
end MatchingFPRAS.OnlineSchedule
end OAI
|