Skip to content
learn
  1. Learn
  2. Machine Learning
  3. Reinforcement Learning

Machine Learning ML-13.01

RL fundamentals and MDPs

The agent-environment loop: states, actions, rewards, returns and discounting, policies and value functions, and the Markov decision process behind them.

Intermediate 14 min Last reviewed

The big idea

In supervised learning every example comes with the right answer. Nobody gives a chess player the right move for every position, a thermostat controller the right valve setting for every minute, or a robot the right motor torque for every millisecond. What they get is feedback on how things went: a win or a loss at the end of the game, an energy bill and a comfort complaint, a fall or a successful step. Reinforcement learningLearning to act by trial and error: an agent interacts with an environment, receives rewards, and improves its policy to maximize total reward over time. RL for short. (RL) learns to act from that kind of feedback.

Think of training a dog. You cannot explain what you want; you can only reward the behaviour when it happens, and the dog has to work out which of the things it just did earned the treat. Two things make this hard. Rewards are often delayed, so the learner must assign credit to actions taken long before. And the learner's own actions decide what it gets to see next, so it must choose between doing what has worked so far and trying something that might work better.

How it works

Step 1: The agent-environment loop

The agent-environment loop An agent box on the left and an environment box on the right. An arrow over the top carries the action a t from agent to environment. Two arrows along the bottom return to the agent: the reward r t plus 1 and the next state s t plus 1. The action arrow and the reward arrow glow. action a_t reward r_t+1 next state s_t+1 agent policy pi(a | s) environment unknown dynamics Figure 1. Each step: the agent sees a state, picks an action, and the environment answers with a reward and a new state.

At each time step the agent observes a stateEverything the agent uses to choose an action at one moment: a board position, sensor readings, the user's recent history. , picks an actionA choice the agent makes at one step, from a discrete set (move left) or a continuous range (apply 0.3 newtons). , and the environment returns a rewardA single number the environment returns after each action, saying how good the immediate outcome was. The agent's goal is to maximize the total over time. and the next state . The loop repeats until the episode ends (a game finishes, a robot falls) or forever, for a continuing task such as cooling a building.

The reward is the only goal signal. Everything you want the agent to do must be expressed as reward, which is both the strength of RL and its main trap: an agent will maximize exactly what you reward, including loopholes you did not foresee.

Step 2: Return and discounting

The agent does not maximize the next reward; it maximizes the returnThe total of future rewards from a time step on, usually discounted: G_t = r_t+1 + gamma r_t+2 + gamma^2 r_t+3 + ..., the sum of rewards from now on. With a discount factorA number gamma between 0 and 1 that multiplies each later reward once more, so a reward k steps away counts gamma^k as much as one now. between 0 and 1:

How much a future reward counts Three curves of gamma to the power k for k from 0 to 30. With gamma 0.5 the weight is below 0.01 after 7 steps. With gamma 0.9 it falls to about 0.35 at 10 steps and 0.04 at 30. With gamma 0.99 it is still 0.74 at 30 steps. 05101520253000.250.50.751steps into the future kweight gamma^k gamma 0.5 gamma 0.9 gamma 0.99 Figure 2. The discount sets how far ahead the agent looks: roughly 1 / (1 - gamma) steps.

A worked example with : rewards of 0, 0, 0, then 10 give . A reward of 1 right now gives . So the agent should take the long route to the 10. With , the same route is worth , still slightly better than 1, and one more step of delay would flip the choice. Discounting keeps returns finite for continuing tasks, and since , it acts like a horizon of about steps: 10 steps at 0.9, 100 at 0.99.

Step 3: Policies and value functions

A policyThe agent's rule for choosing actions: a mapping from states to actions, or to probabilities over actions, written pi(a | s). says what the agent does in each state, deterministically or as probabilities. Two functions measure how good a policy is:

  • The state value : the expected return starting in and following .
  • The action valueThe expected return from taking action a in state s and following the policy afterwards, written Q(s, a). Choosing the action with the highest Q gives a greedy policy. : the expected return from taking action in , then following .

is the more useful one when you do not know the environment's dynamics: with in hand, acting well is just picking , with no need to predict where actions lead. Both satisfy a Bellman equation that links a state to its successors:

Replace the average over the policy's actions with a max and you get the optimality equation from Sequential decisions: MDPs (bridge to RL) , whose solution gives the best possible policy.

Step 4: The Markov decision process

