Implement a function error_dependent_updates that performs Q-learning with a variable number of weight updates per transition, where the number of updates is determined by the magnitude of the temporal-difference (TD) error.
The core idea is that transitions with large TD errors represent surprising experiences that warrant more learning iterations, while small errors only need a single update. This adaptive approach allocates more computation where it matters most.
Your function receives:
transitions: A list of tuples (state, action, reward, next_state, done) representing the agent's experience stream. States and actions are integers. If done is True, the transition is terminal.num_states: Total number of states.num_actions: Total number of actions.alpha: Learning rate for Q-value updates.gamma: Discount factor.error_threshold: A positive float that controls how the TD error magnitude maps to the number of updates.max_updates: An integer cap on the maximum number of updates per transition.For each transition in order:
k to perform: use integer floor division of the absolute TD error by the threshold, then clamp to be at least 1 and at most max_updates.k successive updates to Q(s, a), recomputing the TD error from the current Q-values before each individual update.Initialize all Q-values to zero.
Return a dictionary with:
'Q': The final Q-table as a list of lists, with values rounded to 4 decimal places.'update_counts': A list of integers, one per transition, indicating how many updates were performed for that transition.