Reinforcement Learning
RL: When MDP is unknown - Control
2023-12-29
The essence of reinforcement learning lies in predicting the value of a state and determining the optimal action policy to maximize expected return. When the MDP is known (i.e., when the reward R(s, a) and probability P(s', r|s, a) are known), policy evaluation involves iterating policy improvement through value calculation and policy computation using a greedy method. However, when the MDP is unknown, policy evaluation predicts the action-value function q(s, a) through MC and TD, and the action policy is determined using an ε-greedy policy.

The ε-greedy policy function π(a|s) means to take a random action at the early stage of the episode, then it takes getting less chance of the random but higher probability of the greedy action. Its graphical representation is given below.

As the number of Episode k increases, chance of the random choice, ε is diminishing in a linear way (e.g., ε = 1 - 0.01*k), and its implementation in Excel is as follows:
If(RAND(0,1)<\(\epsilon\)) \(\pi (a|s)\)= random action (e.g., RANDBETWEEN(1,4))
else \(\pi (a|s)=\arg \max _a^{ } q(s,a)\)
where the argmax function reports the action which yields the highest return q(s, a) at the state among the actions.
(1) MC Control
In MC control, the expected return is calculated after completing one episode completely, and then the entire q(s, a) is updated. Here, to complete the episode by using ε-greedy policy, the argmax output is obtained using q(s, a) from the previous episode.
\(G_t=R_{t+1}+\gamma G_{t+1}\)
\(q(s,a)=q(s,a)+\alpha (G_t-q(s,a))\)
The same 4x4 Grid World problem is used to implement the MC Control in Excel. As shown in the Excel worksheets below, there are a few setup for the learning rate, the discount rate, ε-greedy policy, and the immediate reward R, and the columns are as follows:
- The discount rate = 1
- The learning rate = 0.001
- All the immediate reward R = -1
- Column ε = max(1- 0.01*(k-1), 0.1)
- Column rand is the random number between 0 and 1
- Column randbetween is the action id randomly selection between 1 and 4
- Column State(s) is the current state s among 0 ~ 15
- Columns Q(s,1), Q(s,2), Q(s,3), Q(s,4) are the action values to be used for the argmax function for the greedy action

(step 1) Generate episodes using ε-greedy based on the q(s,a) table from the previous episode.
(step 1.1) Column reference is the row index of the q(s,a) table from the previous episode later to find argmax q(s, a).
(step 1.2) Column greedy is the action index of the argmax q(s, a). =MATCH(MAX(H7:K7),H7:K7,0)
(step 1.3) Column action is the result of the ε-greedy policy. If rand()<ε, π(a|s)=RANDBETWEEN(1,4), otherwise take the value in the Column greedy.
(step 1.4) As the result of the action in the Column action, next state is recorded in the Column state (s') following the rule below, except when s=15, the Terminal state, set s' = 0.
- action=1, (East) s' = if(mod(s,4)=3, s, s+1)
- action=2, (West) s' = if(mod(s,4)=0, s, s-1)
- action=3, (North) s' = if(floor(s/4,1)=0, s, s-4)
- action=4, (South) s' = if(floor(s/12,1)=0, s+4, s)
(step 1.5) Update the expected return Gt.
\(G_t=R_{t+1}+\gamma G_{t+1}\) =IF($F7=15,0,$B$4+$B$1*O8)
\(G_t=0\) at the Terminal state st=15

(step 2) Update q(s,a) table whose total number of columns is 15x4=60. Only update the current state s and action a among them.
=IF(AND(R$5=$F8,R$6=$M8),R7+$B$2*($O8-R7),R7)
For any change in s or a, update q(s, a) = q(s, a) + α(Gt-q(s,a))
otherwise, q(s, a) = q(s, a)
Following plot show some of the action value q(s,a) change over the 2000 episode learning by MC Control.

After the enough episodic learning, one of the optimal actions is shown in the episode number 1968 in yellow as the list of states(action) Start/s0(a4)-s4(a1)-s5(a1)-s6(a1)-s7(a4)-s11(a4)-s15/End

Using the final q(s,a) from the MC control learning, the action policy at each state can be expressed as follow, and it agrees well with the human perception.

(2) TD Control - SARSA and Q learning
In MC Control, while using MC as the action policy, the expected returns Gt could only be calculated after a complete episode, and after that, q(s,a) is updated.
As discussed in the value evaluation blog, TD can update V(s) before the episode ends using the Bellman state equation itself as the expected return. In the same context, the Bellman equation can be used to control actions as follows.
\(q_{\pi }(s,a)=E_{\pi }[r'+\gamma q_{\pi }(s',a')]\)
Here, we can think that if we have enough episodes given, the average of the \(r'+\gamma q_{\pi }(s',a')\) will converge into the expected return qπ(s,a).
In the end, updating qπ(s,a) is done in the same manner with TD learning.
\(q_{\pi }(s,a)=q_{\pi }(s,a)+\alpha \lbrace r'+\gamma q_{\pi }(s',a')-q_{\pi }(s,a)\rbrace \)
which is the so-called "SARSA" as a TD action control learning.
When using the Bellman optimality equation instead in the same context
\(q^*(s,a)=q^*(s,a)+\alpha \lbrace r'+\gamma \max q^*(s',a')-q^*(s,a)\rbrace \),
which is the so-called "Q-learning" algorithm.
Here, SARSA is referred to as On-policy which means the action policy should be on-line (can't separate from the learning), because as indicated in the above expression, we need to know the next action value qπ(s',a') to update the current qπ(s,a).
Comparisons between SARSA and Q-learning are tabulated as below.
| SARSA | Q-Learning | |
| Action Policy, π(a|s) | ε-greedy | ε-greedy |
| TD Target | ε-greedy using qπ(s',a') | ε-greedy using q*(s',a') |
| Policy | On-policy | Off-policy |
To implement TD control through Excel, let's examine the case of the same 4x4 Grid World. In the Excel settings below, everything is the same as MC control, except that to calculate argmax Q(s,a) for implementing the ε-greedy policy, instead of Q(s,a) from the previous episodes in MC, TD uses the Q(s,a) from the immediate previous step.

Since the overall procedures are repeating that at step t, by choosing an action (a) according to the policy from state (s) to obtain a reward (r) and move to (s'), then repeat by selecting action (a'), i.e.
S→A→R→S'→A'
the name SARSA came after.
TD Target uses the value corresponding to q(s',a') in the q(s, a) table at the current step to define as follows:
SARSA:
\(\text{TD target}=r'+\gamma q(s',a')\)
Q-learning:
\(\text{TD target}=r'+\gamma \max _{a'}^{ }\ q(s',a')\)
Update q(s, a) at the next step using the following equation
\(q(s,a)=q(s,a)+\alpha \lbrace \text{TD target}-q(s,a)\rbrace \)
Shown below are the action value q(s,a) change over the 3000 episodes. To get the value converge, the plots show that it requires more than several thousands of the episodes to run.

As shown below, after the enough episodic learning, both SARSA and Q-learning show the action policy from the state value function work as desired to get the optimal path.
By SARSA:

By Q-learning:
