In model-based reinforcement learning, an agent can learn a model of the environment and use it to plan updates without additional real interaction. A naive approach (like Dyna-Q) performs random planning updates. Prioritized sweeping improves efficiency by focusing updates on state-action pairs whose values would change the most.
The core idea is to maintain a priority queue that tracks how "urgent" each update is. When a Q-value changes significantly, all predecessor state-action pairs that transition into the updated state may also need updating, so they are added to the queue with appropriate priorities.
Implement prioritized_sweeping that processes a sequence of real experiences and performs prioritized model-based planning after each one.
Your function should:
(num_states, num_actions)(state, action) to (reward, next_state, done)s, which (s_bar, a_bar) pairs lead to s according to the current model(s, a, r, s_next, done):
theta, insert (s, a) into the priority queue (keeping the maximum priority if already present)n_planning planning steps by repeatedly popping the highest-priority pair, updating its Q-value, and propagating priority to its predecessorsParameters:
num_states: Number of statesnum_actions: Number of actionsexperiences: List of (state, action, reward, next_state, done) tuplesalpha: Learning rate for Q-value updatesgamma: Discount factortheta: Minimum priority threshold (only update if priority > theta)n_planning: Maximum number of planning steps per real experienceReturns:
(num_states, num_actions)