Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on. You've got this! 🚀
Reinforcement Learning
Reinforcement Learning is how AI learns to make sequences of decisions by receiving rewards for good actions and penalties for bad ones. It is the technique behind game-playing AI and robotics.
How Reinforcement Learning Works
Think about training a dog. You do not explain the rules in language. Instead:
- Dog does something good: reward with a treat
- Dog does something bad: say 'no'
Over many repetitions, the dog learns which behaviors lead to treats.
Reinforcement Learning works the same way:
- The AI (called the agent) takes an action in an environment
- The environment gives back a reward (positive) or penalty (negative)
- The agent learns to take actions that maximize total rewards over time
Labeled data → supervised, no labels → unsupervised, rewards → RL
Key Concepts
- Agent: the AI that is learning (the dog, the game character, the robot)
- Environment: the world the agent interacts with (the room, the game, the road)
- Action: what the agent does (move left, jump, brake)
- Reward: feedback the agent gets after an action (score increase, penalty, crash)
- Policy: the strategy the agent learns (which action to take in each situation)
Simple Reinforcement Learning Illustration
# Simple illustration: an agent learning to navigate a grid
# The agent gets rewarded for reaching the goal
# Grid: S = start, G = goal, X = wall
# Agent starts at S and must reach G
import random
random.seed(42)
grid = [
["S", ".", ".", "."],
[".", "X", "X", "."],
[".", ".", ".", "G"],
]
def get_reward(position):
row, col = position
cell = grid[row][col]
if cell == "G":
return 10 # big reward for reaching goal
elif cell == "X":
return -5 # penalty for hitting wall
else:
return -0.1 # small penalty to encourage finding goal quickly
# Simulate a few random episodes (simplified RL)
print("Reinforcement Learning: Agent exploring the grid")
print()
total_rewards = []
for episode in range(5):
position = (0, 0) # Start position
total_reward = 0
steps = 0
while steps < 20:
# Random action (left/right/up/down)
action = random.choice(["right", "down", "left", "up"])
row, col = position
if action == "right" and col < 3: col += 1
elif action == "down" and row < 2: row += 1
elif action == "left" and col > 0: col -= 1
elif action == "up" and row > 0: row -= 1
position = (row, col)
reward = get_reward(position)
total_reward += reward
steps += 1
if grid[row][col] == "G":
break
total_rewards.append(total_reward)
reached = "GOAL!" if grid[position[0]][position[1]] == "G" else "did not reach goal"
print(f" Episode {episode+1}: {steps} steps, total reward: {total_reward:.1f} ({reached})")
print()
print("In real RL, the agent learns from these episodes and improves over time")Real-World Applications
- Game playing: AlphaGo learned to play Go better than any human through reinforcement learning
- Robotics: robots learn to walk, grasp objects, and navigate environments
- Recommendation systems: some platforms use RL to optimize long-term user engagement
- Trading: some algorithmic trading systems use RL to make buy and sell decisions
- Self-driving: certain autonomous driving decisions use RL concepts
Key Takeaways
- Reinforcement Learning is how AI learns to make sequences of decisions by receiving rewards for good actions and penalties for bad ones.
- Agent: the AI that is learning (the dog, the game character, the robot)
- Environment: the world the agent interacts with (the room, the game, the road)
- Action: what the agent does (move left, jump, brake)