Skip to content

Commit c1f83e9

Browse files
committed
rollout notebook boilerplate
1 parent 699a676 commit c1f83e9

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed

python/rollout.ipynb

+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "6adc68e0-a943-44ab-9af5-4bc62cc19f34",
6+
"metadata": {
7+
"id": "MpkYHwCqk7W-"
8+
},
9+
"source": [
10+
"![MuJoCo banner](https://raw.githubusercontent.com/google-deepmind/mujoco/main/banner.png)\n",
11+
"\n",
12+
"# <h1><center>Rollout Tutorial <a href=\"https://colab.research.google.com/github/google-deepmind/mujoco/blob/main/python/rollout.ipynb\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" width=\"140\" align=\"center\"/></a></center></h1>\n",
13+
"\n",
14+
"This notebook provides a tutorial for [**MuJoCo** physics](https://github.com/google-deepmind/mujoco#readme), using the native Python bindings.\n",
15+
"\n",
16+
"This notebook describes a utility included in the MuJoCo Python library that performs simulation \"rollouts\" with an underlying C++ function. The rollouts can be multithreaded. We provide examples of the speed of rollout compared to pure Python rollouts and MJX and give an application example that couples \"rollout\" with MuJoCo's \"minimze\" utility.\n",
17+
"\n",
18+
"<!-- Copyright 2025 DeepMind Technologies Limited\n",
19+
"\n",
20+
" Licensed under the Apache License, Version 2.0 (the \"License\");\n",
21+
" you may not use this file except in compliance with the License.\n",
22+
" You may obtain a copy of the License at\n",
23+
"\n",
24+
" http://www.apache.org/licenses/LICENSE-2.0\n",
25+
"\n",
26+
" Unless required by applicable law or agreed to in writing, software\n",
27+
" distributed under the License is distributed on an \"AS IS\" BASIS,\n",
28+
" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
29+
" See the License for the specific language governing permissions and\n",
30+
" limitations under the License.\n",
31+
"-->"
32+
]
33+
},
34+
{
35+
"cell_type": "markdown",
36+
"id": "c14f80a6-17c3-4199-a822-7ea48964b7ca",
37+
"metadata": {
38+
"id": "YvyGCsgSCxHQ"
39+
},
40+
"source": [
41+
"# All imports"
42+
]
43+
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": null,
47+
"id": "0f9fbad1-59d0-40ac-b2b6-99f37313670f",
48+
"metadata": {
49+
"id": "Xqo7pyX-n72M"
50+
},
51+
"outputs": [],
52+
"source": [
53+
"!pip install mujoco\n",
54+
"\n",
55+
"# Set up GPU rendering.\n",
56+
"from google.colab import files\n",
57+
"import distutils.util\n",
58+
"import osc\n",
59+
"import subprocess\n",
60+
"if subprocess.run('nvidia-smi').returncode:\n",
61+
" raise RuntimeError(\n",
62+
" 'Cannot communicate with GPU. '\n",
63+
" 'Make sure you are using a GPU Colab runtime. '\n",
64+
" 'Go to the Runtime menu and select Choose runtime type.')\n",
65+
"\n",
66+
"# Add an ICD config so that glvnd can pick up the Nvidia EGL driver.\n",
67+
"# This is usually installed as part of an Nvidia driver package, but the Colab\n",
68+
"# kernel doesn't install its driver via APT, and as a result the ICD is missing.\n",
69+
"# (https://github.com/NVIDIA/libglvnd/blob/master/src/EGL/icd_enumeration.md)\n",
70+
"NVIDIA_ICD_CONFIG_PATH = '/usr/share/glvnd/egl_vendor.d/10_nvidia.json'\n",
71+
"if not os.path.exists(NVIDIA_ICD_CONFIG_PATH):\n",
72+
" with open(NVIDIA_ICD_CONFIG_PATH, 'w') as f:\n",
73+
" f.write(\"\"\"{\n",
74+
" \"file_format_version\" : \"1.0.0\",\n",
75+
" \"ICD\" : {\n",
76+
" \"library_path\" : \"libEGL_nvidia.so.0\"\n",
77+
" }\n",
78+
"}\n",
79+
"\"\"\")\n",
80+
"\n",
81+
"# Configure MuJoCo to use the EGL rendering backend (requires GPU)\n",
82+
"print('Setting environment variable to use GPU rendering:')\n",
83+
"%env MUJOCO_GL=egl\n",
84+
"\n",
85+
"# Check if installation was succesful.\n",
86+
"try:\n",
87+
" print('Checking that the installation succeeded:')\n",
88+
" import mujoco\n",
89+
" mujoco.MjModel.from_xml_string('<mujoco/>')\n",
90+
"except Exception as e:\n",
91+
" raise e from RuntimeError(\n",
92+
" 'Something went wrong during installation. Check the shell output above '\n",
93+
" 'for more information.\\n'\n",
94+
" 'If using a hosted Colab runtime, make sure you enable GPU acceleration '\n",
95+
" 'by going to the Runtime menu and selecting \"Choose runtime type\".')\n",
96+
"\n",
97+
"print('Installation successful.')\n",
98+
"\n",
99+
"# Other imports and helper functions\n",
100+
"import time\n",
101+
"import itertools\n",
102+
"import numpy as np\n",
103+
"\n",
104+
"# Graphics and plotting.\n",
105+
"print('Installing mediapy:')\n",
106+
"!command -v ffmpeg >/dev/null || (apt update && apt install -y ffmpeg)\n",
107+
"!pip install -q mediapy\n",
108+
"import mediapy as media\n",
109+
"import matplotlib.pyplot as plt\n",
110+
"\n",
111+
"# More legible printing from numpy.\n",
112+
"np.set_printoptions(precision=3, suppress=True, linewidth=100)\n",
113+
"\n",
114+
"from IPython.display import clear_output\n",
115+
"clear_output()\n"
116+
]
117+
},
118+
{
119+
"cell_type": "markdown",
120+
"id": "0de33571-592a-4021-a970-86224a7dc4da",
121+
"metadata": {
122+
"id": "nqqtSIkGe-ay"
123+
},
124+
"source": [
125+
"# `rollout.rollout`\n",
126+
"\n",
127+
"The `rollout.rollout` function in the `mujoco` Python library runs the simulation for a fixed number steps. It can run in single or multi-threaded modes. The speedup over pure Python is significant."
128+
]
129+
}
130+
],
131+
"metadata": {
132+
"kernelspec": {
133+
"display_name": "Python 3 (ipykernel)",
134+
"language": "python",
135+
"name": "python3"
136+
},
137+
"language_info": {
138+
"codemirror_mode": {
139+
"name": "ipython",
140+
"version": 3
141+
},
142+
"file_extension": ".py",
143+
"mimetype": "text/x-python",
144+
"name": "python",
145+
"nbconvert_exporter": "python",
146+
"pygments_lexer": "ipython3",
147+
"version": "3.10.12"
148+
}
149+
},
150+
"nbformat": 4,
151+
"nbformat_minor": 5
152+
}

0 commit comments

Comments
 (0)