Skip to main content
Open In Colab

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 M=⟨S,P,R,A,γ⟩\mathcal M = \langle \mathcal S, \mathcal P, \mathcal R, \mathcal A, \gamma \rangle. It produces a sequence of experiences (St,At,Rt+1),(St+1,At+1,Rt+2),…(S_t, A_t, R_{t+1}), (S_{t+1}, A_{t+1}, R_{t+2}), \dots through the loop below. agent-env-interface Agent-Environment Interface The notation we use is summarized below. Capital letters denote random variables and small letters their values: StS_t is the random state variable at time tt and sts_t is the specific state that occurs. Following Sutton and Barto, the value functions defined later on this page, vπv_\pi and qπq_\pi, are written in small letters, and the capital letters VV and QQ denote estimates of them. The loop is pseudo coded below:
This interface between the agent and the environment covers many problems besides MDPs, including RL. When the problem is not an MDP, the agent sees the environment through sensor observations and builds its own estimate of the state. At the start of each episode the environment and the agent are reset (lines 3 and 4), and the environment produces an initial state. The agent then produces an action given the state (line 6), and the environment produces the next state and the reward given the action (line 7). The agent.act and env.step cycle repeats until the time step reaches TT 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 StS_t wholly and perfectly.
Markov processes are sometimes called memoryless, but an MDP can still carry memory if the state is defined to hold it. For example, the augmented state Xt=(St,St−1)X_t = (S_t, S_{t-1}) is Markov even when StS_t alone depends on the two previous states:p(xt∣xt−1)=p(st,st−1∣st−1,st−2)=p(st∣st−1,st−2).p(x_t \mid x_{t-1}) = p(s_t, s_{t-1} \mid s_{t-1}, s_{t-2}) = p(s_t \mid s_{t-1}, s_{t-2}).A process in which StS_t depends on the two previous states is a second-order Markov chain.The DQN agent that DeepMind trained on Atari games such as Ms. Pac-Man uses this idea. It stacks the last four frames into one state. A single frame does not show which way the ghosts are moving, so the game is not Markov in single frame terms.

The dynamics of a finite MDP

