Implement dyna_q(num_states, num_actions, P, R, terminal_states, alpha, gamma, epsilon, num_episodes, n_planning) and return the learned Q-table, a NumPy array of shape (num_states, num_actions) initialised to zeros. P[s, a] is the next-state distribution (the environment may be stochastic) and R[s, a] the reward.
Tests call np.random.seed(...) before dyna_q, so use the global np.random generator with exactly these calls in this order:
state = np.random.choice(non_terminal), where non_terminal is the ascending list of non-terminal states.np.random.rand() < epsilon the action is np.random.randint(num_actions), otherwise np.argmax(Q[state]) (ties -> lowest index). Then next_state = np.random.choice(num_states, p=P[state, action]) and reward = R[state, action].(reward, next_state) for each visited (state, action); pairs keep their first-observation order.n_planning times: i = np.random.randint(number of stored pairs), take the i-th stored pair and apply the same Q-learning update with its stored reward and next state.state = next_state; the episode ends when the state is terminal.