Model-Free RL Method
In model-based method, we need firstly model the environment by learning/estimating the transition and reward functions. However, in model-free method, we consider learning the value/utility functions V ( s ) V(s) V(s) or U ( s ) U(s) U(s) or action-value functions Q ( s , a ) Q(s,a) Q(s,a) directly without learning P ( s ′ │ s , a ) P(s^′│s,a) P(s′│s,a) and R ( s , a , s ′ ) R(s,a,s^′) R(s,a,s′). There are also two main kinds of approaches, one is called Monte Carlo (MC) Learning and another one is called Temporal Difference (TD) Learning. The main difference between them is how often to update the policy.
Monte Carlo Learning
Firstly, we consider the prediction problem, how to compute/estimate the U ( s ) U(s) U(s) or Q ( s , a ) Q(s,a) Q(s,a)?
In Monte Carlo learning, the agent executes a set of trails based on the GLIE scheme (trading the exploration and exploitation off). In our example environment, a trail starts from location ( 1 , 1 ) (1,1) (1,1) and ends in either terminal states ( 4 , 2 ) (4,2) (4,2) or ( 4 , 3 ) (4,3) (4,3), e.g., shown below with the rewards in subscripts:
The trails collected by the agent are samples or data, on which we can learn based. You must remember the Bellman equation for the utility function, and we have another version of it for the Q-function:
U ( s ) = R ( s ) + γ m a x a ∈ A ( s ) ∑ s ′ P ( s ′ │ s , a V ( s ′ ) U(s)=R(s)+{\gamma} max_{a \in A(s)} \sum_{s^′}{P(s^′│s,aV(s^′)} U(s)=R(s)+γmaxa∈A(s)s′∑P(s′│s,aV(s′)
Q ( s , a ) = R ( s ) + γ ∑ s ′ P ( s ′ │ s , a ) m a x a ′ Q ( s ′ , a ′ ) Q(s,a)=R(s)+{\gamma} \sum_{s^′}{P(s^′│s,a) max_{a^′}{Q(s^′,a^′)}} Q(s,a)=R(s)+γs′∑P(s′│s,a)maxa′Q(s′,a′)
where γ {\gamma} γ is the discounted factor.
Using these two equations, we can compute the utility or Q-value for each state, for example, in the trail 3, the return or reward-to-go of state ( 1 , 1 ) (1,1) (1,1) can be computed as (suppose γ = 0.9 {\gamma}=0.9 γ=0.9 here):
U ( [ 1 , 1 ] ) = − 0.04 + 0.9 × ( − 0.04 + 0.9 × ( − 0.04 + 0.9 × ( − 0.04 + 0.9 × ( − 1 ) ) ) ) = − 0.794 U([1,1])=−0.04+0.9 \times (−0.04+0.9 \times (−0.04+0.9 \times (−0.04+0.9 \times (−1))))=−0.794 U([1,1])=−0.04+0.9×(−0.04+0.9×(−0.04+0.9×(−0.04+0.9×(−1))))=−0.794
It can be an estimation of the return of state ( 1 , 1 ) (1,1) (1,1), while we have many many trials/samples, take an average value of all estimations, then we can get an approximate value for each state. And in infinitely many trails, sample average will converge to the expected value.
Here, actually you can see that in trail 1, location ( 1 , 2 ) (1,2) (1,2) appears two times, in this case, if we only use the first one, it becomes a first visit method, or if we use both of the samples, it becomes a every visit method, we will focus on the first visit method in this section.
Assume state s s s is encountered k k k times giving k k k returns. We can rewrite the average, U k ( s ) U_k(s) Uk(s) of k k k returns G 1 ( s ) , . . . , G k ( s ) G_1(s), ..., G_k(s) G1(s),...,Gk(s) as:
U k ( s ) = U k − 1 ( s ) + 1 k ( G k ( s ) − U k − 1 ( s ) ) U_k(s)=U_{k−1}(s)+ \frac{1}{k} (G_k(s)−U_{k−1}(s)) Uk(s)=Uk−1(s)+k1(Gk(s)−Uk−1(s))
where U k − 1 ( s ) U_{k−1}(s) Uk−1(s) is the estimate after receiving k − 1 k−1 k−1 returns, so G k ( s ) − U k − 1 ( s ) G_k(s)−U_{k−1}(s) Gk(s)−Uk−1(s) can be considered the prediction error for the k k k-th return.
In a similar way, we can also choose to learn the Q-functions instead of learning the value functions. After that, for the control problem, we only need to greedy choose the policy by:
π ( s ) = a r g m a x a ′ Q ( s , a ′ ) {\pi}(s)=arg max_{a′}{Q(s,a^′)} π(s)=argmaxa′Q(s,a′)
There is another problem you may want to know, how to sample trails in MC learning? As we have stated above, it’s better to using a GLIE scheme, for example, ϵ {\epsilon} ϵ-greedy, which means that for a probability of ϵ {\epsilon} ϵ, using the current policy (computed by the control problem part) to select a current-best action, and for a probability of 1 − ϵ 1−{\epsilon} 1−ϵ, choosing actions randomly.
MC learning is an instance of supervised learning: each example has state as input and observed return as output, so it’s simple and unbiased. However, the biggest disadvantage for MC learning is that we need to wait till the end of episode to get one trail and then learn can be done. So let’s introduce another method, which can learn after each step – temporal difference learning.
Temporal Difference Learning
Temporal difference (TD) learning exploits more of the Bellman equation constraints than Monte Carlo learning. For a transition from state s s s to s ′ s′ s′, TD learning does:
U π ( s ) ← U π ( s ) + α ( R ( s ) + γ U π ( s ′ ) − U π ( s ) ) U^{\pi}(s) \leftarrow U^{\pi}(s)+ \alpha (R(s)+{\gamma}U^{\pi}(s^′)−U^{\pi}(s)) Uπ(s)←Uπ(s)+α(R(s)+γUπ(s′)−Uπ(s))
where α {\alpha} α is the learning rate and γ {\gamma} γ is the discounted factor.
In the formula, we can see that it will increase U π ( s ) U^{\pi}(s) Uπ(s) if R ( s ) + γ U π ( s ′ ) R(s)+{\gamma}U^{\pi}(s^′) R(s)+γUπ(s′) is larger than U π ( s ) U^{\pi}(s) Uπ(s) and decrease it otherwise. So we call R ( s ) + γ U π ( s ′ ) R(s)+{\gamma} U^{\pi} (s^′) R(s)+γUπ(s′) the TD target and R ( s ) + γ U π ( s ′ ) − U π ( s ) R(s)+{\gamma}U^{\pi}(s^′)−U^{\pi}(s) R(s)+γUπ(s′)−Uπ(s) the TD error. The update formula will converge if α {\alpha} α decreases appropriately with the number of times the state has been visited, e.g. α ( n ) = O ( 1 / n ) {\alpha}(n)=O(1/n) α(n)=O(1/n).
The TD learning algorithm can be illustrated as following:
As you can see, TD can learn online after every step, instead, MC needs complete episode before learning. However, TD target depends only on one measured reward, while MC target depends on the sum of many rewards, so TD target has lower variance but is biased. Usually TD can converge faster than MC in practice.
To make it more accurate, we can also set more than one steps in TD target, which is called n-step TD: Let R t + γ R t + 2 . . . + γ n − 1 R t + n − 1 + γ n U ^ ( S t + n ) R_t+{\gamma}R_{t+2}...+{\gamma}^{n−1} R_{t+n−1}+{\gamma}^n \hat{U}(S_{t+n}) Rt+γRt+2...+γn−1Rt+n−1+γnU^(St+n) be the target for TD update. TD sets n n n equals to 1 while MC sets n n n to ∞ \infty ∞. Usually, an intermediate value of n n n tends to work better.
Based on the n-step TD that using one value of n n n, a new method averages over different values of n n n is called ***TD( λ \lambda λ)***. It sets the following as the target for update:
G t λ = ( 1 − λ ) ∑ n = 1 ∞ λ ( n − 1 ) G t : t + n G_t^{\lambda}=(1−{\lambda}) \sum_{n=1}^{\infty}{{\lambda}^(n−1) G_{t:t+n}} Gtλ=(1−λ)n=1∑∞λ(n−1)Gt:t+n
It calculates the weighted sum of n-step returns, where the n-th step is weighted by λ n − 1 {\lambda}^{n−1} λn−1, and this can be computed efficiently using a method called eligibility traces. Actually, it converges to TD as λ {\lambda} λ approaches 0, and to MC as λ {\lambda} λ approaches 1.
Now, we have seen the basic rule for prediction in TD method, now we turn the aim from utility function to Q-function, there are two different approaches to update the Q-function. They are called On-Policy methods and Off-Policy methods respectively and two examples for them are SARSA (State-Action-Reward-State-Action) algorithm and Q-learning algorithm.
- SARSA Algorithm:
Q ( s , a ) ← Q ( s , a ) + α ( R ( s ) + γ Q ( s ′ , a ′ ) − Q ( s , a ) ) Q(s,a) \leftarrow Q(s,a)+{\alpha}(R(s)+{\gamma}Q(s^′,a^′)−Q(s,a)) Q(s,a)←Q(s,a)+α(R(s)+γQ(s′,a′)−Q(s,a))
where a ′ a′ a′ is the action taken at s ′ s′ s′ based on the ϵ {\epsilon} ϵ-greedy action selection with the current policy.
- Q-learning Algorithm:
Q ( s , a ) ← Q ( s , a ) + α ( R ( s ) + γ m a x a ′ Q ( s ′ , a ′ ) − Q ( s , a ) ) Q(s,a) \leftarrow Q(s,a)+{\alpha}(R(s)+{\gamma} max_{a^′}{Q(s^′,a^′)}−Q(s,a)) Q(s,a)←Q(s,a)+α(R(s)+γmaxa′Q(s′,a′)−Q(s,a))
where a ′ a′ a′ represents all available actions at state s ′ s′ s′.
The biggest difference between these two methods is the m a x max max operator.
SARSA is an on-policy method, which means that it only tries to improve the same policy that is used to generate the training data. For example, in the learning, for the current policy π n o w {\pi}_{now} πnow, at the state s s s, the agent selects the action a a a (based on π n o w {\pi}_{now} πnow and the action-selection policy, e.g. ϵ {\epsilon} ϵ-greedy, same thing below), then the environment transits to state s ′ s′ s′ and returns reward r r r, finally, on the new state s ′ s′ s′, the agent again selects an action a ′ a′ a′, for now, we have get a tuple ( s , a , r , s ′ , a ′ ) (s,a,r,s^′,a^′) (s,a,r,s′,a′), using the formula above, we can do one-step update of the Q-function. In this method, selecting a ′ a′ a′ and selecting a a a is based on the same policy, so we generate data from the policy and update the same policy.
Q-learning is an off-policy method, which means that the updated policy can be different from the policies that are used to generate data. This is because of the m a x max max operation, just as the formula shows, to update the Q-function for a ( s , a ) (s,a) (s,a) pair, we need the m a x a ′ Q ( s ′ , a ′ ) max_{a^′}Q(s^′,a^′) maxa′Q(s′,a′) part. Here, we choose the maximum value among all possible actions, the new action a ′ a′ a′ in not limited to the current policy, we can use the data collected by other policies. You may have many differernt policies that generates and collects data in the environment, which are called behaivor policy, they can all be used to update a target policy. Here is also a pseudo-code of the Q-learning algorithm: