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
| Type | What it learns from | Goal | Example |
|---|---|---|---|
| Supervised | Examples with the right answers | Map an input to the correct answer | A spam filter trained on emails marked spam or not spam |
| Unsupervised | Data without answers | Find hidden groups or structure | Grouping customers with similar buying habits |
| Reinforcement | Experience: situations, actions and rewards | Collect the most reward over many steps | A game AI choosing the moves that win |
The building blocks
| Term | Meaning | In a game of chess |
|---|---|---|
| Agent | The learner that makes decisions | The AI player |
| Environment | The world the agent acts in | The board and the opponent |
| State | The situation the agent sees right now | The current position of all pieces |
| Action | A move the agent can make; all possible moves form the action space | Moving a knight |
| Reward | Feedback that says how good the outcome was | +1 for a win, −1 for a loss |
| Policy | The agent's strategy: which action to pick in each state | Its playing style |
| Episode | One full attempt from start to finish | One complete game |
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.
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.1Why 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 DQN | Why 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 action | It 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:
- Let the agent act until the episode ends, for example until a simulated car crashes.
- Record every state, action and reward along the way.
- Make the actions that led to high reward more likely next time.
- Make the actions that led to low reward less likely.
- 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
| Step | What the AI did |
|---|---|
| 1 · Learn from experts | A network first copied moves from thousands of human expert games (supervised learning) |
| 2 · Play itself | It then played millions of games against versions of itself, learning from wins and losses (reinforcement learning) |
| 3 · Judge positions | A second network learned to estimate who is winning from any board position |
| Result | In 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 minDesign a reward, then try to break it
- Ask a chatbot: Design a simple reward system for a robot vacuum cleaner that should clean a flat quickly.
- Then ask: How could the robot cheat this reward without really cleaning well?
- 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.