Concepts / n-step Sarsa

n-step Sarsa

The n-step target is a quantity used by an action-value update rule.

  • Programming

Delayed Learning Signal

n-step Sarsa delays an action-value update until rewards from several steps can contribute to the return. The method therefore has two separate ideas to keep distinct: first, it constructs an n-step target; second, it uses that target in an action-value update. The target is a learning quantity supplied to the update rule. It is not the update rule itself.

Target into Update

The n-step target summarizes information from a later portion of the episode. That information can include several successive rewards and, when the episode has not ended within the available range, a later estimated action value Q(Sτ+n, Aτ+n). Once the target has been obtained, the usual action-value update rule from n-step Sarsa uses it to update the earlier action value associated with time τ. In other words, the target answers what learning quantity is available, while the update rule answers how that quantity changes an action value.

contributemay contributesuppliesupdatesSeveral rewardsinformation from laterstepsn-step targetlearning quantityAction-value updateruleuses the targetEarlier action valuevalue associated with τLater Q estimateincluded when the episodecontinues
How does the computed n-step target flow into an update of the action value, and why is the target not itself the update?

Separating the calculation from the change

Suppose an earlier state-action pair is identified by τ. Several later rewards are available, and the episode has not ended before the n-step window finishes.

Construct the target: Combine the rewards available in the n-step window with the later estimated action value Q(Sτ+n, Aτ+n).

Identify the update location: Use τ to identify the earlier state-action pair whose action value is being updated.

Apply the action-value rule: Pass the target into the usual action-value update rule. The target supplies the learning quantity; it does not replace the update rule.

The result is an updated action value for the state-action pair at time τ, not merely the target by itself.

Building the Return Window

The n-step target is built from a window that begins with the experience at the update time τ. Rewards from successive steps contribute to that window. If τ + n is still before the terminal time T, the target also includes the discounted later action value Q(Sτ+n, Aτ+n). If the episode ends within the available range, the return stops at the terminal time instead of using that later Q estimate.

discounted contributiondiscounted contributiondiscounted contributionlater estimatefactor conventionReward 1first contributionn-step targetcombined returnReward 2next contributionEmpty productequals 1Reward nlast reward in windowLater Q estimateused when τ + n is before T
How do successive rewards and a later estimated action value combine over the n-step window, including when a product has zero factors?

Continuing versus terminal windows

Compare two n-step windows that begin at the same update time τ.

Continuing episode: When τ + n is still before T, use the rewards in the window and include the later estimated action value Q(Sτ+n, Aτ+n).

Episode ending inside the window: When the episode ends before the window reaches τ + n, use the rewards up to T and stop there.

Boundary product: If part of the construction contains no factors, apply the convention that the product of zero factors equals 1.

The terminal time determines whether the target ends with rewards alone or also contains a later Q estimate.

Reading τ, T, and n

The pseudocode advances through an episode while storing states, actions, and rewards. At each processing time t, it calculates the update time as τ = t - n + 1. This delay means the algorithm can update an earlier state-action pair only after enough subsequent experience has become available. The value τ identifies which earlier pair is updated, n determines the length of the delayed information window, and T marks when the episode ends.

τ = t - n + 1identifieswindow extends n stepscompare withepisode endsτearlier pair being updatedAction-value updatefor the pair at τtcurrent processing timeEpisode stopafter terminal timeτ + nend of information windowTepisode-ending time
How do the update time τ, episode-ending time T, and step count n determine which experience is updated and when the algorithm stops?
SymbolRoleQuestion to ask while tracing
τUpdate timeWhich earlier state-action pair is being updated?
TEpisode-ending timeHas the episode ended before the full window is available?
nStep countHow many steps of later information can contribute?
τ + nWindow endpointShould a later Q estimate be included?

The main time markers in an n-step Sarsa execution.

Action Selection and Tree Backup

During n-step Sarsa, an ε-greedy policy selects actions by combining greedy choice with random action selection. The selected actions become part of the states, actions, and rewards stored for the delayed return. The source also states that, in the stated Tree Backup formulation, the Tree Backup target is used with the usual action-value update rule from n-step Sarsa. Thus, the shared point is the update rule that consumes a target. The difference described at this level is how Tree Backup accounts for alternative actions in its backup, rather than following only the sampled action sequence in the same way.

generates sampled actionssupplies targetsupplies targetupdatesε-greedy policyselects actionsn-step Sarsa targetsampled action sequenceUsual action-valueupdateuses a targetAction valueupdated at τTree Backup targetaccounts for alternativeactions
Which parts of the backup are shared by Tree Backup and n-step Sarsa, and where does Tree Backup account for alternative actions differently?

Tracing a Divergence

Read the pseudocode as an execution trace rather than as isolated lines. A useful debugging trace follows the information in order: the transition that produced the next reward and state, whether that next state was terminal, the computed τ, whether τ was nonnegative, and whether the later Q term belongs in the target. The first incorrect landmark is usually more informative than the final incorrect action value.

inspectcontinue tracetestif update is dueconstruct targetNext transitionreward and next stateTerminal checkis the next state terminal?τ calculationτ = t - n + 1τ nonnegativeis an update due?Later Q checkdoes Q(Sτ+n, Aτ+n) belong?Computed updatecompare with expectedresult
As an execution proceeds step by step, where does the computed result first diverge from the expected result?

A trace with an incorrect window decision

An expected result includes only rewards because the episode ended before the full n-step window. An observed result also includes a later Q estimate.

Inspect the transition: Confirm which reward and next state were produced by the transition.

Inspect termination: Check whether the next state was terminal. If the episode ended within the available range, the return should stop at T.

Inspect τ: Recompute τ as t - n + 1 and verify that the update concerns the intended earlier state-action pair.

Inspect the later Q term: Include Q(Sτ+n, Aτ+n) only when τ + n is still before T. If the episode already ended, its presence explains the divergence.

The first divergence is the decision about whether the episode has ended before the full n-step window. That decision controls whether a later Q estimate belongs in the target.

Credit Along a Successful Path

The Gridworld example in the source illustrates why n-step methods can learn faster from one successful episode. Suppose an agent follows a sequence of actions and eventually reaches a location of high reward. With one-step Sarsa, only the last action in the sequence leading to that high reward is strengthened by that episode. With n-step Sarsa, the last n actions in the sequence can be strengthened, so information from the high reward travels farther backward through the successful path.

strengthensreachesreachescan reachOne-step Sarsalast action strengthenedHigh rewardend of successful pathLast actionclosest action in pathn-step Sarsalast n actions strengthenedEarlier actionsadditional actions within nsteps
How far backward through a successful action sequence does credit travel with n-step Sarsa compared with one-step Sarsa?

Common Trace Mistakes

  • Treating the n-step target as the update itself.

    The target is a quantity used by the usual action-value update rule. It is not the same object as the rule that updates the action value.

    Fix: Track the target first, then track the action-value update for the earlier pair identified by τ.

  • Updating the most recent state-action pair instead of the pair at τ.

    The pseudocode calculates τ as t - n + 1, and τ identifies the earlier pair whose n-step information is now available.

    Fix: Recompute τ at each processing time and use it to locate the action value being updated.

  • Including a later Q estimate after the episode has already ended.

    When the episode ends within the available range, the return uses rewards up to T instead of a later action value.

    Fix: Compare τ + n with T before deciding whether the later Q term belongs in the target.

  • Ignoring the zero-factor product convention.

    The stated convention defines a product of zero factors as 1.

    Fix: Use 1 for a product containing no factors.

  • Assuming n-step learning makes every value immediately correct.

    The advantage is that information travels farther backward in one episode, not that every estimate becomes immediately correct.

    Fix: Ask how far the selected n-step return carries information and whether the episode ended before the full window was available.

Trace Practice

MEDIUM

A trace reports that an n-step target includes a later Q estimate. Before accepting the result, list the checks you would perform in order: inspect the transition and next state, check whether the next state was terminal, recompute τ, verify that τ is nonnegative, and compare τ + n with T. State which comparison determines whether the later Q estimate belongs in the target.

Hints
  • The update location is identified by τ, not simply by the most recent time.
  • The terminal time determines whether the return stops with rewards.
  • A later Q estimate belongs only when τ + n is still before T.

What do you think happens?

If an episode ends before the n-step window reaches τ + n, should the target include Q(Sτ+n, Aτ+n)?

  • Yes, because every n-step target always includes a later Q estimate.
  • No, the return stops at the terminal time and uses the available rewards.
  • Only if τ is negative.
Reveal answer

Answer: No, the return stops at the terminal time and uses the available rewards.

The later Q estimate is included when τ + n is still before T. If the episode ends within the available range, the return stops at T instead.

Essential Takeaways

  1. The n-step target is a learning quantity, while the action-value update rule uses that quantity to change an earlier action value.
  2. The target combines several later rewards and, when the episode continues far enough, a later estimated action value.
  3. The update time is τ = t - n + 1; n controls the delayed window, and T controls whether the return stops at the episode ending or includes a later Q estimate.
  4. A product of zero factors equals 1, and this convention must be applied when the target construction reaches an empty product.
  5. n-step methods can carry information from a high reward back through the last n actions of a successful sequence, whereas one-step Sarsa strengthens only the last action from that episode.

Key Takeaways

  • Separate the n-step target from the action-value update rule that consumes it.
  • Use τ to locate the earlier state-action pair being updated and T to determine whether a later Q estimate is allowed.
  • Trace transitions, termination, τ, and the later Q decision in that order when debugging.
  • Remember that n-step returns can strengthen more of a successful action sequence than one-step Sarsa.