A finite MDP is described by its dynamics: p(s′,r∣s,a)=Pr⁡{St+1=s′,Rt+1=r∣St=s,At=a},p(s', r \mid s, a) = \Pr\{ S_{t+1} = s', R_{t+1} = r \mid S_t=s, A_t=a \}, where s′s' denotes the next state. The function pp maps S×R×S×A\mathcal{S} \times \mathcal{R} \times \mathcal{S} \times \mathcal{A} to [0,1][0,1]. 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: p(s′∣s,a)=Pr⁡[St+1=s′∣St=s,At=a]=∑r∈Rp(s′,r∣s,a).p(s' \mid s,a) = \Pr[S_{t+1}=s' \mid S_t=s, A_t=a] = \sum_{r \in \mathcal{R}} p(s', r \mid s , a). Stacking these probabilities gives the state transition tensor P\mathcal P, with entries Pss′a=Pr⁡[St+1=s′∣St=s,At=a].\mathcal P^a_{ss'} = \Pr[S_{t+1}=s' \mid S_t=s, A_t=a]. 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 sxys_{xy} is the cell in column xx and row yy, counted from the bottom left, and the agent starts in s11s_{11}. The cell in column 2, row 2 is a wall. The states s43s_{43} and s42s_{42} 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.
Output from cell 3 The 4x3 gridworld. Each cell shows its state and the reward for entering it: +1+1 and −1-1 at the two exits and −0.04-0.04 everywhere else. The gray cell is the wall. For the action N, the state transition matrix Pss′N\mathcal P^{\mathrm N}_{ss'} has one row for each state ss the agent can act in and one column for each next state s′s'. Blank entries are zero.
s \ s′s₁₃s₂₃s₃₃s₄₃s₁₂s₃₂s₄₂s₁₁s₂₁s₃₁s₄₁
s₁₃0.90.1
s₂₃0.10.80.1
s₃₃0.10.80.1
s₁₂0.80.2
s₃₂0.80.10.1
s₁₁0.80.10.1
s₂₁0.10.80.1
s₃₁0.80.10.1
s₄₁0.80.10.1
Each row is a probability distribution over the next states, so it sums to 1. Take the start state s11s_{11}. The move north succeeds with probability 0.8 and reaches s12s_{12}. A slip to the east reaches s21s_{21} with probability 0.1. A slip to the west runs into the edge of the grid, so the agent stays in s11s_{11} with probability 0.1. In s12s_{12} both slips are blocked, one by the edge and one by the wall, and the agent stays in s12s_{12} with probability 0.2. The exits have no rows, because the episode ends when the agent enters one. The actions E, S and W give three more matrices of the same shape, and the four together form the tensor P\mathcal P.

Reward function and returns

The action also makes the environment send the agent a reward Rt+1R_{t+1}. 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 RtR_t. This is a convention. The index t+1t+1 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 aa in state ss: r(s,a)=E[Rt+1∣St=s,At=a]=∑r∈Rr∑s′∈Sp(s′,r∣s,a).r(s,a) = \mathbb{E}[ R_{t+1} \mid S_t=s, A_t=a] = \sum_{r \in \mathcal{R}} r \sum_{s' \in \mathcal{S}} p(s', r \mid s , a). Its values form the matrix Rsa\mathcal{R}^a_s. In the gridworld the reward depends only on the state the agent enters, Rt+1=R(St+1)R_{t+1} = R(S_{t+1}), 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 r(s,a)r(s, a) is the average of their rewards, weighted by their probabilities: r(s,a)=∑s′p(s′∣s,a) R(s′).r(s,a) = \sum_{s'} p(s' \mid s, a)\, R(s'). 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 2(dt−dt+1)2(d_t - d_{t+1}) for progress toward the goal, where dd is its distance to the goal, and pays a penalty for turning, so its reward is a function R(s,a,s′)R(s, a, s'). The sum still collapses, because fixing ss, aa and s′s' fixes the reward: r(s,a)=∑s′p(s′∣s,a) R(s,a,s′).r(s,a) = \sum_{s'} p(s' \mid s, a)\, R(s, a, s'). 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 r(s,a)r(s, a): the action is given, and the only randomness left is in the environment’s dynamics p(s′,r∣s,a)p(s', r \mid s, a), 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 p(s′∣s,a)p(s' \mid s, a).
s \ aNESW
s₁₃-0.040-0.040-0.040-0.040
s₂₃-0.040-0.040-0.040-0.040
s₃₃0.0640.7920.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
Most entries are −0.04-0.04, because every outcome of those actions is an ordinary step. The entries differ only next to the exits. In s33s_{33}, the move east enters the +1+1 exit with probability 0.8, and otherwise the agent stays in s33s_{33} or slips to s32s_{32}: r(s33,E)=0.8 (+1)+0.2 (−0.04)=0.792.r(s_{33}, \mathrm E) = 0.8\,(+1) + 0.2\,(-0.04) = 0.792. The move north from s33s_{33} reaches the exit only by a slip, so r(s33,N)=0.1 (+1)+0.9 (−0.04)=0.064r(s_{33}, \mathrm N) = 0.1\,(+1) + 0.9\,(-0.04) = 0.064. In s32s_{32} the move east enters the −1-1 exit with probability 0.8, and r(s32,E)=−0.808r(s_{32}, \mathrm E) = -0.808. The reward function looks only one step ahead. From s23s_{23} every action has r=−0.04r = -0.04, although the +1+1 exit is two moves away. The return, defined next, accounts for the rewards that come later.

Returns

