← back to the work log

Reinforcement Learning: Theory Notes

last updated

Reinforcement Learning

Rather than explicitly modelling the dynamics of the robot and its environment, reinforcement learning (RL) allows an autonomous system (agent) to learn a control strategy (policy) through interaction with its environment.

At each timestep, the agent observes the current state of the environment, chooses an action according to its policy, and receives a reward indicating how useful that action was.

agent–environment interaction
The reinforcement learning loopAn agent at the top sends an action along the right arrow to the environment below. Reward and next state return along two separate arrows on the left.Agentchooses an actionEnvironmentresponds to the actionaction atstatest+1rewardrt+1

The agent observes state sₜ and sends action aₜ to the environment.

This interaction is typically modelled through a Markov Decision Process (MDP). More formally, a length-TT MDP is a tuple:

M=⟨S,A,D,r,γ,ρ,T⟩\mathcal{M} = \langle \mathcal{S}, \mathcal{A}, \mathcal{D}, r, \gamma, \rho, T \rangle

where:

  • S\mathcal{S} is the state space. A state sts_t describes the state of the robot and environment at timestep tt. For a robot, this might include joint positions, joint velocities, and camera observations.
  • A\mathcal{A} is the action space. An action ata_t is a command sent to the robot, such as a joint velocity, torque, or target joint position.
  • D\mathcal{D} represents the environment dynamics: how the current state and action lead to the next state.
  • rr is the reward function, which assigns a scalar reward to a transition.
  • γ\gamma is the discount factor, which determines how much the agent values future rewards compared to immediate rewards.
  • ρ\rho describes the distribution of possible starting states.
  • TT is the number of timesteps in the trajectory.

As the agent interacts with the environment over time, it generates a trajectory:

τ=(s0,a0,r0,s1,a1,r1,…,sT−1,aT−1,rT−1,sT)\tau = (s_0,a_0,r_0,s_1,a_1,r_1,\dots,s_{T-1},a_{T-1},r_{T-1},s_T)

A natural question is: what is the probability that a particular trajectory τ\tau occurs? In general, the probability of what happens next could depend on the entire history of everything that has happened before it. For example, the probability of reaching st+1s_{t+1} might depend on

P(st+1∣st,at,st−1,at−1,…,s0,a0)P(s_{t+1}\mid s_t,a_t,s_{t-1},a_{t-1},\dots,s_0,a_0)

and the action chosen by the agent could similarly depend on its entire history:

P(at∣st,at−1,st−1,…,s0,a0)P(a_t\mid s_t,a_{t-1},s_{t-1},\dots,s_0,a_0)

The Markov assumption simplifies this by assuming that the current state sts_t contains all the information we need about the past. Therefore, the next state only depends on the current state and action:

P(st+1∣st,at)P(s_{t+1}\mid s_t,a_t)

and the agent's action only depends on the current state:

P(at∣st)P(a_t\mid s_t)

This lets us factor the probability of an entire trajectory into a much simpler expression:

P(τ)=P(s0)∏t=0T−1P(st+1∣st,at) P(at∣st)P(\tau) = P(s_0) \prod_{t=0}^{T-1} P(s_{t+1}\mid s_t,a_t)\,P(a_t\mid s_t)

We already defined the initial-state distribution as ρ\rho, the environment dynamics as D\mathcal{D}, and the agent's policy as π\pi, so we can rewrite this as:

P(τ)=ρ(s0)∏t=0T−1D(st,at,st+1) πθ(at∣st)P(\tau) = \rho(s_0) \prod_{t=0}^{T-1} \mathcal{D}(s_t,a_t,s_{t+1})\,\pi_\theta(a_t\mid s_t)

We often write the policy as πθ\pi_\theta because it is parameterized by learnable parameters θ\theta, which are optimized to maximize the return associated with the trajectories the policy produces.

To evaluate how good a trajectory is, we define its return as the sum of the rewards collected over the trajectory:

G(τ)=∑t=0T−1γtrtG(\tau) = \sum_{t=0}^{T-1}\gamma^t r_t

The discount factor γ\gamma determines how strongly we value rewards further into the future.

However, a policy can produce many different trajectories, so rather than optimizing the return of one individual trajectory, we care about the expected return across trajectories generated by the policy:

J(πθ)=Eτ∼Pθ;D[G(τ)]J(\pi_\theta) = \mathbb{E}_{\tau \sim P_{\theta;\mathcal{D}}}\left[G(\tau)\right]

The goal of reinforcement learning is therefore to find the policy parameters θ\theta that maximize this expected return.

