Code
Code
Gridworld policy iteration example
The grid world below is the example of Sutton and Barto (Sutton & Barto, 2018):- It is an undiscounted episodic MDP ().
- The nonterminal states are . There is one terminal state, drawn twice as the shaded corners.
- The four actions move the agent up, down, left or right with probability 1. An action that would leave the grid leaves the state unchanged.
- The reward is on every transition until the terminal state is reached.
- The starting policy is the uniform random policy, .
Policy evaluation
Evaluating the random policy starts from and applies the sweep above with . Each of the four actions has probability and leads to a single next state, so the sweep averages the four neighbors: In the move up leaves the grid, so appears as its own neighbor. Every nonterminal state gets . The figure below shows for more sweeps, each next to its greedy policy; at the values have converged to of the random policy.Policy improvement
The improvement step picks, in each state, the move with the largest . With , the four moves from are worth so , while in all four moves give , so all four are greedy. In the figure, each is paired with its greedy policy, with every tied move drawn.Policy iteration on the 4x3 gridworld
The code below runs policy iteration on a different MDP: the 4x3 grid world of Artificial Intelligence: A Modern Approach, Figure 17.2. Unlike the 4x4 example above, its moves are noisy. The intended move succeeds with probability 0.8, and with probability 0.1 each the agent moves at a right angle to it.- Terminal states: +1 at (3,0), -1 at (3,1)
- Wall: (1,1)
- Step cost: -0.04
- Discount factor γ = 1.0
Code
Code
References
- Sutton, R., Barto, A. (2018). Reinforcement Learning: An Introduction. Link
Found an error on this page? Report an issue.

