TorchLean API

NN.Runtime.RL.Algorithms.Tabular

Tabular Reinforcement Learning #

This module implements typed, total update rules for classic finite-state / finite-action RL:

Choose the bootstrap target with the appropriate rule, then apply update to a tensor coordinate. The same update works for state-value vectors, Q-tables, and higher-dimensional value tensors.

Primary references:

def Runtime.RL.Tabular.update {α : Type} [TorchLean.Storage α] [TorchLean.Storage.Update α] [Add α] [Sub α] [Mul α] {shape : Spec.Shape} (values : TorchLean.Tensor α shape) (coordinate : shape.Coord) (target stepSize : α) :

Move one tensor entry toward a supplied target by stepSize.

The target rule determines the algorithm; the update itself is independent of the table's shape. For Double Q-learning, update either table with a target computed using that table as selector and the other as evaluator. Arithmetic keeps the order current + stepSize * (target - current).

Instances For
    def Runtime.RL.Tabular.row {α : Type} [TorchLean.Storage α] {nStates nActions : ℕ} (q : TorchLean.Tensor α [nStates, nActions]) (state : Fin nStates) :
    TorchLean.Tensor α [nActions]

    Extract the action-value row Q[s, :].

    Instances For
      def Runtime.RL.Tabular.maximum {α : Type} [TorchLean.Storage α] [Context α] {nStates nActions : ℕ} (q : TorchLean.Tensor α [nStates, nActions]) (state : Fin nStates) :
      α

      Max action value at a state, defaulting to 0 for empty action spaces.

      Instances For
        def Runtime.RL.Tabular.greedy? {α : Type} [TorchLean.Storage α] [Context α] {nStates nActions : ℕ} (q : TorchLean.Tensor α [nStates, nActions]) (state : Fin nStates) :
        Option (Fin nActions)

        Greedy action at a state, if the action space is nonempty.

        Instances For
          def Runtime.RL.Tabular.expectation {α : Type} [TorchLean.Storage α] [Context α] {nStates nActions : ℕ} (q : TorchLean.Tensor α [nStates, nActions]) (state : Fin nStates) (policy : TorchLean.Tensor α [nActions]) :
          α

          Policy-weighted action value. Weights are used as supplied, without normalization.

          Instances For
            def Runtime.RL.Tabular.sarsaTarget {α : Type} [TorchLean.Storage α] [Context α] {nStates nActions : ℕ} (q : TorchLean.Tensor α [nStates, nActions]) (nextState : Fin nStates) (nextAction : Fin nActions) (reward gamma : α) (done : Bool := false) :
            α

            SARSA target r + γ Q(s', a').

            Instances For
              def Runtime.RL.Tabular.expectedSarsaTarget {α : Type} [TorchLean.Storage α] [Context α] {nStates nActions : ℕ} (q : TorchLean.Tensor α [nStates, nActions]) (nextState : Fin nStates) (nextPolicy : TorchLean.Tensor α [nActions]) (reward gamma : α) (done : Bool := false) :
              α

              Expected SARSA target r + γ * E_{a' ~ π(.|s')}[Q(s', a')].

              Instances For
                def Runtime.RL.Tabular.qLearningTarget {α : Type} [TorchLean.Storage α] [Context α] {nStates nActions : ℕ} (q : TorchLean.Tensor α [nStates, nActions]) (nextState : Fin nStates) (reward gamma : α) (done : Bool := false) :
                α

                Q-learning target r + γ max_a Q(s', a).

                Instances For
                  def Runtime.RL.Tabular.doubleQTarget {α : Type} [TorchLean.Storage α] [Context α] {nStates nActions : ℕ} (selector evaluator : TorchLean.Tensor α [nStates, nActions]) (nextState : Fin nStates) (reward gamma : α) (done : Bool := false) :
                  α

                  Double Q-learning / Double DQN-style target: choose the greedy action under selector, evaluate it under evaluator.

                  Instances For