In reinforcement learning with eligibility traces, a key challenge arises in off-policy control: when the agent takes an action that deviates from the greedy policy, the eligibility traces accumulated so far may no longer be valid for updating toward the optimal value function.
Action-conditional trace clearing addresses this by resetting all eligibility traces to zero whenever a non-greedy action is observed. When the agent follows the greedy policy, traces decay normally by a factor of . When the agent deviates (e.g., explores), all traces are wiped clean so that past state-action pairs do not receive credit through an exploratory step.
Implement a function that processes a single episode and updates Q-values using accumulating eligibility traces with action-conditional clearing.
Inputs:
Q: numpy array of shape (n_states, n_actions) -- current Q-valuesepisode: list of tuples (state, action, reward, next_state, done, next_action_greedy) where:
state, action: current state and action takenreward: reward receivednext_state: resulting statedone: boolean, True if the episode terminates after this stepnext_action_greedy: boolean, True if the action that will be taken at next_state is greedy (ignored when done=True)gamma: discount factoralpha: learning ratelam: trace decay parameter (lambda)Processing each step:
Returns: