Borrowing Hazards from Simulation for Safe Real-World Fine-Tuning

A robot that learns to be safe stops producing examples of being unsafe. Its estimate of where danger begins then drifts, and safety failures return late in training. Breaking Safety Paradox with Feasible Dual Policy Iteration calls this the safety paradox and fixes it by running a second, deliberately reckless policy that seeks out constraint violations. That fix cannot run on physical hardware. Simulation can produce violations freely, but its picture of danger is off wherever simulated physics differs from reality. This note proposes splitting the job. The simulator supplies knowledge of where the hazards are, learned from cheap, deliberate violations. A small amount of strictly safe real-world data supplies knowledge of where the robot actually ends up. The two are combined in a learned correction to the simulator's danger estimate, and that corrected estimate, rather than the sparse stream of real violations, drives the penalty weight that keeps the policy safe during real-world fine-tuning.

The argument rests on one observation. In many robotic constraints, such as collisions, joint limits, and speed limits, the cost is a known function of state. What the reality gap changes is the dynamics. So a cost critic trained in simulation (a network predicting future constraint violations) still encodes the hazard geometry correctly. It is wrong only about which states the real dynamics lead to. Evaluating that simulated critic at real next states, and learning a residual on top of it, should let the system learn about real boundaries without ever crossing them.

Core Claim

Constrained reinforcement learning usually enforces a cost budget with a Lagrange multiplier, a penalty weight that rises when the agent overspends its budget and falls when it underspends. When a simulation-trained policy is fine-tuned on hardware, this multiplier is fed by measured real costs. Those measurements are nearly always zero for a safe policy, then jump abruptly when the boundary has moved. The claim is that real-world Lagrangian fine-tuning is unstable mainly because this measurement is sparse and biased, not because the controller that updates the multiplier is badly tuned. In effect, the safety paradox reappears in the dual variable. Fixing the measurement should therefore beat fixing the controller.

Concretely, let $Q_c^{\text{sim}}$ be a cost critic trained in simulation on data enriched with violations by a violation-seeking dual policy, and let $\Delta_\psi$ be a residual network with parameters $\psi$. The real-world cost of policy $\pi$ is estimated as

$$\hat C(\pi) = \mathbb{E}_{s_0}\big[Q_c^{\text{sim}}(s_0,\pi) + \Delta_\psi(s_0,\pi)\big] + \beta\, U_\psi(\pi),$$

