diff --git a/GoogleColab/google_collab_example.ipynb b/GoogleColab/google_collab_example.ipynb new file mode 100644 index 0000000000..be78428708 --- /dev/null +++ b/GoogleColab/google_collab_example.ipynb @@ -0,0 +1,880 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "google_collab_example.ipynb", + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "K_o-h6q0Z0kj", + "colab_type": "text" + }, + "source": [ + "# Unity ML-Agents Toolkit for Google Colab\n", + "## Environment Basics\n", + "\n", + "This notebook contains a walkthrough of the basic functions of the Python API for the Unity ML-Agents toolkit in Google Colab environment. For instructions on building a Unity environment, see [here](https://github.com/Unity-Technologies/ml-agents/blob/master/docs/Learning-Environment-Create-New.md)." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "H5S7mbFraM-q", + "colab_type": "text" + }, + "source": [ + "## 1. Required system level packages & download repository to user workspace\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "fp5Y-mYRZsk_", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 1000 + }, + "outputId": "d2e64ea9-f1e6-40c6-9e1c-cc921acc5dc7" + }, + "source": [ + "!git clone --branch release_1 https://github.com/Unity-Technologies/ml-agents.git \n", + "!git clone https://github.com/ugurkanates/MLAgents-Google-Collab.git\n", + "!apt install python-opengl\n", + "!apt install ffmpeg\n", + "!apt install xvfb\n", + "!apt install x11-utils\n" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Cloning into 'ml-agents'...\n", + "remote: Enumerating objects: 86, done.\u001b[K\n", + "remote: Counting objects: 1% (1/86)\u001b[K\rremote: Counting objects: 2% (2/86)\u001b[K\rremote: Counting objects: 3% (3/86)\u001b[K\rremote: Counting objects: 4% (4/86)\u001b[K\rremote: Counting objects: 5% (5/86)\u001b[K\rremote: Counting objects: 6% (6/86)\u001b[K\rremote: Counting objects: 8% (7/86)\u001b[K\rremote: Counting objects: 9% (8/86)\u001b[K\rremote: Counting objects: 10% (9/86)\u001b[K\rremote: Counting objects: 11% (10/86)\u001b[K\rremote: Counting objects: 12% (11/86)\u001b[K\rremote: Counting objects: 13% (12/86)\u001b[K\rremote: Counting objects: 15% (13/86)\u001b[K\rremote: Counting objects: 16% (14/86)\u001b[K\rremote: Counting objects: 17% (15/86)\u001b[K\rremote: Counting objects: 18% (16/86)\u001b[K\rremote: Counting objects: 19% (17/86)\u001b[K\rremote: Counting objects: 20% (18/86)\u001b[K\rremote: Counting objects: 22% (19/86)\u001b[K\rremote: Counting objects: 23% (20/86)\u001b[K\rremote: Counting objects: 24% (21/86)\u001b[K\rremote: Counting objects: 25% (22/86)\u001b[K\rremote: Counting objects: 26% (23/86)\u001b[K\rremote: Counting objects: 27% (24/86)\u001b[K\rremote: Counting objects: 29% (25/86)\u001b[K\rremote: Counting objects: 30% (26/86)\u001b[K\rremote: Counting objects: 31% (27/86)\u001b[K\rremote: Counting objects: 32% (28/86)\u001b[K\rremote: Counting objects: 33% (29/86)\u001b[K\rremote: Counting objects: 34% (30/86)\u001b[K\rremote: Counting objects: 36% (31/86)\u001b[K\rremote: Counting objects: 37% (32/86)\u001b[K\rremote: Counting objects: 38% (33/86)\u001b[K\rremote: Counting objects: 39% (34/86)\u001b[K\rremote: Counting objects: 40% (35/86)\u001b[K\rremote: Counting objects: 41% (36/86)\u001b[K\rremote: Counting objects: 43% (37/86)\u001b[K\rremote: Counting objects: 44% (38/86)\u001b[K\rremote: Counting objects: 45% (39/86)\u001b[K\rremote: Counting objects: 46% (40/86)\u001b[K\rremote: Counting objects: 47% (41/86)\u001b[K\rremote: Counting objects: 48% (42/86)\u001b[K\rremote: Counting objects: 50% (43/86)\u001b[K\rremote: Counting objects: 51% (44/86)\u001b[K\rremote: Counting objects: 52% (45/86)\u001b[K\rremote: Counting objects: 53% (46/86)\u001b[K\rremote: Counting objects: 54% (47/86)\u001b[K\rremote: Counting objects: 55% (48/86)\u001b[K\rremote: Counting objects: 56% (49/86)\u001b[K\rremote: Counting objects: 58% (50/86)\u001b[K\rremote: Counting objects: 59% (51/86)\u001b[K\rremote: Counting objects: 60% (52/86)\u001b[K\rremote: Counting objects: 61% (53/86)\u001b[K\rremote: Counting objects: 62% (54/86)\u001b[K\rremote: Counting objects: 63% (55/86)\u001b[K\rremote: Counting objects: 65% (56/86)\u001b[K\rremote: Counting objects: 66% (57/86)\u001b[K\rremote: Counting objects: 67% (58/86)\u001b[K\rremote: Counting objects: 68% (59/86)\u001b[K\rremote: Counting objects: 69% (60/86)\u001b[K\rremote: Counting objects: 70% (61/86)\u001b[K\rremote: Counting objects: 72% (62/86)\u001b[K\rremote: Counting objects: 73% (63/86)\u001b[K\rremote: Counting objects: 74% (64/86)\u001b[K\rremote: Counting objects: 75% (65/86)\u001b[K\rremote: Counting objects: 76% (66/86)\u001b[K\rremote: Counting objects: 77% (67/86)\u001b[K\rremote: Counting objects: 79% (68/86)\u001b[K\rremote: Counting objects: 80% (69/86)\u001b[K\rremote: Counting objects: 81% (70/86)\u001b[K\rremote: Counting objects: 82% (71/86)\u001b[K\rremote: Counting objects: 83% (72/86)\u001b[K\rremote: Counting objects: 84% (73/86)\u001b[K\rremote: Counting objects: 86% (74/86)\u001b[K\rremote: Counting objects: 87% (75/86)\u001b[K\rremote: Counting objects: 88% (76/86)\u001b[K\rremote: Counting objects: 89% (77/86)\u001b[K\rremote: Counting objects: 90% (78/86)\u001b[K\rremote: Counting objects: 91% (79/86)\u001b[K\rremote: Counting objects: 93% (80/86)\u001b[K\rremote: Counting objects: 94% (81/86)\u001b[K\rremote: Counting objects: 95% (82/86)\u001b[K\rremote: Counting objects: 96% (83/86)\u001b[K\rremote: Counting objects: 97% (84/86)\u001b[K\rremote: Counting objects: 98% (85/86)\u001b[K\rremote: Counting objects: 100% (86/86)\u001b[K\rremote: Counting objects: 100% (86/86), done.\u001b[K\n", + "remote: Compressing objects: 100% (68/68), done.\u001b[K\n", + "remote: Total 37503 (delta 32), reused 46 (delta 18), pack-reused 37417\u001b[K\n", + "Receiving objects: 100% (37503/37503), 463.47 MiB | 32.59 MiB/s, done.\n", + "Resolving deltas: 100% (26716/26716), done.\n", + "Note: checking out '92163c8031a90891962baa12089e35187b8093b6'.\n", + "\n", + "You are in 'detached HEAD' state. You can look around, make experimental\n", + "changes and commit them, and you can discard any commits you make in this\n", + "state without impacting any branches by performing another checkout.\n", + "\n", + "If you want to create a new branch to retain commits you create, you may\n", + "do so (now or later) by using -b with the checkout command again. Example:\n", + "\n", + " git checkout -b \n", + "\n", + "Cloning into 'MLAgents-Google-Collab'...\n", + "remote: Enumerating objects: 167, done.\u001b[K\n", + "remote: Counting objects: 100% (167/167), done.\u001b[K\n", + "remote: Compressing objects: 100% (117/117), done.\u001b[K\n", + "remote: Total 167 (delta 39), reused 163 (delta 37), pack-reused 0\u001b[K\n", + "Receiving objects: 100% (167/167), 32.06 MiB | 19.39 MiB/s, done.\n", + "Resolving deltas: 100% (39/39), done.\n", + "Checking out files: 100% (264/264), done.\n", + "Reading package lists... Done\n", + "Building dependency tree \n", + "Reading state information... Done\n", + "Suggested packages:\n", + " libgle3\n", + "The following NEW packages will be installed:\n", + " python-opengl\n", + "0 upgraded, 1 newly installed, 0 to remove and 31 not upgraded.\n", + "Need to get 496 kB of archives.\n", + "After this operation, 5,416 kB of additional disk space will be used.\n", + "Get:1 http://archive.ubuntu.com/ubuntu bionic/universe amd64 python-opengl all 3.1.0+dfsg-1 [496 kB]\n", + "Fetched 496 kB in 1s (770 kB/s)\n", + "Selecting previously unselected package python-opengl.\n", + "(Reading database ... 144433 files and directories currently installed.)\n", + "Preparing to unpack .../python-opengl_3.1.0+dfsg-1_all.deb ...\n", + "Unpacking python-opengl (3.1.0+dfsg-1) ...\n", + "Setting up python-opengl (3.1.0+dfsg-1) ...\n", + "Reading package lists... Done\n", + "Building dependency tree \n", + "Reading state information... Done\n", + "ffmpeg is already the newest version (7:3.4.6-0ubuntu0.18.04.1).\n", + "0 upgraded, 0 newly installed, 0 to remove and 31 not upgraded.\n", + "Reading package lists... Done\n", + "Building dependency tree \n", + "Reading state information... Done\n", + "The following NEW packages will be installed:\n", + " xvfb\n", + "0 upgraded, 1 newly installed, 0 to remove and 31 not upgraded.\n", + "Need to get 784 kB of archives.\n", + "After this operation, 2,266 kB of additional disk space will be used.\n", + "Get:1 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 xvfb amd64 2:1.19.6-1ubuntu4.4 [784 kB]\n", + "Fetched 784 kB in 1s (1,090 kB/s)\n", + "Selecting previously unselected package xvfb.\n", + "(Reading database ... 146788 files and directories currently installed.)\n", + "Preparing to unpack .../xvfb_2%3a1.19.6-1ubuntu4.4_amd64.deb ...\n", + "Unpacking xvfb (2:1.19.6-1ubuntu4.4) ...\n", + "Setting up xvfb (2:1.19.6-1ubuntu4.4) ...\n", + "Processing triggers for man-db (2.8.3-2ubuntu0.1) ...\n", + "Reading package lists... Done\n", + "Building dependency tree \n", + "Reading state information... Done\n", + "The following additional packages will be installed:\n", + " libxxf86dga1\n", + "Suggested packages:\n", + " mesa-utils\n", + "The following NEW packages will be installed:\n", + " libxxf86dga1 x11-utils\n", + "0 upgraded, 2 newly installed, 0 to remove and 31 not upgraded.\n", + "Need to get 209 kB of archives.\n", + "After this operation, 711 kB of additional disk space will be used.\n", + "Get:1 http://archive.ubuntu.com/ubuntu bionic/main amd64 libxxf86dga1 amd64 2:1.1.4-1 [13.7 kB]\n", + "Get:2 http://archive.ubuntu.com/ubuntu bionic/main amd64 x11-utils amd64 7.7+3build1 [196 kB]\n", + "Fetched 209 kB in 1s (358 kB/s)\n", + "Selecting previously unselected package libxxf86dga1:amd64.\n", + "(Reading database ... 146795 files and directories currently installed.)\n", + "Preparing to unpack .../libxxf86dga1_2%3a1.1.4-1_amd64.deb ...\n", + "Unpacking libxxf86dga1:amd64 (2:1.1.4-1) ...\n", + "Selecting previously unselected package x11-utils.\n", + "Preparing to unpack .../x11-utils_7.7+3build1_amd64.deb ...\n", + "Unpacking x11-utils (7.7+3build1) ...\n", + "Setting up libxxf86dga1:amd64 (2:1.1.4-1) ...\n", + "Setting up x11-utils (7.7+3build1) ...\n", + "Processing triggers for man-db (2.8.3-2ubuntu0.1) ...\n", + "Processing triggers for libc-bin (2.27-3ubuntu1) ...\n", + "/sbin/ldconfig.real: /usr/local/lib/python3.6/dist-packages/ideep4py/lib/libmkldnn.so.0 is not a symbolic link\n", + "\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "AWWeUz9saTBG", + "colab_type": "text" + }, + "source": [ + "## 2. Installation of Python packages \n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "t0KzoFL-Z8OP", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 1000 + }, + "outputId": "49ae96fe-48c9-4046-fab5-9992dd12a4e2" + }, + "source": [ + "!pip install pyvirtualdisplay\n", + "!pip install -e ml-agents/ml-agents-envs/\n", + "!pip install -e ml-agents/ml-agents/\n", + "!pip install -e ml-agents/gym-unity/\n" + ], + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Collecting pyvirtualdisplay\n", + " Downloading https://files.pythonhosted.org/packages/69/ec/8221a07850d69fa3c57c02e526edd23d18c7c05d58ed103e3b19172757c1/PyVirtualDisplay-0.2.5-py2.py3-none-any.whl\n", + "Collecting EasyProcess\n", + " Downloading https://files.pythonhosted.org/packages/48/3c/75573613641c90c6d094059ac28adb748560d99bd27ee6f80cce398f404e/EasyProcess-0.3-py2.py3-none-any.whl\n", + "Installing collected packages: EasyProcess, pyvirtualdisplay\n", + "Successfully installed EasyProcess-0.3 pyvirtualdisplay-0.2.5\n", + "Obtaining file:///content/ml-agents/ml-agents-envs\n", + "Requirement already satisfied: cloudpickle in /usr/local/lib/python3.6/dist-packages (from mlagents-envs==0.16.0) (1.3.0)\n", + "Requirement already satisfied: grpcio>=1.11.0 in /usr/local/lib/python3.6/dist-packages (from mlagents-envs==0.16.0) (1.28.1)\n", + "Requirement already satisfied: numpy<2.0,>=1.14.1 in /usr/local/lib/python3.6/dist-packages (from mlagents-envs==0.16.0) (1.18.4)\n", + "Requirement already satisfied: Pillow>=4.2.1 in /usr/local/lib/python3.6/dist-packages (from mlagents-envs==0.16.0) (7.0.0)\n", + "Requirement already satisfied: protobuf>=3.6 in /usr/local/lib/python3.6/dist-packages (from mlagents-envs==0.16.0) (3.10.0)\n", + "Requirement already satisfied: six>=1.5.2 in /usr/local/lib/python3.6/dist-packages (from grpcio>=1.11.0->mlagents-envs==0.16.0) (1.12.0)\n", + "Requirement already satisfied: setuptools in /usr/local/lib/python3.6/dist-packages (from protobuf>=3.6->mlagents-envs==0.16.0) (46.3.0)\n", + "Installing collected packages: mlagents-envs\n", + " Running setup.py develop for mlagents-envs\n", + "Successfully installed mlagents-envs\n", + "Obtaining file:///content/ml-agents/ml-agents\n", + "Requirement already satisfied: grpcio>=1.11.0 in /usr/local/lib/python3.6/dist-packages (from mlagents==0.16.0) (1.28.1)\n", + "Requirement already satisfied: h5py>=2.9.0 in /usr/local/lib/python3.6/dist-packages (from mlagents==0.16.0) (2.10.0)\n", + "Requirement already satisfied: mlagents_envs==0.16.0 in ./ml-agents/ml-agents-envs (from mlagents==0.16.0) (0.16.0)\n", + "Requirement already satisfied: numpy<2.0,>=1.13.3 in /usr/local/lib/python3.6/dist-packages (from mlagents==0.16.0) (1.18.4)\n", + "Requirement already satisfied: Pillow>=4.2.1 in /usr/local/lib/python3.6/dist-packages (from mlagents==0.16.0) (7.0.0)\n", + "Requirement already satisfied: protobuf>=3.6 in /usr/local/lib/python3.6/dist-packages (from mlagents==0.16.0) (3.10.0)\n", + "Requirement already satisfied: pyyaml in /usr/local/lib/python3.6/dist-packages (from mlagents==0.16.0) (3.13)\n", + "Requirement already satisfied: tensorflow<3.0,>=1.7 in /usr/local/lib/python3.6/dist-packages (from mlagents==0.16.0) (2.2.0)\n", + "Requirement already satisfied: six>=1.12.0 in /usr/local/lib/python3.6/dist-packages (from mlagents==0.16.0) (1.12.0)\n", + "Requirement already satisfied: cloudpickle in /usr/local/lib/python3.6/dist-packages (from mlagents_envs==0.16.0->mlagents==0.16.0) (1.3.0)\n", + "Requirement already satisfied: setuptools in /usr/local/lib/python3.6/dist-packages (from protobuf>=3.6->mlagents==0.16.0) (46.3.0)\n", + "Requirement already satisfied: tensorflow-estimator<2.3.0,>=2.2.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow<3.0,>=1.7->mlagents==0.16.0) (2.2.0)\n", + "Requirement already satisfied: google-pasta>=0.1.8 in /usr/local/lib/python3.6/dist-packages (from tensorflow<3.0,>=1.7->mlagents==0.16.0) (0.2.0)\n", + "Requirement already satisfied: gast==0.3.3 in /usr/local/lib/python3.6/dist-packages (from tensorflow<3.0,>=1.7->mlagents==0.16.0) (0.3.3)\n", + "Requirement already satisfied: termcolor>=1.1.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow<3.0,>=1.7->mlagents==0.16.0) (1.1.0)\n", + "Requirement already satisfied: wheel>=0.26; python_version >= \"3\" in /usr/local/lib/python3.6/dist-packages (from tensorflow<3.0,>=1.7->mlagents==0.16.0) (0.34.2)\n", + "Requirement already satisfied: keras-preprocessing>=1.1.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow<3.0,>=1.7->mlagents==0.16.0) (1.1.0)\n", + "Requirement already satisfied: absl-py>=0.7.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow<3.0,>=1.7->mlagents==0.16.0) (0.9.0)\n", + "Requirement already satisfied: wrapt>=1.11.1 in /usr/local/lib/python3.6/dist-packages (from tensorflow<3.0,>=1.7->mlagents==0.16.0) (1.12.1)\n", + "Requirement already satisfied: scipy==1.4.1; python_version >= \"3\" in /usr/local/lib/python3.6/dist-packages (from tensorflow<3.0,>=1.7->mlagents==0.16.0) (1.4.1)\n", + "Requirement already satisfied: tensorboard<2.3.0,>=2.2.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow<3.0,>=1.7->mlagents==0.16.0) (2.2.1)\n", + "Requirement already satisfied: opt-einsum>=2.3.2 in /usr/local/lib/python3.6/dist-packages (from tensorflow<3.0,>=1.7->mlagents==0.16.0) (3.2.1)\n", + "Requirement already satisfied: astunparse==1.6.3 in /usr/local/lib/python3.6/dist-packages (from tensorflow<3.0,>=1.7->mlagents==0.16.0) (1.6.3)\n", + "Requirement already satisfied: requests<3,>=2.21.0 in /usr/local/lib/python3.6/dist-packages (from tensorboard<2.3.0,>=2.2.0->tensorflow<3.0,>=1.7->mlagents==0.16.0) (2.23.0)\n", + "Requirement already satisfied: google-auth<2,>=1.6.3 in /usr/local/lib/python3.6/dist-packages (from tensorboard<2.3.0,>=2.2.0->tensorflow<3.0,>=1.7->mlagents==0.16.0) (1.7.2)\n", + "Requirement already satisfied: tensorboard-plugin-wit>=1.6.0 in /usr/local/lib/python3.6/dist-packages (from tensorboard<2.3.0,>=2.2.0->tensorflow<3.0,>=1.7->mlagents==0.16.0) (1.6.0.post3)\n", + "Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.6/dist-packages (from tensorboard<2.3.0,>=2.2.0->tensorflow<3.0,>=1.7->mlagents==0.16.0) (3.2.1)\n", + "Requirement already satisfied: werkzeug>=0.11.15 in /usr/local/lib/python3.6/dist-packages (from tensorboard<2.3.0,>=2.2.0->tensorflow<3.0,>=1.7->mlagents==0.16.0) (1.0.1)\n", + "Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in /usr/local/lib/python3.6/dist-packages (from tensorboard<2.3.0,>=2.2.0->tensorflow<3.0,>=1.7->mlagents==0.16.0) (0.4.1)\n", + "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.6/dist-packages (from requests<3,>=2.21.0->tensorboard<2.3.0,>=2.2.0->tensorflow<3.0,>=1.7->mlagents==0.16.0) (2020.4.5.1)\n", + "Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.6/dist-packages (from requests<3,>=2.21.0->tensorboard<2.3.0,>=2.2.0->tensorflow<3.0,>=1.7->mlagents==0.16.0) (2.9)\n", + "Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.6/dist-packages (from requests<3,>=2.21.0->tensorboard<2.3.0,>=2.2.0->tensorflow<3.0,>=1.7->mlagents==0.16.0) (3.0.4)\n", + "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.6/dist-packages (from requests<3,>=2.21.0->tensorboard<2.3.0,>=2.2.0->tensorflow<3.0,>=1.7->mlagents==0.16.0) (1.24.3)\n", + "Requirement already satisfied: rsa<4.1,>=3.1.4 in /usr/local/lib/python3.6/dist-packages (from google-auth<2,>=1.6.3->tensorboard<2.3.0,>=2.2.0->tensorflow<3.0,>=1.7->mlagents==0.16.0) (4.0)\n", + "Requirement already satisfied: cachetools<3.2,>=2.0.0 in /usr/local/lib/python3.6/dist-packages (from google-auth<2,>=1.6.3->tensorboard<2.3.0,>=2.2.0->tensorflow<3.0,>=1.7->mlagents==0.16.0) (3.1.1)\n", + "Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.6/dist-packages (from google-auth<2,>=1.6.3->tensorboard<2.3.0,>=2.2.0->tensorflow<3.0,>=1.7->mlagents==0.16.0) (0.2.8)\n", + "Requirement already satisfied: requests-oauthlib>=0.7.0 in /usr/local/lib/python3.6/dist-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.3.0,>=2.2.0->tensorflow<3.0,>=1.7->mlagents==0.16.0) (1.3.0)\n", + "Requirement already satisfied: pyasn1>=0.1.3 in /usr/local/lib/python3.6/dist-packages (from rsa<4.1,>=3.1.4->google-auth<2,>=1.6.3->tensorboard<2.3.0,>=2.2.0->tensorflow<3.0,>=1.7->mlagents==0.16.0) (0.4.8)\n", + "Requirement already satisfied: oauthlib>=3.0.0 in /usr/local/lib/python3.6/dist-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.3.0,>=2.2.0->tensorflow<3.0,>=1.7->mlagents==0.16.0) (3.1.0)\n", + "Installing collected packages: mlagents\n", + " Running setup.py develop for mlagents\n", + "Successfully installed mlagents\n", + "Obtaining file:///content/ml-agents/gym-unity\n", + "Requirement already satisfied: gym in /usr/local/lib/python3.6/dist-packages (from gym-unity==0.16.0) (0.17.1)\n", + "Requirement already satisfied: mlagents_envs==0.16.0 in ./ml-agents/ml-agents-envs (from gym-unity==0.16.0) (0.16.0)\n", + "Requirement already satisfied: scipy in /usr/local/lib/python3.6/dist-packages (from gym->gym-unity==0.16.0) (1.4.1)\n", + "Requirement already satisfied: numpy>=1.10.4 in /usr/local/lib/python3.6/dist-packages (from gym->gym-unity==0.16.0) (1.18.4)\n", + "Requirement already satisfied: cloudpickle<1.4.0,>=1.2.0 in /usr/local/lib/python3.6/dist-packages (from gym->gym-unity==0.16.0) (1.3.0)\n", + "Requirement already satisfied: six in /usr/local/lib/python3.6/dist-packages (from gym->gym-unity==0.16.0) (1.12.0)\n", + "Requirement already satisfied: pyglet<=1.5.0,>=1.4.0 in /usr/local/lib/python3.6/dist-packages (from gym->gym-unity==0.16.0) (1.5.0)\n", + "Requirement already satisfied: grpcio>=1.11.0 in /usr/local/lib/python3.6/dist-packages (from mlagents_envs==0.16.0->gym-unity==0.16.0) (1.28.1)\n", + "Requirement already satisfied: Pillow>=4.2.1 in /usr/local/lib/python3.6/dist-packages (from mlagents_envs==0.16.0->gym-unity==0.16.0) (7.0.0)\n", + "Requirement already satisfied: protobuf>=3.6 in /usr/local/lib/python3.6/dist-packages (from mlagents_envs==0.16.0->gym-unity==0.16.0) (3.10.0)\n", + "Requirement already satisfied: future in /usr/local/lib/python3.6/dist-packages (from pyglet<=1.5.0,>=1.4.0->gym->gym-unity==0.16.0) (0.16.0)\n", + "Requirement already satisfied: setuptools in /usr/local/lib/python3.6/dist-packages (from protobuf>=3.6->mlagents_envs==0.16.0->gym-unity==0.16.0) (46.3.0)\n", + "Installing collected packages: gym-unity\n", + " Running setup.py develop for gym-unity\n", + "Successfully installed gym-unity\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "VrJOdPCmaa8Z", + "colab_type": "text" + }, + "source": [ + "*A kernel restart process required after installation in order to system recognize packages that have built*.\n", + "\n", + "*Or you can comment this section and manually restart runtime*" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "qjn2K_qaaXwA", + "colab_type": "code", + "colab": {} + }, + "source": [ + "import os\n", + "os._exit(00)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "WzEtFHR5a2Sx", + "colab_type": "text" + }, + "source": [ + "## 3. Import python packages\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Ao73ZHlfaxKU", + "colab_type": "code", + "colab": {} + }, + "source": [ + "from pyvirtualdisplay import Display\n", + "import sys\n", + "import os\n", + "import random\n", + "import matplotlib.pyplot as plt\n", + "import numpy as np\n", + "from IPython.display import clear_output \n", + "import mlagents_envs\n", + "from mlagents_envs.environment import UnityEnvironment\n", + "from mlagents_envs.side_channel.engine_configuration_channel import EngineConfig, EngineConfigurationChannel" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "hPud2f3Ea-4j", + "colab_type": "text" + }, + "source": [ + "*Name of the Unity environment binary to launch*\n", + "\n", + "*Use headless(server) builds from Unity when building an executable*" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "i8XxSAoUbnPB", + "colab_type": "code", + "colab": {} + }, + "source": [ + "env_name = \"MLAgents-Google-Collab/headless_collab/basic.x86_64\" \n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "vEu808A6bz1c", + "colab_type": "text" + }, + "source": [ + "*Set permission to use launch file as an executable*\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "9OVoeyE2byHu", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "6214f5ee-14cf-4435-d538-bd43e6deb1db" + }, + "source": [ + "!chmod -R 755 env_name\n" + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "chmod: cannot access 'env_name': No such file or directory\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "fheubdJEb42T", + "colab_type": "text" + }, + "source": [ + "*Check Python Version*" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "GhdfMnNkb3cI", + "colab_type": "code", + "colab": {} + }, + "source": [ + "if (sys.version_info[0] < 3):\n", + " raise Exception(\"ERROR: ML-Agents Toolkit (v0.3 onwards) requires Python 3\")" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "0cSsdeiJcBYv", + "colab_type": "text" + }, + "source": [ + "*Creating virtual display to work on headless server*" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "26NqMeZpcAbn", + "colab_type": "code", + "colab": {} + }, + "source": [ + "dis = Display(visible=0, size=(400, 400))\n", + "dis.start()\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "L7kpEqJlcJLB", + "colab_type": "text" + }, + "source": [ + "## 4. Set the port number for Unity - Python socket communication\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "epu5hh69cTES", + "colab_type": "code", + "colab": {} + }, + "source": [ + "PORT_NUMBER = 3055\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "e9lE0s3UdiR7", + "colab_type": "text" + }, + "source": [ + "*Set the time scale of the engine*" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "XRsTNI3sdg8V", + "colab_type": "code", + "colab": {} + }, + "source": [ + "engine_configuration_channel = EngineConfigurationChannel()\n", + "engine_configuration_channel.set_configuration_parameters(time_scale = 3.0)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "vpm3wqWpdoI1", + "colab_type": "text" + }, + "source": [ + "*Initialize the launcher*" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "meSBMCV5dmUx", + "colab_type": "code", + "colab": {} + }, + "source": [ + "env = UnityEnvironment(file_name=env_name, side_channels = [engine_configuration_channel],base_port=PORT_NUMBER)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "9tfb_ZT-drCU", + "colab_type": "text" + }, + "source": [ + "*Reset the environment*" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "f0kdm2HzdqHo", + "colab_type": "code", + "colab": {} + }, + "source": [ + "env.reset()" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "O_duyRVOdvRM", + "colab_type": "text" + }, + "source": [ + "*Set the default behaviour spec to work with*" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "6dcNKNA1dt3y", + "colab_type": "code", + "colab": {} + }, + "source": [ + "behavior_name = env.get_behavior_names()[0]\n", + "behavior_spec = env.get_behavior_spec(behavior_name)\n", + "observation_space = behavior_spec.observation_shapes[0]\n", + "action_space = behavior_spec.action_shape" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "jcsDKsIhd0av", + "colab_type": "text" + }, + "source": [ + "*Get the state of the agents*" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "5VqqmIZhdzqh", + "colab_type": "code", + "colab": {} + }, + "source": [ + "step_result = env.get_steps(behavior_name)\n", + "num_agents = len(step_result[0].obs[0])" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "kaf5E1Ced4Gm", + "colab_type": "text" + }, + "source": [ + "*Examine the number of observations per Agent*" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "1TFOlTpxd3Yo", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "3383a685-118e-4aaa-aa4d-69ffa9d2bddd" + }, + "source": [ + "print(\"Number of observations : \", observation_space[0], \"Number of Actions : \",action_space)\n" + ], + "execution_count": 13, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Number of observations : 20 Number of Actions : (3,)\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "MPV6jT3Fd-HP", + "colab_type": "text" + }, + "source": [ + "*Check if there is any visual observation from agent*" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "oSQOe474d7yI", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "ec9d1db3-0821-4ca0-ac82-9139fd38069d" + }, + "source": [ + "vis_obs = any([len(shape) == 3 for shape in behavior_spec.observation_shapes])\n", + "print(\"Is there a visual observation ?\", vis_obs)" + ], + "execution_count": 14, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Is there a visual observation ? False\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "4oH0dWXxeDwq", + "colab_type": "text" + }, + "source": [ + "*Examine the visual observations*" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "pHFHLoEZeB6G", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 52 + }, + "outputId": "ac32936b-b8f2-499b-f65d-3a8fb7a4f436" + }, + "source": [ + "if vis_obs:\n", + " vis_obs_index = next(i for i,v in enumerate(behavior_spec.observation_shapes) if len(v) == 3)\n", + " print(\"Agent visual observation looks like:\")\n", + " obs = step_result.obs[vis_obs_index]\n", + " plt.imshow(obs[0,:,:,:])\n", + "else:\n", + " print(\"First Agent observation looks like: \\n{}\".format(step_result[0].obs[0]))\n" + ], + "execution_count": 15, + "outputs": [ + { + "output_type": "stream", + "text": [ + "First Agent observation looks like: \n", + "[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "GTN9XmyUeHg0", + "colab_type": "text" + }, + "source": [ + "## 5.Utility Functions\n", + "\n", + "*If agent reached any terminal state this method will return True*\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "GE8gA4gjeGvT", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def check_done(\n", + " step_result : tuple\n", + "):\n", + " if len(step_result[1]) != 0:\n", + " return True\n", + " elif len(step_result[1]) == 0:\n", + " return False" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7O-OJ_SHeWDD", + "colab_type": "text" + }, + "source": [ + "*Simple function to plot graphs using matplotlib*" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "-o4LyyYAeUYB", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def plot(\n", + " frame_idx: int, \n", + " scores: list\n", + "):\n", + " clear_output(True)\n", + " plt.figure(figsize=(20, 5))\n", + " plt.subplot(131)\n", + " plt.title('frame %s. score: %s' % (frame_idx, np.mean(scores[-10:])))\n", + " plt.plot(scores)\n", + " plt.show()" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gUFMr_r1ec0c", + "colab_type": "text" + }, + "source": [ + "## 6.Example of an agent playing taking random actions in environment\n", + "\n", + "*You can set how many random episode to play*\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "UOTFeckMeadm", + "colab_type": "code", + "colab": {} + }, + "source": [ + "RANDOM_EPISODE_NUMBER = 10" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "IX41qIuJejgJ", + "colab_type": "text" + }, + "source": [ + "*You can set plotting to True for graphics displayed in notebook*" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "KKox3cNUeidK", + "colab_type": "code", + "colab": {} + }, + "source": [ + "PLOT_GRAPHICS = True" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "5olTo65Get-u", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 194 + }, + "outputId": "19273833-c7c9-4411-862e-884577b31e37" + }, + "source": [ + "total_frames = 0 \n", + "total_rewards = list()\n", + "for episode in range(RANDOM_EPISODE_NUMBER):\n", + " env.reset()\n", + " step_result = env.get_steps(behavior_name)\n", + " done = False\n", + " episode_rewards = 0\n", + " while not done:\n", + " total_frames += 1\n", + " if behavior_spec.is_action_continuous():\n", + " action = np.random.randn(num_agents, action_space) \n", + " if behavior_spec.is_action_discrete():\n", + " branch_size = behavior_spec.discrete_action_branches\n", + " action = np.column_stack([np.random.randint(0,branch_size[i],size=num_agents) for i in range(len(branch_size))])\n", + "\n", + " env.set_actions(behavior_name,action)\n", + " env.step()\n", + " step_result = env.get_steps(behavior_name)\n", + " done = check_done(step_result)\n", + " if not done:\n", + " next_state = step_result[0].obs[0] \n", + " episode_rewards += step_result[0].reward[0]\n", + " else:\n", + " next_state = step_result[1].obs[0]\n", + " episode_rewards += step_result[1].reward[0]\n", + " \n", + " print(\"Total reward this episode: {}\".format(episode_rewards))\n", + " total_rewards.append(episode_rewards)" + ], + "execution_count": 22, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Total reward this episode: -0.5599999818950891\n", + "Total reward this episode: 0.030000004917383194\n", + "Total reward this episode: 0.9100000113248825\n", + "Total reward this episode: -0.4299999848008156\n", + "Total reward this episode: -0.8299999497830868\n", + "Total reward this episode: -0.49999998323619366\n", + "Total reward this episode: -0.8899999745190144\n", + "Total reward this episode: 0.6300000175833702\n", + "Total reward this episode: -0.1399999912828207\n", + "Total reward this episode: -0.009999994188547134\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "LP7UnP--e0Dp", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file diff --git a/docs/Python-API.md b/docs/Python-API.md index c849f82f05..461b9b115c 100644 --- a/docs/Python-API.md +++ b/docs/Python-API.md @@ -109,7 +109,7 @@ A `BaseEnv` has the following methods: act. - **Close : `env.close()`** Sends a shutdown signal to the environment and terminates the communication. -- **Behavior Specs : `env.behavior_specs`** Returns a Mapping of +- **Behavior Specs : `env.get_behavior_spec(behavior_name: str)`** Returns a Mapping of `BehaviorName` to `BehaviorSpec` objects (read only). A `BehaviorSpec` contains information such as the observation shapes, the action type (multi-discrete or continuous) and the action shape. Note that