Skip to content
Lambdian

Q-learning: off-policy TD control

Learn optimal action values from any exploratory experience with the max-based TD update.

Animated explanation

The idea

Q-learning is the most famous reinforcement-learning algorithm, and its update is one line. After taking action aa in state ss and seeing reward rr and next state s′s', move Q(s,a)Q(s,a) towards r+γmax⁡a′Q(s′,a′)r + \gamma\max_{a'}Q(s',a'): the reward now, plus the value of acting optimally from the next state.

The max makes it off-policy: whatever the agent actually does next (it may explore randomly), the update always learns about the greedy policy. So the agent can explore freely, even learn from someone else's play or from old logs, and its QQ still converges to the optimal Q∗Q^\ast. Act greedily on Q∗Q^\ast and you behave optimally, without ever having known the environment's rules.

Exploration schedules

Convergence requires visiting every state–action pair infinitely often. In practice: ε-greedy with ε decaying from 1 to 0.05, optimistic initial values (start Q high so untried actions look attractive), or softmax (Boltzmann) exploration with a temperature.

Maximisation bias

Taking a max over noisy estimates is biased upwards: some action looks better than it is just by chance. Double Q-learning keeps two tables, using one to choose the best next action and the other to evaluate it. The same trick (Double DQN) fixes overestimation in deep Q-networks.

From tables to networks

Tables need one entry per state–action pair: impossible for images or continuous states. Replacing the table with a function approximator, such as a neural network trained on the same TD target, gives DQN, the next module's starting point.

The algorithm, step by step

  1. 1
    Initialise Q(s, a) = 0 (terminal states stay 0).
  2. 2
    For each episode: s ← start. Repeat: choose a ε-greedily from Q(s, ·); take it, observe r, s′.
  3. 3
    Update Q(s,a)←Q(s,a)+α[r+γmax⁡a′Q(s′,a′)−Q(s,a)]Q(s,a) \leftarrow Q(s,a) + \alpha[r + \gamma\max_{a'}Q(s',a') - Q(s,a)] (target r if s′ is terminal).
  4. 4
    s ← s′ until the episode ends. Decay ε over time.

The maths

Update

Q(s,a)←Q(s,a)+α [r+γmax⁡a′Q(s′,a′)−Q(s,a)]Q(s,a) \leftarrow Q(s,a) + \alpha\,[r + \gamma\max_{a'}Q(s',a') - Q(s,a)]

Double Q-learning target

r+γ QB(s′,arg⁡max⁡a′QA(s′,a′))r + \gamma\,Q_B\big(s', \arg\max_{a'}Q_A(s',a')\big)

Greedy policy

π(s)=arg⁡max⁡aQ(s,a)\pi(s) = \arg\max_a Q(s,a)

Strengths and weaknesses

Strengths

  • Learns the optimal policy off-policy from any exploratory data
  • Model-free and simple
  • Proven convergence in the tabular case

Weaknesses

  • Maximisation bias (fixed by Double Q-learning)
  • Can behave riskily while exploring (it ignores its own exploration)
  • Tabular form doesn't scale; function approximation can diverge

Reach for it when…

Discrete actions and states (or as DQN with networks), when you want the optimal policy and can explore safely, e.g. in simulation.

Check your understanding

Question 1

Q1Why is Q-learning called off-policy?

Question 2

Q2Q(s,a) = 2, r = 1, γ = 0.9, Q(s′,·) = [3, 5], α = 0.5. New Q(s,a)?

Question 3

Q3What does Double Q-learning fix?

Lambdian Lab

Q-learning on the slippery gridworld

Using the provided gridworld and env_step:

  • q_learning(episodes, alpha, gamma, eps_start, eps_end, seed): ε decays linearly from eps_start to eps_end over the episodes; return Q as a dict state → array(4).
  • greedy(Q): dict state → action for non-terminal states.
  • The checks compare your greedy policy with the optimal one from value iteration.
Open the labPython + NumPy runs in your browser. Sign-in keeps your code and attempts.