Documentation

CslibFrontier.DataStructure.Online.Basic

Online request-serving problems and competitive analysis #

This file defines a shared action and cost model for deterministic online algorithms and offline reference schedules, following the amortized competitive-analysis framework of Sleator and Tarjan, Amortized Efficiency of List Update and Paging Rules and Cormen, Leiserson, Rivest, and Stein, Introduction to Algorithms, third edition, Chapter 17 and Problem 17-5 ("Competitive analysis of self-organizing lists with move-to-front").

An online Problem specifies an observable configuration type State, a request type Req, an initial-state predicate ValidInit, a single-step transition relation ValidStep, and a single-step cost stepCost. Both online strategies (DetStrategy) and offline schedules (ValidSchedule) are evaluated with the same ValidInit, ValidStep, and scheduleCost definitions:

This module covers deterministic strategies (for which step-by-step adaptive request generation produces an ordinary request list whose oblivious replay induces the identical execution). Randomized strategies against an oblivious adversary, as distributions over deterministic strategies, are in CslibFrontier.DataStructure.Online.Randomized; adaptive adversaries against randomized strategies are not modelled.

Main definitions #

Main results #

References #

An online request-serving problem with a common action and cost model for online strategies and offline schedules.

  • State : Type

    Observable configuration space shared by online and offline algorithms.

  • Req : Type

    Request type arriving one item at a time.

  • ValidInit : self.State → Prop

    Admissible initial configurations.

  • ValidStep : self.State → self.Req → self.State → Prop

    Admissible single-step transitions s ↦ s' serving request r.

  • stepCost : self.State → self.Req → self.State → ℕ

    Nonnegative integer cost charged for transition s ↦ s' on request r.

A state trajectory states validly serves reqs starting from s.

Total step cost of a state trajectory states serving reqs starting from s.

Final configuration reached after traversing states from initial configuration s.