A Markov decision processA model of sequential decision making: states, actions, transition probabilities P(s' | s, a), rewards, and a discount factor. (MDP) packages all of this: states , actions , transition probabilities , rewards and the discount . "Markov" means the next state depends only on the current state and action, not on how you got there. If your observation leaves out something that matters (a single video frame does not show which way the ball is moving), stack recent observations or keep a memory until the state is Markov enough.

What makes RL different from the planning of Sequential decisions: MDPs (bridge to RL) is that the agent does not know or the rewards. It has to learn from samples of experience. Methods split into:

  • Value-based: learn , act greedily on it (Q-learning and deep Q-networks, later in this module).
  • Policy-based: adjust the policy's parameters directly toward higher return (policy gradients, PPO).
  • Actor-critic: both at once, a policy that acts and a value function that judges.
  • Model-based: learn and the rewards, then plan with the learned model.

Going deeper

Code: evaluate policies, check by simulation, find the best one

mdp.pyruns with python3 + numpy
import numpy as np

# A corridor MDP: cells 0..6, start in cell 2. Cell 0 is an exit worth +1, cell 6 an exit worth +10.
# Actions: 0 = left, 1 = right. A move goes the wrong way with probability 0.1. Other steps pay 0.
N, START, SLIP = 7, 2, 0.1
EXIT = {0: 1.0, 6: 10.0}

def outcomes(s, a):                          # list of (probability, next state, reward)
    step = -1 if a == 0 else 1
    out = []
    for p, d in [(1 - SLIP, step), (SLIP, -step)]:
        s2 = s + d
        out.append((p, s2, EXIT.get(s2, 0.0)))
    return out

def evaluate(policy, gamma):                 # Bellman expectation: V(s) = sum_a pi(a|s) sum p [r + gamma V(s')]
    V = np.zeros(N)
    for _ in range(500):
        V = np.array([0.0 if s in EXIT else
                      sum(pa * sum(p * (r + gamma * V[s2]) for p, s2, r in outcomes(s, a)) for a, pa in enumerate(policy[s]))
                      for s in range(N)])
    return V

def monte_carlo(policy, gamma, episodes=4000, seed=0):   # average discounted return of real episodes
    rng, total = np.random.default_rng(seed), 0.0
    for _ in range(episodes):
        s, G, k = START, 0.0, 0
        while s not in EXIT:
            a = rng.choice(2, p=policy[s])
            p, s, r = outcomes(s, a)[0 if rng.random() < 1 - SLIP else 1]
            G += gamma ** k * r; k += 1
        total += G
    return total / episodes

policies = {"always left": [[1, 0]] * N, "always right": [[0, 1]] * N, "random": [[0.5, 0.5]] * N}
for gamma in [0.5, 0.9]:
    print(f"gamma {gamma}: value of the start cell")
    for name, pi in policies.items():
        print(f"  {name:13s} V = {evaluate(pi, gamma)[START]:6.3f}   Monte Carlo {monte_carlo(pi, gamma):6.3f}")
    Q = lambda s, a, V: sum(p * (r + gamma * V[s2]) for p, s2, r in outcomes(s, a))
    V = np.zeros(N)
    for _ in range(500):                     # value iteration: V(s) = max_a Q(s, a)
        V = np.array([0.0 if s in EXIT else max(Q(s, 0, V), Q(s, 1, V)) for s in range(N)])
    best = "".join("." if s in EXIT else "<>"[int(Q(s, 1, V) > Q(s, 0, V))] for s in range(N))
    print(f"  optimal policy {best}, V(start) = {V[START]:.3f}")

Output

gamma 0.5: value of the start cell
  always left   V =  0.424   Monte Carlo  0.427
  always right  V =  0.905   Monte Carlo  0.920
  random        V =  0.246   Monte Carlo  0.237
  optimal policy .<>>>>., V(start) = 0.927
gamma 0.9: value of the start cell
  always left   V =  0.861   Monte Carlo  0.861
  always right  V =  6.614   Monte Carlo  6.657
  random        V =  1.883   Monte Carlo  1.847
  optimal policy .>>>>>., V(start) = 6.614

A corridor of 7 cells. The agent starts in cell 2; cell 0 is an exit worth +1 and cell 6 an exit worth +10; each move goes the wrong way with probability 0.1. The code evaluates three fixed policies with the Bellman expectation equation, checks each with a Monte Carlo average over 4,000 simulated episodes, and finds the optimal policy with value iteration.

  • The Bellman equation and simulation agree to within sampling noise: always right is worth 0.905 by the equation and 0.920 by simulation at , and 6.614 and 6.657 at . That is what value means: an expected return.
  • The random policy is poor (0.246 and 1.883): wandering wastes discount.
  • The discount changes the optimal policy. At every cell heads right, for the 10. At , cell 1 heads left for the nearby 1, because the 10 is five risky steps away, and the optimal start value 0.927 beats always-right's 0.905 because a slip into cell 1 is followed by grabbing the +1.

Designing rewards

A reward function is a specification, and agents exploit specifications. Reward a cleaning robot for picking up dirt and it may learn to dump dirt in order to pick it up again. Common defences: reward the outcome you want rather than a proxy for it, keep rewards simple, add small costs for time or energy, and watch the learned behaviour, not just the reward curve. Shaping rewards (extra rewards for progress) can speed learning but can also change the optimal policy if done carelessly.

Exploration

An agent that only does what currently looks best never learns whether anything else is better. Every RL method needs some exploration: random actions some of the time, optimism toward untried actions, or noise in a stochastic policy. The next lesson studies this trade-off in its simplest form.

Trade-offs

OptionWins whenLoses when
Low discount (0.5 to 0.9) Faster, more stable learning; good for short-horizon tasks Short-sighted; ignores payoffs more than a few dozen steps away
High discount (0.99 and up) Values long-term consequences Noisier returns, slower learning, more sensitive to errors
Model-free RL No need to model the environment; works from raw experience Needs many interactions
Model-based RL Sample-efficient; can plan ahead Errors in the learned model compound over long plans

Common mistakes

  • Rewarding a proxy (points scored, dirt collected) and being surprised when the agent games it.
  • Confusing reward (one step) with return (the discounted sum) and value (the expected return).
  • Using a state that is not Markov, such as a single frame, so the agent cannot tell situations apart.
  • Picking by habit. It sets the effective horizon; match it to how far ahead consequences matter.
  • Forgetting exploration, so the agent settles on the first thing that worked.

Where you will see this

The same loop describes a game-playing program choosing moves, a robot arm choosing torques, a data-centre controller choosing cooling settings and a recommender choosing what to show over a user's many visits. Language model fine-tuning with human feedback treats the model as the policy and a learned preference score as the reward ( RLHF ). The standard reference for everything in this module is the textbook Reinforcement Learning: An Introduction by Sutton and Barto, whose second edition (2018) is free to read online.

Check yourself

  1. Rewards of 0, 0, 5 arrive over the next three steps. What is the return with gamma = 0.9?

    Show answer

    B. 4.05 G = 0 + 0.9 x 0 + 0.9^2 x 5 = 0.81 x 5 = 4.05. The reward two steps after the first is discounted twice.

  2. Why is Q(s, a) more convenient than V(s) when the environment's dynamics are unknown?

    Show answer

    B. Choosing the best action from Q needs only argmax over a; choosing from V needs P(s' | s, a) to predict where each action leads With V you must look one step ahead through the model. Q already includes that step, so a greedy policy is just the action with the largest Q.

  3. An agent sees one video frame of a ball game per step and cannot tell which way the ball moves. What is wrong?

    Show answer

    B. The observation is not Markov; stack several frames or add memory The next state depends on velocity, which one frame does not show. Stacking frames restores the information.

  4. In the corridor code, why does the optimal policy at gamma 0.5 send cell 1 left?

    Show answer

    B. The +10 is five risky steps away, so after discounting it is worth less than the +1 one step away 0.5^4 x 10 is about 0.6, before slips, against about 0.9 for a step left that succeeds 90 percent of the time. At gamma 0.9 the far +10 wins.

  5. A team's robot vacuum learns to push dirt into a pile and then 'collect' it repeatedly. What is the root cause?

    Show answer

    B. The reward measured a proxy (dirt collected) rather than the goal (a clean floor) The agent maximized what was rewarded. Fix the reward, for example by rewarding a clean floor at the end, not each pickup.

Recap

  • RL learns from rewards, not labels: an agent acts, the environment returns a reward and a new state, and the loop repeats.
  • The goal is the return, the discounted sum of future rewards; gamma sets an effective horizon of about 1 / (1 - gamma) steps.
  • A policy maps states to actions; V(s) and Q(s, a) are expected returns, linked to their successors by Bellman equations.
  • An MDP is states, actions, transitions, rewards and a discount. RL solves it without being given the transitions or rewards.
  • Rewards are specifications that agents exploit, and every method needs exploration.

Get new lessons by email

Get new posts on backend and AI internals. No spam, unsubscribe anytime.

Your email is only used to send new posts. Privacy