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).

🏁→→↓↑🕳️→↓↑←←🏆🏆 +10 reward🕳️ −10 penaltyarrows = best movelearned by trial
Q-learning tries moves, collects rewards and penalties, and learns the best move from every square.

A small Q-table (higher = better long-term reward)

SituationGo leftGo straightGo right
At junction A2.1−1.06.5 ✓
At junction B7.8 ✓3.2−5.0
Near the exit0.49.6 ✓1.1

How it works, step by step

  1. Start with a Q-table full of zeros: the agent knows nothing.
  2. In the current situation, choose an action: usually the best-known one, sometimes a random one to explore.
  3. Take the action, receive a reward or penalty, and see the new situation.
  4. Update that table score a little towards: reward now + the best score expected from the new situation.
  5. 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 min

Learn by reward

  1. Draw a 3x3 grid with a start, a goal (+10) and a trap (-10).
  2. Play it a few times, noting which moves led to rewards.
  3. 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.

Build it for real · guided projectFind customer groups with K-means clusteringAbout 45 min · Copy-paste code · step by step
Test what you learned

Take a quick quiz, or look up any word in the glossary.