Please enable JavaScript to use CodeHS

AI Explained

Learning from rewards

Reinforcement learning is trial and error with a scoreboard. An agent takes actions, gets rewards or penalties, and slowly figures out a strategy that maximizes its score.

AI ~12 min High school
The big idea

Rewards and policies

In supervised learning, the model is given correct answers. In reinforcement learning (RL), the model is only given a reward signal. It has to discover which actions lead to high rewards.

The strategy the agent learns is called a policy. A policy is just a rule: “If I am in this situation, what action should I take?” The goal of RL is to find the policy that maximizes the total reward over time.

Core idea: RL agents learn from consequences. A video game bot gets points for winning; a robot arm gets a reward for moving closer to a target.


Value iteration

Values and value iteration

One simple RL algorithm is value iteration. Every cell in the grid gets a number: the expected total future reward from that cell. The agent then picks the action that moves it to the cell with the highest value.

The update rule is:

V(s) = R(s) + max_action V(s')

In words: the value of a state is the immediate reward plus the best value you can reach from there. Repeating this update across every cell eventually gives the correct values, and from those values you can read off the best policy.


Interactive

Grid world

The robot starts in the top-left corner and wants to reach the green goal. Red cells are walls. Click any non-goal cell to toggle a wall. Press Train to run value iteration, then press Run episode to watch the robot follow the learned policy.

🤖 Robot 🟩 Goal 🟥 Wall Click cells to toggle walls
Value grid: not trained yet

From games to chat

RL and human feedback

Reinforcement learning is not just for games. It is also how modern chatbots are fine-tuned to be helpful and safe. In RLHF (Reinforcement Learning from Human Feedback), humans rank several model answers, and a reward model learns to predict which answers humans prefer. The language model is then trained to produce answers with high predicted reward.

RLHF can push a model toward being polite, concise, and accurate, but it also raises new questions: if the reward model is wrong, the chatbot might game the score instead of being truly helpful. That is why alignment research is ongoing.


Code

Q-learning in a gridworld

Reinforcement learning agents learn by trying actions and updating value estimates. This Python agent explores a tiny one-dimensional world.


Check yourself
?

Why does the robot need training?

Why not just have the robot move toward the goal directly? What problem does value iteration solve?

The robot does not know where the goal is or which cells are walls. Value iteration figures out which states are good and which actions lead to them. From there, the robot can follow a reliable policy.


Quick quiz

Three questions

1. What does an RL agent learn?

2. In value iteration, what is V(s)?

3. What is RLHF?

Keep learning

Reinforcement learning helps models behave better, but it also exposes a harder problem: making models reason correctly and stay grounded in facts. That is the focus of the next tutorial.

Part of the AI Explained tutorial series.