Q-learning
Reinforcement learning Q-learning is a reinforcement learning algorithm. An agent tries actions, receives rewards or penalties, and slowly fills a table of scores (the Q-table) saying how good each action is in each situation. Over time it learns to pick the action with the best long-term reward. It balances exploring new actions with exploiting what already works.
Everyday analogy
Finding the fastest route to the office. You try different roads, remember which were quick, and usually take the best one, but now and then you try a new road in case it's even better.
Example
Used for: game-playing AI, robots learning to walk or grip, traffic-signal timing, managing energy in buildings. Large problems use neural networks instead of a table (deep Q-learning).
A small Q-table (higher = better long-term reward)
| Situation | Go left | Go straight | Go right |
|---|---|---|---|
| At junction A | 2.1 | −1.0 | 6.5 ✓ |
| At junction B | 7.8 ✓ | 3.2 | −5.0 |
| Near the exit | 0.4 | 9.6 ✓ | 1.1 |
How it works, step by step
- Start with a Q-table full of zeros: the agent knows nothing.
- In the current situation, choose an action: usually the best-known one, sometimes a random one to explore.
- Take the action, receive a reward or penalty, and see the new situation.
- Update that table score a little towards: reward now + the best score expected from the new situation.
- Repeat over thousands of tries (episodes) until the scores settle.
Worked example
A robot learns to escape a maze: +10 for reaching the exit, −1 for every step, −5 for hitting a wall. At first it wanders randomly. After many attempts its Q-table points to the shortest, safest path, as in the table above.
✓ Good for
- Learns from experience, with no labelled answers
- Great for step-by-step decisions where actions affect the future
- The idea is simple and general
⚠ Watch out for
- Needs a huge number of tries
- The table becomes too big for complex problems (then neural networks are used)
- Designing the right rewards is tricky: AI may find loopholes
Try it yourself
About 15 minLearn by reward
- Draw a 3x3 grid with a start, a goal (+10) and a trap (-10).
- Play it a few times, noting which moves led to rewards.
- Write a small table of the best move from each square.
What you should see: That table is a Q-table, learned only from trial and reward.
Use any free AI chatbot (ChatGPT, Claude, Gemini or Copilot). Never paste private or confidential information.