1e6: return 1e12 # the policy diverged\n return J\nREAL=np.linspace(0.5,2.5,21) # what reality's gain might actually be\nKgrid=np.linspace(-1.2,0.0,121)\ndef best_K(train_gains):\n tg=np.atleast_1d(np.asarray(train_gains,float))\n J=[np.mean([cost(K,g) for g in tg]) for K in Kgrid]\n return float(Kgrid[int(np.argmin(J))])\ndef worst_real(K): return max(cost(K,g) for g in REAL)\nprint(\"cost, the reality range, and a policy search are defined\")","label":"Setup · cost, the reality range, a policy search"},{"code":"# Train the policy in SIMULATION only, at the nominal gain g=1. It looks perfect...\nKnom=best_K([1.0])\nprint(f\"sim-tuned policy K={Knom:.2f} cost at nominal g=1: {cost(Knom,1.0):.3f}\")\n# ...until reality's gain differs. Check the WORST case across the real range:\nprint(f\"worst-case cost on real hardware: {worst_real(Knom):.3g} (huge = it blew up)\")","label":"Tune in sim only -> brittle on hardware"},{"code":"# Domain randomization: train the SAME policy across a whole RANGE of gains, so it\n# is robust to whichever one reality hands you. Set the training range.\nTRAIN_GAINS = [1.0] # <- TODO: nominal only is brittle. Randomize: np.linspace(0.5,2.5,21)\nKdr=best_K(TRAIN_GAINS)\nprint(f\"policy K={Kdr:.2f} worst-case real cost {worst_real(Kdr):.3g} (nominal-only was {worst_real(Knom):.3g})\")\nassert worst_real(Kdr) < 0.5*worst_real(Knom), \"randomize the training gains (np.linspace(0.5,2.5,21)) so the policy survives reality\"\nprint(\"PASS - domain randomization made the policy robust to the reality gap.\")","label":"Domain randomization -> robust · set TRAIN_GAINS"}],"intro":"Watch a sim-tuned policy fail across the reality gap, then use domain randomization to make it robust.","key":"robot-learning/sim-to-real-gap","kind":"python","title":"Crossing the Reality Gap"}">
PYTHON · NUMPY · IN-BROWSER

Crossing the Reality Gap

Watch a sim-tuned policy fail across the reality gap, then use domain randomization to make it robust.

You read

the arrays and values already in scope

You change

the code you write in each cell

Fixed

the dataset and the checks that grade you