Using Mario to Derive the Fundamentals of Reinforcement Learning
"TLDR: This article uses the Mario game as an example to provide an accessible introduction to the foundational theories of reinforcement learning (RL), including core concepts such as policy functions, value functions, and advantage functions. It elaborates in detail on the application of Monte Carlo methods and temporal difference methods in policy optimization, and explores how the PPO algorithm addresses the issue of unstable training."
Basic Definitions
-
: The probability distribution of taking action in state . This is a policy function that samples to select actions. In the Mario game, it means deciding whether to move left, move right, or jump given the current state.
-
: The value function, representing the expected cumulative reward obtainable from state onward. In the game, it refers to the total score you can accumulate from the current position until the game ends.
-
On Policy: Optimizing the current policy while collecting data. In the game, it means optimizing the policy based on historical score rewards before the current game has even ended.
-
Off Policy: Optimizing the policy only after collecting a complete set of data. In the game, it means you must finish an entire game, obtain the final score, and only then aggregate the historical data to optimize the policy.
-
Advantage function: : represents the expected cumulative reward obtainable in the future after taking action in state and entering the next state. Clearly, if the action taken yields a higher future reward than the average future reward of state , then it is a good action. For example, if there's a coin above Mario's head, choosing to jump will provide more points in the future, while choosing not to jump means missing out and getting fewer future points. Obviously, jumping is better.
All theoretical derivations in RL revolve around one thing: how to optimize the policy function so that can choose the most beneficial action for the future in every state.
If there existed an omniscient value function that knew the value of every state, things would be easy. In the current state, you could enumerate all possible actions, see which action leads to the next state with the greatest advantage gain, and pick that one. Unfortunately, is very difficult to define directly—no one can predict the future.
Monte Carlo Methods
RL training based on Monte Carlo methods aims to find a policy that maximizes the reward score.
The total future reward obtainable after taking action in state :
After Mario finishes an entire game, we obtain the sequence , with every reward at each moment recorded. Then the expected cumulative future reward for a given state is easy to compute:
Next, we use the Monte Carlo method to optimize the policy function:
Explanation:
-
The original is an estimate—a prediction of how much cumulative reward will be obtained in the future.
-
is the actual cumulative reward computed after the game ends, representing the future reward obtained by taking action in state .
-
So we update based on the difference to fit the true value.
-
is the learning rate.
This completes one update of the policy function. What's next? Play another round of Mario, obtain another sequence and the scores at each step.
However, to ensure the model doesn't get stuck in a local optimum, we can have Mario take the action with maximum with probability in state , ensuring there's still some probability of trying new actions.
Temporal Difference Methods
Monte Carlo methods are good, but you need to finish an entire game before training once. In large games, completing one round could take an unknown amount of time. So we want a way to optimize the policy function during the game based on the historical state sequence. This is where temporal difference comes in.
Same as before, we want to find a that maximizes .
Policy update:
Training process: In state , take action , receive reward , enter the next state , and then update .
Note: Both values are randomly initialized, but even so, using to update is reasonable. With sufficiently long training, the updates to depend on the true environment reward function and state transition probabilities, so convergence is achievable.
A more rigorous proof relies on the recursive nature of the Bellman expectation equation. Since this derivation is complex, we won't go deeper here.
After completing one round of training, when sampling actions in the next round, similar to the Monte Carlo method, we still need to use an -greedy strategy to ensure there remains an opportunity to explore new actions.
PPO (Proximal Policy Optimization)
After solving the slow training problem, RL training still faces many other issues, such as training instability. When policy gradient updates are too large, the model's policy can easily collapse, swinging from one extreme to another. For example, Mario discovers that if he doesn't move, he won't die, so he simply gives up and does nothing—at least the score won't be negative.
Once we understand the phenomenon, the solution naturally emerges: limit the update magnitude of the policy function. Don't take steps that are too large. We can use KL divergence to quantify the difference between two distributions and constrain it within a certain range.
Derivation: