The agent-environment interface
This section reviews the agent-environment interface with the notation used in the rest of the MDP chapter, and defines the quantities that allow us to pose an optimization problem statement based on the Bellman equations. A Markov decision process is the 5-tuple . It produces a sequence of experiences through the loop below.
The loop is pseudo coded below:
agent.act and env.step cycle repeats until the time step reaches or the environment terminates. The agent.update call (line 8) holds the agent’s learning algorithm: over many time steps and episodes it collects data and learns to maximize the objective.
In a fully observed MDP the agent perceives the environment state wholly and perfectly.
The dynamics of a finite MDP
A finite MDP is described by its dynamics: where denotes the next state. The function maps to . Summing it over the right variables gives the state transition model and the reward function. Together with the policy and the value functions, these are the ingredients of every method in this chapter.State transition
The agent’s action changes the environment state. The state transition model gives the probability of each next state: Stacking these probabilities gives the state transition tensor , with entries One dimension indexes the action, and for each action the other two form a state transition matrix.Example: the 4x3 gridworld
The gridworld below makes these definitions concrete. It comes from Artificial Intelligence: A Modern Approach by Russell and Norvig. The state is the cell in column and row , counted from the bottom left, and the agent starts in . The cell in column 2, row 2 is a wall. The states and are exits: the episode ends when the agent enters one of them. The actions are the four moves N, E, S and W. Every move is noisy. The intended move happens with probability 0.8, and with probability 0.1 each the agent moves at a right angle to it. A move into the wall or off the grid leaves the agent where it is.Code
Code
Code
Code
| s \ s′ | s₁₃ | s₂₃ | s₃₃ | s₄₃ | s₁₂ | s₃₂ | s₄₂ | s₁₁ | s₂₁ | s₃₁ | s₄₁ |
|---|---|---|---|---|---|---|---|---|---|---|---|
| s₁₃ | 0.9 | 0.1 | |||||||||
| s₂₃ | 0.1 | 0.8 | 0.1 | ||||||||
| s₃₃ | 0.1 | 0.8 | 0.1 | ||||||||
| s₁₂ | 0.8 | 0.2 | |||||||||
| s₃₂ | 0.8 | 0.1 | 0.1 | ||||||||
| s₁₁ | 0.8 | 0.1 | 0.1 | ||||||||
| s₂₁ | 0.1 | 0.8 | 0.1 | ||||||||
| s₃₁ | 0.8 | 0.1 | 0.1 | ||||||||
| s₄₁ | 0.8 | 0.1 | 0.1 |
Reward function and returns
The action also makes the environment send the agent a reward . The reward signal defines the goal: the agent’s objective is to maximize the cumulative reward in the long run, and the reward is the main basis for changing a policy. Some texts write the reward as . This is a convention. The index reflects that the environment takes one step to respond to the agent’s action. Summing the dynamics over the next state gives the reward function, the expected reward of taking action in state : Its values form the matrix . In the gridworld the reward depends only on the state the agent enters, , so each next state comes with exactly one reward. The double sum in the definition then reduces to a sum over the next states, and is the average of their rewards, weighted by their probabilities: In many tasks the reward also depends on the state the agent leaves and on the action. The TurtleBot in Robot Navigation as an MDP earns for progress toward the goal, where is its distance to the goal, and pays a penalty for turning, so its reward is a function . The sum still collapses, because fixing , and fixes the reward: The full double sum is needed only when the reward is random even for a given transition, for example the time one leg of the drive home in Temporal Difference (TD) Prediction takes, which depends on traffic. The policy plays no part in : the action is given, and the only randomness left is in the environment’s dynamics , the joint model of the next state and the reward. When the reward is fixed by the transition, as in the gridworld and the TurtleBot, it reduces to the state transition model .Code
Code
| s \ a | N | E | S | W |
|---|---|---|---|---|
| s₁₃ | -0.040 | -0.040 | -0.040 | -0.040 |
| s₂₃ | -0.040 | -0.040 | -0.040 | -0.040 |
| s₃₃ | 0.064 | 0.792 | 0.064 | -0.040 |
| s₁₂ | -0.040 | -0.040 | -0.040 | -0.040 |
| s₃₂ | -0.136 | -0.808 | -0.136 | -0.040 |
| s₁₁ | -0.040 | -0.040 | -0.040 | -0.040 |
| s₂₁ | -0.040 | -0.040 | -0.040 | -0.040 |
| s₃₁ | -0.040 | -0.040 | -0.040 | -0.040 |
| s₄₁ | -0.808 | -0.136 | -0.040 | -0.136 |
Returns
The objective is stated through the return, a function of the rewards after time step . Some texts call it the utility. In the simplest case it is the total discounted reward, The discount factor sets the present value of future rewards: a reward received steps in the future counts times as much as the same reward received now. With the agent cares only about the next reward . That is enough only when actions do not affect later rewards; in general, grabbing the immediate reward can lower the return. As approaches 1, later rewards count more and the agent plans further ahead. The definition uses two indices. The time step marks where you are in the trajectory, and counts the rewards after it, up to infinity. This is the infinite-horizon case, with no final time step . If and the rewards are bounded, , the sum is finite: The return is a random variable: each trajectory, sampled from the policy and the environment, gives a different return. To see this in the gridworld, fix a deterministic policy , which picks one action in every state. The Policy function section below defines policies in general. The one used here is drawn in the figure: from it moves north to and then east along the top row. In the bottom row it moves west, back to the left column, and in it moves north.Code
Code
Code
Code
Policy function
The agent’s behavior is given by a policy , which says what action to take in every state. A policy can be- a deterministic function of the state the environment is in (or, by extension, the state the agent believes it is in; think of a posterior belief):
- or a stochastic function of the state, given as a conditional distribution over actions:
Value functions
The value of a state is the reward you can expect to collect from that state onward. A reward measures how good a state is right now. A value also accounts for the states that are likely to follow and the rewards available there, so it measures how good the state is in the long run.State value
The state-value function is the expected return when you start in state and follow the policy . Other texts call it the expected utility. The expectation is needed because is a random variable: the states of a trajectory that starts in depend on the policy and on the environment’s transitions. For example, if the two trajectories above were the only ones from and were equally likely, the value of would be The two trajectories are not the only ones, and they are not equally likely. Averaging the return over many sampled trajectories estimates the expectation:Code
Code
Action value
The action-value function is the expected return when you start in state , take action , and follow afterwards: It tells you how good each action is in state , which is what you need to choose an action. Its relation to the state value follows from the definition: for the deterministic policy of the gridworld, , and for a stochastic policy . The action value is again an expectation over trajectories. The code samples 10,000 episodes from for each of the four first actions. The first action is fixed, and the policy chooses every action after it.Code
Code
References
- Sutton, R., Barto, A. (2018). Reinforcement Learning: An Introduction. Link
Found an error on this page? Report an issue.

