Download data/lean/OAI/Combinatorics/KServer/Basic.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 9.25 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Combinatorics/KServer/Basic.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Combinatorics/KServer/Basic.lean
-
curl -L -o Basic.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Combinatorics/KServer/Basic.lean
9.25 kB
| import Mathlib | |
| namespace OAI | |
| noncomputable section | |
| open scoped BigOperators | |
| namespace KServer | |
| universe u | |
| /-- Labeled configurations; no injectivity condition, so repetitions are allowed. -/ | |
| abbrev Configuration (k : β) (X : Type u) := Fin k β X | |
| /-- The finite probability simplex of the available server labels. -/ | |
| def LabelDistribution (k : β) := | |
| {p : Fin k β β // (β i, 0 β€ p i) β§ β i, p i = 1} | |
| /-- A behavioral policy knows the entire past request/choice history and the new | |
| request. It is a single rule for all horizons. Pairing the past requests and | |
| choices is equivalent to the histories X^t Γ [k]^(t-1) in the manuscript. -/ | |
| abbrev Policy (k : β) (X : Type u) := | |
| List (X Γ Fin k) β X β LabelDistribution k | |
| /-- Serve one request by moving exactly the indicated label to the request. -/ | |
| def serve {k : β} {X : Type u} (s : Configuration k X) (i : Fin k) (r : X) : | |
| Configuration k X := Function.update s i r | |
| /-- Deterministic movement of a label string on a fixed finite request sequence. -/ | |
| def serviceCost {k : β} {X : Type u} [MetricSpace X] (s : Configuration k X) : | |
| (Ο : List X) β (Fin Ο.length β Fin k) β β | |
| | [], _ => 0 | |
| | r :: rs, j => | |
| dist (s (j 0)) r + serviceCost (serve s (j 0) r) rs (fun i => j i.succ) | |
| /-- Probability of a finite label string under the successive behavioral choices. -/ | |
| def pathProbability {k : β} {X : Type u} (A : Policy k X) | |
| (h : List (X Γ Fin k)) : (Ο : List X) β (Fin Ο.length β Fin k) β β | |
| | [], _ => 1 | |
| | r :: rs, j => | |
| (A h r).val (j 0) * pathProbability A (h ++ [(r, j 0)]) rs (fun i => j i.succ) | |
| /-- Exact finite expected total movement against an oblivious input. -/ | |
| def expectedCost {k : β} {X : Type u} [MetricSpace X] | |
| (A : Policy k X) (s : Configuration k X) (Ο : List X) : β := | |
| β j : Fin Ο.length β Fin k, pathProbability A [] Ο j * serviceCost s Ο j | |
| /-- Offline optimum over the finite set of all label strings. For k β₯ 2 this | |
| range is nonempty and finite, hence its real infimum is the attained minimum. | |
| There is no restriction on the metric itself. -/ | |
| def optimalCost {k : β} {X : Type u} [MetricSpace X] | |
| (s : Configuration k X) (Ο : List X) : β := | |
| sInf (Set.range (serviceCost s Ο)) | |
| /-- Literal main objective, with arbitrary universes, arbitrary metric spaces, | |
| repeated initial positions, and one policy and additive constant for all finite | |
| inputs. Real constants are finite; an embedding witnesses at least k+1 points. | |
| The final implication encodes availability of B=0 for distinct starts. -/ | |
| def ReductionStatement : Prop := | |
| β C : β, β (k : β), 2 β€ k β | |
| β (X : Type u) (_ : MetricSpace X), | |
| (β e : Fin (k + 1) β X, Function.Injective e) β | |
| β s : Configuration k X, | |
| β A : Policy k X, β B : β, | |
| 0 β€ B β§ (Function.Injective s β B = 0) β§ | |
| β Ο : List X, | |
| expectedCost A s Ο β€ C * (Real.log (k + 1)) ^ 2 * optimalCost s Ο + B | |
| /- Finite behavioral experiments and product compactness. -/ | |
| instance (k : β) : TopologicalSpace (LabelDistribution k) := | |
| inferInstanceAs (TopologicalSpace | |
| {probability : Fin k β β // (β label, 0 β€ probability label) β§ β label, probability label = 1}) | |
| instance (k : β) : CompactSpace (LabelDistribution k) := by | |
| apply isCompact_iff_compactSpace.mp | |
| apply IsCompact.of_isClosed_subset | |
| (isCompact_Icc : IsCompact (Set.Icc (0 : Fin k β β) 1)) | |
| Β· change IsClosed | |
| ({probability : Fin k β β | β label, 0 β€ probability label} β© | |
| {probability : Fin k β β | β label, probability label = 1}) | |
| apply IsClosed.inter | |
| Β· simp only [Set.ofPred_forall] | |
| exact isClosed_iInter fun label => isClosed_le continuous_const (continuous_apply label) | |
| Β· exact isClosed_eq (by fun_prop) continuous_const | |
| Β· rintro probability β¨nonnegative, totalβ© | |
| refine β¨nonnegative, ?_β© | |
| intro label | |
| calc | |
| probability label β€ β other, probability other := | |
| Finset.single_le_sum (fun other _ => nonnegative other) (Finset.mem_univ label) | |
| _ = 1 := total | |
| /-- A pure choice, also used to fill unreachable decision histories. -/ | |
| def pureDistribution {k : β} (i : Fin k) : LabelDistribution k := | |
| β¨fun j => if j = i then 1 else 0, by | |
| constructor | |
| Β· intro j; dsimp only; split_ifs <;> norm_num | |
| Β· simpβ© | |
| instance (k : β) [NeZero k] : Nonempty (LabelDistribution k) := | |
| β¨pureDistribution 0β© | |
| lemma serviceCost_nonneg {k : β} {X : Type u} [MetricSpace X] | |
| (s : Configuration k X) (Ο : List X) (j : Fin Ο.length β Fin k) : | |
| 0 β€ serviceCost s Ο j := by | |
| induction Ο generalizing s with | |
| | nil => simp [serviceCost] | |
| | cons r rs ih => exact add_nonneg dist_nonneg (ih _ _) | |
| lemma pathProbability_nonneg {k : β} {X : Type u} (A : Policy k X) | |
| (h : List (X Γ Fin k)) (Ο : List X) (j : Fin Ο.length β Fin k) : | |
| 0 β€ pathProbability A h Ο j := by | |
| induction Ο generalizing h with | |
| | nil => simp [pathProbability] | |
| | cons r rs ih => exact mul_nonneg ((A h r).property.1 _) (ih _ _) | |
| /-- Decompose a complete label string into its first decision and its tail. -/ | |
| lemma sum_labelStrings {k n : β} (f : (Fin (n + 1) β Fin k) β β) : | |
| β j, f j = β i : Fin k, β js : Fin n β Fin k, f (Fin.cons i js) := by | |
| rw [β Fintype.sum_equiv (Fin.consEquiv (fun _ : Fin (n + 1) => Fin k)) | |
| (fun p => f (Fin.cons p.1 p.2)) f (fun _ => rfl), Fintype.sum_prod_type] | |
| lemma pathProbability_sum {k : β} {X : Type u} (A : Policy k X) | |
| (h : List (X Γ Fin k)) (Ο : List X) : | |
| β j : Fin Ο.length β Fin k, pathProbability A h Ο j = 1 := by | |
| induction Ο generalizing h with | |
| | nil => simp [pathProbability] | |
| | cons r rs ih => | |
| change (β j : Fin (rs.length + 1) β Fin k, pathProbability A h (r :: rs) j) = 1 | |
| rw [sum_labelStrings] | |
| simp only [pathProbability, Fin.cons_zero, Fin.cons_succ] | |
| simp_rw [β Finset.mul_sum, ih, mul_one] | |
| exact (A h r).property.2 | |
| lemma expectedCost_nonneg {k : β} {X : Type u} [MetricSpace X] | |
| (A : Policy k X) (s : Configuration k X) (Ο : List X) : | |
| 0 β€ expectedCost A s Ο := | |
| Finset.sum_nonneg fun _ _ => mul_nonneg (pathProbability_nonneg _ _ _ _) | |
| (serviceCost_nonneg _ _ _) | |
| lemma continuous_pathProbability {k : β} {X : Type u} | |
| (h : List (X Γ Fin k)) (Ο : List X) (j : Fin Ο.length β Fin k) : | |
| Continuous fun A : Policy k X => pathProbability A h Ο j := by | |
| induction Ο generalizing h with | |
| | nil => exact continuous_const | |
| | cons r rs ih => | |
| change Continuous fun A : Policy k X => | |
| (A h r).val (j 0) * pathProbability A (h ++ [(r, j 0)]) rs (fun i => j i.succ) | |
| apply Continuous.mul _ (ih _ _) | |
| exact (continuous_apply (j 0)).comp | |
| (continuous_subtype_val.comp ((continuous_apply r).comp (continuous_apply h))) | |
| /-- Equation (cost-polynomial): a fixed finite input has continuous expected | |
| cost on the full, possibly infinite, product of decision simplexes. -/ | |
| lemma continuous_expectedCost {k : β} {X : Type u} [MetricSpace X] | |
| (s : Configuration k X) (Ο : List X) : | |
| Continuous fun A : Policy k X => expectedCost A s Ο := by | |
| exact continuous_finsetSum _ fun j _ => | |
| (continuous_pathProbability [] Ο j).mul continuous_const | |
| /-- The topological step of Lemma `compact-policy`. The finite feasibility | |
| premise is the *remaining quantitative obligation*, not asserted here. This | |
| lemma does not use finite/bounded/discrete assumptions on the request space. -/ | |
| theorem compact_policy_of_finite_feasibility {k : β} {X : Type u} [MetricSpace X] | |
| (s : Configuration k X) (bound : List X β β) | |
| (hfinite : β F : Finset (List X), β A : Policy k X, | |
| β Ο β F, expectedCost A s Ο β€ bound Ο) : | |
| β A : Policy k X, β Ο : List X, expectedCost A s Ο β€ bound Ο := by | |
| let constraints (Ο : List X) : Set (Policy k X) := | |
| {A | expectedCost A s Ο β€ bound Ο} | |
| have hclosed (Ο : List X) : IsClosed (constraints Ο) := | |
| isClosed_le (continuous_expectedCost s Ο) continuous_const | |
| have hfip (F : Finset (List X)) : (β Ο β F, constraints Ο).Nonempty := by | |
| obtain β¨A, hAβ© := hfinite F | |
| exact β¨A, by simpa only [Set.mem_iInter, constraints, Set.mem_ofPred_eq] using hAβ© | |
| obtain β¨A, hAβ© := CompactSpace.iInter_nonempty hclosed hfip | |
| exact β¨A, by simpa only [Set.mem_iInter, constraints, Set.mem_ofPred_eq] using hAβ© | |
| /-! Section02's matching and strongly lazy simulation, including repeated | |
| positions and arbitrary metric spaces. -/ | |
| def configurationAfter {k : β} {X : Type u} (s : Configuration k X) | |
| (h : List (X Γ Fin k)) : Configuration k X := | |
| h.foldl (fun c ri => serve c ri.2 ri.1) s | |
| def StronglyLazy {k : β} {X : Type u} (s : Configuration k X) (A : Policy k X) : Prop := | |
| β h r, (β i, configurationAfter s h i = r) β | |
| β j, configurationAfter s h j β r β (A h r).val j = 0 | |
| lemma configurationAfter_append {k : β} {X : Type u} (s : Configuration k X) | |
| (h : List (X Γ Fin k)) (r : X) (i : Fin k) : | |
| configurationAfter s (h ++ [(r,i)]) = serve (configurationAfter s h) i r := by | |
| simp [configurationAfter, List.foldl_append] | |
| end KServer | |
| end | |
| end OAI | |