Finite MDPs, bisimulation metrics, aggregation and causal ancestors (Deep Bisimulation for Control)
DefinitionDeepBisimulationControl_BasicObjects of Sections 3-5 and Appendix A of Zhang, McAllister, Calandra, Gal and Levine (2021), on finite Markov decision processes.
- MDP. A finite state space , a finite nonempty action space , transition probabilities with , rewards and a discount factor .
- Policies and values. A policy is a probability distribution on actions for each state; a deterministic policy plays with probability one. With and , the value of is , the optimal value is , and is optimal if . The action value is .
- Policy iteration. A sequence of deterministic policies with , keeping whenever is a maximiser.
- Wasserstein distance. For distributions on and a cost , , the infimum over couplings. A pseudometric is a nonnegative symmetric function with satisfying the triangle inequality.
- Bisimulation metric (Definition 2). For , , and the bisimulation metric is .
- On-policy metric (Theorem 1). and the -bisimulation metric is . Along policy iteration the metric iterates are , .
- Aggregation (Theorem 2). A map onto a finite set of clusters defines the aggregated MDP with averaged transitions and averaged rewards . The average in-cluster distance is . The aggregation is an -aggregation for if every cluster lies within distance of some state.
- Representations. on ; the learning error of an encoder is .
- Factored MDPs and causal ancestors (Definition 3, Theorem 4). The state is with finite factors, and . The causal graph has an edge from each factor in to factor and from each factor in to the reward. is the set of factors with a directed path to the reward.
Formalization Note Policies are stationary Markov policies; in a finite discounted MDP the supremum over them equals the optimum over all policies. The metrics are defined as suprema of iterates from the zero function, as in Ferns, Panangaden and Precup (2004), Theorem 4.5, so that each is a definite function of the data; that they solve their fixed-point equations is part of the statements. Values of the aggregated MDP are only used for surjective , so no cluster is empty.
import Mathlib
/-!
Zhang, McAllister, Calandra, Gal and Levine, *Learning Invariant Representations for Reinforcement
Learning without Reconstruction*, arXiv:2006.10742v2 (ICLR 2021): finite Markov decision processes,
policies and value functions (Section 3), the 1-Wasserstein distance and the bisimulation metric
(Section 3, Definition 2), the on-policy bisimulation operator and policy iteration (Section 5,
Theorem 1, Appendix A), the aggregated MDP of Theorem 2 (following Ferns, Panangaden and Precup,
UAI 2004, Section 5), the learning error of Section 5, and factored MDPs with causal ancestors
(Section 5, Definition 3, Theorem 4 and its proof in Appendix A).
-/
open Finset
noncomputable section
namespace DeepBisimulationControl
section MDP
variable {S A : Type*} [Fintype S] [Fintype A]
/-- Section 3: `P s a` is a probability distribution `P(· | s, a)` on the finite state space. -/
def IsKernel (P : S → A → S → ℝ) : Prop :=
(∀ s a s', 0 ≤ P s a s') ∧ ∀ s a, ∑ s', P s a s' = 1
/-- Section 3: a (stationary, stochastic) policy `a ∼ π(s)`: `pi s` is a probability
distribution on actions. -/
def IsPolicy (pi : S → A → ℝ) : Prop :=
(∀ s a, 0 ≤ pi s a) ∧ ∀ s, ∑ a, pi s a = 1
/-- The deterministic policy that plays `sigma s` in state `s` (Appendix A, proof of Theorem 1:
the policies `π_k(s) = argmax_a …` of policy iteration). -/
def detPolicy [DecidableEq A] (sigma : S → A) : S → A → ℝ :=
fun s a => if a = sigma s then 1 else 0
/-- Section 5, Theorem 1: the on-policy reward `r^π_s = ∑_a π(a|s) R(s,a)`. -/
def policyReward (R : S → A → ℝ) (pi : S → A → ℝ) (s : S) : ℝ :=
∑ a, pi s a * R s a
/-- Section 5, Theorem 1: the on-policy transition distribution
`P^π_s(s') = ∑_a π(a|s) P(s'|s,a)`. -/
def policyKernel (P : S → A → S → ℝ) (pi : S → A → ℝ) (s s' : S) : ℝ :=
∑ a, pi s a * P s a s'
/-- Section 3: the value of policy `π`, the expected discounted return
`V^π(s) = E[∑_t γ^t R(s_t, a_t) | s_0 = s] = ∑_n γ^n ((P^π)^n r^π)(s)`. -/
def policyValue [DecidableEq S] (P : S → A → S → ℝ) (R : S → A → ℝ) (gamma : ℝ)
(pi : S → A → ℝ) (s : S) : ℝ :=
∑' n : ℕ, gamma ^ n * (((Matrix.of (policyKernel P pi)) ^ n).mulVec (policyReward R pi)) s
/-- Section 3: the optimal value function `V*(s) = max_π E[∑_t γ^t R(s_t, a_t)]`, the supremum
of `V^π(s)` over stationary stochastic policies. -/
def optValue [DecidableEq S] (P : S → A → S → ℝ) (R : S → A → ℝ) (gamma : ℝ) (s : S) : ℝ :=
⨆ pi : {pi : S → A → ℝ // IsPolicy pi}, policyValue P R gamma pi.1 s
/-- Section 5, Theorems 1 and 5: `π*` is an optimal policy, `V^{π*} = V*`. -/
def IsOptimalPolicy [DecidableEq S] (P : S → A → S → ℝ) (R : S → A → ℝ) (gamma : ℝ)
(pi : S → A → ℝ) : Prop :=
IsPolicy pi ∧ ∀ s, policyValue P R gamma pi s = optValue P R gamma s
/-- Appendix A, proof of Theorem 1: the action value
`Q^π(s,a) = R(s,a) + γ ∑_{s'} P(s'|s,a) V^π(s')` maximised by policy iteration. -/
def qValue [DecidableEq S] (P : S → A → S → ℝ) (R : S → A → ℝ) (gamma : ℝ)
(pi : S → A → ℝ) (s : S) (a : A) : ℝ :=
R s a + gamma * ∑ s', P s a s' * policyValue P R gamma pi s'
/-- Appendix A, proof of Theorem 1, eqs. (10)-(11): `σ_0, σ_1, …` is a run of policy iteration,
`σ_{k+1}(s) ∈ argmax_a Q^{σ_k}(s,a)`, with the standard tie-breaking rule that
`σ_{k+1}(s) = σ_k(s)` whenever `σ_k(s)` is itself a maximiser. -/
def IsPolicyIteration [DecidableEq S] [DecidableEq A] (P : S → A → S → ℝ) (R : S → A → ℝ)
(gamma : ℝ) (sigma : ℕ → S → A) : Prop :=
∀ k s,
(∀ a, qValue P R gamma (detPolicy (sigma k)) s a ≤
qValue P R gamma (detPolicy (sigma k)) s (sigma (k + 1) s)) ∧
((∀ a, qValue P R gamma (detPolicy (sigma k)) s a ≤
qValue P R gamma (detPolicy (sigma k)) s (sigma k s)) → sigma (k + 1) s = sigma k s)
/-- Section 3, footnote 1: `d` is a pseudometric on the states (distinct states may be at
distance `0`). -/
def IsPseudometric (d : S → S → ℝ) : Prop :=
(∀ s, d s s = 0) ∧ (∀ s t, 0 ≤ d s t) ∧ (∀ s t, d s t = d t s) ∧
∀ s t u, d s u ≤ d s t + d t u
/-- Section 3: `g` is a coupling of the distributions `mu` and `nu` on the states,
`g ∈ Γ(mu, nu)`. -/
def IsCoupling (mu nu : S → ℝ) (g : S → S → ℝ) : Prop :=
(∀ x y, 0 ≤ g x y) ∧ (∀ x, ∑ y, g x y = mu x) ∧ ∀ y, ∑ x, g x y = nu y
/-- Section 3: the 1-Wasserstein distance with ground cost `d`,
`W_1(mu, nu; d) = inf_{g ∈ Γ(mu, nu)} ∑_{x,y} g(x,y) d(x,y)`. -/
def wasserstein1 (d : S → S → ℝ) (mu nu : S → ℝ) : ℝ :=
sInf ((fun g : S → S → ℝ => ∑ x, ∑ y, g x y * d x y) '' {g | IsCoupling mu nu g})
/-- Section 3, Definition 2, eq. (3): the bisimulation operator
`F(d)(s_i,s_j) = max_a (1-c)|R(s_i,a) - R(s_j,a)| + c W_1(P(·|s_i,a), P(·|s_j,a); d)`. -/
def bisimOperator (P : S → A → S → ℝ) (R : S → A → ℝ) (c : ℝ) (d : S → S → ℝ) :
S → S → ℝ :=
fun s t => ⨆ a, (1 - c) * |R s a - R t a| + c * wasserstein1 d (P s a) (P t a)
/-- Section 3, Definition 2 (from Ferns et al.): the bisimulation metric, the solution of eq. (3),
constructed as in Ferns, Panangaden and Precup (2004), Theorem 4.5: `d̃ = sup_n F^n(0)`. -/
def bisimMetric (P : S → A → S → ℝ) (R : S → A → ℝ) (c : ℝ) : S → S → ℝ :=
fun s t => ⨆ n : ℕ, ((bisimOperator P R c)^[n] 0) s t
/-- Section 5, Theorem 1, eq. (5): the on-policy bisimulation operator
`F(d, π)(s_i,s_j) = (1-c)|r^π_{s_i} - r^π_{s_j}| + c W_1(P^π_{s_i}, P^π_{s_j}; d)`. -/
def piBisimOperator (P : S → A → S → ℝ) (R : S → A → ℝ) (c : ℝ) (pi : S → A → ℝ)
(d : S → S → ℝ) : S → S → ℝ :=
fun s t => (1 - c) * |policyReward R pi s - policyReward R pi t| +
c * wasserstein1 d (policyKernel P pi s) (policyKernel P pi t)
/-- Section 5, Theorems 1 and 5: the `π`-bisimulation metric `sup_n F(·, π)^n(0)`, the iterates
of the on-policy operator started at the bottom element `⊥ = 0`. -/
def piBisimMetric (P : S → A → S → ℝ) (R : S → A → ℝ) (c : ℝ) (pi : S → A → ℝ) :
S → S → ℝ :=
fun s t => ⨆ n : ℕ, ((piBisimOperator P R c pi)^[n] 0) s t
/-- Section 5, Theorem 1 and Appendix A: the metric iterates run alongside policy iteration,
`d_0 = ⊥ = 0`, `d_{n+1} = F(d_n, σ_n)`. -/
def policyIterationMetric [DecidableEq A] (P : S → A → S → ℝ) (R : S → A → ℝ) (c : ℝ)
(sigma : ℕ → S → A) : ℕ → S → S → ℝ
| 0 => 0
| n + 1 => piBisimOperator P R c (detPolicy (sigma n)) (policyIterationMetric P R c sigma n)
section Aggregation
variable {C : Type*} [Fintype C] [DecidableEq C]
/-- Section 5, Theorem 2 (aggregated MDP `M̄`, as in Ferns et al. 2004, Section 5): the members
`{s | φ(s) = k}` of cluster `k`. -/
def cluster (phi : S → C) (k : C) : Finset S :=
univ.filter fun s => phi s = k
/-- Section 5, Theorem 2: transitions of the aggregated MDP, averaged over the cluster,
`P̄(k' | k, a) = (1/|k|) ∑_{s ∈ k} ∑_{s' ∈ k'} P(s'|s,a)`. -/
def aggKernel (P : S → A → S → ℝ) (phi : S → C) (k : C) (a : A) (k' : C) : ℝ :=
(∑ s ∈ cluster phi k, ∑ s' ∈ cluster phi k', P s a s') / (cluster phi k).card
/-- Section 5, Theorem 2: rewards of the aggregated MDP, averaged over the cluster,
`R̄(k, a) = (1/|k|) ∑_{s ∈ k} R(s,a)`. -/
def aggReward (R : S → A → ℝ) (phi : S → C) (k : C) (a : A) : ℝ :=
(∑ s ∈ cluster phi k, R s a) / (cluster phi k).card
/-- Appendix A, proof of Theorem 2: `g(s, d)`, the average `d`-distance from `s` to the states of
its own cluster. -/
def avgClusterDist (d : S → S → ℝ) (phi : S → C) (s : S) : ℝ :=
(∑ s' ∈ cluster phi (phi s), d s s') / (cluster phi (phi s)).card
/-- Section 5, Theorem 2: every cluster of `φ` lies in an `ε`-neighborhood of some state for the
distance `d`. -/
def IsEpsAggregation (d : S → S → ℝ) (phi : S → C) (eps : ℝ) : Prop :=
∀ k, ∃ s0 : S, ∀ s, phi s = k → d s0 s ≤ eps
end Aggregation
/-- Section 4: the `ℓ_1` distance `‖x - y‖_1` between two representations in `ℝ^m`. -/
def l1Dist {m : ℕ} (x y : Fin m → ℝ) : ℝ :=
∑ i, |x i - y i|
/-- Section 5, after Theorem 2: the learning error of an encoder `ψ`,
`L = sup_{s_i, s_j} |‖ψ(s_i) - ψ(s_j)‖_1 - d̃(s_i, s_j)|`. -/
def learningError {m : ℕ} (d : S → S → ℝ) (psi : S → Fin m → ℝ) : ℝ :=
⨆ st : S × S, |l1Dist (psi st.1) (psi st.2) - d st.1 st.2|
end MDP
section Factored
variable {I A : Type*} [Fintype I] [DecidableEq I] {X : I → Type*} [∀ i, Fintype (X i)]
/-- Appendix A, proof of Theorem 4: the transition of a factored MDP with state space
`S = ∏_i S^i` and one-step causal dynamics,
`P(s' | s, a) = ∏_i T_i(s'^i | s, a)`. -/
def factoredKernel (T : ∀ i, (∀ j, X j) → A → X i → ℝ) (s : ∀ j, X j) (a : A)
(s' : ∀ j, X j) : ℝ :=
∏ i, T i s a (s' i)
/-- Appendix A, proof of Theorem 4: `f` depends on the state only through the factors in `J`. -/
def DependsOnlyOn {β : Type*} (f : (∀ j, X j) → β) (J : Set I) : Prop :=
∀ s s' : ∀ j, X j, (∀ j ∈ J, s j = s' j) → f s = f s'
/-- Section 5, Definition 3: the causal ancestors `AN(R)` of the reward in the causal graph whose
edges go from each factor in `pa i` to factor `i` and from each factor in `paR` to the reward:
the factors with a directed path to the reward. -/
def causalAncestors (pa : I → Set I) (paR : Set I) : Set I :=
{j | ∃ i ∈ paR, Relation.ReflTransGen (fun x y => x ∈ pa y) j i}
end Factored
end DeepBisimulationControl
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
Throughout, (states) and (actions) are arbitrary finite types, either of which may be empty. All functions below are real-valued. Sums over , , etc. are finite sums over the whole type. Wherever a supremum or infimum of reals is used, the following conventions apply:
- the supremum or infimum of an empty family is ;
- a supremum of a family that is not bounded above is ;
- an infimum of a set that is not bounded below is ;
- division by zero yields .
IsKernel. For , written , holds iff:
- for all , and
- for every and .
If or is empty, the second condition is vacuous.
IsPolicy. For , written , holds iff:
- for all , and
- for every .
If is nonempty and is empty, no such exists. If is empty, every qualifies.
detPolicy. Given a map (with having decidable equality), if and otherwise.
policyReward. For any and any (not required to be a policy),
policyKernel. For any and any (neither required to be normalized),
policyValue. For any , , any real (no range restriction) and any , is the infinite series
- Here is the matrix with entries , is its -th matrix power (with ), and is treated as a column vector.
- The sum is taken in the sense of unconditional summability. If the series is not summable, is defined to be .
optValue. The quantity is
- The supremum ranges over all real functions that are nonnegative and sum to in for each .
- No condition is placed on , or .
- By the conventions above, if no policies exist (e.g. nonempty and empty) or if the values are unbounded above.
IsOptimalPolicy. is optimal iff holds and for every , with and as above (including their junk conventions).
qValue. For any , , , ,
IsPolicyIteration. A sequence of deterministic maps satisfies this iff for every and every the following two conditions hold, where denotes :
- The next action is a maximizer:
- Tie-breaking: if for all , then .
is unconstrained. No condition on , or is imposed.
IsPseudometric. is a pseudometric iff, for all :
- ,
- ,
- , and
- .
Distinct points may be at distance .
IsCoupling. For arbitrary (not required to be nonnegative or to sum to ), is a coupling iff:
- for all ,
- for every , and
- for every .
If or has a negative entry, or if , no coupling exists.
wasserstein1. For any (not required to be a pseudometric) and any ,
This equals when no coupling exists (e.g. unequal total masses), or when the set of costs is unbounded below.
bisimOperator. For any , , any real (no restriction to ) and any ,
This is a maximum over the finite set when is nonempty, and when is empty.
bisimMetric. The quantity is
where is the -fold iterate of the operator above applied to the identically-zero function, and is included.
- It is at any pair for which these values are unbounded above.
- Nothing asserts that is a fixed point of or a pseudometric.
piBisimOperator. For any , , , and ,
There is no maximum over actions.
piBisimMetric. The quantity is
with included. It is where the iterates are unbounded above.
policyIterationMetric. For a sequence , the sequence of functions is defined by
No requirement that is a policy-iteration run is built in.
cluster. For a finite type of cluster labels and ,
This may be empty.
aggKernel. The aggregated transition is
It equals when is empty.
aggReward. The aggregated reward is
It equals when is empty.
avgClusterDist. For any and ,
The cluster always contains , so the denominator is at least .
IsEpsAggregation. For any , and real , this holds iff
- need not lie in the cluster .
- The distance is taken in the order .
- For an empty cluster, the condition only requires to be nonempty.
- The condition is vacuously true if is empty, and false if is empty but is not.
l1Dist. For ,
For this is .
learningError. For any (not required to be the bisimulation metric) and an encoder ,
This is a maximum over the finite set , and equals if is empty.
factoredKernel. Let be a finite index set of factors and a finite type for each . A state is . Given arbitrary real functions for (not required to be nonnegative or normalized), the factored transition is
It equals if is empty.
DependsOnlyOn. A function on depends only on iff
causalAncestors. Let assign parents to each factor, and let be the reward's parents. Then is the set of such that, for some , there is a finite chain
Because is allowed, .
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.