The Policy Gradient
In my course, we reviewed the Linear Quadratic Regulator and its downstream variants like MPC. While this deserves its own post, I'm just not going to get to it. Instead, I'll talk about a newer solution to the optimal control problem that leverages machine learning.
Stochastic Controllers
Up until this point, I've only dealt with deterministic feedback controllers. Given a certain state, we know what our control input will be. Even though this control inputs affect on the next state will often be stochastic due to the noise term in the dynamics, the controller itself was deterministic. Here, we do not make this assumption. Instead, our control input will be sampled from a distribution learned by a parameterized model. Why do we use a stochastic controller? As you'll eventually see, it helps us compute the policy gradient. And what's the policy gradient? Let me give some background.
Say we already have a stochastic controller parameterized by known as . We pass in a state to our model and get a distribution of control inputs. Our model often just predicts the mean of a Gaussian and we have a predetermined variance. Therefore:
Our goal is to find the parameters of a model that maximize the average expected reward (we use reward instead of cost). Say we find a particular trajectory by running our stochastic controller repeatedly—the reward is a discounted return:
The goal can then be stated as:
Since our controller is stochastic, we are saying that we want to find the parameters of our model that will maximize the average return when we find a trajectory using that controller and a start state.
One way to improve our controller is by randomly perturbing the parameters, re-evaluating the expected return, and choosing parameters that perform better. Another way is by computing the policy gradient.
The search space is way too large to directly find the parameters that maximize this function and there is no closed form solution for such a broad problem as there is in a more constrained setting like LQR. Instead, we can use gradient ascent. This requires us to compute the policy gradient with respect to the parameters. The policy tells us which direction to shift the parameters to increase the expected return.
More formally, the gradient ascent algorithm looks as follows:
The tricky part is computing the policy gradient. Let's first take a look at the expanded gradient:
Since the return and trajectory do not explicitly depend on the parameters, we can move the gradient inside and then do a few tricks:
Now our policy gradient can be computed as the expectation over all trajectories of the reward times the gradient of the log probability of that trajectory. First, we can approximate this by sampling some finite trajectories from our controller in Monte Carlo fashion:
This is much more tractable, but we still have an unaddressed gradient . Let's break that down. We know that the probability of a trajectory is given as:
and the log probability:
Now taking the gradient lets remove the transition probability because it does not depend on :
Calculating is simple because we know how to find the probability given a sample from our distribution (think of finding the probability of a sample from a Gaussian). Therefore, we've found a tractable approximation of the policy gradient! A library like PyTorch will help us find the partial derivatives of every parameter in our model with respect to the policy gradient using automatic differentiation.