An episode return sums discounted observed rewards, while a temporal-difference update changes a state estimate using the next reward and a bootstrapped next-state estimate.
Episode returns, temporal difference and terminal states
Keep the two targets separate
A completed stock-control episode reveals a realized sequence of rewards. Backward return calculation uses the rewards through the end. A one-step temporal-difference update uses the current reward plus a discounted estimate of the next state; it can update before the episode finishes. Neither is automatically unbiased for an operational policy when the log selectively records episodes. The trajectory contract supplies the event order.
Stop bootstrapping at a real terminal
When the process reaches closed, its continuation value is zero. The code calculates all discounted returns for one fixed episode and updates a state estimate with a terminal transition. A time-limit truncation may still have future value; treating every cutoff as terminal can bias learning. Log why the episode stopped. Reward maturity also determines whether a transition is ready for training.
Know what each method trades
Full returns reflect realized later outcomes but can be variable and require completed episodes. Bootstrapping can reduce delay and variability but introduces bias from the current value estimate. Compare learning stability on held-out trajectories and across reward timings. A single illustrative episode cannot establish that one estimator is superior. Control learning adds the further question of which action to choose.
Keep behavior policy visible
The observed episode came from a policy that chose replenishment, dispatch and then waiting. Its returns describe those choices. They are not the value of an arbitrary alternative policy, because that alternative may visit different states. Offline support checks what logged transitions can identify.
Assess objective sensitivity
If shortage penalties are recorded late, a quick proxy can make waiting appear attractive until the final outcome arrives. Compare estimated values with mature service outcomes and staffing cost. The project records the reward clock before policy review.
Implementation
episode = [
("low-stock", "replenish", -2.0, "ready-stock", False),
("ready-stock", "dispatch", 5.0, "low-stock", False),
("low-stock", "wait", -7.0, "closed", True),
]
discount = 0.80
def discounted_returns(trajectory, gamma):
result = [0.0] * len(trajectory)
continuation = 0.0
for position in range(len(trajectory) - 1, -1, -1):
_, _, reward, _, terminated = trajectory[position]
continuation = reward + (0.0 if terminated else gamma * continuation)
result[position] = continuation
return result
def td_update(current_value, reward, next_value, terminated, rate, gamma):
target = reward + (0.0 if terminated else gamma * next_value)
return current_value + rate * (target - current_value)
returns = discounted_returns(episode, discount)
terminal_update = td_update(-1.0, -7.0, 99.0, True, 0.5, discount)
assert abs(returns[0] - (-2.48)) < 1e-9
assert terminal_update == -4.0Performance and operating cost
Backward returns over T transitions cost O(T) time and O(T) output storage; a one-step temporal-difference update is O(1) given current values. Delayed or incomplete episodes increase storage and waiting cost. Statistical uncertainty and policy-selection bias require more than this arithmetic.
Common Mistakes
- Do not bootstrap from the next state after a true terminal transition.
- Do not confuse one episode’s realized return with a population policy value.
- Do not train on an outcome before its declared reward window has matured.
