Introduction to Robot Learning · Lecture 21 of 25 · CMU 16-831

Robot Simulation & Sim2Real

A policy that is flawless in simulation can fall flat on its face the instant it touches a real robot. The gap between the two — and the surprisingly simple trick that closes it — is the whole story of this lecture.

Prerequisites: the closed loop & the data problem from Lecture 1, and a little Python. No control theory assumed.
10
Chapters
5
Simulations
3
Code Labs

Chapter 0: Perfect in Sim, Broken in Reality

You spend a week training a quadruped to walk. In simulation it is magnificent — it trots, it climbs stairs, it recovers from shoves, it never falls. You upload the exact same policy to the real robot, set it on the floor, and press go. It takes two steps, its legs splay, and it collapses.

Nothing about the policy changed. The neural network weights are byte-for-byte identical. What changed is everything around the policy: the floor has friction the simulator never modeled, the motors respond a few milliseconds late, the legs weigh slightly more than the CAD file said, and the camera image is grainier than any rendered frame. The policy is answering a question about a world that no longer exists.

This is the reality gap: the difference between the simulated world a policy was trained in and the real world it must run in. And sim2real is the name for the entire research effort to cross that gap — to take a policy born in simulation and make it work on hardware.

The one-sentence problem. A policy trained in simulation has only ever seen one particular world — the simulator's world. The real robot lives in a different world. If the policy is brittle to that difference, it transfers badly. Sim2real is the art of making the policy not care which world it is in.

Watch the reality gap open up

A policy was tuned for one exact friction value (the dashed line). Press Deploy and drag the real friction away from where it trained. The overfit policy degrades fast as reality drifts; later we will build the flat robust curve that does not.

Real friction 1.00
Real friction matches sim: the overfit policy is perfect — for now.

Notice the shape of the failure. Right where the real friction matches the simulator (the dashed line), the overfit policy is perfect. It is only perfect there. Step a little off, and performance falls off a cliff. The policy did not learn to walk; it learned to walk on this exact floor.

Where this lecture goes. First we will ask why we bother simulating at all (Chapter 1) — the answer ties straight back to the data problem from Lecture 1. Then we will dissect the gap (Chapter 2), and meet the single most important fix in modern robot learning: domain randomization (Chapter 3). The rest of the lecture — system ID, learning to adapt, higher-level policies — are all answers to the same question: how do we make a sim-trained policy survive contact with reality?

A policy is perfect in simulation but collapses on the real robot, with identical network weights. What is the most accurate description of what went wrong?

Chapter 1: Why Simulate at All?

If simulation introduces a whole new problem — the reality gap — why not just train on the real robot and skip it? Because of the villain from Lecture 1: the data problem. Reinforcement learning is staggeringly sample-inefficient. A walking policy can need ten million environment steps to converge. At 50 Hz on one real robot, that is over fifty days of nonstop, supervised, breakage-prone operation — and that is before the first thing you want to change.

Simulation manufactures the data firehose that robots otherwise lack. The instructor lists four advantages, and each one is a direct rebuttal to a reason real-robot training is painful:

Advantage of simulated dataWhat it buys you
Cheap, fast, scalableThousands of environments run in parallel on one GPU. A quadrotor policy has been trained in 18 seconds on a laptop; IsaacGym runs thousands of robots at once.
SafeRL explores by trying random, often catastrophic actions. In sim a crash costs nothing; on hardware it snaps a leg.
Labeled (free oracle)The simulator knows the ground-truth state — exact contact forces, terrain height, every object's pose. In the real world those labels are expensive or impossible to get.
No wear and tearA real robot degrades with every rollout. A simulated one is brand new every episode, forever.
The bargain. Simulation trades a hard problem you can almost solve (the data problem) for a different hard problem you can solve (the reality gap). The whole rest of this lecture is about why that trade is worth it — because the tools for closing the gap are cheap, and the data you get in exchange is nearly free.

That third advantage — the free oracle — is quietly the most powerful. In simulation you can read off quantities that no real sensor can ever measure: the precise friction coefficient under each foot, the wind vector hitting a drone, the mass of a payload. Hold onto that fact. In Chapter 6 it becomes the key that unlocks one of the most successful sim2real recipes there is — the privileged teacher.

