Q-Learning: Nudging One Table Cell

Reinforcement LearningEasy+10 XP+10

Q-Learning: Nudging One Table Cell

Tabular Q-learning stores one number per (state, action) pair: how much total future reward the agent expects from taking that action there. After every transition it nudges exactly one of those numbers.

Q[s][a] += α · (r + γ · max(Q[s']) - Q[s][a])

The bracketed part is the temporal-difference error — the gap between what the agent expected and what it now believes after seeing reward r and landing in state s'. α controls how much of that gap to close; γ controls how much distant reward matters.

The max is what makes this off-policy: the agent bootstraps from the best action available next, whether or not it ends up taking it.

Task: write q_update(q, state, action, reward, next_state, alpha, gamma), where q is a 2D list indexed [state][action]. Return the whole updated table as a 2D list, values rounded to 4 decimal places. Only one cell changes.

Examples

q = [[0.0, 0.0], [0.0, 0.0]]
state = 0
action = 1
reward = 1.0
next_state = 1
alpha = 0.5
gamma = 0.9
returns [[0.0, 0.5], [0.0, 0.0]]
q = [[1.0, 2.0], [3.0, 4.0]]
state = 0
action = 0
reward = 0.0
next_state = 1
alpha = 0.1
gamma = 0.9
returns [[1.26, 2.0], [3.0, 4.0]]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run