World
Rewards
Learning
Playback
View
More information about reinforcement learning in a grid world
An agent learning by trial and error
Reinforcement learning is how a machine learns from rewards instead of examples: it tries moves, sees what they bring, and slowly learns which move is best in each situation. Here the situation is a cell of a grid world with walls, a goal and pits. Watch the values spread back from the goal as a heat map, the arrows settle into a policy, and the reward per episode climb on the chart, then paint your own world and see what the agent makes of it. An agent learns by trial and error to cross a grid world: every move costs a little, the green flag pays the goal reward and ends the episode, a pit costs the pit penalty. It keeps a table Q(s, a) of how good each move is in each cell and improves it after every step. The colours show what it has learned, the value of each cell (yellow is good, purple is bad), the white arrows its best move in each cell, and the orange line the path it would take now. ε is how often it tries a random move instead of the best one, α how far each step moves Q towards the new evidence, γ how much a reward later is worth compared with one now. Q-learning learns about the best moves while it explores; SARSA learns about the moves it really makes, exploration included; Expected SARSA averages over them; value iteration knows the rules of the world and computes the values directly. On the cliff, SARSA learns the safe path away from the edge and Q-learning the short path along it, and falls in more often while it explores: the chart shows the sum of rewards per episode of both. Click or drag on the grid to paint walls, goals, pits, the start or floor; add wind that pushes the agent up or ice on which it slips sideways. Play watches it learn (fast forward with Speed), Step and Back go a step or episodes at a time. Save the picture, record a video with footsteps, a chime at the goal and a thud in a pit, or open it in the photo editor with the pencil, where the grid, the values, the walls, the goals and pits, the arrows, the paths, the agent and the chart are layers of their own.
How it works
- Press Play and watch the orange agent wander, fall into pits and reach the goal; after each step it updates its table of values, which colours the cells, and the white arrows show its best move in each cell.
- Compare Q-learning and SARSA on the cliff: both paths and both reward curves are drawn, so you can see which learns the short risky path along the edge and which the safe one.
- Change ε, α, γ, the step cost and the rewards, or let ε decay to 0, and the agent learns again from scratch; Speed goes from one step a second to ten thousand.
- Pick Wall, Goal, Pit, Start or Floor and click or drag on the grid to build your own world, add wind or slippery ice, and see how the learned policy changes.
States, actions and rewards
A grid world is a Markov decision process: the agent is in a state (a cell), picks an action (up, right, down or left), and the world answers with a reward and the next state. Every move costs a little, reaching the goal pays a reward and ends the episode, falling into a pit costs a penalty. The agent does not know any of these rules in advance. Its aim is to maximise the return, the sum of rewards to come, in which a reward t steps away counts γᵗ times as much: the discount γ between 0 and 1 makes it prefer rewards that come sooner.
Learning a table of values
The agent keeps a number Q(s, a) for every cell and move: how much return it expects if it makes that move there and plays well afterwards. After each step it nudges the old value towards a better estimate, the reward it just got plus the discounted value of where it landed: Q(s, a) ← Q(s, a) + α [r + γ V(s′) − Q(s, a)]. This is temporal-difference learning; α sets how far each nudge goes. The best value of a cell, V(s) = max Q(s, a), is the heat map, and the move with the highest Q is the arrow. Good values appear first next to the goal and spread back one step at a time.
Exploring and exploiting
If the agent always took the move that looks best, it would never discover better ones. With ε-greedy exploration it takes a random move with probability ε and the best-looking move otherwise. Exploration costs reward while learning, so ε is often lowered over time; with ε decaying to 0 the agent finally acts greedily. This trade-off between exploring the unknown and exploiting what is known is at the heart of reinforcement learning.
Q-learning, SARSA and the cliff
Q-learning (Chris Watkins, 1989) uses the best value of the next cell in its update, so it learns the values of the greedy policy whatever it actually does: it is off-policy. SARSA (Rummery and Niranjan, 1994) uses the move it really takes next, random ones included: it is on-policy. On Sutton and Barto's cliff, a row of pits between the start and the goal, Q-learning learns the shortest path right along the edge and keeps falling off while it explores, while SARSA learns a path one or two rows away and earns more reward on the way. Expected SARSA averages over the moves its policy might take, and value iteration, which is given the rules of the world, computes the values directly with the Bellman equation, one sweep per episode.
Frequently asked questions
What is reinforcement learning?
A kind of machine learning in which an agent learns by acting: it tries actions in an environment, receives rewards or penalties, and adjusts its behaviour to collect as much reward as possible over time. Nobody tells it the right answer; it has to discover it by trial and error. It has trained programs that play backgammon, Go and video games, robot controllers, and the fine-tuning of language models from human feedback.
What is the difference between Q-learning and SARSA?
Both learn a value Q(s, a) for every state and action from experience. Q-learning updates towards the reward plus the best value of the next state, so it learns about the greedy policy even while it explores (off-policy). SARSA updates towards the value of the action it actually takes next, so its values include the cost of its own exploration (on-policy). That is why, on the cliff, SARSA keeps a safe distance from the edge while Q-learning walks along it.
What do ε, α and γ do?
ε is the exploration rate, the probability of a random move instead of the best one. α is the learning rate, how far each update moves a value towards the new estimate: large values learn fast but stay noisy, small ones learn slowly. γ is the discount factor: at 1 a reward later is worth as much as one now, at 0.9 a reward ten steps away is worth about a third. A step cost or a discount below 1 both make the agent hurry.
What is value iteration?
A planning method for when the rules of the world are known: the probabilities of every move's outcomes and their rewards. It sweeps over all states and sets each value to the best expected reward plus discounted value of the next state, the Bellman optimality equation, until nothing changes. In the app it makes one sweep per episode, so you can watch the values spread from the goal; learners such as Q-learning reach the same values from experience alone.
Why does the agent sometimes fail with ice or wind?
With slippery ice a move goes sideways with some probability, and gusty wind pushes the agent by a varying amount, so the same policy can end differently each time. The best policy then weighs the risks: it may take a longer path away from the pits or even aim at a wall so that a slip can only go one safe way. Success below 100 % can be the best possible, as on the frozen lake.
Can I use the picture?
Yes. Save gives a PNG at the size you choose, Save JSON writes the world, the settings, the learned values and policy and the rewards of every episode, Record films the agent learning with footsteps, a chime at the goal and a thud in a pit, and the pencil opens the picture in the PictorX photo editor with the grid, the values, the walls, the goals and pits, the arrows, the paths, the agent and the chart on layers of their own. In the editor itself it is under Apps ▸ Science ▸ Computing ▸ Reinforcement Learning Gridworld. Everything runs in your browser.
Reinforcement Learning Gridworld: how do I record a video?
Press the red Record button in the playback bar: it films what the stage shows. Press it again to stop, and the video is saved to your device. Recording is free; you only need to sign in. Without a subscription the video carries a small PictorX logo in a corner. The button beside Record opens the video options: the shape (as shown, square, story 9:16 or wide 16:9), the length (until you stop, or 5, 10, 15 or 30 seconds for social media) and the file: MP4, WebM or an animated GIF. For the sound, choose the app's own sounds or one of three music tracks: calm, bright or beat.
Ready to try it?
Open PictorX in your browser — no install, no credit card required to start.
Go to Editor