Sim is the only way to afford the data

Slide how many environment steps a policy needs. On a real robot that is wall-clock days of risky operation; in a massively-parallel simulator it is minutes.

Env steps needed10,000,000
Parallel sims4096
Of the four advantages of simulated data, which one most directly enables a robot to learn things a real sensor could never directly measure (like the exact friction under each foot)?

Chapter 2: Two Kinds of Mismatch

"Sim2real is never easy," the lecture warns, "because it is hard to exactly replicate the real world." But how the replication fails matters, because the two failure modes call for different fixes. The instructor splits the gap cleanly in two, using the language of the model-based RL lectures, where a simulator is just the world's transition function: the next state is produced by feeding the current state, the action, and some environment parameters through the dynamics.

Parametric mismatch
The simulator has the right equations but the wrong numbers: a mass off by 10%, a friction coefficient too high, a motor gain too low.
↓ vs.
Non-parametric mismatch
The simulator is missing the physics entirely: complex aerodynamics, fluid dynamics, tire slip, gear backlash, cable drag. No number you tweak can add an effect the equations don't contain.

A parametric mismatch is the friendlier of the two. The simulator's structure is correct — it is the standard rigid-body dynamics — but a coefficient is wrong. The robot's mass in the CAD file was 2.0 kg; the real one with its cables and battery is 2.3 kg. Friction varies with the floor. These are numbers, and numbers can be randomized over (Chapter 3) or fitted (Chapter 5).

A non-parametric mismatch is deeper. The simulator simply does not model an effect. A drone's simple thrust model knows nothing about the swirling aerodynamics in a tight turn (the problem Neural-Fly tackles); a rigid-body sim has no notion of a deformable tire's contact patch. You cannot fix a missing effect by changing a parameter — the term for it is not even in the equations.

A common misconception — "just make the simulator more accurate." It is tempting to think the fix is always higher fidelity. But chasing fidelity is a losing race: the real world has gear backlash, wear-and-tear, sensor noise, and turbulence that no simulator fully captures, and every increment of realism costs compute and modeling effort. The deeper insight of this lecture is the opposite — rather than make one simulator perfect, make the policy robust to a whole family of imperfect simulators.

Spot the mismatch

Click each real-world effect to classify it. A wrong number in the right equations is parametric; a missing effect is non-parametric.

Pick an effect above.
A drone sim uses a simple "thrust = constant × rotor speed squared" model and ignores the air turbulence the drone stirs up in fast turns. The resulting gap is…

Chapter 3: Domain Randomization — Make Reality Just Another Sim

Here is the idea that reshaped sim2real, and it is almost insultingly simple. Instead of training the policy in one carefully-tuned simulator and hoping it matches reality, you randomize the simulator. Every episode, you roll a fresh world: a new mass, a new friction, a new motor delay, new textures, new lighting. The policy is forced to succeed across thousands of different worlds.

Formally, recall the dynamics carry an environment parameter alongside the state and action. Domain randomization samples that parameter freshly each episode and trains a single policy that works for the whole range. The original 2017 paper put it perfectly:

The Tobin et al. (2017) hypothesis. "With enough variability in the simulator, the real world may appear to the model as just another variation." That is the entire trick. If your policy has handled 10,000 different random simulations, then the real world is simply sim #10,001 — one more sample from a distribution it has already learned to be robust to.

The original paper applied this to perception: they trained an object detector on simulated images with absurd, non-realistic random textures, lighting, and camera angles — and it transferred to real photos accurately to 1.5 cm, with no real images in training at all. But the idea generalizes far beyond pixels. Today people randomize every part of the pipeline: dynamics (mass, inertia, friction), sensors (noise, bias, dropout), actuation (latency, gain), and visuals (textures, lighting). What you randomize is whatever you think might differ between sim and real.

Why does this work? It is robust control in disguise

The instructor frames domain randomization as an approximation of robust control. Robust control's goal is a controller that performs well not for one system, but for a whole set of possible systems — the worst case is bounded. Domain randomization achieves the same thing by brute force: rather than prove a worst-case bound mathematically, it simply trains on the whole set and lets the policy discover behaviors that survive all of them. The price is a real one, and Chapter 4 is entirely about it: a policy that must work everywhere is rarely the best policy anywhere.

