Simulation and sim-to-real
Real robots are slow and costly to train. Simulators let a robot practise a huge number of times. The hard part is making what it learned work in the real world.
In 60 seconds
Simulation and sim-to-real
Real robots are slow and costly to train. Simulators let a robot practise a huge number of times. The hard part is making what it learned work in the real world.
Why simulate?
- Speed: many copies of the robot can practise at the same time, faster than real time.
- Safety: falls and crashes hurt nobody.
- Cost: no motors wear out.
- Perfect information: the simulator knows exactly where everything is, which makes rewards and labels easy.
Reinforcement learning in one page
| Word | Meaning | Grid-world example |
|---|---|---|
| State | The situation right now | Which square the robot is on |
| Action | What the robot can do | Move up, down, left or right |
| Reward | A score after each action | +10 at the goal, -1 for every step |
| Policy | The rule for choosing actions | "In this square, move right" |
| Episode | One try, from start to end | From the start square until the goal |
# new estimate = old estimate + learning_rate * (what we just saw - old estimate)
Q[s, a] = Q[s, a] + alpha * (reward + gamma * best_Q_of_next_state - Q[s, a])alpha is how fast to learn and gamma (between 0 and 1) is how much future rewards count. Real robots have far too many states for a table, so they use a neural network instead, but the idea of learning from reward is the same. You will run a full tiny example in the lab.The reality gap
Domain randomisation
- 1
Build the simulator
Model the robot, objects and scene as well as you reasonably can. - 2
Randomise
Vary physics and visuals widely in each episode. - 3
Train
Use RL, imitation, or both, over many episodes. - 4
Test in the real world
Start slowly, in a safe area, with a way to stop the robot. - 5
Close the loop
Measure what went wrong and improve the simulator or the randomisation.
Other ways to cross the gap
- System identification: measure the real robot carefully (weights, friction, delays) and make the simulator match.
- Fine-tuning on real data: train mostly in simulation, then adjust with a smaller amount of real-world practice or demonstrations.
- Simpler inputs: give the policy things that look the same in sim and reality, such as joint angles, instead of raw images.
Watch and read more
Lab
A tiny Python grid-world where a robot learns, by Q-learning, to reach a goal, plus two experiments about the reality gap.
The problem
Save the starter code as gridbot.py and run it with python3 gridbot.py. It needs only standard Python. The robot learns by trial and error to reach the goal in a 4 x 4 grid with two walls. It should find a shortest path of 6 steps.
- Run it and read the printed path. Draw it on paper.
- Change
range(500)torange(5). Does it still find a good path? Why or why not? - Change the step reward from
-1.0to0.0. Run a few times with different seeds. What changes, and why? (Hint:gammastill makes the goal worth more when it is closer.) - Reality gap experiment. Train as normal. Then, only in the final walk, make the world "slippery": change the last loop so that with probability 0.2 the robot takes a random action instead. Does it still reach the goal? Is the path longer?
- Randomisation experiment. Now make
stepslippery during training too. Does the learned path change? Write two sentences on how this is like domain randomisation.
Practice lab
Edit the code and run it. Python runs in your browser — nothing is sent to our server.
No output. Add print(...) to see values.
You are done when
Hard questions
Try to answer before you reveal. If you can answer these, you understood the lesson.
Q1Why does the -1 reward for every step make the robot prefer short paths?Reveal
Every extra step subtracts 1 from the total reward, so a longer path to the same goal earns less. The Q-values learn this, and the greedy policy picks actions that lead to the goal in fewer steps. With a step reward of 0, the discount gamma still makes a reward reached sooner worth more, so short paths are usually still preferred, but the pull is weaker and learning can be slower.
Q2A policy is trained in a perfectly predictable grid, then the real grid turns out to be slippery. Why can it fail badly, even though the goal and walls did not move?Reveal
The policy only learned what to do on squares along its usual path, because it rarely needed anything else. When a slip pushes it to an unusual square, its values there may be poorly learned, so its choices can be poor. Also, a path that passes close to walls may be fine without slips but risky with them. This is the reality gap in miniature: the training world was simpler than the real one.
Questions people ask
Is a simulator the same as a video game?
They share a lot: both use physics engines and draw 3D scenes. But games only need to look right to a player. Robot simulators need the physics to be accurate enough that what works there also works on a real robot.
Why not do reinforcement learning directly on the real robot?
Sometimes people do, for simple tasks. But RL often needs a very large number of tries, many of them bad. On a real robot that takes a long time, wears out parts and can be unsafe. Simulation, demonstrations or both are used to cut down real-world practice.
Does domain randomisation always work?
No. If the real world has an effect the simulator never includes at all, random variation of other settings will not teach it. Too much randomisation can also make the task so hard that learning slows down. It is a useful tool, not a guarantee.
Can I try reinforcement learning without a robot?
Yes. The lab in this lesson runs a tiny grid-world robot in plain Python. Open-source simulators and RL toolkits also exist; check their current documentation to pick one that runs on your computer.
Lesson test
5 questions. Get 3 right (60%) to pass and complete this lesson.