14 Treat Hunting 101
Reinforcement Learning, MDPs, and the Bellman Equation
🐾 Evergreen Chapter: Core mathematics and theory do not expire. Last verified: 2026-02-24
Prerequisites: Chapter 1-13 Technical Keywords: Reinforcement Learning (RL), Markov Decision Processes (MDP), Agent, Environment, State, Action, Reward, Transition Probability, Discount Factor (\gamma), Return
Imagine Pixel is placed in a brand new, incredibly complex living room. There are couches to jump on, laser pointers to chase, and a robotic treat dispenser in the corner.
There is no human telling him what to do. There is no dataset.
Pixel is the Agent. The living room is the Environment. Pixel looks around and registers his current situation (the State). He decides to jump on the couch (an Action). Because he jumped on the couch, he finds a hidden piece of kibble! He eats it (a Reward). Because he jumped, his location in the room has now changed (the Next State).
He repeats this loop over and over. Look, Act, Get Reward, Update State. This is the core loop of Reinforcement Learning (RL).

14.1 The Markov Decision Process (MDP): The Math of the Room
To teach an AI to navigate this room, we must translate the entire physical universe into five mathematical letters: (S, A, P, R, \gamma). This is called a Markov Decision Process (MDP) (Bellman, 1957).
1. State Space (S): Every possible situation Pixel could be in. (e.g., s_1 = on the floor, s_2 = on the couch, s_3 = next to the dispenser).
2. Action Space (A): Every possible move Pixel can make. (e.g., a_1 = walk forward, a_2 = jump, a_3 = meow).
3. Transition Probability (P): The physics of the room. If Pixel is on the floor (s_1) and decides to jump (a_2), does he always land on the couch (s_2)? Maybe the floor is slippery, so he only succeeds 80% of the time, and 20% of the time he faceplants back onto the floor (s_1). P(s_{next} \mid s_{current}, a)
4. Reward Function (R): The treat! If Pixel reaches the dispenser, the room gives him +10. If he touches the hot stove, the room gives him -50. If he just walks around, the room gives him -1 (to encourage him to find the treats quickly, rather than wandering forever).
Analogy
The MDP is the rulebook of a board game. It defines the board (S), the legal moves (A), the dice rolls (P), and the points (R).

14.2 The Markov Property: Living in the Moment
Why is it called a Markov Decision Process? Because of the Markov Property, a mathematical assumption that simplifies the universe.
The Markov Property states: “Given the present state and current action, the next state distribution P(s' \mid s, a) does not depend on earlier past history.”
If Pixel is currently sitting on the kitchen counter (s_{current}), the probability of him successfully jumping to the fridge (s_{next}) depends only on the fact that he is on the counter and the jump action (a) he chooses. It does not matter if he got to the counter by climbing the curtains, jumping from a chair, or being placed there by a human.
The current state contains all the necessary information. The past can be safely forgotten.
(Note: If the state is just “a frozen picture of a room,” the Markov property fails, because a picture doesn’t show velocity—Pixel might be falling! In RL, the “State” must include everything needed to make a decision, like position AND velocity).
14.3 The Discount Factor (\gamma): Treats Now vs. Treats Later
Pixel is standing in the hallway. He can take one step to the left and get a small treat (+1). Or, he can walk 10 steps down the hallway to the dispenser and get a massive pile of treats (+10).
Which should he choose? In RL, the goal is not to maximize the immediate reward. The goal is to maximize the Cumulative Return (G_t)—the sum of all treats he will get from now until the end of time.
G_t = R_{t+1} + R_{t+2} + R_{t+3} \dots
But there is a problem. What if the game never ends? What if Pixel walks in a circle picking up +1 treats infinitely? The math explodes to infinity.
To prevent the sum from diverging to infinity, we introduce the Discount Factor (\gamma), a number between 0 and 1 (usually 0.99). This also mathematically biases the agent to prefer rewards sooner rather than later. We tell Pixel: “A treat today is worth exactly 1 treat. A treat tomorrow is worth 0.99 treats, a treat 1 week (7 steps) later is worth 0.99^7 \approx 0.93 treats, and it only drops to about 0.50 treats after ~69 steps.”
G_t = R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + \dots
- If \gamma = 0: Pixel becomes completely short-sighted. He only cares about the treat directly in front of his face. He will never plan for the future.
- If \gamma = 0.99: Pixel is a master chess player. He will willingly take a -1 penalty right now if it sets him up for a +100 reward 20 steps in the future.
Catastrophic Failure: The Infinite Loop
If you set \gamma = 1.0 in a continuing environment that never ends, the Return sum may fail to converge—for instance, if every step gives a reward of +1, it explodes to infinity, so the agent cannot compare two actions that both yield unbounded return.
Rosetta Stone
| Kitten World | Math/Concept | Python (gymnasium, preferred) |
|---|---|---|
| “The Room & Physics” | Environment / MDP | import gymnasium as gym; env = gym.make('CartPole-v1') |
| “Look around” | State (S) | state, info = env.reset() |
| “Take a step” | Action (A) | next_state, reward, terminated, truncated, info = env.step(action) |
| “The total treats collected” | Return (G_t) | total_return = 0.0; total_return += (gamma ** t) * reward |
14.4 The Goal: Finding the Perfect Policy (\pi)
We have defined the room. We have defined the treats. We have defined how Pixel calculates the value of the future.
Now, what is the actual output of an RL algorithm? What is Pixel trying to learn?
He is trying to learn a Policy (\pi). A policy is just a mapping from States to Actions. It is Pixel’s internal rulebook. “If I am in state s, I will take action a.”
(Note: You must completely forget middle-school geometry for a second. In Reinforcement Learning, \pi does not mean 3.14. It is just the Greek letter used to stand for Policy.)
a = \pi(s)
- A Deterministic Policy says: “If I am on the couch, I will always jump to the table.”
- A Stochastic Policy says: “If I am on the couch, I will jump to the table 80% of the time, and sleep 20% of the time.” (This uses Probability, from Chapter 2!)
The ultimate goal of Reinforcement Learning is to find the Optimal Policy (\pi^*)—the set of rules that, under the modeled MDP assumptions, maximizes expected discounted return over time.
How does he find \pi^*? By exploring the room, making catastrophic mistakes, updating his math, and trying again.
14.5 Removing the Training Wheels
The Real Engineering
We used a board game with treats to explain MDPs. In formal RL, an MDP is a tuple (S, A, P, R, gamma). The ‘Markov Property’ means that, given the current state and action, next-state probabilities are independent of all earlier history. The agent’s goal is to learn a policy that maximizes the expected discounted return. If you mess up your reward function, the optimizer will ruthlessly exploit it, leading to specification gaming where the agent finds a degenerate, unintended solution.
14.6 Paws and Reflect: Chapter Quiz
Work through these conceptually before checking the answers.
You are training an AI to play a video game. The AI pauses the game forever because the reward for pausing is 0, but the reward for playing and dying is -1. What part of the MDP did you design poorly?
In a maze game, the agent gets +100 for finding the exit. It takes 500 steps to find it. But you didn’t give it any negative rewards for taking steps. What will the agent’s path look like?
If you set the Discount Factor (\gamma) to exactly
1.0in an environment that never ends, what mathematical disaster happens to the Return (G_t)?Why does the Markov Property state that the future depends only on the current state? Why shouldn’t Pixel remember his entire path?
Reveal answers
- The Reward Function (R). If standing still is safer than playing, the agent will learn a policy of complete inaction. You should give a slight negative reward (e.g., -0.01) for every second it wastes, forcing it to move.
- With \gamma = 1.0 (no discounting) and no negative “step penalty,” a chaotic, looping mess—taking 5 steps or 5,000 steps gives the exact same +100 return. With \gamma < 1 (e.g., \gamma = 0.99), discounting does prefer shorter paths (100 \times 0.99^5 \approx 95.1 vs. 100 \times 0.99^{500} \approx 0.66), though adding a small per-step penalty makes shortest-path behavior much sharper and faster to learn.
- The Return may fail to converge (e.g., diverging to infinity if positive rewards keep accumulating)! The math breaks down when actions yield unbounded returns that cannot be compared. Setting \gamma < 1 (with bounded rewards) guarantees the infinite sum converges to a finite number.
- Because storing the entire history of an agent’s life requires infinite memory. The Markov Property assumes that if the “Current State” is designed correctly (including velocity, momentum, etc.), it inherently contains all the context needed to make a decision, saving massive computation.
14.7 Chapter 14 Summary
You’ve learned how to define the world for an exploring agent:
- The RL Loop: Agent → Environment → State → Action → Reward → New State.
- MDP (S, A, P, R, \gamma): The mathematical definition of the room, the moves, the physics, the treats, and the time horizon.
- The Markov Property: Given the present state and action, the next state distribution P(s' \mid s, a) is independent of past history.
- Discount Factor (\gamma): Mathematically preferring rewards now vs. later.
- Policy (\pi): The agent’s rulebook mapping states to actions.
But knowing the rules of the room is not enough. Pixel needs to learn which states are good—which rooms in the house actually have treats?
It is time to learn the Value Function. 🐈📊