Unlike classical model-based approaches, reinforcement learning does not require us to explicitly derive the dynamics of the robot and its environment. Instead, the policy can learn directly from interaction data, allowing it to handle dynamics that are difficult to model and potentially learn directly from high-dimensional sensor observations such as images.

Learning-based approaches also provide a way to improve performance as more data becomes available, rather than requiring us to manually redesign a controller for every new condition or task.

Despite these advantages, reinforcement learning introduces its own limitations. Learning through interaction can require large amounts of experience, and collecting that experience on a physical robot can be slow, expensive, and potentially unsafe. Simulation makes it possible to generate experience much more easily, but introduces a gap between simulated and real-world physics. RL also depends heavily on the reward signal: poorly specified or sparse rewards can make learning difficult or lead to unintended behaviours.

Soft Actor-Critic (SAC)

Now we know in RL, we need a way to learn the parameters for the policy. For my experiment, I used Soft Actor-Critic (SAC), which is an off-policy actor-critic RL algorithm designed for continuous action spaces.[1]

SAC is good for robotics because it handles continuous control well, reuses data efficiently through replay buffers, and integrates nicely with demonstration- and intervention-based training.

soft actor-critic
How Soft Actor-Critic learnsAn actor sends actions to the environment. Transitions enter a replay buffer. The four arrows run clockwise: act, store experience, train the critic from replay, and improve the actor using its value estimates alongside an entropy bonus.Actorchooses actionsEnvironmentrobot + taskReplay bufferstores past experienceCriticestimates action valuesactionstoreexperiencereplay a batchimprovepolicy

The stochastic actor samples an action and applies it to the environment.

SAC learns two main components: an actor and a critic. The actor is just our policy:

πθ(a∣s)\pi_\theta(a\mid s)

Given the current state ss, it determines which action aa the robot should take.

The critic learns a "Q-function":

Qϕ(s,a)Q_\phi(s,a)

Which estimates how much future reward we can expect if we take action aa in state ss and continue following our policy afterward.

Every time the robot interacts with the environment it generates transitions:

(st,at,rt,st+1)(s_t,a_t,r_t,s_{t+1})

And these are stored in the "replay buffer".

SAC repeatedly samples transitions from the replay buffer to train the critic. So for a transition (st,at,rt,st+1)(s_t,a_t,r_t,s_{t+1}), the basic idea is that the value of taking the current action should be equal to the reward we receive now plus the expected value of what happens afterward:

Q(st,at)≈rt+γQ(st+1,at+1)Q(s_t,a_t) \approx r_t + \gamma Q(s_{t+1},a_{t+1})

Once the critic can estimate which actions are valuable, the actor can then be trained to produce actions that receive higher Q-values.

So together, the critic learns which actions are good, while the actor learns to choose those actions.

One of the key characteristics of SAC is that the actor does not output one deterministic action, it is stochastic. This is typically represented using a Gaussian distribution:

at∼N(μθ(st),σθ(st))a_t \sim \mathcal{N}\left(\mu_\theta(s_t),\sigma_\theta(s_t)\right)

This allows the policy to explore different actions instead of immediately committing to the first behaviour that appears to work. SAC explicitly encourages this exploration by modifying the reward objective with an entropy term:

Q(st,at)≈rt+γ[Q(st+1,at+1)−αlog⁡πθ(at+1∣st+1)]Q(s_t,a_t) \approx r_t + \gamma\left[ Q(s_{t+1},a_{t+1}) - \alpha\log\pi_\theta(a_{t+1}\mid s_{t+1}) \right]

The α\alpha term controls how strongly exploration is encouraged. A larger α\alpha encourages a more stochastic policy, while a smaller α\alpha focuses more strongly on maximizing reward.[1]

HIL-SERL

We can extend SAC into Human-in-the-Loop Sample-Efficient Reinforcement Learning (HIL-SERL). Instead of forcing the robot to discover the task entirely through random exploration, HIL-SERL begins by collecting a small number of successful human demonstrations.[2] These demonstrations generate the same transitions we saw before:

(st,at,rt,st+1)(s_t,a_t,r_t,s_{t+1})

but now, the action ata_t can be influenced by a human! The human-in-the-loop part comes in when the policy begins to fail. A human supervisor can temporarily take control of the robot, perform a corrective action, and then return control back to the policy.[2]

So the data used by SAC now comes from three sources:

initial demonstrations+autonomous experience+human interventions\text{initial demonstrations} + \text{autonomous experience} + \text{human interventions}

sources (2) ▾
  1. [1]OpenAI, Spinning Up: Soft Actor-Critic
  2. [2]Luo et al., HIL-SERL: Precise and Dexterous Robotic Manipulation via Human-in-the-Loop Reinforcement Learning