← All algorithms

Reinforcement Learning

RL: When MDP is known

2023-12-28

In the tutorial on the basics of reinforcement learning, we derived the Bellman expectation and optimality equations. Here, we use these equations to determine an optimal policy. Policy iteration uses the Bellman expectation equation, whereas value iteration uses the Bellman optimality equation.

(1) Policy Iteration

Using the given policy, reward function, and transition probabilities, the method involves calculating the value of every state, improving the policy to select the best action in each state, and repeating this process until the policy and value function converge, i.e., until the optimal policy is found as shown in the diagram below.

\(\pi _0\rightarrow v_{\pi 0}\rightarrow \pi _1\rightarrow v_{\pi 1}\rightarrow \pi _2\rightarrow v_{\pi 2}\rightarrow \pi _3\rightarrow v_{\pi 3}\dots \rightarrow \pi _*\rightarrow v_{\pi *}\)

The policy function chosen for improvement at this point usually selects a greedy (Exploit) policy that selects the action that gives the highest value in the given state.

The value for each state is evaluated using the Bellman expectation equation.

\(v_{\pi }(s)=\sum _a^{\ }\pi (a|s)\lbrace R(s,a)+\gamma \sum _{s'}^{\ }P(s'|s,a)v_{\pi }(s')\rbrace \)


For demonstration purposes, we use the 4x4 Grid World problem. The goal is to find the shortest path from s0 to the destination 'End'.

First, set up the MDP as follows:

MDP setup for the 4x4 Grid World problem

Once all initial state values are initialized to 0, the value of the next state under the current policy can be expressed as the sum of each of the four actions from the following equation. It should be noted that the meaning of P(s'|s,a)vπ(s') is that if P(s'|s,a) = 0, then it does not move and stays in place, so s'=s, and therefore vπ(s') = vπ(s). In other words, P(s'|s,a) should not be multiplied itself, but should be used as a condition in the conditional statement.

\(v_{\pi }(s)=\sum _a^{\ }\pi (a|s)\lbrace R(s,a)+\gamma \sum _{s'}^{\ }\text{if}(P(s'|s,a),v_{\pi }(s'),v_{\pi }(s))\rbrace \)

where if(condition, A, B) returns A if the condition is true, otherwise return B.

​For illustration purpose, the state value function at s1 and s5 at the beginning:

\(v_1(s_1)=\sum _a^{\ }\pi (a|s)\lbrace R(s,a)+\gamma \sum _{s'}^{\ }\text{if}(P(s'|s,a),v_{\pi }(s'),v_{\pi }(s))\rbrace \)

\(=0.25*(-1+1*v_0(s_2))+0.25*(-1+1*v_0(s_0))+0.25*(-1+1*v_0(s_1))+0.25*(-1+1*v_0(s_5)) = -1.0\)

\(v_1(s_5)=\sum _a^{\ }\pi (a|s\lbrace R(s,a)+\gamma \sum _{s'}^{\ }\text{if}(P(s'|s,a),v_{\pi }(s'),v_{\pi }(s))\rbrace\)

\(=0.25*(-1+1*v_0(s_6))+0.25*(-1+1*v_0(s_4))+0.25*(-1+1*v_0(s_1))+0.25*(-1+1*v_0(s_9))=-1.0\)

In Excel, without anything special, just keeping the computations according to the order of the Bellman equation for each state, and as the iteration increases, we can create a new state value table based on the previous state values by copying and pasting.

The result after k iterations is as follows. Here, the last k=∞ represents the result of 1000 iterations using the Excel's iterative calculation feature.

The following shows the actions that maximize the value function in each state for each iteration. EWNS represents the direction of action for east, west, north, and south, respectively. It shows that there is no change in the policy after k=6, and it has converged to the final policy.

"Policy Iteration" means to use the policy obtained from above as a new policy function for value evaluation, as follows.

It was shown that even with only 6 iterations of calculation under the initial random policy, the convergence to the optimal policy was achieved. That is, 1000 iterations of calculation are necessary for the convergence of value calculation, but 6 iterations of calculation are sufficient for the optimal policy. In other words, the same result is obtained even if it stops after 6 iterations for optimal policy decision. This is called early stopping.

If our goal is not prediction but control of behavior, in extreme cases, we may only repeat the predicted value once and then are able to improve the policy. That is, we can perform the improvement of the value function and the improvement of the policy proceed alternately.

\(v_0\rightarrow \pi _0\rightarrow v_{1,\pi 0}\rightarrow \pi _1\rightarrow v_{2,\pi 1}\rightarrow \pi _2\rightarrow v_{3,\pi 2}\rightarrow \pi _3\rightarrow v_{4,\pi 3}\dots \rightarrow \pi _*\)

The table below shows that the optimal policy function is obtained by iteration 6 times of value calculation and policy improvement.

In Excel, we only need to use the AND function and MAX function to get the improvement of the policy function.

=IF(AND(C34=C35,C34=D34,C34=C33,C34=B34),0.25,(IF(MAX(C33,B34,C35,D34)=B34,1,0)))

Please refer the worksheet "simpler policy iteration" in the attached Excel file for the detail usage of the functions.

(2) Value Iteration

The method of finding policies through iterative value evaluation uses the Bellman optimality equation below, in short, it determines the policy by choosing the action that gives the highest value at that moment among the available actions.

\(v_*(s)=\max _a^{ }\lbrace r_{|s,a}+\gamma \sum _{s'}^{\ }P(s'|s,a)v_*(s')\rbrace \)

All MDP setups used in the above equation are the same as the Policy Iteration, and the probability for the action policy π(a|s) is no longer necessary (because we choose the action giving the highest value at the moment). As shown in the formula, instead of the sum of expected returns for all possible actions determined by the policy, only the maximum value of expected returns is chosen.

Like before, the transition probability is also modified to use the conditional statements as follows.

\(v_*(s)=\max _a^{ }\lbrace r_{|s,a}+\gamma \sum _{s'}^{\ }\text{if}(P(s'|s,a),v_*(s'),v_*(s)\rbrace \)

For the Value Iteration illustration, the state value function at s1 and s5 at the beginning, k=1:

\(v_1(s_1)= \max \lbrace R(s,a)+\gamma \sum _{s'}^{\ }\text{if}(P(s'|s,a),v_{\pi }(s'),v_{\pi }(s))\rbrace \)

\(=\max \lbrace (-1+1*v_0(s_2)),(-1+1*v_0(s_0)),(-1+1*v_0(s_1)),(-1+1*v_0(s_5))\rbrace \)

\(=\max \lbrace -1,-1,-1,-1\rbrace =-1.0\)

\(v_1(s_5)=\max \lbrace R(s,a)+\gamma \sum _{s'}^{\ }\text{if}(P(s'|s,a),v_{\pi }(s'),v_{\pi }(s))\rbrace \)

\(=\max \lbrace (-1+1*v_0(s_6)),(-1+1*v_0(s_4)),(-1+1*v_0(s_1)),(-1+1*v_0(s_9))\rbrace \)

\(=\max \lbrace -1,-1,-1,-1\rbrace =-1.0\)​

The function used in Excel for the state value function is a combination of MAX and IF.

=MAX(($H$3+$B$1IF($K$3,C9,B9)),($H$4+$B$1IF($K$4,A9,B9)),($H$5+$B$1IF($K$5,B8,B9)),($H$6+$B$1IF($K$6,B10,B9)))

The result after 6 repetitions from the initial condition is as follows, and it shows that there is no change in value thereafter. In other words, the table values ​​found the optimal policy with just 6 repetitions.

​