Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 56

Q-Learning

Learn the value of each action from rewards, using the best next action even if the agent did not take it. One example is on this page.

Intermediate12 min
Q-Learning MahiDev

What You Will Learn

  • Idea
  • Example
  • When to use it

Idea

Q-learning stores a value for every state and action. After a reward, it updates that value toward the reward plus the discounted value of the best action in the next state. The update uses the best next action, not the action the current policy happened to sample. That makes it off-policy.

Example

python
next_qs = [0.2, 0.9]
reward, gamma, q, alpha = 1.0, 0.9, 0.0, 0.5
updated = q + alpha * (reward + gamma * max(next_qs) - q)
print(f"Q: {updated:.2f}")

Output:

text
Q: 0.91

When to use it

  • Use it on small discrete tasks where you can store a table of states and actions.
  • The max over the next state is the off-policy part.
  • A table does not scale to images or huge state spaces. That is what DQN is for.

Loading playground…