Q-Learning Update Rule

Chapter 2 ended with Bellman equations. For Q-learning, the key bridge is the Bellman optimality equation for actions:

Q(s,a)=E[Rt+1+γmaxaQ(St+1,a)St=s,At=a]Q^*(s,a)=\mathbb{E}\left[R_{t+1}+\gamma\max_{a'}Q^*(S_{t+1},a')\mid S_t=s,A_t=a\right]

The optimal Q-value equals the expected immediate reward plus the discounted value of the best next action. In a delivery robot example, Q(s,a)Q^*(s,a) asks: if the robot takes this first move from this situation, how much return should it expect if it behaves optimally afterward?

If we knew PP and RR, we could compute the expectation by averaging over all possible next states and rewards. In real learning problems, the agent usually cannot compute that expectation. It can sample it: take action ata_t, observe reward rt+1r_{t+1}, and see the next state st+1s_{t+1}.

So, we need a way to learn Q(s,a)Q^*(s,a) directly from experience, without knowing PP or RR. That is Q-learning.

After taking action ata_t in state sts_t, receiving reward rt+1r_{t+1}, and observing next state st+1s_{t+1}, Q-learning updates only the table entry for the state-action pair it just used.

Q-learning update

Click the parts of the update rule

The update moves the current Q-value toward a target built from one sampled transition.

Q(st,at)Q(s_t,a_t)<- Q(st,at)Q(s_t,a_t)+α\alpha[rt+1r_{t+1}+γmaxaQ(st+1,a)\gamma\max_{a'}Q(s_{t+1},a')Q(st,at)-Q(s_t,a_t)]

delta

-0.0050

old Q

-0.0050

new Q

-0.0075

Selected part

γmaxaQ(st+1,a)\gamma\max_{a'}Q(s_{t+1},a')

The discounted value of the best action available in the sampled next state.

The bracket is the temporal-difference error. If delta is zero, the current estimate already matched the sampled target.

The update rule is Q(st,at)Q(st,at)+α[rt+1+γmaxaQ(st+1,a)Q(st,at)]Q(s_t,a_t)\leftarrow Q(s_t,a_t)+\alpha\left[r_{t+1}+\gamma\max_{a'}Q(s_{t+1},a')-Q(s_t,a_t)\right]

The bracketed quantity is the temporal-difference error, often written δt=rt+1+γmaxaQ(st+1,a)Q(st,at)\delta_t=r_{t+1}+\gamma\max_{a'}Q(s_{t+1},a')-Q(s_t,a_t) It measures surprise: how different the sampled target was from the current estimate. If δt=0\delta_t=0, the prediction matched the sampled target.

Q-table walkthrough

Build a Q-table in a 3x3 gridworld

Step through a short episode. Each sampled transition updates one state-action entry in the Q-table.

Q-learning update equation

Q(st,at)Q(s_t,a_t)\leftarrowQ(st,at)Q(s_t,a_t)+α\alpha[rt+1r_{t+1}+γmaxaQ(st+1,a)\gamma \max_{a'} Q(s_{t+1},a')-Q(st,at)Q(s_t,a_t)]
old Qalpharewardfuture valueTD error

Actions: ↑ ↓ ← →

Rewards: -0.01, +1.0 goal

γ: 0.9 α: 0.5

(1,1)

all Q = 0

(1,2)

all Q = 0

(1,3)goal

all Q = 0

(2,1)

all Q = 0

(2,2)

all Q = 0

(2,3)

all Q = 0

(3,1)start

all Q = 0

(3,2)

all Q = 0

(3,3)

all Q = 0

Setup

Start with every Q(s,a) entry at 0. The agent begins at (3,1), the goal is at (1,3), each non-goal step gives -0.01, gamma is 0.9, and alpha is 0.5.

Use the buttons above to watch one sampled transition at a time update a single Q-table entry.

The table starts at zero. Reward information propagates backward only when the agent revisits earlier state-action pairs over more experience.
Checkpoint

Which part of the Q-learning update looks into the next state?