Skip to main content
Open In Colab This section solves the 4x3 gridworld of Introduction to MDP with q-value iteration, which keeps an action value q(s,a)q(s, a) for every state and action instead of one value v(s)v(s) per state. Value Iteration solves the same grid, so you can compare the two results directly.

The 4x3 gridworld

The agent is in one of the 11 free cells sxys_{xy}, where x=1,…,4x = 1, \dots, 4 counts columns from the left and y=1,2,3y = 1, 2, 3 counts rows from the bottom. The cell s22s_{22} is a wall. Entering the exit s43s_{43} earns +1+1, and entering the exit s42s_{42} earns −1-1. Reaching either exit ends the episode. Every other move earns −0.04-0.04. The actions are the four moves north, east, south and west. A move into the wall or off the grid leaves the agent where it is. The reward depends only on the cell the agent enters, r(s,a,s′)=r(s′)r(s, a, s') = r(s'), and the discount factor is γ=0.9\gamma = 0.9. The problem is solved twice. First the moves are deterministic, so the agent always reaches the cell it moves toward. Then the moves are noisy, as in the gridworld of the other sections: the intended move happens with probability 0.8, and each move at a right angle to it with probability 0.1.
Output from cell 2 The 4x3 gridworld. Entering the exit s43s_{43} earns +1+1, entering the exit s42s_{42} earns −1-1, and every other move earns −0.04-0.04.

q-value iteration

The optimal action-value function q∗(s,a)q_*(s, a) is the return of taking action aa in state ss and acting optimally afterwards. It satisfies the Bellman optimality equation for action values, q∗(s,a)=∑s′p(s′∣s,a)[r(s,a,s′)+γmax⁡a′q∗(s′,a′)].q_*(s, a) = \sum_{s'} p(s' \mid s, a) \bigl[ r(s, a, s') + \gamma \max_{a'} q_*(s', a') \bigr]. q-value iteration turns this equation into an update. Start from q0=0q_0 = 0 and apply the right-hand side to every pair (s,a)(s, a), using the previous table qkq_k inside the max, to get qk+1q_{k+1}. Stop when no entry changes by more than a small tolerance. Once q∗q_* is known, the optimal policy needs no model at all: in each state, pick the action with the largest q∗(s,a)q_*(s, a). This is value iteration with the action values kept. Value Iteration stores one number per state, vk(s)v_k(s), and computes action values only briefly inside each backup. q-value iteration stores one number per state and action. The two produce the same sequence: with vk(s)=max⁡aqk(s,a)v_k(s) = \max_a q_k(s, a), the update above is exactly the value iteration update. Keeping qq costs more memory, but it removes the model from policy extraction. From v∗v_* you need the model pp again to look one step ahead, while from q∗q_* you only take the argmax in each state. Q-learning estimates the same table from experience, without pp; its capital QQ marks a learned estimate. The code below builds the model as arrays. P[s, a, s'] is the probability of moving from s to s' under action a, and R[s, a, s'] is the reward of that transition. The update works for any model given in this form.

Deterministic moves

With deterministic moves, each action leads to exactly one next cell s′s', so the sum in the update has a single term: qk+1(s,a)=r(s,a,s′)+γmax⁡a′qk(s′,a′)q_{k+1}(s, a) = r(s, a, s') + \gamma \max_{a'} q_k(s', a').
Output from cell 5 Each free cell holds the four values q∗(s,a)q_*(s, a), with each triangle on the side of its move: north at the top, east at the right. The bold number is the greedy action. With deterministic moves, q∗(s,a)q_*(s, a) depends only on how many moves the agent needs to reach the +1+1 exit when it takes aa first and then follows the shortest route. If that takes nn moves, the agent earns −0.04-0.04 on each of the first n−1n - 1 moves and +1+1 on the last, so q∗(s,a)=−0.04 (1+γ+⋯+γn−2)+γn−1.q_*(s, a) = -0.04\,(1 + \gamma + \dots + \gamma^{n-2}) + \gamma^{n-1}. For n=1,…,6n = 1, \dots, 6 this gives 1, 0.860, 0.734, 0.621, 0.519 and 0.427. These are the only values in the table, apart from the −1-1 of moving into the −1-1 exit from s32s_{32} or s41s_{41}. Moving east from s33s_{33} reaches the +1+1 exit in one move. Moving south from s11s_{11} bumps into the edge of the grid and leaves the agent in place, so it needs six moves. Each sweep extends the horizon by one move: qk(s,a)q_k(s, a) is the best return of the first kk moves when the first one is aa. The entry for a route of nn moves reaches its final value in sweep nn. The longest route is the six moves that start by moving south from s11s_{11}, so sweep 6 is the last one that changes the table, and sweep 7 only confirms it. The greedy policy is final earlier, from sweep 4. Some states have two best actions. From s11s_{11}, north and east both reach the +1+1 exit in five moves, one around each side of the wall, and both are worth 0.519. The printed policy lists north because it comes first.

Noisy moves

Now each move reaches the intended cell with probability 0.8 and goes at a right angle to it with probability 0.1 each way. The update averages over the possible next cells.
Output from cell 7 The same table with noisy moves. The greedy actions are the same as with deterministic moves, and almost every value is lower. A noisy move can fail, so on average the agent needs more moves to reach the +1+1 exit. Each extra move costs −0.04-0.04 and delays the +1+1 by another factor of 0.9. Moving east from s33s_{33} is worth 0.928 instead of 1: with probability 0.1 the agent slips north into the edge of the grid and stays in s33s_{33}, and with probability 0.1 it slips south into s32s_{32}. The greedy policy does not change, although the noise brings a new risk. In s32s_{32} the agent still moves north, with q∗(s32,N)=0.585q_*(s_{32}, \text{N}) = 0.585, although a slip east takes it into the −1-1 exit with probability 0.1. Moving west into the wall avoids that risk, but it is worth only 0.503, because the agent then has to take the long way around. In the DP Gridworld demo you can lower the cost of a move and watch the policy in s32s_{32} switch to west. The largest value in each row, max⁡aq∗(s,a)\max_a q_*(s, a), is the optimal state value v∗(s)v_*(s) that Value Iteration computes for this grid. Both methods stop after 16 sweeps. Here the greedy policy is final from sweep 4, while on the Value Iteration page it is final from sweep 3. The difference is one look-ahead step. The greedy action of qkq_k looks one step ahead from vk−1=max⁡aqk−1v_{k-1} = \max_a q_{k-1}, so the greedy policy of qkq_k is the greedy policy of vk−1v_{k-1}. Output from cell 8 The largest change of any entry in each sweep, on a log scale. With deterministic moves the change drops to zero in sweep 7, which the plot cannot show. With deterministic moves the largest change in sweep kk is exactly 0.9k−10.9^{k-1}, the discounted value of the +1+1 exit seen one move further away, and it drops to zero once the longest route is covered. With noisy moves the change shrinks in every sweep but never reaches zero, because a slip can take the agent back to a cell it has left, so the values keep feeding into each other. The loop stops when the change falls below the tolerance of 10−410^{-4}. Key references: (Sutton & Barto, 2018)

References

  • Sutton, R., Barto, A. (2018). Reinforcement Learning: An Introduction. Link

Found an error on this page? Report an issue.