Reinforcement learning from the ground up
Markov decision processes
Add states and transitions to a bandit and you get the standard model of sequential decisions: a table of consequences, a policy, and one equation that ties them together.
The world gets a memory
A Markov decision process is the bandit plus consequences. It is a set of states, a set of actions, a rule for what happens when you act, a reward for each transition, and a discount. The agent chooses; the world moves; the next choice starts from wherever the world landed. The word process is doing work: the sequence matters, not just the final state. It is the standard language for sequential decisions, and most of the vocabulary in this course is defined in it.
The Markov property is the assumption that makes this tractable: the next state and reward depend on the current state and action, and on nothing earlier. History matters only through the present. When that is false, the state is missing something, and no amount of learning will recover it. It is a claim about the representation, not about the world; a good state makes it true.
The transition table
For a finite problem the dynamics are a table: for each state and action, a list of possible next states with a probability and a reward. The probabilities in each row sum to one; the reward may be fixed or may itself be a distribution, but its expected value is what the equations use. The table is the model, and having it is what separates planning from learning. If you cannot write the row, you do not yet understand the action.
Writing the table is a useful discipline even when you will never have it. It forces you to say what the actions do, what is random, and where the reward is attached. Most bugs in a learned agent are bugs in this description, discovered late. A row that sums to 0.9 is a row that will quietly distort every value downstream. Check the rows before you trust the answer; the equations will happily propagate a broken model.
Reading the table from an environment
import gymnasium as gym
env = gym.make("FrozenLake-v1", is_slippery=True)
transitions = env.unwrapped.P
for next_state, probability, reward, done in transitions[0][2]:
print(next_state, round(probability, 3), reward, done)One row of the transition table
Run the calculation, then change the slip probability to 0.4 and confirm the expected reward and the distribution both move.
A policy is a rule for choosing
A policy maps a state to an action, or to a distribution over actions. A deterministic policy is a lookup table; a stochastic one keeps probabilities so that it can remain uncertain, or so that it can explore without abandoning what it knows. The policy is the only object the agent truly controls. Everything else in the algorithm is a way of scoring or improving that mapping.
The policy is the thing being learned, and it is also the thing being evaluated. Value iteration and policy gradients differ mainly in how they represent it and how they improve it. Both are trying to find the policy whose expected return from every state is largest. A policy can be optimal from one state and terrible from another, which is why value is a function, not a number. Two policies can share a value function and still behave differently in states that are never reached.
Draw the states as circles and the transitions as arrows before writing any code. A diagram catches a missing action or a reward attached to the wrong edge faster than a debugger.
One equation holds it together
The value of a state under a policy is the reward you expect now, plus the discounted value of wherever you land. That is the Bellman expectation equation, and it is recursive: the value of each state is written in terms of the values of its successors. One line per state, all coupled, which is exactly a linear system. Solving it is policy evaluation, the subroutine inside almost every control method.
For a fixed policy the equation is linear, one line per state, and it has a unique solution whenever the discount is below one. You can solve it directly with linear algebra or approach it by repeated substitution. The second method is the one that scales, and it is the seed of everything in the next chapter. The first is useful for small problems and for checking the second.
Policy evaluation by substitution
Run fifty sweeps and watch the two values settle. Then change the discount to 0.5 and see how much closer to the immediate rewards they land.
Check what you learned
One question at a time. A wrong answer explains itself — take another go.
Part 1 of 5
What does the Markov property assume?
