Back to the page

In the print window, choose “Save as PDF”.

doubtly doubtly.in

What is Q-Learning in Reinforcement Learning?

Reinforcement Learning · Updated 28 Feb 2025 · Read online: www.doubtly.in/what-is-q-learning-in-reinforcement-learning/

Q-Learning is a model-free off-policy Reinforcement Learning algorithm. Its goal is to find the optimal action-selection policy for any given environment. The algorithm uses a Q-table (also called a Q-function) to store and update expected future rewards for state-action pairs.

In simple terms:


📌 Key Terminologies

TermExplanation
EnvironmentThe world in which the agent operates
State (s)Current situation of the agent
Action (a)What agent can do at the state
Reward (r)Feedback from environment after action
Policy (π)Strategy mapping state to action
Q-TableTable storing values for (state, action) pairs

📌 Objective of Q-Learning

Q∗(s,a)=maximum expected future reward if action ’a’ is taken in state ’s’.Q*(s, a) = \text{maximum expected future reward if action ‘a’ is taken in state ‘s’}.


📌 Q-Learning Formula

The core update rule of Q-learning is: Q(s,a)=Q(s,a)+α[r+γmax⁡a′Q(s′,a′)−Q(s,a)]Q(s, a) = Q(s, a) + α \left[r + γ \max_{a’} Q(s’, a’) – Q(s, a)\right]

Where:


📌 Step-by-Step Process — How Q-Learning Works?

Step 1: Initialization

Step 2: Repeat until learning is complete (for each episode)

Step 2.1: Start in some initial state ss

Step 2.2: Choose action using policy

Step 2.3: Take action, observe reward and next state

r,s′=environment.step(action)r, s’ = \text{environment.step(action)}

Step 2.4: Update Q-value using formula

Q(s,a)=Q(s,a)+α[r+γmax⁡a′Q(s′,a′)−Q(s,a)]Q(s, a) = Q(s, a) + α \left[r + γ \max_{a’} Q(s’, a’) – Q(s, a)\right]

Step 2.5: Move to next state

Step 2.6: Repeat until episode ends (goal reached, or max steps hit)


📌 Example — Grid World

Environment

Q-Table (initially)

StateUpDownLeftRight
(0,0)0000
(0,1)0000
……………

Process

Q(0,0,Right)=Q(0,0,Right)+α[−1+γmax⁡a′Q(0,1,a′)−Q(0,0,Right)]Q(0,0,\text{Right}) = Q(0,0,\text{Right}) + α \left[-1 + γ \max_{a’} Q(0,1,a’) – Q(0,0,\text{Right})\right]


📌 Final Notes — Key Features of Q-Learning

FeatureMeaning
Model-FreeDoesn’t need environment’s internal transition/reward rules
Off-PolicyLearns optimal policy independent of how actions are selected during learning
ConvergenceGuaranteed to converge to optimal Q if learning rate and exploration handled well
Trade-offBetween exploration (try new things) and exploitation (use known good actions)

📌 Visualization Example (Path Found by Q-Learning)

Start -> Right -> Down -> Down -> Right -> Goal

This kind of optimal path is learned over time, and stored in the Q-table.


📌 Summary (Quick Chart)

StepAction
1Initialize Q-table
2Start episode (at initial state)
3Choose action (ε-greedy)
4Take action, get reward + next state
5Update Q-table
6Move to next state
7Repeat until goal reached
8Repeat episodes till convergence

📌 Why Q-Learning is Powerful?

✅ Works in unknown environments
✅ Handles stochastic rewards and transitions
✅ Simple to implement
✅ Guarantees convergence (under conditions)


📌 Limitation

❌ Doesn’t work well if state space is huge (like image inputs) — needs Deep Q-Learning (DQN) for that.

Practise this on Doubtly (free)