Concepts / Tabular n-step TD algorithm

Tabular n-step TD algorithm

The semi-gradient n-step TD algorithm is the function-approximation extension of tabular n-step TD.

  • Programming

Waiting Before Updating

The n-Step TD Algorithm does not immediately update the value estimate for the state just observed. Instead, it waits for several steps of experience, uses those rewards to construct an n-step return G, and then updates an earlier state estimate. The state being updated is the state at time τ, not necessarily the state at the current time step.

The method connects two operations: first calculate the return for Sτ, then move V(Sτ) toward that return.

waitcombine with discountingcontinue to n-step boundarybootstrap when appropriateform targetSτstate being updatedRτ+1reward contributionRτ+2reward contributionRτ+nreward contributionV(Sτ+n)included if termination hasnot occurredGtarget for V(Sτ)
How are rewards from time τ through τ+n−1 combined, and when is the value estimate at time τ+n included?

Finding the Updated State

As experience arrives one step at a time, the algorithm identifies an earlier time index τ. That index determines which state is eligible for an update. The value changed by the update is V(Sτ). The current time step supplies new experience, but τ points back to the state whose return can now be calculated.

selectsupdatesτeligible earlier indexSτstate at τV(Sτ)value estimate updated
As experience arrives, which earlier time index τ becomes eligible, and which state value is changed?

Constructing the Return

The n-step return combines discounted rewards collected from time τ through the n-step boundary. If the episode has not terminated by that boundary, the return also includes a discounted later value estimate, V(Sτ+n). This later estimate is the bootstrapped part of the target. If termination occurs before the boundary, the return uses the rewards available up to termination instead of continuing with a later value estimate.

G = discounted rewards from time τ through time τ+n−1, plus a discounted V(Sτ+n) when the n-step boundary occurs before termination

A three-step target

Suppose the three discounted reward contributions available for Sτ are 2, 1.5, and 0.5. The episode has not terminated at the three-step boundary, and the discounted later value contribution is 3.

Combine rewards: The reward portion of the return is 2 + 1.5 + 0.5 = 4.

Add the continuation estimate: Because the episode has not terminated at the boundary, include the later value contribution of 3.

Form G: The resulting target is G = 4 + 3 = 7.

The n-step return used for Sτ is G = 7.

The numerical contributions in this example are illustrative. The important structure is that several rewards are combined first, and a later value estimate is included only when the episode continues far enough for bootstrapping to apply.

Applying the Value Update

After G has been calculated, the tabular update is V(Sτ) ← V(Sτ) + α[G − V(Sτ)]. The step size α lies in the interval (0, 1], so the update moves the current estimate partway toward G rather than replacing it directly.

Moving toward the return

Suppose V(Sτ) = 4, the calculated return is G = 10, and α = 0.5.

Find the difference: G − V(Sτ) = 10 − 4 = 6.

Scale the difference: α[G − V(Sτ)] = 0.5 × 6 = 3.

Update the estimate: V(Sτ) becomes 4 + 3 = 7.

The updated value estimate is V(Sτ) = 7, not G = 10.

The update changes the value associated with Sτ. It does not directly replace that value with G; α controls how much of the difference is applied.

Return Versus Procedure

The n-step return equation and the complete n-step TD algorithm are related but not identical. The return describes how to construct the target G from rewards and, when appropriate, a later value estimate. The complete algorithm describes when experience is collected, how τ is identified, when the return can be calculated, how terminal time T affects the calculation, and when the update is performed.

thencheckyesuse Gcheck episodenot reachedreachedno update yetObserve transitionnext reward and stateIdentify τearlier eligible indexτ eligiblecontinue only when updateis possibleCalculate Grewards plus conditionalbootstrapUpdate V(Sτ)move toward GTerminal time Tlimits further experienceFinish pendingupdatesafter termination
What happens at each time step from observing a transition through computing G, updating V(Sτ), and finishing after termination?

Terminal time T is part of the control flow, not merely a detail of the return formula. While the process is before T, the algorithm gathers the next reward and state. Once termination occurs, it must stop extending the experience and complete the updates that are still pending. The reward sum is also limited by termination, so the algorithm must not bootstrap beyond the terminal point.

From Tables to Function Approximation

Part of the methodTabular n-step TDSemi-gradient n-step TD
Experience structureUses several steps of episode experienceUses the same multi-step experience structure
TargetUses an n-step return GUses a generalized n-step return
Value representationUses tabular state valuesUses a function-approximation value estimate
Conceptual relationshipBase methodNatural function-approximation extension

The semi-gradient n-step TD algorithm is not presented as an unrelated method. It begins with the tabular n-step TD method, generalizes the n-step return, and uses that generalized return in a function-approximation setting. The key conceptual path is therefore tabular method, generalized return, then semi-gradient method.

Control-Flow Mistakes

  • Updating a value when τ is negative.

    A negative τ refers to an earlier index that is not yet available for the algorithm's update scheme.

    Fix: Check the update condition and proceed only when τ is eligible.

  • Never updating when τ is nonnegative.

    An eligible state has been identified, but control flow prevents its return from being calculated or its value from being changed.

    Fix: Trace the condition around τ, the return calculation, and the update statement.

  • Bootstrapping after the episode has terminated.

    The return should stop at termination and omit the continuation value beyond the terminal point.

    Fix: Use the later value estimate only when the episode continues to the n-step boundary.

  • Treating the return equation as the whole algorithm.

    The equation defines the target, while the complete procedure determines when that target can be used.

    Fix: Check both target construction and the surrounding time and termination control flow.

  • Replacing V(Sτ) directly with G.

    The tabular update moves the estimate by α[G − V(Sτ)], using a fraction controlled by the step size.

    Fix: Apply the full update expression and verify that α is in (0, 1].

Practice Check

EASY

An implementation has collected enough experience for an earlier state Sτ. The episode has not terminated at the n-step boundary, and the return calculation is complete. Explain which value is updated, what target it moves toward, and why the later value estimate is included.

Hints
  • The time index in the update is τ, not the current time index.
  • The target is the n-step return G.
  • The later value estimate is included because termination has not occurred at the boundary.
MEDIUM

A terminal transition occurs before the n-step boundary. List the two changes this causes in the return and in the continuing control flow.

Hints
  • Consider whether a later value estimate can still be used.
  • Consider whether the algorithm should continue gathering new experience.

Key Takeaways

  1. n-step TD waits for several steps of experience before updating an earlier state estimate.
  2. The return G combines discounted rewards and, when the episode continues to the boundary, a discounted later value estimate.
  3. The update changes V(Sτ) using V(Sτ) ← V(Sτ) + α[G − V(Sτ)].
  4. The return equation is one part of the complete algorithm; τ, terminal time T, eligibility checks, and pending updates belong to the surrounding procedure.
  5. Semi-gradient n-step TD extends the tabular method by generalizing the return for function approximation.

Key Takeaways

  • The algorithm delays an update so that an earlier state can use several rewards.
  • τ identifies the state whose value estimate is updated.
  • G contains discounted rewards and conditionally includes a later value estimate.
  • The complete procedure must handle τ, terminal time T, update eligibility, and pending updates.
  • The semi-gradient version is the function-approximation extension of tabular n-step TD.