Think about how you learned to ride a bicycle. Nobody gave you a rulebook with the exact angle to lean at every second. You tried, you wobbled, you fell, and slowly your body worked out what kept you upright. Staying balanced felt good. Falling did not.
That way of learning, by trying things and noticing what turns out well, is the idea behind reinforcement learning. It is the kind of AI that has learned to play board games at expert level and helps robots learn to walk. In this tutorial you will see how it works and run a tiny robot that teaches itself.
The big idea: rewards
In reinforcement learning there is an agent (the learner, like a robot or a game character) inside an environment (the world around it).
At every moment:
- The agent looks at its state (where it is, what it sees).
- It chooses an action (move left, move right, jump).
- The environment gives back a reward: a number that says how good that was. Positive is good, negative is bad.
Nobody tells the agent the right answer. It only gets rewards. Its goal is to collect as much reward as possible over time.
A pet dog learning tricks is similar: sit when asked, get a treat. The trainer never explains anything in words.
Explore or exploit?
Every learner faces a dilemma. Should it explore (try something new that might be better) or exploit (repeat what has worked so far)?
You face it too. At your favourite chaat stall, do you order the pani puri you love, or try the new item on the board? Good agents mostly do what works, with a little exploring mixed in.
Remembering what worked: the Q-table
How does an agent remember? A simple method called Q-learning keeps a table of scores. For each state and each action, it stores a number: "how good is it to do this action here?" At the start every score is 0. After each move, the agent nudges the score up or down depending on the reward it got and how promising its new position looks.
After enough tries, the agent just picks the action with the highest score in each state.
Try it: a robot that finds its charger
Our world is a corridor of 6 tiles, numbered 0 to 5. A little robot starts on tile 0. Its charger is on tile 5. It can move left or right.
- Reaching the charger: +10 reward.
- Every other step: -1 (wasting battery).
The robot has no idea which way the charger is. Let's see if it can learn.
- Open Python on your computer or an online Python editor. No installs needed.
- Type the program below.
- Run it and look at how many moves each episode takes. An episode is one full attempt from the start to the charger.
- Read the final table of what the robot learned.
# A robot learns to walk to the charger using rewards (Q-learning)
import random
random.seed(7)
N = 6 # tiles 0..5; the charger is on tile 5
ACTIONS = ["left", "right"]
Q = [[0.0, 0.0] for _ in range(N)] # Q[tile][action]: how good is this move?
ALPHA = 0.5 # how much to learn from each try
GAMMA = 0.9 # how much future rewards matter
EPSILON = 0.3 # how often to explore a random move
def step(tile, action):
new_tile = max(0, tile - 1) if action == 0 else min(N - 1, tile + 1)
reward = 10 if new_tile == N - 1 else -1 # charger = +10, every step = -1
return new_tile, reward
for episode in range(1, 51):
tile, total, moves = 0, 0, 0
while tile != N - 1:
if random.random() < EPSILON:
action = random.randint(0, 1) # explore
else:
action = 0 if Q[tile][0] > Q[tile][1] else 1 # use what it learned
new_tile, reward = step(tile, action)
best_next = 0 if new_tile == N - 1 else max(Q[new_tile])
Q[tile][action] += ALPHA * (reward + GAMMA * best_next - Q[tile][action])
tile, total, moves = new_tile, total + reward, moves + 1
if episode in (1, 2, 10, 50):
print(f"Episode {episode:2}: {moves:2} moves, total reward {total}")
print()
print("What the robot learned on each tile:")
for t in range(N - 1):
best = ACTIONS[0] if Q[t][0] > Q[t][1] else ACTIONS[1]
print(f"Tile {t}: go {best}")
Here is the output we got:
Episode 1: 20 moves, total reward -9
Episode 2: 9 moves, total reward 2
Episode 10: 5 moves, total reward 6
Episode 50: 5 moves, total reward 6
What the robot learned on each tile:
Tile 0: go right
Tile 1: go right
Tile 2: go right
Tile 3: go right
Tile 4: go right
In the first episode the robot wandered for 20 moves. By episode 10 it took the shortest possible route: 5 moves. Nobody programmed "go right". It worked that out from rewards alone.
Because of the random exploring, your numbers might be slightly different on another version of Python, but the robot should still end up learning "go right" everywhere.
Experiment
- Change
random.seed(7)to another number. Does the robot still learn the route? - Set
EPSILON = 0. Now it never explores. Does it still find the charger quickly? Why? - Change the step reward from
-1to0. Is there still a reason for the robot to hurry? - Make the corridor longer: set
N = 10.
For parents
Reinforcement learning is one of the main branches of machine learning, alongside learning from labelled examples. This small program shows the core loop honestly, without hype. A good discussion question: "If you reward the wrong thing, what might the robot learn?" It connects to real concerns about designing AI goals carefully.
What you learned
- In reinforcement learning, an agent learns by trial and error from rewards.
- Each step has a state, an action and a reward.
- Agents balance exploring new actions with exploiting what already works.
- Q-learning stores a score for each state and action, and improves it after every move.
- The reward you choose shapes what the agent learns.
Level up
This corridor is a one-dimensional warm-up. In a full robotics and reinforcement learning course, students train agents in grid worlds with traps, tune how fast agents learn, deal with slippery, unpredictable floors, and evaluate their agents properly. Want to build more? Book a free demo class and watch your child train an AI that learns.