Lecture 19: Reward Models and Linear Dynamical Systems
Stanford CS229 lecture 19 notes: Reward Models and Linear Dynamical Systems.
- cs229
- machine-learning
- course-notes
On this page
Designing the reward function#
In many problems, the reward function is very hard to specify. Take a self-driving car. What's the reward for "driving well"? A big penalty for crashing, a small one for crossing lane markers, a bonus for smooth driving, reaching the destination fast...? You have to trade off many things, and how you weigh them changes the behavior.
One approach covered here is to learn the reward from demonstrations of a human expert, which is called inverse reinforcement learning. We'll stick to the case where reward is given.
Linear Dynamical Systems and LQR#
There's a special class of problems for which the optimal policy has a closed form: Linear Quadratic Regulation (LQR).
Linear dynamics:
Quadratic reward:
where and are positive semi-definite matrices. The reward is largest (zero) when and . So the goal is to keep the system near zero (like keeping a helicopter hovering at a point) using as little control effort as possible.
Finite horizon LQR#
The problem is over time steps. We solve it by dynamic programming, working backwards from time .
At the last step, the value function is quadratic: , where and .
Then going backwards for , the value function stays quadratic: , with:
(The recursion is called the discrete Riccati equation.)
The optimal policy#
It's a linear function of the state:
Two beautiful facts:
- The policy depends on but not on , and is the only thing that depends on the noise . So the optimal policy is the same regardless of the noise. You can ignore it when computing .
- All of this is computed in closed form, with just matrix operations.
graph TD
A["Start from t = T: Φ_T = -U"] --> B["Compute Φ_(t) from Φ_(t+1)"]
B --> C["Compute L_t"]
C --> D["Apply: a_t = L_t · s_t"]What if the problem is not linear?#
Most real problems aren't linear and quadratic. But we can often approximate them as such.
Linearizing around a point#
Take a non-linear model . Around some operating point , use the first order Taylor expansion:
This is linear in and . So we can run LQR on it. It works well if the system stays near the operating point, like a helicopter hovering.
Differential Dynamic Programming (DDP)#
For tasks where the system moves along a trajectory (e.g., a car following a path), do this:
- Come up with a rough trajectory using some initial controller.
- Linearize the dynamics (and quadratize the reward) around the trajectory at each time step.
- Run LQR (which now gives time-varying , ) to compute a better controller.
- Simulate the new controller to get a new trajectory.
- Repeat until it converges.
This is a very practical method for control problems like helicopter flight.