In temporal-difference learning, n-step methods bridge the gap between one-step TD methods and full Monte Carlo returns. The n-step SARSA algorithm uses a multi-step return that looks ahead n steps into the future before bootstrapping from the current action-value estimate.
Implement a function n_step_sarsa_update(episode, Q, n, alpha, gamma) that processes a complete episode trajectory and returns updated Q-values.
episode: A list of (state, action, reward) tuples representing a complete episode. Each tuple at index t contains the state visited, the action taken, and the reward received at that time step. The episode terminates after the last tuple.Q: A dictionary mapping (state, action) pairs to their current estimated values (floats).n: An integer specifying the number of lookahead steps.alpha: The learning rate (float).gamma: The discount factor (float).Return a new dictionary with the same keys as Q, containing the updated Q-values after processing all time steps of the episode sequentially. Updates should be applied incrementally -- each update modifies the Q-values used by subsequent updates within the same episode.
n, use all remaining rewards without a bootstrap term.t + n.t = 0, 1, 2, ... in order.