Reinforcement Learning
RL: Actor-Critic
2023-12-30
As a Policy Gradient algorithm, Reinforce algorithm used the expected return Gt as the value function q(s,a) for policy gradient calculation.
\(G_t=R_{t+1}+\gamma R_{t+2}+\gamma ^2R_{t+3}+\dots =\sum _{k=0}^{\infty }\gamma ^kR_{t+k+1}\)
\(\nabla_\theta J(\theta)=E\left[\nabla_\theta\log (\pi _\theta(a|s))G_t\right]\)
If the value function q(s, a) instead of Gt is used at the step, it is called an actor-critic method. In cases where actions are continuous, or when the selection of states or actions is discrete but the space is infinitely large, continuous functions such as artificial neural networks are better way to present the value functions and policy functions. In this case, typically, the policy function is represented as a function of the parameter θ, and the value function is expressed as a function of the parameter w.
Recalling Reinforce algorithm, the policy function πθ(a|s) was represented by the Softmax function. As the episode progresses, the values of θ(s,a) or w(s,a) for each state and action should be progressively improved, where πθ(a|s) plays the role of the actor determining the agent's actions and qw(s,a) or vw(s) play the role of the critic, evaluating the outcome of the selected action.
The ultimate goal of the actor-critic algorithm is still to find the optimal policy function, and to improve the stability and efficiency of computation, the following types of qw(s, a) alternatives are used.
Q-Actor-Critic:
\(\nabla_\theta J(\theta)=E\left[\nabla_\theta\log (\pi _\theta(a|s))q(s,a)\right]\)
Advantage Actor-Critic:
\(\nabla_\theta J(\theta)=E\left[\nabla_\theta\log (\pi _\theta(a|s))\left\{q(s,a)-v(s)\right\}\right]\)
Time Difference (TD) Actor-Critic:
\(\nabla_\theta J(\theta)=E\left[\nabla_\theta\log (\pi _\theta(a|s))\left\{r+\gamma v(s')-v(s)\right\}\right]\), or
\(\nabla_\theta J(\theta)=E\left[\nabla_\theta\log (\pi _\theta(a|s))\left\{r+\gamma q(s',a')-q(s,a)\right\}\right]\)
In this blog, the next TD actor-critic using the state value function, v(s) will be illustrated.
\(\nabla_\theta J(\theta)=E\left[\nabla_\theta\log (\pi _\theta(a|s))\left\{r+\gamma v(s')-v(s)\right\}\right]\)
The way executing the algorithm is identical with Reinforce algorithm, except using the following instead of Gt,
\(r+\gamma v(s')-v(s)\).
The state value function v(s) is updated in the same way with the TD for prediction using TD Target = \(r+\gamma v(s')\). (Recall that TD prediction uses the random selection as the action policy, meanwhile TD control uses the ε-greedy as the policy).
\(\delta \equiv r+\gamma v(s')-v(s)\)
\(\theta'=\theta+\alpha_1\nabla_\theta\log (\pi _\theta(a|s))\delta\)
\(v'(s)=v(s)+\alpha_2\delta\)
If v(s) is a continuous function of w, use the Gradient Ascent method to update w.
\(\delta \equiv r+\gamma v(s')-v(s)\)
\(\theta'=\theta+\alpha_1\nabla_\theta\log (\pi _\theta(a|s))\delta\)
\(w'=w+\alpha_2\nabla_wv_w(s)\delta\)
Here α1 is the learning rate for the policy function parameter θ, and α2는is the learning rate for the value function.
If the state value function v(w,si) is a linear funcion of w(si) such that v(w,si) = w(si)+bias, the gradient of v to the w \(\nabla_wv_w=1\). Therefore, the use of TD value function is the same meaning with the modeling v as a linear function of w.
\(\left\{v'(s)=v(s)+\alpha_2\delta\right\} \equiv \left\{w'=w+\alpha_2\delta\right\}\)
For Excel demo, the same 3x3 Grid World problem to implement Actor-Critic is used. Worksheet structure point of view, the state value function, v, per state and TD target columns are all required to add to the same worksheet for Reinforce Algorithm.

(step 1) TD Target
\(r+\gamma v(s')\)
TD Target is the immediate reward at the next state (s') plus the discounted value function at the next state (s') as a result of the current action policy (a) at the current state (s)

(step 2) Update the state value function v
Calculate the δ which is the difference between the TD Target and the current state value v(s)
\(\delta = TD Target -v(s)\)
Update the state value function v(s) by TD prediction method.
\(v(s)=v(s)+\alpha_2\delta\)

(step 3) Policy function and the Policy gradient
Update the Policy function and its gradient in the same way with the Reinforce algorithm but using the δ instead of Gt.
Following show the change of the state value function v(s) and policy function πθ(a|s) as the episodes progress.


If v(s) is tabulated at the state position, the green states below show the optimal route to arrive at the terminal.

Meanwhile, the optimal route decided by the policy function πθ(a|s) is shown in the below.

Please note that the optimal route could be intrinsically decided by the value function as shown, but the action at each state will be exclusively chosen by the policy function.