States, actions, slippery transitions and rewards. Build a world, turn the discount factor and watch the best route change, and see a value as the average of a thousand futures.
Now actions have consequences that last. A Markov decision process is a set of states, a set of actions, the probabilities of where each action leads, and the rewards collected along the way. The agent wants the most total reward over time, not just right now — and the future is uncertain.
Drive the orange agent with the arrows and feel the floor slip under you; hover or tap a cell to see where each action can lead. Then paint your own walls, pits and goals and drive again.
Orange arrows are the transition fan P(s′ | s, a) for the outlined cell and the highlighted action (thicker means likelier; a loop means bumping a wall and staying put). Rewards arrive on entering a cell: the step reward on floor, the goal’s or pit’s reward on those, which end the episode. The keyboard arrows also drive once the grid has focus.
A Markov decision process (MDP) is a tuple (S, A, P, R, γ): a set of states S, a set of actions A, transition probabilities P(s′ | s, a) giving the chance of landing in s′ after taking a in s, a reward function R (here a reward for arriving in each cell), and a discount factor γ that the next demo explores. In the gridworld every cell is a state and there are four actions. With slip 0.2 the agent moves as intended with probability 0.8 and slides to each side with probability 0.1 — the numbers on the transition fan.
The defining assumption is the Markov property: the distribution of the next state and reward depends only on the current state and action, not on the path that led there. P(Sₜ₊₁ | Sₜ, Aₜ, Sₜ₋₁, Aₜ₋₁, …) = P(Sₜ₊₁ | Sₜ, Aₜ). A state is whatever you need to know to predict the future; if the history matters, it belongs in the state.
Goals and pits are terminal states: entering one ends an episode, and the next episode begins again at the start. A policy π(a | s) says what to do in each state — deterministically, like a field of arrows, or as probabilities, like the random agent, which picks each of the four actions with probability ¼. Solving an MDP means finding a policy that makes the expected total reward as large as possible.
The 4×3 world is the textbook example from Stuart Russell and Peter Norvig’s Artificial Intelligence: A Modern Approach, with its 0.8 / 0.1 / 0.1 moves, +1 and −1 exits and −0.04 per step. The framework is older: Richard Bellman put its central idea, the principle of optimality, at the heart of Dynamic Programming (1957), and Ronald Howard’s 1960 book Dynamic Programming and Markov Processes popularised MDPs and introduced policy iteration.
Raise the slip until the agent abandons the bridge for the long way round, then drag across the map to see where that choice flips. Try the myopic preset, where only the next reward counts.
Cells are coloured by the optimal value V* (emerald good, rose bad) with the greedy policy’s arrows; the thick line is the most likely path from the start. The map on the right solves the world for every slip and γ at the current step reward: amber where the agent takes the bridge, sky where it goes around, grey where it never heads for the goal. Click or drag on it to jump there. The γ slider moves evenly in log horizon.
The agent maximises the return, the discounted sum of future rewards: Gₜ = Rₜ₊₁ + γRₜ₊₂ + γ²Rₜ₊₃ + … = Σₖ γᵏRₜ₊ₖ₊₁, with 0 ≤ γ ≤ 1. A reward k steps away counts γᵏ as much as one received now. The weights sum to 1/(1 − γ), so the effective horizon is roughly 1/(1 − γ) steps: about 10 for γ = 0.9, 100 for γ = 0.99. At γ = 0 only the very next reward matters, so far from the goal every action looks equally good and the agent goes nowhere.
Why discount at all? For tasks that never end, γ < 1 keeps the infinite sum finite whenever rewards are bounded (|Gₜ| ≤ Rmax / (1 − γ)). It also models uncertainty about whether the future will arrive, and a plain preference for sooner rewards, as in economics. For episodic tasks that are certain to end, γ = 1 is perfectly well defined. Discounting and the step reward interact: a negative step reward also hurries the agent, because every extra step costs something — which is why the bridge survives at higher slips when the step reward is harsher.
The optimal policy maximises expected return. It will gladly accept a real chance of falling in a pit if the expected value of the shortcut is higher; an MDP agent has no extra fear of disaster beyond what the rewards encode. Risk-sensitive reinforcement learning changes the objective when that matters. Notice too that the choice is not monotone in γ. With a short horizon the pits right beside the bridge loom larger than a goal several steps away; with a long horizon the detour’s extra steps cost little. At some slips the shortcut wins only for middling γ — look for the amber bulge in the map.
Press Play and launch a swarm of futures from the start; watch their returns pile into the histogram and the average close in on the exact value. Switch to the random policy, or click cells to move the start.
Each orange dot is one future (the agent at the start, replayed with different luck); the slip is 0.2, the goal pays +1, pits −1, other steps 0, and γ = 0.95, so reaching the goal in t steps is worth 0.95ᵗ. The histogram stacks goal futures (emerald), pit futures (rose) and futures cut off at 200 steps (grey). The dashed line is Vπ(start) computed exactly by policy evaluation; the faint dotted curves in the lower chart are ± 1.96 σ/√N around it.
The value of a state under a policy is its expected return, Vπ(s) = Eπ[Gₜ | Sₜ = s]: the average over every future that could unfold from s if the agent follows π. The action value Qπ(s, a) = Eπ[Gₜ | Sₜ = s, Aₜ = a] is the same average when the first action is fixed. Both can be computed exactly from the model by policy evaluation, which is what the dashed line shows; the Bellman Equations lesson shows how.
Or they can be estimated by simply running the policy and averaging the returns. By the law of large numbers the average converges to Vπ(s), and the standard error is σ/√N, where σ is the spread of a single return: a hundred times more futures buys one more decimal place. This Monte Carlo idea needs no model at all, only experience — see also Learning from Experience, which builds a learner from it.
The mean hides a lot. Even under the optimal policy the returns here are bimodal: a hump of futures that reach the goal and a smaller one that slipped into a pit, with the value sitting in the gap where no future actually lands. Distributional reinforcement learning (Marc Bellemare, Will Dabney and Rémi Munos, 2017) learns that whole distribution instead of only its mean.
Spot a mistake or have an idea?