@[simp]
theorem Cslib.DataStructure.Online.Problem.validSchedule_cons (P : Problem) (s s' : P.State) (r : P.Req) (rs : List P.Req) (ss : List P.State) :
P.ValidSchedule s (r :: rs) (s' :: ss) ↔ P.ValidStep s r s' ∧ P.ValidSchedule s' rs ss
theorem Cslib.DataStructure.Online.Problem.ValidSchedule.induction {P : Problem} {motive : (s : P.State) → (reqs : List P.Req) → (states : List P.State) → P.ValidSchedule s reqs states → Prop} (nil : ∀ (s : P.State), motive s [] [] trivial) (cons : ∀ (s : P.State) (r : P.Req) (rs : List P.Req) (s' : P.State) (ss : List P.State) (hstep : P.ValidStep s r s') (hrest : P.ValidSchedule s' rs ss), motive s' rs ss hrest → motive s (r :: rs) (s' :: ss) ⋯) {s : P.State} {reqs : List P.Req} {states : List P.State} (hv : P.ValidSchedule s reqs states) :
motive s reqs states hv

Induction along a valid schedule, consuming requests and post-step states in lockstep.

Use it as induction hv using Problem.ValidSchedule.induction for a goal about hv : P.ValidSchedule s reqs states: the two length-mismatch cases are impossible and do not appear, and the cons case receives the step P.ValidStep s r s' and the induction hypothesis for the rest of the schedule from s'. For a concrete problem whose State or Req unfolds to another type, name it: induction hv using Problem.ValidSchedule.induction (P := problem α).

@[simp]
theorem Cslib.DataStructure.Online.Problem.scheduleCost_cons (P : Problem) (s s' : P.State) (r : P.Req) (rs : List P.Req) (ss : List P.State) :
P.scheduleCost s (r :: rs) (s' :: ss) = P.stepCost s r s' + P.scheduleCost s' rs ss
@[simp]
theorem Cslib.DataStructure.Online.Problem.finalState_cons (P : Problem) (s s' : P.State) (ss : List P.State) :
P.finalState s (s' :: ss) = P.finalState s' ss
theorem Cslib.DataStructure.Online.Problem.finalState_append (P : Problem) (s : P.State) (ss₁ ss₂ : List P.State) :
P.finalState s (ss₁ ++ ss₂) = P.finalState (P.finalState s ss₁) ss₂

Concatenating two state trajectories composes finalState.

theorem Cslib.DataStructure.Online.Problem.ValidSchedule.length_eq {P : Problem} {s : P.State} {reqs : List P.Req} {states : List P.State} (h : P.ValidSchedule s reqs states) :
states.length = reqs.length

Every valid schedule has one post-step configuration per request.

theorem Cslib.DataStructure.Online.Problem.validInit_finalState (P : Problem) (hstep : ∀ {s : P.State} {r : P.Req} {s' : P.State}, P.ValidInit s → P.ValidStep s r s' → P.ValidInit s') {s : P.State} {reqs : List P.Req} {states : List P.State} (hs : P.ValidInit s) (hv : P.ValidSchedule s reqs states) :
P.ValidInit (P.finalState s states)

If every valid step lands in P.ValidInit, then P.finalState along any valid schedule from an admissible initial state remains in P.ValidInit.

theorem Cslib.DataStructure.Online.Problem.validSchedule_append (P : Problem) {s : P.State} {reqs₁ reqs₂ : List P.Req} {states₁ states₂ : List P.State} (h₁ : P.ValidSchedule s reqs₁ states₁) (h₂ : P.ValidSchedule (P.finalState s states₁) reqs₂ states₂) :
P.ValidSchedule s (reqs₁ ++ reqs₂) (states₁ ++ states₂)

Valid schedules compose sequentially when intermediate states match.

theorem Cslib.DataStructure.Online.Problem.scheduleCost_append (P : Problem) {s : P.State} {reqs₁ reqs₂ : List P.Req} {states₁ states₂ : List P.State} (hlen : states₁.length = reqs₁.length) :
P.scheduleCost s (reqs₁ ++ reqs₂) (states₁ ++ states₂) = P.scheduleCost s reqs₁ states₁ + P.scheduleCost (P.finalState s states₁) reqs₂ states₂

Schedule costs add across concatenated trajectories of matching lengths.

@[reducible, inline]

Stateful specification of valid offline transitions, used to attach Cslib.DataStructure.Amortized potential certificates between offline configurations and an online strategy's memory.

Client program replaying a given request list and offline state trajectory in offlineSpec.

theorem Cslib.DataStructure.Online.Problem.runClient_scheduleClient (P : Problem) {s : P.State} {reqs : List P.Req} {states : List P.State} (hv : P.ValidSchedule s reqs states) :
P.offlineSpec.runClient s (P.scheduleClient reqs states) = ((), P.finalState s states, reqs.zip states)

Interpreting scheduleClient on a valid schedule reaches finalState and records the zipped (request, nextState) history.

theorem Cslib.DataStructure.Online.Problem.validHistory_zip (P : Problem) {s : P.State} {reqs : List P.Req} {states : List P.State} (hv : P.ValidSchedule s reqs states) :
P.offlineSpec.ValidHistory s (reqs.zip states)

Every ValidSchedule yields an admissible offlineSpec operation history.

theorem Cslib.DataStructure.Online.Problem.totalCost_zip (P : Problem) (c : ℕ) {s : P.State} {reqs : List P.Req} {states : List P.State} (hv : P.ValidSchedule s reqs states) :
P.offlineSpec.totalCost (fun (s₀ : P.offlineSpec.State) (o : P.offlineSpec.Op) => c * P.stepCost s₀ o.1 o.2) s (reqs.zip states) = c * P.scheduleCost s reqs states

Weighted offline charges in offlineSpec equal c * scheduleCost.

Explicit prefix-history memory for packaging a history-dependent online decision rule as a DetStrategy.

  • initState : P.State

    Initial configuration at the start of the request stream.

  • history : List P.Req

    Requests observed so far in arrival order.

  • curState : P.State

    Current observable configuration.

A deterministic online strategy for P. Its transition step sees only its internal memory Mem and the newly arrived request P.Req, never future requests, while projecting to valid transitions of P.

def Cslib.DataStructure.Online.DetStrategy.ofHistory {P : Problem} (next : P.State → List P.Req → P.State → P.Req → P.State) (hnext : ∀ {s₀ : P.State} {hist : List P.Req} {s : P.State} (r : P.Req), P.ValidInit s₀ → (∃ (states : List P.State), P.ValidSchedule s₀ hist states ∧ P.finalState s₀ states = s) → P.ValidStep s r (next s₀ hist s r)) :

Package any prefix-dependent online decision function next s₀ prefix curState r that chooses admissible transitions along reachable histories as a DetStrategy.

Evolve internal memory across a request list.

Observable state trajectory produced starting from internal memory m.

Observable state trajectory produced from initial configuration s₀.

Total cost incurred starting from internal memory m on reqs.

Total cost incurred starting from initial configuration s₀ on reqs, evaluated in the same Problem.scheduleCost model as offline schedules.

@[simp]
theorem Cslib.DataStructure.Online.DetStrategy.runMem_cons {P : Problem} (A : DetStrategy P) (m : A.Mem) (r : P.Req) (rs : List P.Req) :
A.runMem m (r :: rs) = A.runMem (A.step m r) rs
@[simp]
theorem Cslib.DataStructure.Online.DetStrategy.statesFrom_cons {P : Problem} (A : DetStrategy P) (m : A.Mem) (r : P.Req) (rs : List P.Req) :
A.statesFrom m (r :: rs) = A.project (A.step m r) :: A.statesFrom (A.step m r) rs
@[simp]
theorem Cslib.DataStructure.Online.DetStrategy.costFrom_cons {P : Problem} (A : DetStrategy P) (m : A.Mem) (r : P.Req) (rs : List P.Req) :
A.costFrom m (r :: rs) = P.stepCost (A.project m) r (A.project (A.step m r)) + A.costFrom (A.step m r) rs
theorem Cslib.DataStructure.Online.DetStrategy.validMem_runMem {P : Problem} (A : DetStrategy P) {m : A.Mem} (hm : A.ValidMem m) (reqs : List P.Req) :
A.ValidMem (A.runMem m reqs)

Running runMem preserves ValidMem.

theorem Cslib.DataStructure.Online.DetStrategy.validSchedule_statesFrom {P : Problem} (A : DetStrategy P) {m : A.Mem} (hm : A.ValidMem m) (reqs : List P.Req) :
P.ValidSchedule (A.project m) reqs (A.statesFrom m reqs)

statesFrom always produces a ValidSchedule from A.project m.

theorem Cslib.DataStructure.Online.DetStrategy.validSchedule_states {P : Problem} (A : DetStrategy P) {s₀ : P.State} (hs₀ : P.ValidInit s₀) (reqs : List P.Req) :
P.ValidSchedule s₀ reqs (A.states s₀ reqs)

From any admissible initial configuration s₀, A.states s₀ reqs is a ValidSchedule.

theorem Cslib.DataStructure.Online.DetStrategy.cost_eq_costFrom {P : Problem} (A : DetStrategy P) {s₀ : P.State} (hs₀ : P.ValidInit s₀) (reqs : List P.Req) :
A.cost s₀ reqs = A.costFrom (A.init s₀) reqs

From an admissible initial configuration s₀, A.cost s₀ reqs agrees with A.costFrom (A.init s₀) reqs.

P.finalState along A.statesFrom m reqs equals A.project (A.runMem m reqs).

@[simp]

statesFrom produces exactly one observable state per request.

@[simp]
theorem Cslib.DataStructure.Online.DetStrategy.length_states {P : Problem} (A : DetStrategy P) (s₀ : P.State) (reqs : List P.Req) :
(A.states s₀ reqs).length = reqs.length

states produces exactly one observable state per request.

theorem Cslib.DataStructure.Online.DetStrategy.statesFrom_take {P : Problem} (A : DetStrategy P) (m : A.Mem) (reqs : List P.Req) (k : ℕ) :
A.statesFrom m (List.take k reqs) = List.take k (A.statesFrom m reqs)

Online trajectories are prefix-consistent: the states produced on the first k requests are the first k states produced on the whole request sequence.

theorem Cslib.DataStructure.Online.DetStrategy.states_take {P : Problem} (A : DetStrategy P) (s₀ : P.State) (reqs : List P.Req) (k : ℕ) :
A.states s₀ (List.take k reqs) = List.take k (A.states s₀ reqs)

Prefix consistency of states: an online strategy's trajectory on a prefix of the request sequence is the corresponding prefix of its trajectory on the whole sequence.

Competitive ratio predicates #

A is (c, b)-competitive from a fixed initial state s₀ when its cost on every request sequence is at most c times any valid offline schedule's cost from s₀, plus b.

A is (c, b)-competitive when it is (c, b)-competitive from every admissible initial state s₀ shared by the online and offline algorithms.

A is (c, b)-competitive across arbitrary initial states when its cost from any admissible s₀ is at most c times any valid offline schedule's cost from any admissible s_off, plus b.

Competitiveness across arbitrary initial states implies same-initial-state competitiveness.

theorem Cslib.DataStructure.Online.DetStrategy.IsCompetitive.mono {P : Problem} {A : DetStrategy P} {c₁ c₂ b₁ b₂ : ℕ} (hc : c₁ ≤ c₂) (hb : b₁ ≤ b₂) (h : A.IsCompetitive c₁ b₁) :
A.IsCompetitive c₂ b₂

Monotonicity of IsCompetitive in the competitive ratio c and additive constant b.

Potential method via Cslib.DataStructure.Amortized #

Native step execution of A over P.offlineSpec: A steps using only the request o.1, ignoring the offline target state o.2, and emits its single-step cost in P.

Semantic realization pairing P.offlineSpec with online memory invariant A.ValidMem.

theorem Cslib.DataStructure.Online.DetStrategy.exists_executes_scheduleClient {P : Problem} (A : DetStrategy P) (m : A.Mem) {s_off : P.State} {reqs : List P.Req} {offStates : List P.State} (hv : P.ValidSchedule s_off reqs offStates) :
∃ (traces : List ℕ), Executes A.Exec (P.scheduleClient reqs offStates) m () (A.runMem m reqs) traces ∧ total id traces = A.costFrom m reqs

Every valid offline schedule drives a concrete Executes trace of A whose summed trace weights equal A.costFrom m reqs.

theorem Cslib.DataStructure.Online.DetStrategy.amortized_bound {P : Problem} (A : DetStrategy P) {potential : P.State → A.Mem → ℕ} {c : ℕ} (Cert : Amortized A.impl id potential fun (s : P.offlineSpec.State) (o : P.offlineSpec.Op) => c * P.stepCost s o.1 o.2) {s_off : P.State} {m : A.Mem} {reqs : List P.Req} {offStates : List P.State} (hm : A.ValidMem m) (hv : P.ValidSchedule s_off reqs offStates) :
A.costFrom m reqs + potential (P.finalState s_off offStates) (A.runMem m reqs) ≤ c * P.scheduleCost s_off reqs offStates + potential s_off m

Telescoping competitive potential bound derived directly from Cslib.DataStructure.Amortized.client: if potential satisfies the step inequality against c * stepCost, then A.costFrom plus final potential is bounded by c * P.scheduleCost plus initial potential.

theorem Cslib.DataStructure.Online.DetStrategy.competitive_of_amortized {P : Problem} (A : DetStrategy P) {potential : P.State → A.Mem → ℕ} {c b : ℕ} (Cert : Amortized A.impl id potential fun (s : P.offlineSpec.State) (o : P.offlineSpec.Op) => c * P.stepCost s o.1 o.2) (hinit : ∀ (s₀ : P.State), P.ValidInit s₀ → potential s₀ (A.init s₀) ≤ b) :

A Cslib.DataStructure.Amortized certificate whose initial potential on (s₀, A.init s₀) is at most b establishes A.IsCompetitive c b.

theorem Cslib.DataStructure.Online.DetStrategy.competitiveAnyInit_of_amortized {P : Problem} (A : DetStrategy P) {potential : P.State → A.Mem → ℕ} {c b : ℕ} (Cert : Amortized A.impl id potential fun (s : P.offlineSpec.State) (o : P.offlineSpec.Op) => c * P.stepCost s o.1 o.2) (hinit : ∀ (s₀ s_off : P.State), P.ValidInit s₀ → P.ValidInit s_off → potential s_off (A.init s₀) ≤ b) :

A Cslib.DataStructure.Amortized certificate whose initial potential on (s_off, A.init s₀) is at most b across all admissible initial states establishes A.IsCompetitiveAnyInit c b.

Explicit adversarial request generation #

Generate n requests step-by-step against A starting from internal memory m, choosing each next request via chooseReq (A.project m) before advancing A on that request.

def Cslib.DataStructure.Online.DetStrategy.adaptiveRequests {P : Problem} (A : DetStrategy P) (chooseReq : P.State → P.Req) (s₀ : P.State) (n : ℕ) :

Generate n adversarial requests against A starting from initial configuration s₀.

@[simp]
@[simp]
theorem Cslib.DataStructure.Online.DetStrategy.adaptiveRequestsFrom_succ {P : Problem} (A : DetStrategy P) (chooseReq : P.State → P.Req) (m : A.Mem) (n : ℕ) :
A.adaptiveRequestsFrom chooseReq m (n + 1) = chooseReq (A.project m) :: A.adaptiveRequestsFrom chooseReq (A.step m (chooseReq (A.project m))) n
@[simp]
theorem Cslib.DataStructure.Online.DetStrategy.adaptiveRequestsFrom_length {P : Problem} (A : DetStrategy P) (chooseReq : P.State → P.Req) (m : A.Mem) (n : ℕ) :
(A.adaptiveRequestsFrom chooseReq m n).length = n
@[simp]
theorem Cslib.DataStructure.Online.DetStrategy.adaptiveRequests_length {P : Problem} (A : DetStrategy P) (chooseReq : P.State → P.Req) (s₀ : P.State) (n : ℕ) :
(A.adaptiveRequests chooseReq s₀ n).length = n
theorem Cslib.DataStructure.Online.DetStrategy.mem_adaptiveRequestsFrom {P : Problem} (A : DetStrategy P) {chooseReq : P.State → P.Req} {Q : P.Req → Prop} (hQ : ∀ {m : A.Mem}, A.ValidMem m → Q (chooseReq (A.project m))) {m : A.Mem} (hm : A.ValidMem m) {n : ℕ} {r : P.Req} (hr : r ∈ A.adaptiveRequestsFrom chooseReq m n) :
Q r

Every request emitted by adaptiveRequestsFrom satisfies Q whenever chooseReq selects a request satisfying Q on every reachable observable state.

theorem Cslib.DataStructure.Online.DetStrategy.mem_adaptiveRequests {P : Problem} (A : DetStrategy P) {chooseReq : P.State → P.Req} {Q : P.Req → Prop} (hQ : ∀ {m : A.Mem}, A.ValidMem m → Q (chooseReq (A.project m))) {s₀ : P.State} (hs₀ : P.ValidInit s₀) {n : ℕ} {r : P.Req} (hr : r ∈ A.adaptiveRequests chooseReq s₀ n) :
Q r

Every request emitted by adaptiveRequests from an admissible s₀ satisfies Q when chooseReq preserves Q on reachable states.

theorem Cslib.DataStructure.Online.DetStrategy.costFrom_adaptiveRequestsFrom_eq {P : Problem} (A : DetStrategy P) {chooseReq : P.State → P.Req} {w : ℕ} (hw : ∀ {m : A.Mem}, A.ValidMem m → P.stepCost (A.project m) (chooseReq (A.project m)) (A.project (A.step m (chooseReq (A.project m)))) = w) {m : A.Mem} (hm : A.ValidMem m) (n : ℕ) :
A.costFrom m (A.adaptiveRequestsFrom chooseReq m n) = n * w

If chooseReq forces exact step cost w at every reachable state of A, then n generated requests force exact online cost n * w.

theorem Cslib.DataStructure.Online.DetStrategy.cost_adaptiveRequests_eq {P : Problem} (A : DetStrategy P) {chooseReq : P.State → P.Req} {w : ℕ} (hw : ∀ {m : A.Mem}, A.ValidMem m → P.stepCost (A.project m) (chooseReq (A.project m)) (A.project (A.step m (chooseReq (A.project m)))) = w) {s₀ : P.State} (hs₀ : P.ValidInit s₀) (n : ℕ) :
A.cost s₀ (A.adaptiveRequests chooseReq s₀ n) = n * w

From an admissible initial state s₀, if chooseReq forces step cost w at every reachable state, then A.adaptiveRequests chooseReq s₀ n has exact online cost n * w.

theorem Cslib.DataStructure.Online.DetStrategy.le_costFrom_adaptiveRequestsFrom {P : Problem} (A : DetStrategy P) {chooseReq : P.State → P.Req} {w : ℕ} (hw : ∀ {m : A.Mem}, A.ValidMem m → w ≤ P.stepCost (A.project m) (chooseReq (A.project m)) (A.project (A.step m (chooseReq (A.project m))))) {m : A.Mem} (hm : A.ValidMem m) (n : ℕ) :
n * w ≤ A.costFrom m (A.adaptiveRequestsFrom chooseReq m n)

If chooseReq forces step cost at least w at every reachable state of A, then n generated requests force online cost at least n * w.

theorem Cslib.DataStructure.Online.DetStrategy.le_cost_adaptiveRequests {P : Problem} (A : DetStrategy P) {chooseReq : P.State → P.Req} {w : ℕ} (hw : ∀ {m : A.Mem}, A.ValidMem m → w ≤ P.stepCost (A.project m) (chooseReq (A.project m)) (A.project (A.step m (chooseReq (A.project m))))) {s₀ : P.State} (hs₀ : P.ValidInit s₀) (n : ℕ) :
n * w ≤ A.cost s₀ (A.adaptiveRequests chooseReq s₀ n)

From an admissible initial state s₀, if chooseReq forces step cost at least w at every reachable state, then A.adaptiveRequests chooseReq s₀ n has online cost at least n * w.