Search

Search papers and lessons

Goal-Conditioned Reinforcement Learning

Level 3 · 10 min read

Goal-conditioned reinforcement learning trains one policy and one value function that take the goal as an input, so a single agent can be told which of many outcomes to reach.

On this page

  • Colour = topic mix
  • Probability
  • Linear algebra
  • Reinforcement learning
  • Neural network basics
  • Stochastic processes
  • Discrete math
  • Sign in to see your progress

TL;DR

A standard reinforcement learning agent learns to do one task, defined by one reward function. Goal-conditioned reinforcement learning gives the agent a goal as an extra input, so one policy and one value function can handle many goals. The agent learns how to reach any target it is asked for, not just the one it was trained on.

Where you'll see it: robot arms that must reach arbitrary positions, navigation to any location, and language-conditioned agents that follow instructions naming different outcomes.

Intuition

One map, many destinations

Think of a taxi driver who knows the whole city. They do not learn one route from home to the airport and then start over for the next trip. They learn how streets connect, and any destination becomes a question they can answer. Goal-conditioned RL aims for the same property: the policy asks "where am I, and where am I going?" and acts on both.

The naive alternative is one agent per goal. That works for three goals, but a robot that must reach any point in a room would need infinitely many agents, and each one would learn from scratch. Nothing learned while reaching one point helps with another. Conditioning on the goal lets experience be shared. A step that moved the arm toward one target also teaches the agent how the arm responds to actions in general.

Why the goal must be an input

If the goal were fixed inside the reward, the value function would only ever answer one question. Making the goal an input means the value function answers a whole family of questions at once: "how good is this state if the target is that?" A neural network can represent that family as one function of the state and the goal. This is the idea that Schaul and colleagues called a universal value function approximator in 2015.

The reward is usually sparse. The agent gets a signal only when it reaches the goal, and nothing otherwise. Sparse rewards are hard to learn from, because most episodes teach almost nothing. Goal-conditioned learning has a helpful trick for this, which the math section introduces: the agent can treat a state it did reach as if that state had been the goal all along.

The math

A goal-conditioned Markov decision process is a Markov Decision Process with an extra goal variable. It has states s∈Ss \in \mathcal{S}, actions a∈Aa \in \mathcal{A}, goals g∈Gg \in \mathcal{G}, transition probabilities P(s′∣s,a)P(s' \mid s, a), a discount factor γ∈[0,1)\gamma \in [0, 1), and a reward r(s,a,g)r(s, a, g) that depends on the goal. A common sparse reward is r(s,a,g)=1r(s, a, g) = 1 when the agent reaches the goal on this step and 00 otherwise.

Definition: Goal-conditioned value function

For a policy π(a∣s,g)\pi(a \mid s, g) that takes the goal as input, the value of a state under goal gg is

Vπ(s,g)=E ⁣[∑t=0∞γt r(st,at,g) | s0=s,  at∼π(⋅∣st,g)].V^\pi(s, g) = \mathbb{E}\!\left[\sum_{t=0}^{\infty} \gamma^t \, r(s_t, a_t, g) \,\middle|\, s_0 = s,\; a_t \sim \pi(\cdot \mid s_t, g)\right].

The action-value function Qπ(s,a,g)Q^\pi(s, a, g) is defined the same way, with the first action fixed to aa.

Here E\mathbb{E} is the expected value over the randomness of the transitions and the policy, γ\gamma is the discount that makes later rewards count for less, and sts_t, ata_t are the state and action at step tt. The only change from ordinary RL is the extra argument gg everywhere.

The Bellman equation with a goal

The recursion from Value Function still holds, with the goal carried along:

Q∗(s,a,g)=Es′ ⁣[r(s,a,g)+γmax⁡a′Q∗(s′,a′,g)].Q^*(s, a, g) = \mathbb{E}_{s'}\!\left[ r(s, a, g) + \gamma \max_{a'} Q^*(s', a', g) \right].

Learning uses the same tools as Q-Learning or the critic in Actor-Critic Methods. The difference is in the data. Each stored transition (s,a,s′,g)(s, a, s', g) can be used to update the value for its goal, and a deep network approximates Q(s,a,g)Q(s, a, g) as a Feedforward Neural Network with ss, aa and gg concatenated as input.

Hindsight relabelling

Suppose the agent was asked for goal gg and reached state sTs_T instead. Under the sparse reward, the whole episode gives zero reward, and nothing is learned. The trick is to replace gg with the achieved state sTs_T and recompute the reward. The episode now succeeded at a goal that was true for it, so the reward is 11 at the end. This is the core of hindsight experience replay, and it is valid because the transitions and the policy do not depend on which goal was asked for, only on the state and action.

Worked examples

Example
A corridor with five cells

The agent stands on cells 00 to 44 and moves left or right by one cell. Movement is deterministic. The reward r(s,a,g)=1r(s, a, g) = 1 is given on the step that enters goal gg, and 00 otherwise. Use γ=0.9\gamma = 0.9.

Let d=∣g−s∣d = |g - s| be the distance from the state to the goal. The agent needs dd moves, and the reward arrives on the last one, so under the best policy

V∗(s,g)=γ d−1for d≥1.V^*(s, g) = \gamma^{\,d-1} \quad \text{for } d \ge 1.
  1. Goal g=4g = 4, state s=2s = 2. The distance is d=2d = 2, so V∗(2,4)=0.91=0.9V^*(2, 4) = 0.9^{1} = 0.9.
  2. Check with the Bellman equation. Moving right from 22 gives reward 00 and lands on 33. Then V∗(3,4)=1V^*(3, 4) = 1, because one more move enters the goal. So V∗(2,4)=0+0.9⋅1=0.9V^*(2, 4) = 0 + 0.9 \cdot 1 = 0.9. This matches step 1.
  3. Goal g=0g = 0, state s=2s = 2. The distance is again 22, so V∗(2,0)=0.9V^*(2, 0) = 0.9.
  4. Goal g=4g = 4, state s=0s = 0. The distance is 44, so V∗(0,4)=0.93=0.729V^*(0, 4) = 0.9^{3} = 0.729.

Interpretation: one function of (s,g)(s, g) gives the answer for every goal. The same rule covers goals to the left and right, and the value falls off with distance at the rate set by γ\gamma. A single learned network that captures this rule answers the question for any goal in the corridor.

12345600.20.40.60.81distance d to goalvalue
  • gamma = 0.9
  • gamma = 0.5
Value of a state under a sparse goal reward falls off as gamma to the power d minus 1. A smaller discount makes distant goals look much less worth reaching.
Example
Relabelling a failed episode

The agent is asked for goal g=4g = 4 and walks right twice, from cell 00 to cell 22. It never reaches 44, so the sparse reward is 00 on every step. Store the episode with γ=0.9\gamma = 0.9 and a learning rate of 1 for clarity.

  1. Original transitions: (0,right,1,g=4)(0, \text{right}, 1, g{=}4) with reward 00, and (1,right,2,g=4)(1, \text{right}, 2, g{=}4) with reward 00. Both give zero learning signal.
  2. Relabel with the achieved final state g′=2g' = 2. Now the episode reached its goal at the last step, so the reward on (1,right,2,g′=2)(1, \text{right}, 2, g'{=}2) is 11.
  3. Update the last transition. Its target is r+γ⋅0=1r + \gamma \cdot 0 = 1, since 22 is terminal for goal 22. So Q(1,right,2)=1Q(1, \text{right}, 2) = 1.
  4. Update the first transition under the relabelled goal. Its target is 0+0.9⋅max⁡a′Q(1,a′,2)=0.9⋅1=0.90 + 0.9 \cdot \max_{a'} Q(1, a', 2) = 0.9 \cdot 1 = 0.9. So Q(0,right,2)=0.9Q(0, \text{right}, 2) = 0.9.
  5. Compare with the original goal. The same episode left Q(0,right,4)Q(0, \text{right}, 4) and Q(1,right,4)Q(1, \text{right}, 4) unchanged at zero, because the reward was never earned for goal 44.

Interpretation: the failed run taught the agent two correct facts about reaching cell 22. It did not need to succeed at its original task to learn something useful. The relabelling is safe here because the transition dynamics do not depend on which goal was asked for.

Tradeoffs and failure modes

Goal conditioning helps when the goals share structure, so that skills transfer between them. It does not help if every goal needs a completely different strategy, and it adds input dimensions that make learning harder.

SituationWhat goes wrongBetter choice
Sparse reward, goals far apartAlmost every episode gives zero signalHindsight relabelling of achieved states, as in Hindsight Experience Replay
Goals drawn from a large or continuous setValue must generalise across goals, and bad generalisation gives confident wrong valuesReward shaping by distance, or a learned embedding of the goal
Goal is a whole state (an image, a full arm pose)The input is high-dimensional and the goal space has many irrelevant featuresAchieved-goal and desired-goal projections onto task-relevant features
Goals that are unreachableRelabelling can teach the agent to chase states it cannot controlRestrict relabelling to states the agent can actually reach
Policy and value trained on different goal distributionsValue estimates are accurate only for goals seen in trainingSample goals from the same distribution used at test time

A second failure mode is the bias that relabelling introduces. Relabelled episodes are not samples from the goal distribution the agent will face, so mixing them with original goals can shift the policy toward the goals it happened to reach. Most implementations keep a mix and tune the fraction of relabelled goals.

History

The idea of a policy that is told what to achieve is older than deep learning. Kaelbling's 1993 work on learning to achieve goals is an early example of treating goals as an input to learned behaviour. Schaul, Horgan, Gregor and Silver introduced universal value function approximators in 2015, which made the value function itself depend on the goal and showed how a single network could represent it. The problem they were solving was transfer: an agent should reuse what it learned about one goal when asked for another.

Andrychowicz and colleagues added hindsight experience replay in 2017. Their contribution was the relabelling trick above, which turns failed episodes into training data for goals the agent did reach. Their experiments used simulated robot-arm pushing, sliding and pick-and-place tasks with sparse, binary rewards, which they reported were hard to learn without relabelling.

Check yourself

QuestionWhy does a single goal-conditioned value function V(s, g) help more than one value function per goal?Show answer

Experience is shared across goals. A transition that moved the agent toward one target also tells the network how actions change the state in general, which helps for every other target. Separate value functions per goal learn from scratch for each one and cannot reuse anything.

QuestionIn the corridor example, what is V*(0, 4) with gamma = 0.9 and the sparse reward?Show answer

The distance from 0 to 4 is 4 moves, so the reward arrives on the fourth move and is discounted three times. V*(0, 4) = 0.9 to the power 3, which is 0.729.

QuestionAn agent is asked for goal 4, walks to cell 2 and stops. Why can relabelling the episode with goal 2 be useful, and what would make it wrong?Show answer

Relabelling turns the failed run into a success for goal 2, so the reward at the last step is 1 and the value of reaching cell 2 is learned. It is wrong if the goal 2 cannot be controlled, or if the policy will later be asked only for goals far from the states it can reach, since the relabelled data then does not match the distribution the agent is tested on.

QuestionWhy is the reward in the sparse setting hard to learn from, and what does goal conditioning add to the fix?Show answer

Most episodes earn zero reward, so most updates carry no information about which actions were good. Goal conditioning adds a way to reuse those episodes: any state the agent reached can serve as a goal, so the same run gives a non-zero reward for some goal.

QuestionWrite the Bellman equation for Q*(s, a, g) in words, and say which term carries the goal.Show answer

The value of taking action a in state s toward goal g is the expected immediate reward for that goal, plus the discounted best value of the next state, still toward the same goal g. The goal appears in the reward and in every later value, because the target is fixed for the whole episode.

Sources