Skip to main content
Open In Colab As we saw there are two questions in MDP problems:
  1. Bellman Expectation Backup and Policy Evaluation answer the prediction question: given a policy π\pi, how good is it?
  2. In Policy Improvement, acting greedily on qπq_\pi gave a better policy, and after a few rounds the policy stopped changing. This section takes up the control question: which policy is best ?
We now shows that the policy that stops its improvement is optimal.

Optimal value functions

The optimal value functions are the best values that any policy achieves: v∗(s)=max⁡πvπ(s),q∗(s,a)=max⁡πqπ(s,a).\begin{aligned} v_*(s) &= \max_\pi v_\pi(s), \\ q_*(s, a) &= \max_\pi q_\pi(s, a). \end{aligned} v∗(s)v_*(s) is the largest expected return you can get starting from state ss. q∗(s,a)q_*(s, a) is the largest expected return if you start in ss, take action aa, and act optimally afterwards. The max in these definitions is over every policy, and there are ∣A∣∣S∣|\mathcal A|^{|\mathcal S|} deterministic policies alone. The Bellman optimality equations replace it with a max over the few actions available in a single state. The next section shows why that is allowed.

From policy improvement to optimality

The greedy step connects the two questions. Evaluate a policy π\pi to get qπq_\pi, then switch to the greedy policy π′\pi', which takes the action with the largest qπ(s,a)q_\pi(s, a) in every state. The policy improvement theorem (Sutton & Barto, 2018) guarantees that vπ′(s)≥vπ(s)v_{\pi'}(s) \ge v_\pi(s) in every state. If you repeat the two steps, the policy keeps improving until the greedy step no longer changes it. At that point π\pi is greedy with respect to its own action values. More specifically, evaluate π\pi to get its action values qπ(s,a)q_\pi(s, a). In every state, the action that π\pi takes has the largest of these values: π(s)∈arg⁡max⁡aqπ(s,a).\pi(s) \in \arg\max_a q_\pi(s, a). The values must come from π\pi itself. Every deterministic policy is greedy with respect to some value function, but only a policy that is greedy with respect to its own qπq_\pi is optimal. Taking a different action once and then following π\pi cannot do better. If it could, that action would have a larger qπ(s,a)q_\pi(s, a), and the greedy step would have switched to it. The final policy of the Policy Improvement example, π′′=(right,right,stay)\pi'' = (\text{right}, \text{right}, \text{stay}), shows the condition. Its values are vπ′′=(7.481,8.659,10)v_{\pi''} = (7.481, 8.659, 10), and its action values qπ′′(s,a)=r(s,a)+γ∑s′p(s′∣s,a) vπ′′(s′)q_{\pi''}(s, a) = r(s, a) + \gamma \sum_{s'} p(s' \mid s, a)\, v_{\pi''}(s') are: For example, qπ′′(s0,right)=−0.1+0.9 (0.8⋅8.659+0.2⋅7.481)=7.481q_{\pi''}(s_0, \text{right}) = -0.1 + 0.9\,(0.8 \cdot 8.659 + 0.2 \cdot 7.481) = 7.481. In every state the action that π′′\pi'' takes has the larger value, so the greedy step returns π′′\pi'' unchanged. The earlier policy π′=(stay,right,stay)\pi' = (\text{stay}, \text{right}, \text{stay}) fails the test. Its values are vπ′(s0)=0v_{\pi'}(s_0) = 0 and vπ′(s1)=8.659v_{\pi'}(s_1) = 8.659, so in s0s_0 moving right is worth qπ′(s0,right)=−0.1+0.9 (0.8⋅8.659+0.2⋅0)=6.134q_{\pi'}(s_0, \text{right}) = -0.1 + 0.9\,(0.8 \cdot 8.659 + 0.2 \cdot 0) = 6.134, more than the 0 of staying. That is why round 2 of the example switches s0s_0 to right. Every policy satisfies the Bellman expectation equation for the state value, vπ(s)=∑aπ(a∣s) qπ(s,a).v_\pi(s) = \sum_a \pi(a \mid s)\, q_\pi(s, a). For this deterministic policy, π(a∣s)=1\pi(a \mid s) = 1 for the action a=π(s)a = \pi(s) and 00 for every other action. The sum keeps only the term qπ(s,π(s))q_\pi(s, \pi(s)), which is the largest action value in state ss: vπ(s)=max⁡aqπ(s,a).v_\pi(s) = \max_a q_\pi(s, a). A stochastic policy can satisfy the same equation. If π(a∣s)\pi(a \mid s) spreads its probability only over actions that tie for the largest qπ(s,a)q_\pi(s, a), the weighted sum still equals the max. If it gives any probability to an action with a smaller value, the sum falls below the max, and the greedy step improves the policy. So an optimal policy can be stochastic only by splitting its probability among equally good actions, and a deterministic optimal policy always exists. This is the Bellman optimality equation. Its solution is v∗v_*. So the Bellman optimality equation is the Bellman expectation equation of a policy that is greedy with respect to its own action values. Compare the equation with the definition of v∗v_* in Optimal value functions: v∗(s)=max⁡πvπ(s)=max⁡aq∗(s,a).v_*(s) = \max_\pi v_\pi(s) = \max_a q_*(s, a). The first max runs over whole policies, which choose an action in every state. The second runs over the few actions in state ss. They are equal because the action taken in ss only affects what happens from ss onward, and q∗(s,a)q_*(s, a) already assumes the best choices in every later state. So an optimal policy can be built one state at a time, by taking the action with the largest q∗(s,a)q_*(s, a) in each state.

The max over actions as a backup tree

The figure draws the equation v∗(s)=max⁡aq∗(s,a)v_*(s) = \max_a q_*(s, a) from the previous paragraph as a backup tree. The prediction tree for vπ(s)v_\pi(s) from Bellman Expectation Backup is drawn on the left for comparison.
Output from cell 3 Backup diagrams for the state value. Prediction averages the action branches with the policy weights π(a∣s)\pi(a \mid s), and control takes their maximum. The branches from each action to the next states are the same in both. The agent chooses the action, so control takes the max over the action branches. The environment chooses the next state, so both prediction and control average over p(s′∣s,a)p(s' \mid s, a): the agent picks its action but not the outcome of the action. In equations, v∗(s)=max⁡aq∗(s,a),q∗(s,a)=r(s,a)+γ∑s′p(s′∣s,a) v∗(s′),\begin{aligned} v_*(s) &= \max_a q_*(s, a), \\ q_*(s, a) &= r(s, a) + \gamma \sum_{s'} p(s' \mid s, a)\, v_*(s'), \end{aligned} where r(s,a)r(s, a) is the expected reward of taking action aa in state ss, and p(s′∣s,a)p(s' \mid s, a) is the probability of moving to s′s'. The second equation has no max, because the environment makes the choice at that level. The backup for the action value starts one level lower, at a state and action pair. The environment moves first, and the agent’s choice comes at the next state. Output from cell 4 Backup diagrams for the action value. The environment’s branches come first and are averaged in both cases. The agent’s choice at the next state is where control takes the maximum.

The Bellman optimality equations

Substituting each of the two equations into the other gives the Bellman optimality equations:
v∗(s)=max⁡a(r(s,a)+γ∑s′p(s′∣s,a) v∗(s′)),q∗(s,a)=r(s,a)+γ∑s′p(s′∣s,a)max⁡a′q∗(s′,a′).\begin{aligned} v_*(s) &= \max_a \Big( r(s, a) + \gamma \sum_{s'} p(s' \mid s, a)\, v_*(s') \Big), \\ q_*(s, a) &= r(s, a) + \gamma \sum_{s'} p(s' \mid s, a) \max_{a'} q_*(s', a'). \end{aligned}
Compare them with the Bellman expectation equations in Bellman Expectation Backup. The only change is that the policy-weighted sums ∑aπ(a∣s)\sum_a \pi(a \mid s) and ∑a′π(a′∣s′)\sum_{a'} \pi(a' \mid s') have become max⁡a\max_a and max⁡a′\max_{a'}.

A worked example

Suppose a backup at state ss sees two actions, with action values 1 and 3. The prediction backup for the uniform random policy averages them, 0.5⋅1+0.5⋅3=20.5 \cdot 1 + 0.5 \cdot 3 = 2. The control backup takes the better one, max⁡(1,3)=3\max(1, 3) = 3, and records that the second action is the one to take. Repeating the control backup in every state until the values stop changing is value iteration. The DP gridworld demo shows the same difference on a whole grid. Its policy evaluation button runs the prediction backup for the current policy, and its value iteration button runs the control backup.

Reading off the optimal policy

Once you know q∗q_*, an optimal policy takes the best action in each state: π∗(a∣s)={1if a=arg⁡max⁡a′q∗(s,a′),0otherwise.\pi_*(a \mid s) = \begin{cases} 1 & \text{if } a = \arg\max_{a'} q_*(s, a'), \\ 0 & \text{otherwise.} \end{cases} This needs no model of the environment. If you only have v∗v_*, you need one more step of lookahead through the model, π∗(s)=arg⁡max⁡a(r(s,a)+γ∑s′p(s′∣s,a) v∗(s′))\pi_*(s) = \arg\max_a \big( r(s, a) + \gamma \sum_{s'} p(s' \mid s, a)\, v_*(s') \big).

Solving the equations

The max makes the optimality equations nonlinear, so they have no closed-form solution like the linear system of policy evaluation. When the model is known, dynamic programming solves them iteratively. Policy Iteration alternates policy evaluation with greedy improvement, and Value Iteration applies the optimality backup directly. Both reuse the values of successor states instead of recomputing them, which is the idea behind dynamic programming. Without a model, Q-learning estimates q∗q_* from sampled transitions.

References

  • Sutton, R., Barto, A. (2018). Reinforcement Learning: An Introduction. Link
  • (2020). Markov Decision Processes 1 - Value Iteration | Stanford CS221: AI (Autumn 2019). Link

Found an error on this page? Report an issue.