Reinforcement Learning (Q-Learning)
Comprehensive notes, formulas, and practice questions for Reinforcement Learning (Q-Learning).
What you'll learn
- The core framework: agent, environment, state, action, reward
- What a Q-table stores and how it is updated
- The Bellman equation — the heart of Q-Learning
- Epsilon-greedy exploration vs exploitation
- The roles of learning rate (α) and discount factor (γ)
- A concrete example: navigating a grid world
Key concepts
The Reinforcement Learning Framework
An agent learns by interacting with an environment:
- Agent observes the current state (s).
- Agent chooses an action (a).
- Environment transitions to a new state (s').
- Environment gives the agent a reward (r).
- Agent updates its knowledge and repeats.
| Component | Description | Example (Grid World) |
|---|---|---|
| State (s) | Current situation | Grid cell position (row, col) |
| Action (a) | Choice available | Move Up/Down/Left/Right |
| Reward (r) | Feedback signal | +10 (goal), −1 (each step), −5 (wall) |
| Episode | One complete run | Start to reaching goal or falling |
The goal: learn a policy — a mapping from states to actions — that maximises total cumulative reward.
Q-Table
A Q-table stores the expected total reward for taking each action in each state.
Q(s, a) = Expected cumulative reward starting from state s, taking action a, then acting optimally.
Example Q-table fragment (4-state, 4-action grid):
| State | Up | Down | Left | Right |
|---|---|---|---|---|
| (0,0) | 0.0 | 0.5 | 0.0 | 2.1 |
| (0,1) | 0.0 | 8.3 | 0.5 | 1.2 |
| (1,0) | 5.0 | 0.0 | 0.0 | 4.0 |
| (1,1) | 9.0 | 0.0 | 0.0 | 0.0 |
At each state, the agent picks the action with the highest Q-value.
The Bellman Equation (Q-Learning Update)
After taking action a from state s and arriving in state s', the Q-value is updated:
Q(s, a) ← Q(s, a) + α × [r + γ × max Q(s', a') − Q(s, a)]
| Symbol | Name | Role |
|---|---|---|
| α (alpha) | Learning rate | How much new info overwrites old (0 to 1) |
| γ (gamma) | Discount factor | How much future rewards are valued (0 to 1) |
| r | Immediate reward | Signal received after this action |
| max Q(s', a') | Best future Q-value | Estimate of optimal future reward |
- γ close to 1 → agent plans far ahead (long-sighted)
- γ close to 0 → agent is greedy (only cares about immediate reward)
Epsilon-Greedy Exploration
The agent faces the exploration-exploitation dilemma:
- Exploit: Choose the best-known action (maximise Q).
- Explore: Try a random action (discover better options).
Epsilon-greedy strategy:
- With probability ε, take a random action (explore).
- With probability 1 − ε, take the best-known action (exploit).
Typically, ε starts high (lots of exploration early on) and decays over time as the agent learns.
| ε value | Behaviour |
|---|---|
| ε = 1.0 | Always random — pure exploration |
| ε = 0.0 | Always greedy — pure exploitation |
| ε = 0.1 | 10% random, 90% greedy — typical late training |
Grid World Example
Start . . .
. . . .
. . . Goal(+10)
Wall(−5) at (1,1)
Training episode 1:
- Agent at (0,0), Q-table all zeros.
- ε = 0.9 → takes random action → moves Right to (0,1).
- Reward = −1 (step cost). Update Q(0,0, Right).
- Continues until reaching Goal → reward +10.
After many episodes:
- Q-table values converge.
- Agent reliably navigates from Start to Goal in minimum steps.
Quick check
-
Describe the interaction loop in reinforcement learning: what are the four things that cycle between the agent and the environment?
-
In the Bellman equation, what does the discount factor γ control? What behaviour results when γ = 0 versus γ = 0.99?
-
An agent always picks the action with the highest Q-value (ε = 0). What is the risk of this strategy early in training?
-
After 1000 training episodes in a grid world, the Q-table at state (2,3) shows Q-values: Up=1.2, Down=0.3, Left=0.8, Right=9.1. What action will the trained agent take, and why?
-
Why do we need a learning rate (α) less than 1.0? What would happen if α = 1.0 every update?
Key Takeaways (TL;DR)
- What you'll learn
- Key concepts
- Quick check
Master this topic with Drishti OS
Get unlimited mock tests, AI-powered mentorship, and complete video courses when you join.
Start Free Practice