Not the same as data augmentation. It looks like supervised-learning augmentation (jitter the images), but it is deeper. Augmentation perturbs a fixed dataset's inputs. Domain randomization perturbs the dynamics of the world the policy acts in — it changes the consequences of actions, not just the appearance of states. The policy is learning to be robust to a changing environment, not just a noisy image.

Let us make the central claim concrete. We will train two policies — one on a single fixed friction, one over a randomized range — and measure each on a set of held-out "real" frictions it never saw. The randomized one should win on the worst case. You will write the one line that turns single-value training into range training.

The slogan for domain randomization is "the real world is just sim #10,001." What does that capture?

Chapter 4: The Sweet Spot — How Much to Randomize

Domain randomization sounds like a free lunch: randomize everything, get robustness. It is not. There is a fundamental tension hiding in the width of the randomization range, and getting it wrong fails in two opposite ways.

Too narrow, and you are back to overfitting. If you randomize friction only between 0.95 and 1.05 but the real floor is 1.4, the policy never trained near reality and transfers badly — the same cliff from Chapter 0, just slightly wider.

Too wide, and a different problem appears: the policy may fail to train at all. If the mass can be anything from a feather to a boulder, no single set of weights can be good across that whole range. The optimal action in a feather-light world is the wrong action in a boulder-heavy one. The policy is asked to be excellent everywhere, gives up, and settles for being mediocre everywhere — or never converges.

The tradeoff in one line. Randomization breadth trades robustness against trainability and peak performance. Narrow ranges are easy to train and sharp, but brittle. Wide ranges are robust, but blunt and hard to learn. The art is a range wide enough to contain reality but no wider.

Find the sweet spot

