{ "cells": [ { "metadata": { "id": "MpkYHwCqk7W-" }, "cell_type": "markdown", "source": [ "# **MuJoCo** tutorial with `dm_control` Python bindings\n", "\n", "[](https://colab.research.google.com/github/google-deepmind/dm_control/blob/main/dm_control/mujoco/tutorial.ipynb)\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, { "metadata": { "id": "_UbO9uhtBSX5" }, "cell_type": "markdown", "source": [ ">
Copyright 2021 The dm_control Authors.
\n", ">Licensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
\n", ">Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
" ] }, { "metadata": { "id": "aThGKGp0cD76" }, "cell_type": "markdown", "source": [ "This notebook provides an overview tutorial of the **MuJoCo** physics simulator, using the `dm_control` Python bindings. It is similar to the notebook in `dm_control/tutorial.ipynb`, but focuses on teaching MuJoCo itself, rather than the additional features provided by the Python package. \n", "\n", "**A Colab runtime with GPU acceleration is required.** If you're using a CPU-only runtime, you can switch using the menu \"Runtime > Change runtime type\"." ] }, { "metadata": { "id": "YkBQUjm6gbGF" }, "cell_type": "markdown", "source": [ "" ] }, { "metadata": { "id": "YvyGCsgSCxHQ" }, "cell_type": "markdown", "source": [ "### Installing `dm_control` on Colab" ] }, { "metadata": { "id": "IbZxYDxzoz5R" }, "cell_type": "code", "source": [ "#@title Run to install MuJoCo and `dm_control`\n", "import distutils.util\n", "import os\n", "import subprocess\n", "if subprocess.run('nvidia-smi').returncode:\n", " raise RuntimeError(\n", " 'Cannot communicate with GPU. '\n", " 'Make sure you are using a GPU Colab runtime. '\n", " 'Go to the Runtime menu and select Choose runtime type.')\n", "\n", "# Add an ICD config so that glvnd can pick up the Nvidia EGL driver.\n", "# This is usually installed as part of an Nvidia driver package, but the Colab\n", "# kernel doesn't install its driver via APT, and as a result the ICD is missing.\n", "# (https://github.com/NVIDIA/libglvnd/blob/master/src/EGL/icd_enumeration.md)\n", "NVIDIA_ICD_CONFIG_PATH = '/usr/share/glvnd/egl_vendor.d/10_nvidia.json'\n", "if not os.path.exists(NVIDIA_ICD_CONFIG_PATH):\n", " with open(NVIDIA_ICD_CONFIG_PATH, 'w') as f:\n", " f.write(\"\"\"{\n", " \"file_format_version\" : \"1.0.0\",\n", " \"ICD\" : {\n", " \"library_path\" : \"libEGL_nvidia.so.0\"\n", " }\n", "}\n", "\"\"\")\n", "\n", "print('Installing dm_control...')\n", "!pip install -q dm_control>=1.0.39\n", "\n", "# Configure dm_control to use the EGL rendering backend (requires GPU)\n", "%env MUJOCO_GL=egl\n", "\n", "print('Checking that the dm_control installation succeeded...')\n", "try:\n", " from dm_control import suite\n", " env = suite.load('cartpole', 'swingup')\n", " pixels = env.physics.render()\n", "except Exception as e:\n", " raise e from RuntimeError(\n", " 'Something went wrong during installation. Check the shell output above '\n", " 'for more information.\\n'\n", " 'If using a hosted Colab runtime, make sure you enable GPU acceleration '\n", " 'by going to the Runtime menu and selecting \"Choose runtime type\".')\n", "else:\n", " del suite, pixels\n", "print('dm_control installation succeeded.')" ], "outputs": [], "execution_count": null }, { "metadata": { "id": "wtDN43hIJh2C" }, "cell_type": "markdown", "source": [ "# Imports\n", "\n", "Run both of these cells:" ] }, { "metadata": { "id": "T5f4w3Kq2X14" }, "cell_type": "code", "source": [ "#@title All `dm_control` imports required for this tutorial\n", "\n", "# The basic mujoco wrapper.\n", "from dm_control import mujoco\n", "\n", "# Access to enums and MuJoCo library functions.\n", "from dm_control.mujoco.wrapper.mjbindings import enums\n", "from dm_control.mujoco.wrapper.mjbindings import mjlib\n", "\n", "# Composer high level imports\n", "from dm_control import composer\n", "from dm_control.composer.observation import observable\n", "from dm_control.composer import variation\n", "\n", "# Imports for Composer tutorial example\n", "from dm_control.composer.variation import distributions\n", "from dm_control.composer.variation import noises\n", "from dm_control.locomotion.arenas import floors\n", "\n", "# Control Suite\n", "from dm_control import suite\n", "\n", "# Run through corridor example\n", "from dm_control.locomotion.walkers import cmu_humanoid\n", "from dm_control.locomotion.arenas import corridors as corridor_arenas\n", "from dm_control.locomotion.tasks import corridors as corridor_tasks\n", "\n", "# Soccer\n", "from dm_control.locomotion import soccer\n", "\n", "# Manipulation\n", "from dm_control import manipulation" ], "outputs": [], "execution_count": null }, { "metadata": { "id": "gKc1FNhKiVJX" }, "cell_type": "code", "source": [ "#@title Other imports and helper functions\n", "\n", "# General\n", "import copy\n", "import os\n", "import time\n", "import itertools\n", "from IPython.display import clear_output\n", "import numpy as np\n", "\n", "# Graphics-related\n", "import matplotlib\n", "import matplotlib.animation as animation\n", "import matplotlib.pyplot as plt\n", "from IPython.display import HTML\n", "import PIL.Image\n", "# Internal loading of video libraries.\n", "\n", "# Use svg backend for figure rendering\n", "%config InlineBackend.figure_format = 'svg'\n", "\n", "# Font sizes\n", "SMALL_SIZE = 8\n", "MEDIUM_SIZE = 10\n", "BIGGER_SIZE = 12\n", "plt.rc('font', size=SMALL_SIZE) # controls default text sizes\n", "plt.rc('axes', titlesize=SMALL_SIZE) # fontsize of the axes title\n", "plt.rc('axes', labelsize=MEDIUM_SIZE) # fontsize of the x and y labels\n", "plt.rc('xtick', labelsize=SMALL_SIZE) # fontsize of the tick labels\n", "plt.rc('ytick', labelsize=SMALL_SIZE) # fontsize of the tick labels\n", "plt.rc('legend', fontsize=SMALL_SIZE) # legend fontsize\n", "plt.rc('figure', titlesize=BIGGER_SIZE) # fontsize of the figure title\n", "\n", "# Inline video helper function\n", "if os.environ.get('COLAB_NOTEBOOK_TEST', False):\n", " # We skip video generation during tests, as it is quite expensive.\n", " display_video = lambda *args, **kwargs: None\n", "else:\n", " def display_video(frames, framerate=30):\n", " height, width, _ = frames[0].shape\n", " dpi = 70\n", " orig_backend = matplotlib.get_backend()\n", " matplotlib.use('Agg') # Switch to headless 'Agg' to inhibit figure rendering.\n", " fig, ax = plt.subplots(1, 1, figsize=(width / dpi, height / dpi), dpi=dpi)\n", " matplotlib.use(orig_backend) # Switch back to the original backend.\n", " ax.set_axis_off()\n", " ax.set_aspect('equal')\n", " ax.set_position([0, 0, 1, 1])\n", " im = ax.imshow(frames[0])\n", " def update(frame):\n", " im.set_data(frame)\n", " return [im]\n", " interval = 1000/framerate\n", " anim = animation.FuncAnimation(fig=fig, func=update, frames=frames,\n", " interval=interval, blit=True, repeat=False)\n", " return HTML(anim.to_html5_video())\n", "\n", "# Seed numpy's global RNG so that cell outputs are deterministic. We also try to\n", "# use RandomState instances that are local to a single cell wherever possible.\n", "np.random.seed(42)" ], "outputs": [], "execution_count": null }, { "metadata": { "id": "jZXz9rPYGA-Y" }, "cell_type": "markdown", "source": [ "# Model definition, compilation and rendering\n", "\n" ] }, { "metadata": { "id": "MRBaZsf1d7Gb" }, "cell_type": "markdown", "source": [ "We begin by describing some basic concepts of the [MuJoCo](http://mujoco.org/) physics simulation library, but recommend the [official documentation](http://mujoco.org/book/) for details.\n", "\n", "Let's define a simple model with two geoms and a light." ] }, { "metadata": { "id": "ZS2utl59ZTsr" }, "cell_type": "code", "source": [ "#@title A static model {vertical-output: true}\n", "\n", "static_model = \"\"\"\n", "