Skip to content

Rework the algorithm/environment classes and constructors - #254

Open
RemyDegenne wants to merge 9 commits into
mainfrom
rename
Open

RemyDegenne wants to merge 9 commits into
mainfrom
rename

Conversation

@RemyDegenne

@RemyDegenne RemyDegenne commented Sep 17, 2026 •

Copy link
Copy Markdown
Collaborator

AI disclorure: the new list of names and plan for the organisation of algorithms and environments were done manually. The changes in the code to follow the plan (mostly renames) are done with AI.

See the new naming-convention README at LeanMachineLearning/SequentialLearning/README.md . It gives the current names for algorithms or environments that don't depend on some parts of the problem, as well as proposed names for not yet implemented classes and constructors (marked MISSING).

  • Add Markov algorithms, that depend only on the observation (the state in an MDP) but not the history.
  • Allow oblivious and stationary environments to have non-Unit observations. The old Unit-restricted constructors are now Environment.bandit and Environment.banditSeq.

Renames:

Kind Old New
Class IsDeterministicAlg Algorithm.IsDeterministic
Class IsDeterministicEnv Environment.HasDeterministicFeedback
Class IsObliviousEnv Environment.IsOblivious (meaning changed, see above)
Accessor nextAction, actionZero Algorithm.nextAction, Algorithm.actionZero
Accessor feedbackFun, feedbackFunZero Environment.feedbackFun, Environment.feedbackFunZero
Time zero Algorithm.p0 Algorithm.policyZero
Time zero Environment.obs0 Environment.obsZero
Time zero Environment.ν0 Environment.feedbackZero
Constructor detAlgorithm Algorithm.deterministic
Constructor randomSampling Algorithm.const
Constructor detEnvironment Environment.detFeedback
Constructor obliviousEnv ν (no observations) Environment.banditSeq ν
Constructor stationaryEnv ν (no observations) Environment.bandit ν
Constructor onlineEvalEnv Environment.evalSeq (now built on banditSeq)
Constructor evalEnv Environment.eval

Notable breaking changes: the names Environment.stationary and Environment.oblivious still exist, but now mean the versions with observations. Code that used the old observation-free environments should switch to Environment.bandit and Environment.banditSeq.

@RemyDegenne
RemyDegenne marked this pull request as ready for review October 3, 2026 13:09

@paulorauber paulorauber left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have left some very minor comments. Many have nothing to do with this PR, and are just about things that we did not get to discuss/minor nitpicks.

/-- The conditional distribution of the feedback at time `n` given the action at time `n` is `ν`. -/
lemma condDistrib_feedback_stationaryEnv [StandardBorelSpace 𝓨] [Nonempty 𝓨]
(h : IsAlgEnvSeq O A Y alg (stationaryEnv ν) P) (n : ℕ) :
lemma condDistrib_feedback_bandit [StandardBorelSpace 𝓨] [Nonempty 𝓨]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not strictly about this PR, but there are leftover condDistrib (rather than hasCondDistrib ) lemmas that we may want to remove.

(env.feedback n).sectR (history O A Y n ω, O n ω)

/-- The means of the feedback distribution as a function of the action chosen at time `n`. -/
noncomputable def Environment.means (env : Environment 𝓞 𝓐 𝓨) (O : ℕ → Ω → 𝓞) (A : ℕ → Ω → 𝓐)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also not about this PR, but I think the name Environment.means works in the plural if n comes before k in the list of arguments. Otherwise Environment.mean may be a better name.

simp_rw [h_eq]
fun_prop

@[fun_prop]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this probably runs into the old fun_prop issue with the hypothesis h .

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can safely remove this file and consult it whenever we consider adding new kinds of environments/algorithms. I found it a bit difficult to parse before looking at the code.

{m𝓨 : MeasurableSpace 𝓨} {mΩ : MeasurableSpace Ω}

/-- One round of interaction: an observation, then an action, then a feedback. -/
abbrev Round (𝓞 𝓐 𝓨 : Type*) := 𝓞 × 𝓐 × 𝓨

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, what made you decide for Round and History as abbreviations?

structure Environment (𝓞 𝓐 𝓨 : Type*) [MeasurableSpace 𝓞] [MeasurableSpace 𝓐] [MeasurableSpace 𝓨]
where
/-- Law of the observation of round `n` given the past rounds. -/
obs : (n : ℕ) → Kernel (Hist 𝓞 𝓐 𝓨 n) 𝓞

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

obs as an abbreviation is a bit inconsistent with the rest of the names in these basic structures.


/-- Conditionally on the event `(O 0, A 0) = p`, the first feedback has law `env.ν0 p`. -/
/-- Conditionally on the event `(O 0, A 0) = p`, the first feedback has law `env.feedbackZero p`. -/
lemma IsAlgEnvSeq.hasLaw_feedback_zero_cond [MeasurableSingletonClass 𝓞]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is no longer needed.

and the observation process of an algorithm-environment sequence is `noObs`. -/

/-- Every probability measure on `Unit` is `Measure.dirac ()`. -/
lemma Measure.eq_dirac_unit (μ : Measure Unit) [IsProbabilityMeasure μ] :

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and below seem to be candidates for Mathlib?

{P : Measure Ω} [IsFiniteMeasure P] {N : ℕ}

/-- Step of the algorithm-environment sequence: the round at time `n`. -/
def step (O : ℕ → Ω → 𝓞) (A : ℕ → Ω → 𝓐) (Y : ℕ → Ω → 𝓨) (n : ℕ) (ω : Ω) : Round 𝓞 𝓐 𝓨 :=

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the subject of renaming, I would suggest renaming step to round, except that round is taken by Mathlib. So I would instead suggest renaming Round to Step. This is so that we have a more consistent way of naming random variables based on the type of their values.


/-- `hist n` is the history before time `n`: the rounds at times `0, ..., n - 1`.
This is a random variable on the measurable space `ℕ → Round 𝓞 𝓐 𝓨`. -/
def hist (n : ℕ) (h : ℕ → Round 𝓞 𝓐 𝓨) : Hist 𝓞 𝓐 𝓨 n := fun i ↦ h i

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could also be called history to match the random variable in Algorithm.lean.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants