Part VI: Sequential Decision Making
Chapter 26: Advanced Reinforcement Learning

Hierarchical RL and Temporal Abstraction

"I do not walk to the kitchen one muscle twitch at a time. I issue an order, 'go to the kitchen', and let some subordinate worry about the thousand little steps. When it reports back, I issue the next order. I am paid to choose goals, not footsteps, and the secret of my productivity is that I never once read the footnotes."

A Manager Policy Delegating the Boring Steps to Its Subordinates
Big Picture

Flat reinforcement learning chooses one primitive action per timestep, so on a task that needs a thousand carefully ordered steps before any reward arrives it must assign credit across a thousand-link chain of decisions, an exponentially thin needle in an exponentially large haystack. Hierarchical reinforcement learning (HRL) breaks that chain by letting an agent act at more than one time scale: a high-level policy commits to a temporally-extended behavior (an "option", or a subgoal handed to a worker) that runs for many primitive steps before returning control, so the high level reasons over a handful of decisions where the flat agent faced a thousand. The options framework formalizes a temporally-extended action as a triple of an initiation set, an intra-option policy, and a termination condition, and it turns the underlying Markov decision process into a semi-Markov decision process (SMDP) whose Bellman equations discount over the random duration of each option rather than over single steps. This section motivates HRL from the long-horizon credit-assignment problem (a direct sequel to the long-dependency pathology of Section 9.4), builds the options/SMDP theory with its option-level Bellman equations, surveys modern HRL (the option-critic, FeUdal manager-worker networks, goal-conditioned HIRO, and unsupervised skill discovery), connects temporal abstraction to planning and world models (forward to Chapter 29), and implements a from-scratch two-level manager-worker hierarchy that solves a sparse-reward gridworld on which a flat $Q$-learner never even finds the goal, then collapses the same idea to a clean library-style version. You leave able to recognize when a task needs temporal abstraction, to read and write option-level value updates, and to build a working hierarchy.

In Section 26.4 we bounded an agent's risk with explicit constraints, keeping it inside a safety budget with constrained, Lagrangian methods. This section changes the unit of decision entirely. Instead of asking the agent to choose better primitive actions, we let it choose behaviors: sequences of primitive actions, bundled and named, selected as wholes. That single move, from acting over actions to acting over temporally-extended actions, is the content of hierarchical reinforcement learning, and it is the most direct structural answer the field has to the problem that flat RL handles worst: the long horizon with a sparse reward. We use the unified notation of Appendix A throughout: $s$ a state, $a$ a primitive action, $\pi$ a policy, $r$ a reward, $\gamma$ the discount, and we add $o$ for an option and $g$ for a subgoal.

Why does temporal abstraction deserve its own section rather than a remark that "you can group actions"? Because grouping actions correctly changes the mathematics of the problem, not just its engineering. An option that runs for a random number of steps turns the discrete-time MDP of Chapter 22 into a semi-Markov process whose value equations must discount over duration, it changes what a Bellman backup means, and it changes the effective horizon over which credit must travel from thousands of primitive steps to a handful of option choices. Get the abstraction right and a task that was hopeless for a flat agent becomes routine; get it wrong (options that never terminate, subgoals the worker cannot reach, a manager that learns faster than its worker) and the hierarchy is worse than the flat baseline it replaced. This section gives you the theory to reason about that boundary and the code to sit on the right side of it.

The competencies this section installs are four. To state precisely why flat RL's credit-assignment difficulty grows with the horizon and the reward sparsity. To define an option as an initiation-set, intra-option-policy, termination triple and to write the SMDP Bellman equations that value options. To place the modern HRL architectures (option-critic, FeUdal, HIRO, skill discovery) on a single map of "where does the abstraction come from". And to implement a two-level hierarchy from scratch and verify it solves a sparse-reward task a flat agent cannot. These are the load-bearing skills for the control-and-imitation methods of Chapter 27 and the planning-over-abstractions of Chapter 29.

1. The Long-Horizon, Sparse-Reward Problem and Why Flat RL Struggles Beginner

A tall manager robot pointing at a far mountain peak while a small worker robot handles the immediate stepping stones, illustrating hierarchical RL splitting long goals into short subgoals.
Figure 26.6: Hierarchy beats long sparse rewards by splitting the job in two: a manager picks distant subgoals while a worker handles the next few steps, so neither has to plan the whole journey at once.

A flat reinforcement learner chooses one primitive action at every timestep and learns from the reward signal that follows. On a task where reward is dense (something informative arrives almost every step) this is fine: the gradient of return with respect to each decision is well estimated because feedback is frequent and local. The trouble begins when the reward is sparse and the horizon is long: the agent must execute a long, specific sequence of primitive actions before any reward distinguishes success from failure, and until that reward arrives every action looks equally good or bad. Credit for the eventual outcome must then be assigned backward across the entire chain, and the longer the chain, the thinner the signal that reaches its early links.

This is the control-theoretic twin of a pathology we have already met. In Section 9.4 we saw that a recurrent network propagating a gradient backward across many timesteps multiplies one Jacobian per step, and the product vanishes (or explodes) geometrically with depth, so a learning signal from the distant future barely reaches the distant past. Temporal-difference credit assignment in a long-horizon MDP has the same shape: bootstrapped value information seeps backward one Bellman backup per step, discounted by $\gamma$ each time, so a reward $H$ steps away is attenuated by $\gamma^{H}$ before it influences an early decision. With $\gamma = 0.99$ and $H = 1000$ that factor is $\gamma^{H} \approx e^{-10} \approx 4.5 \times 10^{-5}$: the reward is, for practical purposes, invisible to the actions that earned it. Long-horizon RL is BPTT's long-dependency problem wearing a control costume.

Compounding the attenuation is exploration. To even observe the sparse reward once, a flat agent acting by local exploration (epsilon-greedy, or small policy noise) must stumble onto the entire successful sequence by chance. If success requires a specific length-$H$ sequence over $|\mathcal{A}|$ actions, the probability of hitting it by undirected exploration is on the order of $|\mathcal{A}|^{-H}$, which is astronomically small for any non-trivial $H$. The agent cannot learn from a reward it never sees, and it cannot reliably see a reward that demands a long precise sequence. The two difficulties reinforce each other: sparse reward starves credit assignment, and the long horizon starves exploration.

Same task, two decision granularities Flat reward H primitive steps, signal attenuated by γH HRL option o₁ option o₂ option o₃ reward 3 option decisions, signal attenuated by γ3 at the option level
Figure 26.5.1: The same sparse-reward task seen by a flat agent (top, $H$ primitive decisions before reward) and a hierarchical agent (bottom, a few option decisions). At the option level the reward is only a handful of decisions away, so the SMDP discount $\gamma^{\tau}$ over option durations attenuates the credit signal far less than the flat $\gamma^{H}$, which is the mechanism by which temporal abstraction shortens the effective horizon.

Temporal abstraction is the structural fix. If the agent could commit to a behavior that runs for many primitive steps, then at the level where it makes decisions the horizon collapses: a task that was a thousand primitive steps deep might be only a handful of option choices deep, and credit must travel across that handful, not the thousand. The discount seen at the option level is $\gamma$ raised to the option's duration rather than to a single step, so reward that was attenuated by $\gamma^{1000}$ at the primitive level might be attenuated by only $\gamma^{3}$ at a three-option level. Acting at multiple time scales is therefore not a convenience but a direct shortening of the chain over which credit must propagate, exactly as Figure 26.5.1 contrasts. The rest of this section makes "a behavior that runs for many steps" mathematically precise and then builds one.

Key Insight: Temporal Abstraction Shortens the Credit-Assignment Horizon

The single reason HRL helps on long-horizon sparse-reward tasks is that it changes the number of decisions between a state and the reward, not the wall-clock length of the episode. A flat agent makes one decision per primitive step, so an $H$-step task is an $H$-decision credit-assignment problem and the reward is discounted by $\gamma^{H}$. A hierarchical agent whose options each run for tens of steps makes $H/\bar{\tau}$ decisions, where $\bar{\tau}$ is the mean option length, so the same task is an $(H/\bar{\tau})$-decision problem and the reward is discounted by roughly $\gamma^{H/\bar{\tau}}$ at the level that matters. Longer options mean fewer decisions mean a shorter effective horizon mean a stronger learning signal reaching early choices. Every benefit of temporal abstraction, easier credit assignment, more directed exploration, faster planning, descends from this one change in decision count.

2. The Options Framework and the Semi-MDP View Intermediate

The classical formalization of a temporally-extended action is the option, introduced by Sutton, Precup, and Singh. An option $o$ is a triple

$$o = \langle \mathcal{I}_o,\; \pi_o,\; \beta_o \rangle,$$

where the three components answer three questions about the behavior. The initiation set $\mathcal{I}_o \subseteq \mathcal{S}$ says where the option may begin: the option is available for selection only in states $s \in \mathcal{I}_o$. The intra-option policy $\pi_o(a \mid s)$ says what the option does while it runs: a policy over primitive actions that executes until termination. The termination condition $\beta_o(s) \in [0,1]$ says when the option ends: the probability that the option stops upon reaching state $s$, returning control to the level above. A primitive action is the degenerate special case of an option that is available everywhere, takes exactly one action, and always terminates after one step ($\beta_o \equiv 1$), so options strictly generalize actions and a flat MDP is an HRL problem with only one-step options.

An agent equipped with a set of options $\mathcal{O}$ chooses among them with a policy over options $\mu(o \mid s)$, which selects an option in an initiation state, runs that option's intra-option policy until it terminates, then chooses the next option, and so on. Crucially the options run for a random number of primitive steps $\tau$ (random because $\beta_o$ is stochastic and the environment is stochastic), and this is what takes us out of the ordinary discrete-time MDP. A process in which actions take random, state-dependent amounts of time is a semi-Markov decision process (SMDP), and the value functions of an SMDP discount over those durations.

Concretely, define for an option $o$ started in state $s$ two SMDP quantities: the expected discounted reward accumulated while the option runs, and the discounted distribution over the state where it terminates. Let $\tau$ be the (random) option duration and $r_{t+1}, \dots, r_{t+\tau}$ the primitive rewards collected. Write the option's reward model and transition model as

$$R_o(s) = \mathbb{E}\!\left[\sum_{i=1}^{\tau} \gamma^{i-1} r_{t+i} \,\Big|\, s_t = s,\, o\right], \qquad P_o(s' \mid s) = \mathbb{E}\!\left[\gamma^{\tau}\,\mathbb{1}[s_{t+\tau} = s'] \,\Big|\, s_t = s,\, o\right],$$

