BLOG
Code-as-a-Policy and the Sim-to-Real Gap: Transfer Across Robots and Tasks
TL;DR
- In our previous post, we said the main cause of the sim-to-real gap was the robot itself, not the task. This post tests that claim twice, with transfer across robots and with transfer across tasks.
- Across robots: we moved the same task from the dual-arm Robot A to the single-arm Robot B. The task carried over smoothly. Most of the effort went into characterizing the robot itself.
- Across tasks: keeping Robot B, we changed the parts, the base the pegs go into, and the placement positions. The control programs improved in simulation worked on the real robot.
- If the gap is in the robot, the fixes should target the robot. In the second half, we go through domain randomization and three other approaches (real-to-sim, active exploration, and online adaptation), and describe two cases from Physical RSI: reflecting real-robot data in the simulator, and a spiral search.

1. What we wanted to check
Physical RSI (recursive self-improvement) adds a self-improvement loop to Agent-as-a-Policy, in which a coding agent operates the robot (our first post). The loop improves a control program, that is, code, and that code runs as the robot's policy. In this post we call it a Code-as-a-Policy. In our previous post, we ran the improved program on real robots and saw that the main cause of the sim-to-real gap is the robot itself, not the task.
If that is right, two things should follow:
- Transfer across robots (cross-embodiment transfer): when we move the same task to a different robot, the task carries over unchanged, and the gap shows up as differences in the robot.
- Transfer across tasks: when we change the task on the same robot, almost no new gap appears.
We checked both on real hardware (Figure 1).
2. Transfer across robots: most of the work was in the robot
At the end of our previous post, we described moving the task to a second robot, Robot B. Robot B is a single-arm robot. It differs from the dual-arm Robot A in the number of arms and joints, and in its motors, gripper, and cameras.
We kept the task flow that we had set in simulation almost unchanged: pick up a part, insert its pegs into the holes of the base plate, and place the part in the output tray. The task moved over smoothly (Figure 2).
Most of the work went into characterizing the robot itself. For example:
- A joint stops short of the commanded position.
- Camera images arrive some time after they were captured.
- The gripper moves differently from its simulation model.
Each of these differs from machine to machine, and none of them is in the simulator or the documentation. They are the same kind of problem we found on Robot A.
We handled them the same way as before. We had the agent measure the robot on the real hardware, and kept the results as robot data (measurements and models specific to that robot) and skills (reusable procedures).
3. Transfer across tasks: almost no new gap appeared
Next, we kept Robot B and tried different tasks. For example, we changed the parts, the base the pegs go into, and where the parts are placed.
For each task, the agent improved the control program in simulation, and we ran it on the real robot. Because the robot was the same, what we had learned about it still held. The programs ran smoothly.
Parts, base, and placement can all be varied in simulation. The robot's own properties do not change when the task changes. If the main cause of the gap is the robot, then as long as the robot stays the same, changing the task should produce almost no new gap. Within what we tried, that is what we saw on the real robot.
4. If the gap is in the robot, how do we deal with it?
The robot's actual latency, friction, joint zero positions, and so on are not known exactly in advance. Approaches to the gap differ in where they handle that uncertainty, and we sort them into four (Figure 3). Our previous post covered domain randomization, and robot data and skills. Here we go through all four briefly.
Figure 3 is a schematic in which the spread of simulator settings (latency, friction, and so on) is drawn as dots in two dimensions. The star is the real robot's true value, which we usually do not know exactly. The crosses are measurements on the real robot, the dashed ellipse is the range of settings, and the rings are estimates.

Domain randomization
The uncertainty is covered by the training range. We train over a wide range of simulator conditions so that the real robot's values fall somewhere inside it. It works even when the real values are unknown. But because the policy has to work under every condition, a range that is too wide can make it conservative. For an accessible introduction, see Lilian Weng's blog post.
Real-to-sim
The uncertainty goes into the simulator. This reverses the usual direction: real-robot data comes back into the simulator and brings it closer to the robot. Examples include system identification, which estimates mass and friction from the robot's motion, and learning a motor model from real data (Hwangbo et al., 2019). Domain randomization covers a wide range, while real-to-sim narrows it to a region near the real robot's values.
We use this approach in Physical RSI as well. After a few runs on the real robot, we hand the logs to the agent, which analyzes them and estimates two things:
- ① The robot's parameters (mass, friction, and so on).
- ② The pattern of errors on the real robot (for example, how far a joint stops short of its target, or how a grasped part shifts in the hand).
Both are reflected in the simulator (Figure 4). The robot data in our previous post, a simulation model matched to the real robot plus measured parameters, belongs here.
We also use the identified parameters to estimate force. Robot B has no force sensor, so we estimate external force from the motor torques. The left side of Figure 4 (①) shows an example: it compares a period when nothing touches the arm (the estimate should be zero) with a period when the arm presses on a surface with a steady force. With the CAD parameters, the estimate has an offset even without contact; with the identified parameters, that offset becomes smaller. While pressing, some difference remains. The right side of Figure 4 (②) is a schematic of the error pattern: the errors on the real robot have a bias and a spread, and we put errors with the same tendency into the simulator.

