Reinforcement Learning: Definition, Example and Code
Quick answer
Reinforcement learning explained: agent, environment, reward and policy, a worked corridor example, a short Python bandit and the 2013 Atari paper.
Reinforcement learning is a way for a program, called an agent, to learn by trial and error. It acts in an environment, receives a reward signal after each action, and gradually learns which actions earn the most reward over time. No labelled answers are supplied. The agent finds good behaviour by trying things.
Key takeaways
- Reinforcement learning (RL) learns from rewards after actions, not from labelled examples.
- The core loop is agent, action, environment, reward, and a new state.
- The agent must balance exploring new actions and exploiting what already works.
- Designing the reward is often the hardest practical step.
On this page:
- What reinforcement learning means
- How it works, with a worked example
- Where it is used
- Common mistakes and confusions
- Related terms
- Frequently asked questions
| Term | Learns from | Typical question it answers |
|---|---|---|
| Reinforcement learning | Rewards after actions | Which action should I take next? |
| Supervised learning | Labelled examples | What is the label for this input? |
| Unsupervised learning | Unlabelled data | What structure is in this data? |
| Deep reinforcement learning | Rewards, with a neural network | Which action, from raw input like pixels? |
What does reinforcement learning mean?
The Journal of Artificial Intelligence Research (JAIR) survey on the subject, by Kaelbling and co-authors, describes reinforcement learning as the problem faced by an agent that learns behaviour through trial-and-error interactions with a dynamic environment. The journal page lists it as published in 1996.
Four words carry most of the meaning. The agent is the learner. The environment is everything it interacts with. A reward is a number the environment returns after an action. A policy is the agent's rule for choosing an action in a given state. These are standard textbook definitions, as in Sutton and Barto's Reinforcement Learning: An Introduction.
How does reinforcement learning work?
The agent observes a state, picks an action, and gets a reward and a new state. It repeats this many times and adjusts its policy to collect more reward. The JAIR survey lists central issues such as trading off exploration and exploitation, learning from delayed reinforcement and coping with hidden state.
An illustrative corridor with made-up numbers: a delivery robot starts in cell 1 of a 3-cell corridor. The store is in cell 3. Every move costs -1, and the move that reaches the store gives +10 instead.
- Episode 1: left (it bumps the wall and stays) gives -1, right gives -1, right gives +10. Total 8.
- Episode 2: right gives -1, right gives +10. Total 9.
After both episodes the agent has evidence that going right from cell 1 is worth more than going left. That growing estimate of future reward is what it learns.
Exploration versus exploitation shows up even in a tiny bandit problem. The code below has two actions with made-up reward probabilities (0.3 and 0.7) and an epsilon-greedy rule: 10 percent of the time it tries a random action, otherwise it picks the action with the higher estimate.
import random
random.seed(1)
p = [0.3, 0.7] # made-up reward probabilities
q = [0.0, 0.0] # estimated value of each action
n = [0, 0] # times each action was tried
eps = 0.1
for _ in range(1000):
if random.random() < eps:
a = random.randrange(2)
else:
a = 0 if q[0] >= q[1] else 1
r = 1 if random.random() < p[a] else 0
n[a] += 1
q[a] += (r - q[a]) / n[a]
print([round(x, 2) for x in q], n)
# output with seed 1: [0.2, 0.72] [80, 920]The estimate for the better action ends near its true 0.7, and that action is chosen most of the time. The estimate for the weaker action is noisier because it is tried less often.
Where is reinforcement learning used?
It suits problems where a sequence of decisions matters. The best-known research example is the 2013 arXiv paper on Atari games by Mnih and colleagues. It describes a convolutional network trained with a Q-learning variant, taking raw pixels as input and outputting a value function that estimates future rewards.
The same method, with no change to architecture or learning algorithm, was applied to seven Atari 2600 games from the Arcade Learning Environment. According to the paper, it outperformed previous approaches on six of them and surpassed a human expert on three.
An illustrative Indian scenario: a quick-commerce route planner in Bengaluru that picks routes and is rewarded for on-time drops. It is a made-up case that only shows the shape of the problem.
What are the common mistakes and confusions?
- Thinking RL needs labels. It needs a reward signal, which can be sparse or delayed.
- Rewarding the wrong thing. An agent maximises the reward you wrote, not the goal you meant. A route agent rewarded only for speed may ignore safety.
- Never exploring. An agent that always picks its current best action can miss a better one.
- Mixing it up with reinforcement from human feedback. That is one use of reward signals, not the whole field.
Which terms are related?
See types of machine learning for where RL sits beside supervised and unsupervised learning, and AI vs machine learning vs deep learning for the wider picture. For agents that act in the world, read the core elements of an AI agent and AI agents vs agentic AI vs generative AI.
Next step
Run the bandit code, then change the probabilities to 0.5 and 0.55 and compare how long the agent takes to prefer the better action. For the maths behind it, read maths for machine learning.
Frequently asked questions
What is reinforcement learning in simple words?
It is learning by trial and error. An agent acts in an environment, gets a reward after each action, and slowly learns which actions earn the most reward over time.
How is reinforcement learning different from supervised learning?
Supervised learning learns from labelled examples that give the right answer. Reinforcement learning gets only a reward after acting, so it has to discover good actions by trying them.
What is a reward in reinforcement learning?
A reward is a number the environment returns after an action. The agent tries to collect as much total reward as possible, so how the reward is designed shapes what it learns.
What are an agent and an environment?
The agent is the learner that chooses actions. The environment is everything it interacts with, and it responds to each action with a new state and a reward.
What is exploration versus exploitation?
Exploration means trying actions to learn more. Exploitation means using the best action found so far. The JAIR survey lists trading off the two as a central issue.