The objective is stated through the return, a function of the rewards after time step tt. Some texts call it the utility. In the simplest case it is the total discounted reward, Gt=Rt+1+γRt+2+⋯=∑k=0∞γkRt+1+k.G_t = R_{t+1} + \gamma R_{t+2} + \dots = \sum_{k=0}^{\infty}\gamma^k R_{t+1+k}. The discount factor sets the present value of future rewards: a reward received kk steps in the future counts γk−1\gamma^{k-1} times as much as the same reward received now. With γ=0\gamma = 0 the agent cares only about the next reward Rt+1R_{t+1}. That is enough only when actions do not affect later rewards; in general, grabbing the immediate reward can lower the return. As γ\gamma approaches 1, later rewards count more and the agent plans further ahead. The definition uses two indices. The time step tt marks where you are in the trajectory, and kk counts the rewards after it, up to infinity. This is the infinite-horizon case, with no final time step TT. If γ<1\gamma < 1 and the rewards are bounded, ∣Rt∣≤Rmax⁡|R_t| \le R_{\max}, the sum is finite: ∣Gt∣≤∑k=0∞γkRmax⁡=Rmax⁡1−γ.|G_t| \le \sum_{k=0}^{\infty}\gamma^k R_{\max} = \frac{R_{\max}}{1-\gamma}. 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 π\pi, 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 s11s_{11} it moves north to s13s_{13} and then east along the top row. In the bottom row it moves west, back to the left column, and in s32s_{32} it moves north.
Output from cell 7 The fixed policy π\pi. The arrow in each cell is the action π(s)\pi(s). The code samples two episodes from s11s_{11} under this policy with γ=1\gamma = 1. For each time step it lists the state, the action, the reward and the return GtG_t.
Both episodes end at the +1+1 exit, by different routes. In τ1\tau_1 every move succeeds, the episode lasts 5 steps, and G0=4 (−0.04)+1=0.84G_0 = 4\,(-0.04) + 1 = 0.84. In τ2\tau_2 the first move slips east to s21s_{21}, and the policy takes the agent back to s11s_{11} before it heads north. The two extra steps cost 0.040.04 each, so G0=0.76G_0 = 0.76. With γ=0.9\gamma = 0.9 the returns are 0.519 and 0.344: discounting shrinks the +1+1 more in the longer episode. In both tables GtG_t grows as the agent nears the exit, although every step costs 0.040.04. The return counts only the rewards from time tt onward, so a later time step has fewer step costs left before the +1+1. Returns at successive time steps are related by a recursion: Gt=Rt+1+γRt+2+γ2Rt+3+γ3Rt+4+…=Rt+1+γ(Rt+2+γRt+3+γ2Rt+4+… )=Rt+1+γGt+1.\begin{aligned} G_t &= R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + \gamma^3 R_{t+4} + \dots \\ &= R_{t+1} + \gamma (R_{t+2} + \gamma R_{t+3} + \gamma^2 R_{t+4} + \dots) \\ &= R_{t+1} + \gamma G_{t+1}. \end{aligned} In the two tables above, each GtG_t equals Rt+1R_{t+1} plus the Gt+1G_{t+1} in the next row.

Policy function

The agent’s behavior is given by a policy π\pi, which says what action to take in every state. A policy can be
  1. 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):
a=π(s),a = \pi(s),
  1. or a stochastic function of the state, given as a conditional distribution over actions:
a∼π(a∣s)=Pr⁡(At=a∣St=s).a \sim \pi(a \mid s) = \Pr(A_t=a \mid S_t=s). This chapter uses stationary Markov policies: the distribution over actions depends only on the current state, not on the time step or on earlier states, so At∼π(⋅∣St)A_t \sim \pi(\cdot \mid S_t) for all t≥0t \ge 0. Nothing is lost by this restriction for the problems here. In infinite-horizon discounted MDPs, and in episodic tasks whose rewards do not depend on the time step, some stationary deterministic policy is optimal. A task with a fixed horizon is an exception: the best action can depend on how many steps remain, unless the time step is part of the state. The gridworld policy drawn in the returns example is deterministic: π(s11)=π(s12)=N\pi(s_{11}) = \pi(s_{12}) = \mathrm N, π(s13)=π(s23)=π(s33)=E\pi(s_{13}) = \pi(s_{23}) = \pi(s_{33}) = \mathrm E, π(s32)=N\pi(s_{32}) = \mathrm N, and π(s21)=π(s31)=π(s41)=W\pi(s_{21}) = \pi(s_{31}) = \pi(s_{41}) = \mathrm W. Its route along the left column and the top row is longer than the route through s31s_{31} and s32s_{32}, but it stays away from the −1-1 exit, which a slip can reach from s32s_{32} and s41s_{41}. A simple stochastic policy is the uniform random policy, π(a∣s)=1/4\pi(a \mid s) = 1/4 for each of the four 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 vπ(s)v_\pi(s) is the expected return when you start in state ss and follow the policy π\pi. Other texts call it the expected utility. vπ(s)=Eπ[Gt∣St=s]v_\pi(s) = \mathbb{E}_\pi[G_t \mid S_t=s] The expectation is needed because GtG_t is a random variable: the states of a trajectory that starts in ss depend on the policy and on the environment’s transitions. For example, if the two trajectories above were the only ones from s11s_{11} and were equally likely, the value of s11s_{11} would be vπ(s11)=12(G0τ1+G0τ2)=12(0.84+0.76)=0.80.v_\pi(s_{11}) = \frac{1}{2}(G^{\tau_1}_0 + G^{\tau_2}_0) = \frac{1}{2}(0.84 + 0.76) = 0.80. The two trajectories are not the only ones, and they are not equally likely. Averaging the return over many sampled trajectories estimates the expectation:
Output from cell 10 Returns of 10,000 trajectories from s11s_{11} under the fixed policy, with γ=1\gamma = 1. The average of the 10,000 returns is 0.749, with a standard error of 0.002. The largest return, 0.84, belongs to the trajectories in which every move succeeds, like τ1\tau_1. Each slip adds one or more steps, and each extra step lowers the return by 0.04, which spreads the returns to the left of 0.84. In 1.2% of the trajectories a slip takes the agent into the −1-1 exit, and those returns are negative, down to −1.64-1.64. The average over many trajectories approaches the expectation that defines vπ(s11)v_\pi(s_{11}), but it remains an estimate. Another set of 10,000 trajectories would give a slightly different number, and the error shrinks like 1/n1/\sqrt{n} with the number of trajectories nn without ever vanishing. The exact value needs no sampled trajectories. The Policy Evaluation (Prediction) section computes it with the Bellman expectation equation, which relates the value of each state to the values of the states that can follow it through the transition model. With γ=0\gamma=0 the return keeps only its first term, Gt=Rt+1G_t = R_{t+1}, and the value becomes the expected reward of the next transition: vπ(s)=Eπ[Rt+1∣St=s]=∑aπ(a∣s) r(s,a).v_\pi(s) = \mathbb{E}_\pi[R_{t+1} \mid S_t=s] = \sum_{a} \pi(a \mid s)\, r(s,a). For the deterministic policy of the gridworld this is one entry of the reward table: vπ(s33)=r(s33,E)=0.792v_\pi(s_{33}) = r(s_{33}, \mathrm E) = 0.792, vπ(s32)=r(s32,N)=−0.136v_\pi(s_{32}) = r(s_{32}, \mathrm N) = -0.136, vπ(s41)=r(s41,W)=−0.136v_\pi(s_{41}) = r(s_{41}, \mathrm W) = -0.136, and −0.04-0.04 in every other state. The value of s11s_{11} drops from about 0.75 to −0.04-0.04, because the +1+1 at the end of the episode no longer counts.