Even so, differences outside the range we measured and factors missing from the model remain.
Active exploration
Active exploration is a way to do real-to-sim with fewer measurements. It chooses which experiments to run on the real robot for system identification so that they yield the most information (active system identification). It is close to optimal experimental design, and its aim differs from the "exploration" of reinforcement learning, which seeks reward. For example, ASID trains an exploration policy in simulation to maximize the information gained about the unknown parameters (Fisher information), then runs it on the real robot (Memmel et al., 2024). Real-robot trials cost time and money, so needing fewer measurements is an advantage. The effort of running the robot to measure it does not go away.
Online adaptation
The uncertainty is made up for with signals available while the robot runs. While the robot is moving, it uses those signals to estimate properties that were not known beforehand, or to switch its motion. For example, RMA estimates a latent summary of properties such as friction or the weight of what the robot is holding from its recent motion, and feeds that estimate to the control program (Kumar et al., 2021). It can handle changes that cannot be measured beforehand. Structurally it resembles classical adaptive control, with an estimator and a controller, but both are learned and there is no stability guarantee. Also, most RMA-style methods are trained in simulation with randomized conditions, so they only handle changes that fall within that range and are slow enough for the estimate to keep up. So online adaptation adds an estimating module on top of domain randomization.
Physical RSI's control programs also contain an example of online adaptation: a spiral search for putting the pegs into the holes, which a human added by giving the agent the idea (Figure 5). The robot presses the part onto the surface with roughly constant force and moves it in a spiral until the pegs find the holes. The pressing force is measured with the torque-based estimate (the same method as ① in Figure 4), and the controller keeps that value steady. The insertion position error does not need to be known in advance: the search finds the holes from contact signals alone. It does assume that the error falls within the search range.

But the self-improvement loop did not find the spiral search on its own. The loop picks programs by their score in the simulator. The simulator did not reproduce the insertion position error that occurs on the real robot, and domain randomization did not cover it either, so the spiral search was never selected. We got it only after a human engineer put the idea into the prompt: find the holes by feeling the contact force.
5. Summary and limitations
Within what we tried, the two checks support the view that the main cause of the sim-to-real gap is the robot itself. Once a robot has been characterized, we expect a new task to come down mostly to improving the Code-as-a-Policy, which can be done in simulation with compute.
The four approaches differ in where they handle the uncertainty about the robot. The more of it we can measure on the real robot and put into the simulator (real-to-sim), the more problems we can solve in simulation. What still remains is covered by the randomization range or by online adaptation.
There are three limitations:
- We checked only two robots and one family of tasks: picking up a part, inserting its pegs, and placing it.
- Errors that the simulator does not contain can be hard for the self-improvement loop to handle alone. A human supplied the idea behind the spiral search. Whether the loop can find such methods on its own is still an open question.
- When we measure a robot, people still clamp it in place and start the measurement by hand.
References
- Weng (2019). Domain Randomization for Sim2Real Transfer. Lil'Log.
- Hwangbo et al. (2019). Learning agile and dynamic motor skills for legged robots. Science Robotics, 4(26).
- Memmel et al. (2024). ASID: Active Exploration for System Identification in Robotic Manipulation. ICLR 2024.
- Kumar et al. (2021). RMA: Rapid Motor Adaptation for Legged Robots. RSS 2021.
Related articles

Physical RSI: The Sim-to-Real Gap When a Self-Evolved Code-as-a-Policy Runs on Real Robots
Only 1 of the first 21 real-robot runs of a program evolved by Physical RSI placed every part. After we matched the simulator to the hardware, 18 of 21 runs did.

Physical RSI: A Coding Agent Improves Robot Control Programs
A coding agent improves a robot control program by tree search, with no demonstrations or weight updates. Tested on two RoboDojo tasks in simulation.