Skip to content

Commit b923dec

Browse files
committed
Created using Colab
1 parent c1f83e9 commit b923dec

File tree

1 file changed

+206
-150
lines changed

1 file changed

+206
-150
lines changed

python/rollout.ipynb

+206-150
Original file line numberDiff line numberDiff line change
@@ -1,152 +1,208 @@
11
{
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-
]
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "6adc68e0-a943-44ab-9af5-4bc62cc19f34",
6+
"metadata": {
7+
"id": "6adc68e0-a943-44ab-9af5-4bc62cc19f34"
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": "c14f80a6-17c3-4199-a822-7ea48964b7ca"
39+
},
40+
"source": [
41+
"# All imports"
42+
]
43+
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": 1,
47+
"id": "0f9fbad1-59d0-40ac-b2b6-99f37313670f",
48+
"metadata": {
49+
"id": "0f9fbad1-59d0-40ac-b2b6-99f37313670f",
50+
"outputId": "5f9f08f7-3a21-4b84-be37-a08dff819541",
51+
"colab": {
52+
"base_uri": "https://localhost:8080/",
53+
"height": 738
54+
}
55+
},
56+
"outputs": [
57+
{
58+
"output_type": "stream",
59+
"name": "stdout",
60+
"text": [
61+
"Collecting mujoco\n",
62+
" Downloading mujoco-3.2.7-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (44 kB)\n",
63+
"\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/44.4 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m44.4/44.4 kB\u001b[0m \u001b[31m1.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
64+
"\u001b[?25hRequirement already satisfied: absl-py in /usr/local/lib/python3.11/dist-packages (from mujoco) (1.4.0)\n",
65+
"Requirement already satisfied: etils[epath] in /usr/local/lib/python3.11/dist-packages (from mujoco) (1.11.0)\n",
66+
"Collecting glfw (from mujoco)\n",
67+
" Downloading glfw-2.8.0-py2.py27.py3.py30.py31.py32.py33.py34.py35.py36.py37.py38.p39.p310.p311.p312.p313-none-manylinux_2_28_x86_64.whl.metadata (5.4 kB)\n",
68+
"Requirement already satisfied: numpy in /usr/local/lib/python3.11/dist-packages (from mujoco) (1.26.4)\n",
69+
"Requirement already satisfied: pyopengl in /usr/local/lib/python3.11/dist-packages (from mujoco) (3.1.7)\n",
70+
"Requirement already satisfied: fsspec in /usr/local/lib/python3.11/dist-packages (from etils[epath]->mujoco) (2024.10.0)\n",
71+
"Requirement already satisfied: importlib_resources in /usr/local/lib/python3.11/dist-packages (from etils[epath]->mujoco) (6.5.2)\n",
72+
"Requirement already satisfied: typing_extensions in /usr/local/lib/python3.11/dist-packages (from etils[epath]->mujoco) (4.12.2)\n",
73+
"Requirement already satisfied: zipp in /usr/local/lib/python3.11/dist-packages (from etils[epath]->mujoco) (3.21.0)\n",
74+
"Downloading mujoco-3.2.7-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (6.4 MB)\n",
75+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m6.4/6.4 MB\u001b[0m \u001b[31m23.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
76+
"\u001b[?25hDownloading glfw-2.8.0-py2.py27.py3.py30.py31.py32.py33.py34.py35.py36.py37.py38.p39.p310.p311.p312.p313-none-manylinux_2_28_x86_64.whl (243 kB)\n",
77+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m243.4/243.4 kB\u001b[0m \u001b[31m13.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
78+
"\u001b[?25hInstalling collected packages: glfw, mujoco\n",
79+
"Successfully installed glfw-2.8.0 mujoco-3.2.7\n"
80+
]
81+
},
82+
{
83+
"output_type": "error",
84+
"ename": "ModuleNotFoundError",
85+
"evalue": "No module named 'osc'",
86+
"traceback": [
87+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
88+
"\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
89+
"\u001b[0;32m<ipython-input-1-7c86f6e5a05f>\u001b[0m in \u001b[0;36m<cell line: 0>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mgoogle\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcolab\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mfiles\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mdistutils\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mutil\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mosc\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 7\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0msubprocess\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0msubprocess\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'nvidia-smi'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreturncode\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
90+
"\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'osc'",
91+
"",
92+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0;32m\nNOTE: If your import is failing due to a missing package, you can\nmanually install dependencies using either !pip or !apt.\n\nTo view examples of installing some common dependencies, click the\n\"Open Examples\" button below.\n\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n"
93+
],
94+
"errorDetails": {
95+
"actions": [
96+
{
97+
"action": "open_url",
98+
"actionText": "Open Examples",
99+
"url": "/notebooks/snippets/importing_libraries.ipynb"
100+
}
101+
]
102+
}
103+
}
104+
],
105+
"source": [
106+
"!pip install mujoco\n",
107+
"\n",
108+
"# Set up GPU rendering.\n",
109+
"from google.colab import files\n",
110+
"import distutils.util\n",
111+
"import osc\n",
112+
"import subprocess\n",
113+
"if subprocess.run('nvidia-smi').returncode:\n",
114+
" raise RuntimeError(\n",
115+
" 'Cannot communicate with GPU. '\n",
116+
" 'Make sure you are using a GPU Colab runtime. '\n",
117+
" 'Go to the Runtime menu and select Choose runtime type.')\n",
118+
"\n",
119+
"# Add an ICD config so that glvnd can pick up the Nvidia EGL driver.\n",
120+
"# This is usually installed as part of an Nvidia driver package, but the Colab\n",
121+
"# kernel doesn't install its driver via APT, and as a result the ICD is missing.\n",
122+
"# (https://github.com/NVIDIA/libglvnd/blob/master/src/EGL/icd_enumeration.md)\n",
123+
"NVIDIA_ICD_CONFIG_PATH = '/usr/share/glvnd/egl_vendor.d/10_nvidia.json'\n",
124+
"if not os.path.exists(NVIDIA_ICD_CONFIG_PATH):\n",
125+
" with open(NVIDIA_ICD_CONFIG_PATH, 'w') as f:\n",
126+
" f.write(\"\"\"{\n",
127+
" \"file_format_version\" : \"1.0.0\",\n",
128+
" \"ICD\" : {\n",
129+
" \"library_path\" : \"libEGL_nvidia.so.0\"\n",
130+
" }\n",
131+
"}\n",
132+
"\"\"\")\n",
133+
"\n",
134+
"# Configure MuJoCo to use the EGL rendering backend (requires GPU)\n",
135+
"print('Setting environment variable to use GPU rendering:')\n",
136+
"%env MUJOCO_GL=egl\n",
137+
"\n",
138+
"# Check if installation was succesful.\n",
139+
"try:\n",
140+
" print('Checking that the installation succeeded:')\n",
141+
" import mujoco\n",
142+
" mujoco.MjModel.from_xml_string('<mujoco/>')\n",
143+
"except Exception as e:\n",
144+
" raise e from RuntimeError(\n",
145+
" 'Something went wrong during installation. Check the shell output above '\n",
146+
" 'for more information.\\n'\n",
147+
" 'If using a hosted Colab runtime, make sure you enable GPU acceleration '\n",
148+
" 'by going to the Runtime menu and selecting \"Choose runtime type\".')\n",
149+
"\n",
150+
"print('Installation successful.')\n",
151+
"\n",
152+
"# Other imports and helper functions\n",
153+
"import time\n",
154+
"import itertools\n",
155+
"import numpy as np\n",
156+
"\n",
157+
"# Graphics and plotting.\n",
158+
"print('Installing mediapy:')\n",
159+
"!command -v ffmpeg >/dev/null || (apt update && apt install -y ffmpeg)\n",
160+
"!pip install -q mediapy\n",
161+
"import mediapy as media\n",
162+
"import matplotlib.pyplot as plt\n",
163+
"\n",
164+
"# More legible printing from numpy.\n",
165+
"np.set_printoptions(precision=3, suppress=True, linewidth=100)\n",
166+
"\n",
167+
"from IPython.display import clear_output\n",
168+
"clear_output()\n"
169+
]
170+
},
171+
{
172+
"cell_type": "markdown",
173+
"id": "0de33571-592a-4021-a970-86224a7dc4da",
174+
"metadata": {
175+
"id": "0de33571-592a-4021-a970-86224a7dc4da"
176+
},
177+
"source": [
178+
"# `rollout.rollout`\n",
179+
"\n",
180+
"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."
181+
]
182+
}
183+
],
184+
"metadata": {
185+
"kernelspec": {
186+
"display_name": "Python 3 (ipykernel)",
187+
"language": "python",
188+
"name": "python3"
189+
},
190+
"language_info": {
191+
"codemirror_mode": {
192+
"name": "ipython",
193+
"version": 3
194+
},
195+
"file_extension": ".py",
196+
"mimetype": "text/x-python",
197+
"name": "python",
198+
"nbconvert_exporter": "python",
199+
"pygments_lexer": "ipython3",
200+
"version": "3.10.12"
201+
},
202+
"colab": {
203+
"provenance": []
204+
}
33205
},
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-
}
206+
"nbformat": 4,
207+
"nbformat_minor": 5
208+
}

0 commit comments

Comments
 (0)