Implement a function that performs TD(0) value function updates using binary feature representations common in reinforcement learning.
In many RL applications (such as tile coding or coarse coding), states are represented by binary feature vectors where most entries are 0 and only a few are 1. Rather than storing full dense feature vectors, these are represented as lists of active feature indices. A linear value function estimates the value of a state as the sum of weights corresponding to the active features.
Given a weight vector and a sequence of transitions, apply semi-gradient TD(0) updates to the weights. Each transition is a tuple containing:
active_s: list of active feature indices for the current statereward: the observed reward (float)active_s_next: list of active feature indices for the next state (empty list if terminal)done: boolean indicating if the episode terminatedFor each transition, compute the TD error using the current weights, then update the weights. Only the weights at active indices of the current state should be modified.
Parameters:
initial_weights: list of floats, the starting weight vectortransitions: list of tuples as described abovealpha: float, the step sizegamma: float, the discount factorReturn the final weight vector as a numpy array.