math / data /lean /OAI /Combinatorics /KServer /Basic.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
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