Action value

The action-value function qπ(s,a)q_\pi(s,a) is the expected return when you start in state ss, take action aa, and follow π\pi afterwards: qπ(s,a)=Eπ[Gt∣St=s,At=a]q_\pi(s,a) = \mathbb{E}_\pi[G_t \mid S_t=s, A_t=a] It tells you how good each action is in state ss, 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, vπ(s)=qπ(s,π(s))v_\pi(s) = q_\pi(s, \pi(s)), and for a stochastic policy vπ(s)=∑aπ(a∣s) qπ(s,a)v_\pi(s) = \sum_a \pi(a \mid s)\, q_\pi(s,a). The action value is again an expectation over trajectories. The code samples 10,000 episodes from s11s_{11} for each of the four first actions. The first action is fixed, and the policy chooses every action after it.
Output from cell 12 Returns of 10,000 trajectories from s11s_{11} for each first action, with the policy choosing every later action and γ=1\gamma = 1. Most of each histogram sits just below the largest possible return, and each has a thin tail of trajectories that end at the −1-1 exit. The means differ: qπ(s11,N)≈0.743q_\pi(s_{11}, \mathrm N) \approx 0.743, qπ(s11,W)≈0.711q_\pi(s_{11}, \mathrm W) \approx 0.711, qπ(s11,S)≈0.701q_\pi(s_{11}, \mathrm S) \approx 0.701 and qπ(s11,E)≈0.670q_\pi(s_{11}, \mathrm E) \approx 0.670, each with a standard error of about 0.003. N is the action the policy takes, so qπ(s11,N)q_\pi(s_{11}, \mathrm N) estimates the same expectation as vπ(s11)v_\pi(s_{11}), and the two estimates, 0.743 and 0.749, differ by about two standard errors. The other actions cost extra steps. A move south runs into the edge of the grid with probability 0.8 and leaves the agent in s11s_{11}, so it wastes about one step: 0.743−0.04≈0.700.743 - 0.04 \approx 0.70. A move west also runs into the edge, but its slip to the north, with probability 0.1, takes the agent toward the goal, so it does slightly better than south. A move east reaches s21s_{21} with probability 0.8, and the policy then walks back west, so it wastes about two steps: 0.743−0.08≈0.660.743 - 0.08 \approx 0.66. The largest action value in s11s_{11} belongs to N, the action the policy already takes. These are estimates as well. The exact action values also follow from the Bellman expectation equation, in the Policy Evaluation (Prediction) section. 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.