Reinforcement learning: how AI learns by trial and error

Reinforcement learning (RL) teaches an AI agent by trial and error. The agent takes an action, the environment responds with a new situation (the state) and a reward or penalty, and over many attempts the agent learns which actions bring the most reward in the long run. When a neural network does the learning, it's called deep reinforcement learning: the idea behind game-playing AI such as AlphaGo, robots that learn to move, and part of how chatbots are fine-tuned.

Everyday analogy

Learning to ride a bicycle. Nobody hands you an answer sheet. You try, wobble, fall (a penalty), stay up a little longer (a reward), and slowly your body learns what works. The reward often comes several moves after the action that earned it, and that is exactly what makes reinforcement learning hard.

Example

A warehouse robot learns to pick items: +10 for a clean pick, −1 for every second it takes, −20 for dropping something. After thousands of practice runs in a simulator, it learns to pick quickly and gently.

Three ways a machine can learn

TypeWhat it learns fromGoalExample
SupervisedExamples with the right answersMap an input to the correct answerA spam filter trained on emails marked spam or not spam
UnsupervisedData without answersFind hidden groups or structureGrouping customers with similar buying habits
ReinforcementExperience: situations, actions and rewardsCollect the most reward over many stepsA game AI choosing the moves that win

The building blocks

TermMeaningIn a game of chess
AgentThe learner that makes decisionsThe AI player
EnvironmentThe world the agent acts inThe board and the opponent
StateThe situation the agent sees right nowThe current position of all pieces
ActionA move the agent can make; all possible moves form the action spaceMoving a knight
RewardFeedback that says how good the outcome was+1 for a win, −1 for a loss
PolicyThe agent's strategy: which action to pick in each stateIts playing style
EpisodeOne full attempt from start to finishOne complete game
AGENTLooks at the state
→
ACTIONChooses a move
→
ENVIRONMENTChanges in response
→
FEEDBACKNew state + reward
→
↻ LEARNAdjusts its policy and repeats

Reward now or reward later? The discount factor

The agent cares about the total reward it will collect, called the return. Rewards further in the future count a little less, using a discount factor (γ, "gamma") between 0 and 1.

Worked example: discounted return
Return = reward now + γ × next reward + γ² × reward after that + …

γ = 0.9, and the agent expects +10 at each of the next three steps:
Return = 10 + 0.9 × 10 + 0.81 × 10
       = 10 + 9 + 8.1 = 27.1

Why discount? Like money: AED 100 today is worth more than a promise of AED 100 next year. A γ close to 1 makes the agent patient; a γ close to 0 makes it chase quick wins.

Two ways to learn: value or policy

Value learning (e.g. Q-learning)

  • Learns a score, Q(state, action), for how much future reward each action will bring
  • Then simply picks the action with the highest score
  • Like keeping a scorecard for every possible move
  • See Q-learning for the table version

Policy learning (policy gradients)

  • Learns the decision rule directly: the chance of choosing each action
  • Then samples an action from those chances
  • Like building an instinct rather than a scorecard
  • Works when actions are continuous, such as a steering angle

Deep Q-networks: when the table gets too big

A Q-table works for a small maze. But a video game screen has more possible states than any table could hold. A deep Q-network (DQN) replaces the table with a neural network: the state (for example, the screen pixels) goes in, and a Q-score for every possible action comes out. The network is trained to make its prediction match reward received + γ × the best Q-score of the next state. This approach famously learned to play dozens of classic Atari games from nothing but the screen and the score, reaching human-level play on many of them.

Limitation of DQNWhy it matters
Only works with a small set of separate actions"Left, right, fire" is fine; "turn the wheel 12.7°" is not
Always picks the single best-scored actionIt can't learn strategies that need some randomness

Policy gradients: learning the instinct

A policy network outputs a probability for each action, or for continuous actions a most likely value plus a spread (e.g. "steer about 5° left, give or take 2°"). Training is surprisingly intuitive:

  1. Let the agent act until the episode ends, for example until a simulated car crashes.
  2. Record every state, action and reward along the way.
  3. Make the actions that led to high reward more likely next time.
  4. Make the actions that led to low reward less likely.
  5. Repeat many thousands of times.

Practise in a simulator first

Trial and error is fine in a game, but not with a real car or an expensive robot. So agents are usually trained in realistic simulators, where crashes cost nothing, and then moved to the real world. Researchers have trained driving policies entirely in photo-realistic simulators and then used them to drive real cars.

The game of Go: a famous milestone

StepWhat the AI did
1 · Learn from expertsA network first copied moves from thousands of human expert games (supervised learning)
2 · Play itselfIt then played millions of games against versions of itself, learning from wins and losses (reinforcement learning)
3 · Judge positionsA second network learned to estimate who is winning from any board position
ResultIn 2016 AlphaGo beat one of the world's best Go players. A later version, AlphaGo Zero (2017), skipped step 1 and learned from self-play alone

Where reinforcement learning is used, and its risks

Uses

  • Games and simulations
  • Robots that walk, grip and sort
  • Traffic signals, energy and cooling systems
  • Recommendations and ad bidding
  • RLHF: fine-tuning chatbots using human ratings as the reward

Watch out for

  • Needs a huge number of attempts
  • Reward hacking: the agent finds a loophole that scores points without doing the real job
  • Hard to test every situation before going live
  • Safety: never let it learn by trial and error where mistakes hurt people

Try it yourself

About 10 min

Design a reward, then try to break it

  1. Ask a chatbot: Design a simple reward system for a robot vacuum cleaner that should clean a flat quickly.
  2. Then ask: How could the robot cheat this reward without really cleaning well?
  3. Fix the reward rules and ask again.

What you should see: Even simple rewards can be gamed, for example by bumping the same clean spot again and again. That's reward hacking, and why designing rewards is a real skill.

Use any free AI chatbot (ChatGPT, Claude, Gemini or Copilot) where needed. Never paste private or confidential information.

Test what you learned

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