where $U_\psi$ is the disagreement of an ensemble of residuals and $\beta$ sets how pessimistic the estimate is. The residual is trained only on safe real transitions $(s,a,c,s')$ with the target $c + \gamma\,[Q_c^{\text{sim}}(s',a') + \Delta_\psi(s',a')] - Q_c^{\text{sim}}(s,a)$. Here $\gamma$ is the discount factor, $a'$ is the next action, and $\Delta_\psi$ is regularized toward zero away from real data. Because the target queries the frozen simulated critic at the real next state $s'$, the hazard information comes from simulation and the dynamics information comes from reality. The multiplier $\lambda$ then follows a proportional-integral update on $\hat C(\pi) - \epsilon$, with $\epsilon$ the cost budget, in place of the raw real episodic cost.

The closest existing work is Provably Safe Sim-to-Real Transfer. It shows that real samples are only needed in the "mismatch region" where the simulator is wrong, and it collects them with a safe mixture policy, but only for tabular problems solved exactly by linear programming. The present proposal carries the mismatch-region idea to continuous control through a learned residual on the cost critic, and adds the Lagrangian fine-tuning stage that the tabular method avoids.

Research Program

Phase one runs in simulation. A primal policy and a violation-seeking dual policy are trained following Breaking Safety Paradox with Feasible Dual Policy Iteration. From that work we reuse the dual policy, its KL tie to the primal policy, and the importance-weighted critic training. The output is a pretrained safe policy and a cost critic calibrated near the constraint boundary. We keep a rare-event buffer of violating transitions, because Hazard Compression: Catastrophic Forgetting in Diffusion-Based Generative Replay under Distribution Shift shows that hazard representations are erased when they disappear from recent data. That paper's 500-slot buffer cut violations by 99.8% in Cheetah-run.

Phase two runs on the target system, which is "real" in our experiments. The frozen simulated critic is wrapped with the residual ensemble. Target data is collected by the current policy under the pessimistic constraint $\hat C(\pi) \le \epsilon$. The policy is mixed with the frozen simulated safe policy whenever $U_\psi$ is high, in the spirit of the mixture policy of Provably Safe Sim-to-Real Transfer. The multiplier is updated by a proportional-integral rule. Dual Optimistic Ascent (PI Control) is the Augmented Lagrangian Method in Disguise shows this rule is equivalent to an augmented Lagrangian method and damps oscillation. We use it because it is the strongest well-understood controller, so any remaining gain can be credited to the measurement rather than the controller.

The analysis targets one quantity: how accurately the cost critic predicts real violations at boundary states the real policy has never visited. Our testbeds are sim-to-sim, so we can measure this with oracle rollouts in the target dynamics. We will also log the multiplier's trajectory, including overshoot, oscillation amplitude, and the size of the jump at the moment of transfer, to test whether a smooth, model-based measurement removes the windup and seesaw behavior described in Responsive Safety in Reinforcement Learning by PID Lagrangian Methods.

Testbeds come from the file. In each, a source simulator is paired with a perturbed "real" target. The first is Safety-Gymnasium navigation tasks (PointGoal1, CarGoal1) and velocity-constrained locomotion. Targets vary friction (0.5x to 2x), mass, action gain, and actuator delays of one to three steps. The second is the safe DeepMind Control variants from Hazard Compression: Cheetah-run with $|v_t| \le 7$, and Walker-walk with height and velocity limits. The third is the modified-dynamics MuJoCo pairs (Hopper, HalfCheetah) used by Reinforced Grounded Action Transformation for Sim-to-Real Transfer. As a sanity check, we will also reproduce the 5x5 windy Gridworld from Provably Safe Sim-to-Real Transfer, where an exact answer exists.

The primary metrics are target-side cumulative cost during fine-tuning, which applies the Cost Figure of Merit only to target interactions; final target reward and cost; target samples needed to reach the budget; and boundary calibration error of the critic. Physical hardware is beyond what this file supports. That is a real limitation, addressed in part by including actuator-delay targets that mimic common hardware gaps.

Baselines

The naive reference point is zero-shot transfer: the simulation-trained policy runs on the target with no adaptation. This shows how large each gap is and how often a boundary-hugging policy violates constraints the moment dynamics shift. Trained to the constraint boundary as Boundary-Seeking Policy Gradient for Safe Reinforcement Learning prescribes, it should violate under even small shifts. We expect this to motivate the problem rather than threaten the method.

The strongest existing method for the question we ask is PI-Lagrangian fine-tuning on the target from the same simulated initialization, with the multiplier driven by raw target costs and gains set following Responsive Safety in Reinforcement Learning by PID Lagrangian Methods and the augmented-Lagrangian view of Dual Optimistic Ascent. It is the strongest competitor because it differs from our method only in the measurement fed to the multiplier, which is exactly the variable our claim concerns. The closest published numbers are not for fine-tuning across a dynamics gap. Safety Gymnasium reports that PPO-Lag achieved "a 98% reduction in safety violations compared to standard PPO, with only a 45% drop in total reward" on velocity tasks. Breaking Safety Paradox reports near-zero violations across 14 environments for SAC-FDPI, but that result trains directly in the evaluation environment and allows violations. We found no reported result on Lacuna for safe fine-tuning across perturbed-dynamics Safety-Gymnasium or safe DMC tasks.

The second strong competitor is domain randomization with a tightened budget. We train PI-Lagrangian across the full randomization envelope with budget $\epsilon - m$, sweep the margin $m$, and deploy zero-shot. The third is a reachability shield in the style of Sim-to-Lab-to-Real, with a backup controller trained in simulation that overrides unsafe actions during target fine-tuning. That paper reduced training violations by 4% to 77% relative to SQRL and Recovery RL. The fourth is RGAT-grounded simulation training followed by PI-Lagrangian. Its target data collection is unconstrained, so we count the violations that collection incurs. As an upper reference, we run FDPI directly on the target, which is permitted to violate. This gives the reward a method can reach when real violations are free.

All methods receive the same simulator budget, the same target interaction budget, the same number of hyperparameter trials (tuned on a held-out perturbation, not the test perturbations), and the same network sizes and seeds.

Ablations isolate each ingredient. One trains the simulated critic without the violation-seeking dual policy, which tests whether borrowed hazards matter. One replaces the residual with direct fine-tuning of the critic on target data. One drops the uncertainty term. One keeps our corrected critic for policy updates but drives the multiplier with raw target cost, which isolates the measurement claim. We count the proposal as winning if it meets two conditions at equal target interaction budget. First, its target-side cumulative violations must be below those of raw-cost PI-Lagrangian fine-tuning by a margin exceeding seed variance. Second, its final reward at matched target cost must exceed the best margin setting of domain randomization.

Why not just…?

Why not just tune a static penalty on cost and train off-policy? A fixed weight $\beta$ on cost is either too small, so the agent accepts violations, or too large, so it stops moving. On MetaDrive, SafeDreamer reports PPO reward shaping at an average episode cost of 14.1 against 2.2 for constrained planning. Across a reality gap, a static weight has a further problem: the correct value changes when dynamics change, and nothing in the method detects that change. We include a swept static penalty as a cheap baseline, but we do not expect it to compete.

Why not just domain-randomize and tighten the budget? This is the most serious threat. A wide envelope plus a safety margin might match our safety with no target data at all. The literature says randomization tends toward conservatism (Sim-to-Lab-to-Real notes it offers no guarantees against unmodeled shifts), but that is not decisive. Our experiments settle the question by plotting the full reward-versus-target-cost curve across margins. If tightened-budget randomization lies on or above our curve, the proposal fails on that gap, and we will report it. We expect our method's advantage to appear for gaps that are large or structured, such as actuator delays, where a margin wide enough to stay safe gives up a great deal of reward.

Why not just fine-tune the simulated cost critic on real data? Real data from a safe policy contains almost no violations. Plain temporal-difference fine-tuning with a shared network can overwrite the boundary knowledge in regions the real policy never visits. This is the same erasure that Hazard Compression documents for replay models, where the hazard rate in generated data fell from 13.3% to 0.6%. Freezing the simulated critic and learning a residual anchored at zero is designed to prevent this. The naive fine-tuning ablation, scored by boundary calibration error at unvisited states, tests the point directly.

Why not just put a shield in front of PI-Lagrangian fine-tuning? A shield prevents violations but does not correct the critic or the multiplier. Raw target costs stay near zero under shielding, so the multiplier decays and the policy keeps pushing into states where the shield must intervene. The result is a policy that depends on its backup controller. We run shield plus PI-Lagrangian as a baseline and log intervention rate as well as cost. Our method can also sit inside a shield, so the comparison asks whether a corrected measurement lowers interventions and raises reward.

Decisive Checks

The first check is calibration. On each perturbed target, the residual-corrected critic should predict violation rates at unvisited boundary states better than both the simulated critic alone and the naively fine-tuned critic. If it does not, the mechanism is wrong regardless of downstream reward.

The second check is the measurement ablation. Driving the multiplier with $\hat C$ instead of raw target cost should reduce multiplier overshoot at the moment of transfer and reduce target violations during fine-tuning, even when the controller is held fixed at the augmented-Lagrangian setting. That result would support the claim that the measurement, not the controller, is the bottleneck.

The third check is gap scaling with a designed failure case. Benefits should grow with the size of the dynamics gap. They should vanish when we instead move the hazards themselves, shifting obstacle positions between source and target, because then the simulated critic's hazard geometry is wrong and nothing remains to borrow. Seeing that predicted failure would confirm what the method actually relies on.

As a sanity anchor, the tabular Gridworld run should approach the reported 0% unsafe iterations and the 77,958 versus 296,910 sample comparison of Provably Safe Sim-to-Real Transfer when the residual is replaced by an exact table.