Slide the randomization width. The real-world success curve is low when the range is too narrow (reality falls outside training) and falls again when it is too wide (the policy can't learn a single good behavior). Somewhere in the middle is the peak.

Randomization width 0.10
Too narrow: reality lies outside the training range.

The lab below measures this curve directly. You will sweep the randomization width, train a policy at each width over that range, and score it on the true real gain. The curve you produce is hump-shaped — that hump is the entire reason "just randomize more" is bad advice.

A team randomizes a quadruped's mass over an enormous range — 1 kg to 50 kg — and finds the policy now walks worse on the real 12 kg robot than a narrower range did. Why?

Chapter 5: System Identification — Make the Sim Match the Real

Domain randomization makes the policy robust to the gap. System identification attacks the gap from the other side: it makes the simulator match the real robot. Instead of "be robust to any friction," it asks "what is the actual friction, measured from real data?"

The recipe is the inverse of simulation. In simulation you know the parameters and predict the motion. In system ID you observe the real motion and solve backward for the parameters that best explain it. You run the real robot, log how it moved, and fit the simulator's coefficients — mass, friction, motor gain — so that the same commands produce the same motion in sim as they did in reality.

Collect real rollouts
Drive the real robot, record states and actions.
↓ the data carries the true parameters, hidden in the motion
Fit the sim parameters
Find the mass / friction / gain that make sim predictions match the recorded real motion (least squares).
↓ now the simulator's numbers are right
Re-train in the tuned sim
The reality gap (the parametric part) is now small.
Its hard limit. System ID can only fix parametric mismatch — the wrong numbers. It cannot conjure a missing effect: if the simulator has no aerodynamics term, no amount of fitting adds one. This is exactly why the two tools are complementary: fit what you can model (system ID), and randomize over what you can't pin down or didn't model (domain randomization). The best pipelines do both — the lecture calls the combination robust adaptive control.

Let us do the fit by hand. The real robot's motion came from a true (unknown) gain plus sensor noise. We have the commands and the noisy responses. Recover the gain by least squares — the same math that fits a line through scattered points.

System identification fits the simulator's parameters to real-robot data. What can it not do?

Chapter 6: Learning to Adapt — The Privileged Teacher

Domain randomization builds a fixed policy that ignores which world it is in. But there is a smarter target. What if, instead of being robust to every world, the policy could figure out which world it is in and adapt to it on the fly? A policy that reads the environment and adjusts is an approximation of adaptive control — and it does not conflict with randomization; you can do both.

The trouble is obvious. To adapt to the environment parameter — the friction, the payload, the wind — the policy would need to know it. And in the real world that parameter is exactly what you cannot measure. The robot has no friction sensor. So how can a policy that depends on the unknown parameter ever run on hardware?

The answer is one of the most elegant recipes in robot learning, and it leans entirely on the "free oracle" from Chapter 1. It runs in three phases:

Phase 1 (sim) — train the teacher
In simulation the parameter is known (it's the free oracle). Train a "privileged" teacher policy that gets to see it directly — contact, terrain, friction, disturbance. The teacher is excellent because it has perfect information.
↓ but the teacher needs info the real robot lacks
Phase 2 (sim) — train the student
A "student" policy learns to imitate the teacher using only what the real robot can sense — proprioceptive history (IMU, joint angles). It must infer the hidden parameter from that history. This phase is an imitation-learning problem (Lecture 6).
↓ now the student needs no privileged info
Phase 3 (real) — deploy the student
Ship the student to the real robot. It reads its own sensor history, implicitly estimates the environment, and acts like the privileged teacher would have.
The trick in one sentence. Let a teacher cheat with information only the simulator has, then teach a student to reproduce the teacher's behavior using only what the real robot can actually sense. The student learns to infer the hidden world from its own history — recovering adaptation without ever measuring the parameter directly. This is the backbone of Learning by Cheating (driving) and of RL locomotion controllers that conquer rough terrain.

The student doesn't have to imitate in action-space

A nice degree of freedom: the student need not copy the teacher's actions. It can match the teacher in some other space. RMA has the student reproduce the teacher's compressed environment encoding in a latent space. Agile But Safe has the student predict safety-relevant rays. DATT (a drone controller) has the student predict the unknown dynamics directly. In every case the principle is identical: distill privileged knowledge into something the real robot can compute from its own senses.

Privileged teacher → sensor-only student

Press Run. The world's hidden parameter (a wind, in blue) drifts over time. The teacher sees it directly and tracks perfectly. The student can't see it — it must infer it from its recent motion history, lagging slightly but catching up. A fixed (non-adaptive) policy just suffers the offset.

Ready.
Why train a privileged teacher in sim and then a sensor-only student, instead of just training the student directly?

Chapter 7: Higher-Level Policies & Real-World Fine-Tuning

There is one more way to dodge the reality gap, and it is almost philosophical: don't learn the part of the system that differs most between sim and real.

The lowest-level dynamics — exactly how a foot's contact force translates into motion in the next millisecond — are precisely where simulators are least accurate. So instead of learning a low-level controller that commands raw motor torques (and inherits all that mismatch), you learn a higher-level policy that outputs abstract commands — a desired body velocity, a gait frequency — and hand those to a classical low-level controller that the physics actually supports.

This is the structure of CAJun, a continuous-jumping quadruped: a learned high-level RL policy decides the desired base velocity, gait timing, and a swing-leg residual, while a low-level Model Predictive Controller solves for the ground reaction forces. Because the learned part lives in the well-behaved abstract space and the messy contact physics is handled by classical control, CAJun needs little domain randomization, trains in about 20 minutes, jumps farther, and is more robust than an end-to-end policy.

The abstraction lever. The higher the level you learn at, the smaller the reality gap that the learned part has to cross — because abstract commands (go 1 m/s) transfer across sim and real far better than raw torques do. The cost is that you now need a trustworthy classical low-level controller. It is the hybrid philosophy from Lecture 1: learn what's hard to model, keep classical control for what has good physics.

And when the gap still bites: real-world fine-tuning

None of these methods is required to fully close the gap on its own. A common final step is to fine-tune in the real world: take the sim-trained policy as a strong starting point and let it keep learning from a small amount of real data. Because the policy already nearly works, this needs orders of magnitude less real experience than training from scratch — the dangerous, sample-hungry early exploration happened safely in simulation. Sim gets you 95% of the way; a little real data finishes the job.

Four tools, one goal. Domain randomization (be robust to the gap), system ID (shrink the gap), learning to adapt (sense and cancel the gap), higher-level policies (don't expose the learned part to the gap). They stack. Real-world fine-tuning is the polish on top. A serious sim2real system uses several at once.

CAJun learns a high-level policy (desired velocity, gait) and uses a classical MPC for the low-level forces. Why does this reduce the reality gap so effectively?

Chapter 8: Showcase — Swift, the Champion Drone

In 2023, a system called Swift (Kaufmann et al., Nature) did something no autonomous system had done: it beat human world champions in head-to-head first-person-view drone racing — a sport where pilots scream a quadrotor through gates at over 100 km/h on the ragged edge of control. The policy was trained almost entirely in simulation. It is the clearest case study of every idea in this lecture working together.

Why is racing the perfect crucible for sim2real? Because at the limits of flight, every source of the reality gap is screaming at once: the aerodynamics are violently non-parametric (turbulence, propeller wash), latency is fatal at those speeds, and the camera-based perception is noisy. A policy that overfit its simulator would be confidently wrong about the one moment that decides the race.

Swift's recipe is this lecture's checklist:

The headline result. Swift won several races against three distinct human world champions and set the fastest recorded lap — trained in simulation, deployed on a physical drone, the reality gap crossed by exactly the toolkit you just learned. It is the existence proof that sim2real, done well, reaches and exceeds expert human performance in the physical world.

Play with the forces below. Crank the unmodeled aerodynamics (the non-parametric gap), then turn on randomization + residual correction + online adaptation and watch a drone that was missing gates start threading them.

Race a drone through the gates — assemble the sim2real toolkit

Press Run. The drone (warm) chases gates (teal). Raise the unmodeled aerodynamics to widen the reality gap, then flip on the three tools and watch the gate-clear rate climb back up.

Unmodeled aero (gap) 0.60
Ready.

The instructive sequence: max aero with everything off (the drone fights an unseen force and clips gates), then add randomization (it stops overfitting and clears more), then add adaptation (it learns the force and threads them). That progression — from brittle to robust to adaptive — is sim2real in miniature, and it is how Swift flew.

Chapter 9: Connections & Cheat Sheet

You now own the conceptual map of sim2real. Every method in this lecture is an answer to a single question — how does a policy born in simulation survive the real world? — and they attack the reality gap from complementary directions.

MethodWhat it does to the gapBest atLimit
Domain randomizationMakes the policy robust to itUnknown / unmodelable parametersWide ranges blunt performance & hurt training
System IDShrinks it (tunes the sim)Parametric mismatch (wrong numbers)Can't add missing (non-parametric) physics
Learning to adaptSenses & cancels it onlineSlowly-varying hidden parametersNeeds a privileged-teacher / student pipeline
Higher-level policiesAvoids exposing the learned part to itSystems with a good classical low-level controllerRequires that trustworthy low-level controller
Real-world fine-tuningErases the residual gap with real dataFinal polish on a near-working policyStill costs (a little) real, risky data
Cheat sheet — the vocabulary you now own.
  • Reality gap = the difference between the simulated world a policy trained in and the real world it runs in.
  • Why simulate = cheap, fast, scalable, safe, free oracle labels, no wear — it manufactures the data firehose robots lack.
  • Two mismatches = parametric (wrong numbers in the right equations) vs non-parametric (a missing effect entirely).
  • Domain randomization = randomize the sim each episode so the real world is "just sim #10,001"; an approximation of robust control.
  • The sweet spot = randomize wide enough to contain reality, no wider; too narrow is brittle, too wide won't train.
  • System ID = fit the simulator's parameters to real-robot data (least squares); closes only the parametric gap.
  • Privileged teacher → student = teacher cheats with sim ground-truth, student recovers it from real-available sensors; an approximation of adaptive control.
  • Higher-level policies = learn abstract commands, let classical control handle the mismatch-prone low-level dynamics.

Where to go next on this site

This is Lecture 21 of the CMU 16-831 series. The natural neighbors are Lecture 20 — Exploration (just before) and Lecture 22 — Safe RL (just after, where "physics doesn't forgive" gets its own lecture). To go deeper on the ideas here:

The test of this lesson. You should be able to explain, from memory: why we simulate despite the gap; the two kinds of mismatch and which tool fixes each; why "the real world is just sim #10,001"; why more randomization isn't always better; how a privileged teacher trains a sensor-only student; and how Swift stitched all of it together to beat human champions. If you can, you're ready for Lecture 22.

"With enough variability in the simulator, the real world may appear to the model as just another variation."