Implement one planning phase of Dyna-style model-based RL: sample remembered transitions from a learned model and apply Q-learning updates to them.
Implement plan_with_model(Q, model, n_planning_steps, gamma, alpha, seed=42):
Q: NumPy array (num_states, num_actions). Do not modify it; work on a float copy.model: dict mapping (state, action) to (reward, next_state); next_state == -1 marks a terminal transition.model is empty, return the rounded copy of Q unchanged.Randomness: let keys = list(model.keys()) (dictionary insertion order). Call np.random.seed(seed) once, then for each of the n_planning_steps steps draw idx = np.random.randint(len(keys)) and use (s, a) = keys[idx], (r, s_next) = model[(s, a)].
Update, applied sequentially so each step sees earlier updates:
with when s_next == -1.
Return np.round(Q, 4) as a NumPy array of the same shape.