where $R_o(s)$ is the discounted reward earned during the option and $P_o(s' \mid s)$ folds the discount $\gamma^{\tau}$ over the random duration into the transition itself (so $P_o$ is a sub-stochastic, discount-weighted kernel, not a probability distribution). With these two models the option-level Bellman equations have exactly the shape of the ordinary ones, but over options rather than primitive actions:

$$Q_{\mu}(s, o) = R_o(s) + \sum_{s'} P_o(s' \mid s)\, V_{\mu}(s'), \qquad V_{\mu}(s) = \sum_{o} \mu(o \mid s)\, Q_{\mu}(s, o).$$

The optimal option-value equation is the same backup with a max over options available in the initiation set,

$$Q^{*}(s, o) = R_o(s) + \sum_{s'} P_o(s' \mid s)\, \max_{o' \in \mathcal{O}(s')} Q^{*}(s', o'),$$

and a single SMDP $Q$-learning update, after running an option from $s$ to its termination at $s'$ in $\tau$ steps and collecting accumulated discounted reward $G = \sum_{i=1}^{\tau}\gamma^{i-1} r_{t+i}$, is

$$Q(s, o) \;\leftarrow\; Q(s, o) + \alpha\Big[\, G + \gamma^{\tau}\max_{o'} Q(s', o') - Q(s, o)\,\Big].$$

This is ordinary $Q$-learning with two modifications, and both come from the option running for $\tau$ steps rather than one: the immediate reward is the accumulated discounted reward $G$ over the whole option, and the bootstrap discount is $\gamma^{\tau}$ rather than $\gamma$. Setting $\tau \equiv 1$ recovers flat $Q$-learning exactly, which is the precise sense in which the SMDP view generalizes Chapter 22. The decisive consequence is the one Section 1 anticipated: because the bootstrap discount is $\gamma^{\tau}$ with $\tau$ often large, value information jumps many primitive steps in a single backup, so credit propagates across the episode in a number of updates proportional to the option count rather than the primitive-step count.

Numeric Example: The SMDP Multi-Step Discount

Suppose an option runs for exactly $\tau = 20$ primitive steps and collects a reward of $+1$ only on its final step, with $\gamma = 0.95$. The accumulated discounted reward over the option is $G = \gamma^{19}\cdot 1 = 0.95^{19} \approx 0.377$, and the bootstrap of the next option's value is discounted by $\gamma^{\tau} = 0.95^{20} \approx 0.358$. Compare the flat view of the same trajectory: a flat agent would need 20 separate one-step backups, each discounting by $0.95$, for value to travel from the reward back to the option's first state, and after those 20 backups the propagated factor is $0.95^{20} \approx 0.358$, identical, but it took twenty updates instead of one. Now chain three such options before reward (so $H = 60$ primitive steps): the flat agent attenuates by $0.95^{60} \approx 0.046$ and needs sixty backups to move credit end to end, while the three-option agent attenuates by $\gamma^{\tau_1+\tau_2+\tau_3}$ across just three SMDP backups. Same total discounting, an exponential reduction in the number of decisions and updates over which credit must survive: 3 versus 60. That update-count reduction is why the hierarchy learns and the flat agent does not.

Two subtleties matter in practice. First, the option models $R_o$ and $P_o$ are usually not known and must be learned, which is what the intra-option learning methods and the option-critic of Section 3 do. Second, the SMDP view above treats each option as atomic (decisions are made only at option boundaries), but intra-option methods can update an option's value and policy at every primitive step it executes, which is far more data-efficient because every step teaches, not just every boundary. The option-critic of the next subsection is exactly an intra-option, end-to-end-differentiable realization of this idea.

Fun Note: The Tyranny of the Never-Terminating Option

An option that never decides to stop ($\beta_o \equiv 0$) is a manager who delegates a task and then goes on permanent vacation: control never comes back, the high level makes exactly one decision for the whole episode, and the hierarchy quietly degenerates into a single fixed policy. The opposite failure, an option that terminates every step ($\beta_o \equiv 1$), is a micromanager who recalls the worker after every footstep, and the hierarchy degenerates back into the flat agent it was supposed to improve on. Useful temporal abstraction lives in the narrow band between the absentee and the micromanager, and a recurring difficulty in learned HRL is that gradient descent, left unregularized, drifts toward one of these two trivial extremes (usually the micromanager, because terminating often is locally rewarding). The deliberation-cost and termination-regularization tricks of Section 3 exist precisely to keep options from collapsing to length one.

3. Modern HRL: Option-Critic, FeUdal, HIRO, and Skill Discovery Advanced

The classical options framework assumes the options are given. The central question of modern HRL is the opposite: where do the options come from? Four major families answer it differently, and they organize cleanly along that axis.

Learn the options end to end (the option-critic). Bacon, Harb, and Precup's option-critic (2017) makes the intra-option policies $\pi_o$ and the termination functions $\beta_o$ differentiable and trains them, together with the policy over options, directly by gradient ascent on the expected return. The intra-option policy gradient and the termination gradient are derived analogues of the ordinary policy gradient, and the key practical result is the termination gradient: an option's termination probability is pushed up where ending the option and switching is advantageous, and pushed down where continuing is. Left alone this collapses options to length one, so a deliberation cost (a small penalty per option switch) is added to keep options temporally extended, which is the regularizer the fun note above anticipated. The option-critic learns the whole hierarchy from reward alone, with no subgoals specified by hand.

Manager sets a direction, worker follows it (FeUdal networks). The feudal view, from Dayan and Hinton's feudal RL (1993) and modernized by Vezhnevets et al.'s FeUdal Networks (FuN, 2017), splits the agent into a slow Manager and a fast Worker. The Manager operates at a coarse time scale and emits a directional subgoal in a learned latent state space (a vector indicating "move the latent state this way"); the Worker is rewarded, by an intrinsic reward, for producing primitive actions that move the latent state in the Manager's chosen direction. The Manager never speaks in primitive actions and the Worker never sees the environment reward directly, so the temporal abstraction is enforced architecturally: the Manager's directional goals persist over a fixed horizon, giving the hierarchy its multiple time scales by construction.

Manager sets a goal state, worker reaches it (goal-conditioned HRL, HIRO). The most directly useful modern recipe is goal-conditioned: a high-level policy proposes a subgoal $g$ (often a target state or a target in a feature space), and a goal-conditioned low-level policy $\pi(a \mid s, g)$ is trained to reach it, rewarded by an intrinsic reward such as the negative distance to $g$. Nachum et al.'s HIRO (2018) made this work off-policy and data-efficiently, with a goal relabeling trick that re-labels the high-level transitions so the high-level critic stays consistent as the low-level policy changes underneath it. Goal-conditioned hierarchies are attractive because the subgoal interface is interpretable (a subgoal is a place to get to) and because the low level can be reused across tasks that share a state space.

Discover skills with no reward at all (unsupervised skill discovery). A fourth family learns a repertoire of options before any task reward exists, by an intrinsic objective that rewards behaviors for being distinguishable. DIAYN (Eysenbach et al., 2018, "Diversity Is All You Need") maximizes the mutual information between a latent skill code and the states the skill visits, so different codes learn to occupy different regions of state space, yielding a set of distinct, reusable skills with no extrinsic reward. The downstream task then only has to learn a policy over these pre-discovered skills, a much shorter-horizon problem. Skill discovery is the unsupervised-representation-learning idea of Chapter 16 transported into the action space: learn a useful basis of behaviors first, solve tasks over that basis second.

MethodWhere the abstraction comes fromHigh-level interfaceTrained by
Classical optionsgiven by the designeroption id $o = \langle \mathcal{I},\pi,\beta\rangle$SMDP $Q$-learning
Option-criticlearned end to end from rewardoption id, with learned $\pi_o, \beta_o$intra-option + termination gradients, deliberation cost
FeUdal (FuN)architecturally enforceddirectional latent goalManager + Worker, intrinsic directional reward
HIRO (goal-conditioned)learned subgoals, off-policytarget state / feature subgoal $g$off-policy, goal relabeling
DIAYN (skill discovery)unsupervised, before task rewarddiscrete skill code $z$mutual-information / diversity objective
Figure 26.5.2: Modern HRL on one axis, "where does the temporal abstraction come from". The methods range from hand-given options (left) through reward-driven end-to-end learning (option-critic) and architecturally enforced hierarchy (FeUdal) to goal-conditioned subgoals (HIRO) and fully unsupervised skill discovery (DIAYN). The shared idea is a high-level policy choosing a temporally-extended behavior; the methods differ only in what that behavior is and how it is acquired.

Across all four families a single interface recurs: the subgoal as temporal-abstraction boundary. Whether it is an option id, a directional latent vector, a target state, or a skill code, the high level communicates to the low level a commitment that persists for many primitive steps, and that persistence is the temporal abstraction. The design choices, what the subgoal is, how the low level is rewarded for it, how often the high level may change it, are the engineering surface of HRL, and the worked example in Section 5 instantiates the simplest member of the family (a target-cell subgoal with a distance-based intrinsic reward) so the interface is concrete.

Research Frontier: Hierarchy Meets Language and World Models (2024 to 2026)

The most active 2024 to 2026 thread is using language as the subgoal interface. Methods in the spirit of SayCan, Voyager, and Eureka let a large language model propose high-level subgoals ("collect wood", "build a shelter") that a low-level learned or scripted policy executes, turning the language model into a manager policy and grounding its instructions in a learned worker, exactly the manager-worker split of FeUdal with a far richer subgoal space. A second active line couples hierarchy with world models: Director (Hafner et al., 2022) learns a manager that proposes subgoals inside the latent space of a Dreamer-style world model and a worker that reaches them, planning temporally-abstract behaviors entirely in imagination, which is the bridge to Chapter 29. A third line revisits unsupervised skill discovery with metric and Lipschitz-constrained objectives (METRA and related 2023 to 2024 work) that scale skill discovery to high-dimensional pixel control where DIAYN's mutual-information bound was loose. The 2026 practitioner's read: hand-designed options are largely gone, and the live questions are what subgoal space to learn in (raw state, learned latent, or language) and how to discover reusable skills without task reward.

4. Temporal Abstraction in Planning and World Models Advanced

Temporal abstraction pays off twice. Section 1 showed its first payoff, a shorter credit-assignment horizon for learning. Its second payoff is for planning, and it is just as large. Recall from Chapter 22 that planning with a model means simulating forward and backing up values; the cost of a planning rollout scales with how many model steps it spans. If the planner can take option-length steps using the option models $R_o$ and $P_o$ of Section 2, then a plan that would have been a thousand primitive-step rollout becomes a handful of option-step rollouts, an exponential reduction in planning depth, with the SMDP discount $\gamma^{\tau}$ handling the variable durations automatically. Abstraction shortens the planning horizon for exactly the reason it shortens the learning horizon: fewer decisions between start and goal.

This is precisely where HRL meets the world models of Chapter 29. A world model is a learned simulator $\hat{P}(s' \mid s, a)$ that lets an agent imagine consequences without acting; a temporally-abstract world model learns the option-level transition $P_o(s' \mid s)$ directly, predicting where an option will land and how much reward it will accrue without rolling out its every primitive step. Planning over such a model is planning over behaviors, and it is what lets methods like Director (previewed in the research-frontier callout) plan in imagination over hundreds of effective steps at acceptable cost. The two ideas are complementary: the world model supplies the simulator, and temporal abstraction supplies the coarse time scale at which simulating is cheap. Chapter 29 develops learned world models and planning in full; the message to carry there is that hierarchy and planning are not separate tricks but the same lever, fewer decisions, applied once to learning and once to search.

Key Insight: One Lever, Two Payoffs

Temporal abstraction is a single lever, "make each decision span many primitive steps", that pays off in two distinct phases of an agent's life. In learning, it shortens the chain over which TD credit must propagate, so reward signal survives to reach the decisions that earned it (Section 1). In planning, it shortens the depth of model rollouts, so a search reaches a distant goal in a few abstract steps instead of many primitive ones (this section). The option models $R_o, P_o$ of Section 2 are the shared machinery: the same discounted reward-and-transition summary of an option that defines its SMDP value backup also defines the abstract step a planner takes. Build the abstraction once and both the learner and the planner inherit the shorter horizon, which is why temporal abstraction sits at the center of the most capable long-horizon agents in Chapter 29.

5. Worked Example: A Manager-Worker Hierarchy on a Sparse-Reward Gridworld Advanced

We now make the entire section executable on the smallest task that exhibits the long-horizon, sparse-reward difficulty: a gridworld where reward is $+1$ only at a single goal cell far from the start and $0$ everywhere else, with a step budget too short for undirected exploration to stumble onto the goal. We will first show a flat tabular $Q$-learner failing on it, then build a two-level manager-worker hierarchy from scratch that solves it, and finally collapse the hierarchy to a clean, compact version. Code 26.5.1 defines the environment and the flat baseline.

import numpy as np

rng = np.random.default_rng(0)
N = 12                                   # 12x12 grid; goal is far from start -> long horizon
START, GOAL = (0, 0), (N - 1, N - 1)
ACTIONS = [(-1, 0), (1, 0), (0, -1), (0, 1)]   # up, down, left, right
MAX_STEPS = 80                           # budget; shortest path is 22 steps, but sparse reward

def step(pos, a):
    """One primitive step; stay in place if it would leave the grid."""
    nr, nc = pos[0] + ACTIONS[a][0], pos[1] + ACTIONS[a][1]
    if 0 <= nr < N and 0 <= nc < N:
        pos = (nr, nc)
    r = 1.0 if pos == GOAL else 0.0      # SPARSE: reward only at the goal
    return pos, r, pos == GOAL

def flat_q_learning(episodes=4000, alpha=0.5, gamma=0.97, eps=0.2):
    """Tabular epsilon-greedy Q-learning over PRIMITIVE actions."""
    Q = np.zeros((N, N, 4))
    successes = 0
    for ep in range(episodes):
        pos = START
        for _ in range(MAX_STEPS):
            a = rng.integers(4) if rng.random() < eps else int(np.argmax(Q[pos]))
            nxt, r, done = step(pos, a)
            target = r + (0.0 if done else gamma * np.max(Q[nxt]))
            Q[pos][a] += alpha * (target - Q[pos][a])   # flat one-step TD backup
            pos = nxt
            if done:
                successes += 1
                break
    return successes / episodes

print("flat Q-learning success rate: %.3f" % flat_q_learning())
Code 26.5.1: The sparse-reward gridworld and a flat tabular $Q$-learner. The goal sits 22 steps from the start, reward is $+1$ only at the goal, and the per-episode budget is 80 steps, so epsilon-greedy exploration must chain a long correct sequence by chance to ever see the reward. The one-step TD backup is the flat baseline temporal abstraction will beat.
flat Q-learning success rate: 0.004
Output 26.5.1: The flat agent essentially never solves the task: across 4000 episodes its success rate is under one percent, because undirected exploration almost never stumbles onto the distant goal and so the sparse reward never enters its value table to be propagated.

The flat agent fails exactly as Section 1 predicted: it cannot learn from a reward it never sees. Now the hierarchy. The worker is a goal-conditioned policy that, given a target cell as a subgoal, learns to reach it under an intrinsic reward (it gets $+1$ for arriving at its subgoal). The manager chooses subgoals, a coarse grid of waypoint cells, with SMDP $Q$-learning over those subgoal choices, bootstrapping with $\gamma^{\tau}$ over the option duration $\tau$ exactly as Section 2 derived. Code 26.5.2 implements both levels from scratch.

# --- WORKER: goal-conditioned Q over (pos, subgoal) with an INTRINSIC reach reward ---
SUBGOALS = [(r, c) for r in range(0, N, 3) for c in range(0, N, 3)] + [GOAL]  # waypoint cells
SG_INDEX = {g: i for i, g in enumerate(SUBGOALS)}

def reach_subgoal(Qw, pos, g, gamma=0.95, eps=0.2, train=True, alpha=0.5, budget=20):
    """Run the worker toward subgoal g; intrinsic reward +1 on reaching g. Returns
    (new_pos, primitive_steps_tau, extrinsic_reward_sum, reached)."""
    gi = SG_INDEX[g]
    tau, ext = 0, 0.0
    for _ in range(budget):
        a = rng.integers(4) if (train and rng.random() < eps) else int(np.argmax(Qw[pos[0], pos[1], gi]))
        nxt, r_ext, _ = step(pos, a)
        ext += r_ext
        reached = (nxt == g)
        r_int = 1.0 if reached else 0.0                      # INTRINSIC: reach the subgoal
        if train:
            tgt = r_int + (0.0 if reached else gamma * np.max(Qw[nxt[0], nxt[1], gi]))
            Qw[pos[0], pos[1], gi, a] += alpha * (tgt - Qw[pos[0], pos[1], gi, a])
        pos = nxt; tau += 1
        if reached:
            break
    return pos, tau, ext, (pos == g)

# --- MANAGER: SMDP Q-learning over subgoal choices, bootstrapping with gamma**tau ---
def manager_worker(episodes=4000, alpha=0.5, gamma=0.97):
    Qm = np.zeros((N, N, len(SUBGOALS)))              # manager value over (pos, subgoal)
    Qw = np.zeros((N, N, len(SUBGOALS), 4))           # worker goal-conditioned value
    successes = 0
    for ep in range(episodes):
        pos, steps = START, 0
        while steps < MAX_STEPS:
            o = rng.integers(len(SUBGOALS)) if rng.random() < 0.2 else int(np.argmax(Qm[pos]))
            g = SUBGOALS[o]
            nxt, tau, ext, _ = reach_subgoal(Qw, pos, g)       # run the option (worker)
            done = (nxt == GOAL)
            G = ext                                            # accumulated extrinsic reward
            tgt = G + (0.0 if done else (gamma ** tau) * np.max(Qm[nxt]))  # SMDP backup: gamma**tau
            Qm[pos[0], pos[1], o] += alpha * (tgt - Qm[pos[0], pos[1], o])
            pos = nxt; steps += tau
            if done:
                successes += 1; break
    return successes / episodes, Qm, Qw

rate, Qm, Qw = manager_worker()
print("manager-worker success rate: %.3f" % rate)
Code 26.5.2: A two-level manager-worker hierarchy from scratch. The worker is a goal-conditioned $Q$-table trained on an intrinsic reach reward; the manager runs SMDP $Q$-learning over waypoint subgoals, bootstrapping with $\gamma^{\tau}$ over the worker's option duration $\tau$, exactly the option-level backup of Section 2. The manager faces a handful of subgoal decisions where the flat agent faced 80 primitive ones.
manager-worker success rate: 0.971
Output 26.5.2: The hierarchy solves the task the flat agent could not: a 0.97 success rate versus the flat agent's 0.004. The worker quickly learns to reach nearby waypoints under its dense intrinsic reward, and the manager, choosing among only a dozen subgoals, propagates the sparse goal reward across a short option-level chain.

The contrast between Output 26.5.1 and Output 26.5.2 is the whole section in two numbers: under one percent flat, ninety-seven percent hierarchical, on the same environment with the same step budget. The hierarchy wins for the two reasons Section 1 named. The worker's intrinsic reward is dense (reaching a nearby waypoint is easy and frequently rewarded), so the low level learns fast. And the manager's SMDP backup with $\gamma^{\tau}$ moves credit across the episode in a handful of option decisions rather than eighty primitive ones, so the sparse goal reward actually reaches the early subgoal choices. Code 26.5.3 distills the same two-level idea into a compact, reusable shape.

def hierarchical_agent(episodes=4000, gamma=0.97):
    """Compact manager-worker: same algorithm, tightened into a reusable loop."""
    Qm = np.zeros((N, N, len(SUBGOALS)))
    Qw = np.zeros((N, N, len(SUBGOALS), 4))
    wins = 0
    for ep in range(episodes):
        pos, steps = START, 0
        while steps < MAX_STEPS:
            o = rng.integers(len(SUBGOALS)) if rng.random() < 0.2 else int(np.argmax(Qm[pos]))
            nxt, tau, ext, _ = reach_subgoal(Qw, pos, SUBGOALS[o])   # option = worker reaching subgoal
            done = nxt == GOAL
            tgt = ext + (0.0 if done else (gamma ** tau) * Qm[nxt].max())  # one SMDP line
            Qm[pos[0], pos[1], o] += 0.5 * (tgt - Qm[pos[0], pos[1], o])
            pos, steps = nxt, steps + tau
            if done:
                wins += 1; break
    return wins / episodes

print("compact hierarchical success rate: %.3f" % hierarchical_agent())
Code 26.5.3: The clean version. The manager loop is the single SMDP backup line tgt = ext + gamma**tau * Qm[nxt].max() reusing the worker of Code 26.5.2; the whole hierarchical controller is about a dozen lines, down from the roughly forty lines of the two explicit levels in Code 26.5.2. A production stack (option-critic in a library, or HIRO via an offline-RL toolkit) would learn $\pi_o$ and $\beta_o$ with neural networks but keeps this exact $\gamma^{\tau}$ SMDP backup at its core.
compact hierarchical success rate: 0.969
Output 26.5.3: The compact controller matches the explicit two-level version (0.969 versus 0.971), confirming the dozen-line distillation lost no capability: the SMDP backup and the goal-conditioned worker are the entire mechanism.

Read the three code blocks together. Code 26.5.1 established that a flat $Q$-learner cannot solve the sparse-reward task at all. Code 26.5.2 built the manager-worker hierarchy from scratch and solved it, with the worker's dense intrinsic reward and the manager's $\gamma^{\tau}$ SMDP backup doing exactly the work Sections 1 and 2 predicted. Code 26.5.3 compressed the controller to a dozen lines without losing the win. The pedagogical payoff is that temporal abstraction is not exotic: a goal-conditioned worker plus an SMDP backup over subgoals is enough to turn an unsolvable task into a nearly solved one.

Practical Example: A Warehouse Robot That Could Not Find the Charger

Who: A robotics team training a mobile picking robot in a warehouse simulator, with the sensor-and-telemetry control stack threaded through Chapter 32.

Situation: The robot earned reward only for completing a full pick-and-deliver cycle: navigate to a shelf, grasp an item, navigate to a packing station, release. Each cycle was hundreds of low-level motor steps and the reward came once, at the very end.

Problem: A flat deep-RL agent trained for days and never completed a single cycle in evaluation, because, exactly as Output 26.5.1 shows in miniature, undirected exploration never chained the full sequence to see the terminal reward, so nothing entered the value estimate to be learned from.

Dilemma: Reward shaping by hand (adding intermediate rewards) risked the classic failure of the robot gaming the shaped reward instead of completing the task, while waiting for the flat agent to discover the cycle was, by the $|\mathcal{A}|^{-H}$ argument of Section 1, hopeless within any realistic compute budget.

Decision: They adopted a goal-conditioned hierarchy in the HIRO style: a manager proposing target poses (go to shelf, go to station) and a goal-conditioned worker rewarded intrinsically for reaching each pose, with the manager trained by an SMDP backup over the worker's variable option durations, the structure of Code 26.5.2 scaled up with neural networks.

How: The worker reached poses under a dense distance-based intrinsic reward (frequent, easy to learn), and the manager chose a short sequence of poses per cycle, so its credit-assignment problem was a handful of pose decisions rather than hundreds of motor steps, with the off-policy goal relabeling of HIRO keeping the manager consistent as the worker improved.

Result: The hierarchy completed full cycles within hours of training where the flat agent had failed for days, and the learned worker transferred unchanged to a second task (restocking) that shared the navigation skill, the skill-reuse benefit Section 3 noted for goal-conditioned hierarchies.

Lesson: When reward is sparse and the horizon is long, do not shape the reward by hand and hope; give the agent a temporal-abstraction interface (subgoals plus a dense intrinsic reach reward) so the manager faces a short-horizon problem and the worker faces a dense one, and reuse the worker across tasks that share its state space.

Library Shortcut: Options and Skills Off the Shelf

The from-scratch manager and worker of Code 26.5.2 ran about forty lines of explicit two-level bookkeeping (a separate $Q$-table, intrinsic-reward loop, and SMDP backup for each level). Modern frameworks supply the pieces. A goal-conditioned hierarchy can be assembled from a goal-conditioned policy and a hindsight-relabeling replay buffer in a few lines on top of Stable-Baselines3 or CleanRL, with the relabeling and off-policy bookkeeping handled internally; offline skill-conditioned policies come pre-built in d3rlpy; and Gymnasium's wrappers standardize the goal-conditioned observation interface so the manager and worker plug together without custom glue.

import gymnasium as gym
from stable_baselines3 import SAC
from stable_baselines3.her import HerReplayBuffer

# A goal-conditioned worker with Hindsight Experience Replay: the intrinsic
# reach-the-subgoal learning of Code 26.5.2, but relabeling and goal handling are internal.
env = gym.make("FetchReach-v3")                          # dict obs: observation + desired_goal
worker = SAC("MultiInputPolicy", env, replay_buffer_class=HerReplayBuffer,
             replay_buffer_kwargs=dict(n_sampled_goal=4, goal_selection_strategy="future"))
worker.learn(total_timesteps=20_000)                     # learns to reach arbitrary subgoals
# A manager then issues desired_goal vectors and bootstraps with gamma**tau over option length.

The worker's roughly twenty lines of explicit goal-conditioned $Q$-learning and intrinsic reward collapse to the three lines that construct and train an HER-equipped agent, with the subgoal relabeling, the replay buffer, and the off-policy updates all internal. The manager keeps the one $\gamma^{\tau}$ SMDP backup of Code 26.5.2 unchanged, because that is the irreducible content of temporal abstraction.

Common Pitfalls in Hierarchical RL

Four mistakes recur when readers first build a hierarchy, and naming them now saves days of failed runs:

Exercise 26.5.1: Why the SMDP Discount Is an Exponent Conceptual

Starting from the SMDP $Q$-learning update $Q(s,o) \leftarrow Q(s,o) + \alpha[\,G + \gamma^{\tau}\max_{o'}Q(s',o') - Q(s,o)\,]$, explain in one paragraph why the bootstrap discount is $\gamma^{\tau}$ rather than $\gamma$, and show that setting $\tau \equiv 1$ recovers flat one-step $Q$-learning exactly. Then argue, using the $\gamma^{H/\bar\tau}$ versus $\gamma^{H}$ comparison of Section 1, why a hierarchy with mean option length $\bar\tau = 20$ propagates credit across an $H = 200$ task in roughly ten SMDP backups instead of two hundred flat ones.

Exercise 26.5.2: Add Learned Termination to the Worker Coding

Extend the worker of Code 26.5.2 so each option also learns a termination function $\beta_o(s) \in [0,1]$ instead of always running until it reaches the subgoal or exhausts its budget. Add a small deliberation cost (a fixed penalty subtracted from the manager's reward each time it switches options) and sweep that cost from $0$ to $0.5$. Plot the mean option length $\bar\tau$ and the task success rate against the deliberation cost, and confirm that with zero cost the options collapse toward length one (the micromanager failure of the fun note) while too large a cost makes the manager commit too long.

Exercise 26.5.3: Discover Skills Without the Goal Reward Open-Ended

Replace the hand-chosen waypoint subgoals of Code 26.5.2 with a small unsupervised skill-discovery objective in the DIAYN spirit: train a fixed number of discrete "skills", each a policy conditioned on a skill code, by rewarding each skill for visiting states that a learned discriminator can tell apart from the other skills' states. Then train the manager to select among the discovered skills to reach the goal. Compare its success rate and sample efficiency against the hand-designed-subgoal version, and discuss when unsupervised skills help (reusability, no subgoal design) versus when hand-designed subgoals are simply better (you already know the task structure). There is no single right answer; argue the trade against the four families of Section 3.

Key Insight: In-Context RL as a Form of Meta-RL

The manager-worker hierarchy is one answer to the question "how does an agent reuse prior experience efficiently?" A complementary and increasingly important answer is in-context reinforcement learning: rather than storing experience in learned weights or a replay buffer, the agent's entire interaction history (states, actions, rewards) is packed into the context window of a large language or sequence model, and the model improves its policy within a single episode by reading its own past. The canonical formal treatment of this idea is Algorithm Distillation (AD) (Laskin et al., 2022) and its extension to the in-context RL framework studied by LaMer (Lee et al., 2023, "In-context Reinforcement Learning with Algorithm Distillation"). The connection to meta-RL is direct: meta-RL (the subject of its own treatment in advanced RL curricula) trains an agent across a distribution of tasks so that it can adapt to a new task in a small number of interactions, storing its adaptation trajectory in a hidden state. In-context RL achieves the same outcome through a different substrate: the context window of a large transformer is the hidden state, and the model's in-weights prior (trained on many RL histories) corresponds to the meta-learned initialization. Both paradigms answer the same question: how do you make an agent that improves across a new task within minutes of interaction rather than hours of gradient descent? The difference is where the fast-adaptation mechanism lives: in a recurrent hidden state for classical meta-RL, in a long context window for in-context RL. As transformer context windows grow, in-context RL subsumes more of the use cases that required dedicated meta-RL architectures, and the 2024 to 2026 trend is toward treating meta-RL and in-context RL as two points on one spectrum rather than two separate paradigms.

Looking Back: From Reward-Driven Hierarchy to Structure and Demonstrations

This section closes Chapter 26, our deep dive into advanced reinforcement learning, with the field's structural answer to the long horizon: act at multiple time scales, value options over an SMDP, and let a manager delegate to a worker so credit travels across a handful of decisions instead of thousands. Every method in this chapter, from the safety constraints of Section 26.4 to the hierarchies here, has assumed that reward is the teacher: the agent acts, the environment scores, and learning extracts the value of those scores. Chapter 27 questions that assumption. Optimal control replaces trial-and-error with explicit models and cost functions, solving for the best action sequence when the dynamics are known, and imitation learning replaces reward entirely with demonstrations, learning a policy from an expert's behavior when reward is hard to specify but examples are easy to obtain. The temporal-abstraction interface you built here reappears there too: a demonstrated trajectory is naturally segmented into skills, and an imitation learner can recover options from it. We move from learning behavior out of reward to learning it out of structure (control) and example (imitation), the two settings where pure reward is the wrong tool.

We have turned the long-horizon, sparse-reward problem that defeats flat RL into a short-horizon problem by changing the unit of decision from the primitive action to the temporally-extended option, formalized that change as an SMDP with $\gamma^{\tau}$ Bellman backups, surveyed the modern methods that learn the abstraction rather than hand it over, and built a manager-worker hierarchy that solves a task a flat agent never even reaches. The thread that runs through every part of this, fewer decisions between a state and its reward, is the same thread that will run through planning in Chapter 29. Next, Chapter 27 sets reward aside altogether and asks what an agent can learn when it is given a model and a cost (optimal control) or an expert's demonstrations (imitation learning) instead.