Markov Decision Processes
An MDP is the rulebook for learning by trial and error: state, action, reward, next state, repeated so the agent can chase long term return.
Why Does This Exist?
You cannot train sequential choices without writing down the rules first. What states exist, which moves are legal, what the world does next, and what counts as success. Without that shared model every algorithm means something slightly different by reward or value.
The Markov decision process gives you that contract in five parts. It also names the delay problem directly: a move that looks fine now can cost you ten steps later, so the objective sums rewards far into the future with discounting. This page defines the five parts and works one return by hand. For the values built on top see value functions, for the recursion see bellman equations, and for the mapping from states to moves see policy.
You need one idea first. Expectation averages over randomness. Transitions and rewards vary, so returns and values are always averages over possible futures, not single fixed paths.
Think of It Like This
Delivering parcels across a small town
Your state is the corner you stand on. Your actions are turn left, turn right, or go straight. Each choice moves you to a new corner, sometimes with a traffic delay you did not choose. Each leg pays a small cost in minutes, and a delivered parcel pays a bonus.
You want the route with the best total over the whole shift, not the fastest single block. A shortcut that saves one minute now but strands you in a jam later loses. That total over time is the return, and your usual route is the policy.
Where the analogy stops: town maps stay fixed while many RL worlds change as you learn, and traffic randomness is mild compared with a truly stochastic transition model.
How It Actually Works
An MDP is the tuple . Let be state, action, next state, reward at time , and in the discount factor.
- is the state space, every situation the agent can be in.
- is the action space, every move it can take.
- is the transition probability of landing in from after .
- is the immediate reward for that triple.
- weights near rewards above far ones.
The Markov property says the future needs only the present:
History does not matter once you know . That is what keeps the math workable.
A policy maps states to action chances. The return sums discounted rewards from time :
The state value is expected return from under , and fixes the first action too. Those expectations lead straight to the Bellman recursion covered in the linked page.
Worked example
Three steps, , rewards , , :
- Step 1 contributes .
- Step 2 contributes .
- Step 3 contributes .
- Return .
Late reward still matters but less than immediate reward. That 2.62 is the number the diagram carries around the loop.
Code
The snippet adds the same discounted sum.
def discounted_return(rewards: list[float], gamma: float = 0.9) -> float: """Sum rewards with compounding discount gamma.""" total = 0.0 for t, r in enumerate(rewards): total += (gamma**t) * r return total
if __name__ == "__main__": print(round(discounted_return([1.0, 0.0, 2.0], gamma=0.9), 2))# -> expected output:2.62Watch Out For
Tabulating every state until the table explodes
The symptom: value iteration works on a grid demo, then stalls on a real task with dozens of sensors. Twenty binary features already mean about one million states, and continuous sensors mean infinitely many. The code runs but never visits the same state twice.
The fix: keep the MDP framing and change the solver. Use function approximation that generalizes across states, and track sampled Bellman error instead of sweeping a table.
Paying for the proxy instead of the goal
The symptom: the agent scores high and behaves badly. A cleaning robot paid per area covered drives in circles, because the reward counts motion rather than dirt removed. Training curves look great while the real task fails.
The fix: rewrite the reward to match the outcome you want, then test on held out starts. When a proxy is all you have, cap its loopholes with penalties before scaling training.
The Quick Version
- An MDP is : states, actions, transition chances, rewards, and discount.
- The Markov property says the next state needs only the current state and action, not the full history.
- Return sums discounted future rewards, so near rewards weigh more than far ones.
- Rewards 1.0, 0.0, and 2.0 at discount 0.9 give return 2.62, with the late 2.0 worth 1.62 today.
- Values are expected returns, and Bellman equations turn them into one step backups.