The 4x3 gridworld
The agent is in one of the 11 free cells , where counts columns from the left and counts rows from the bottom. The cell is a wall. Entering the exit earns , and entering the exit earns . Reaching either exit ends the episode. Every other move earns . 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, , and the discount factor is . 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.Code
Code
q-value iteration
The optimal action-value function is the return of taking action in state and acting optimally afterwards. It satisfies the Bellman optimality equation for action values, q-value iteration turns this equation into an update. Start from and apply the right-hand side to every pair , using the previous table inside the max, to get . Stop when no entry changes by more than a small tolerance. Once is known, the optimal policy needs no model at all: in each state, pick the action with the largest . This is value iteration with the action values kept. Value Iteration stores one number per state, , 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 , the update above is exactly the value iteration update. Keeping costs more memory, but it removes the model from policy extraction. From you need the model again to look one step ahead, while from you only take the argmax in each state. Q-learning estimates the same table from experience, without ; its capital 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.
Code
Code
Deterministic moves
With deterministic moves, each action leads to exactly one next cell , so the sum in the update has a single term: .Code
Code
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.Code
Code
References
- Sutton, R., Barto, A. (2018). Reinforcement Learning: An Introduction. Link
Found an error on this page? Report an issue.

