123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Unity ML-Agents Toolkit\n",
- "## Environment Basics\n",
- "This notebook contains a walkthrough of the basic functions of the Python API for the Unity ML-Agents toolkit. For instructions on building a Unity environment, see [here](https://github.com/Unity-Technologies/ml-agents/blob/master/docs/Getting-Started-with-Balance-Ball.md)."
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 1. Set environment parameters\n",
- "\n",
- "Be sure to set `env_name` to the name of the Unity environment file you want to launch. Ensure that the environment build is in `../envs`."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [],
- "source": [
- "env_name = \"../envs/Bachelorarbeit-Cozmo\" # Name of the Unity environment binary to launch\n",
- "train_mode = True # Whether to run the environment in training or inference mode"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 2. Load dependencies\n",
- "\n",
- "The following loads the necessary dependencies and checks the Python version (at runtime). ML-Agents Toolkit (v0.3 onwards) requires Python 3."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Python version:\n",
- "3.6.7 |Anaconda, Inc.| (default, Oct 28 2018, 19:44:12) [MSC v.1915 64 bit (AMD64)]\n"
- ]
- }
- ],
- "source": [
- "import matplotlib.pyplot as plt\n",
- "import numpy as np\n",
- "import sys\n",
- "\n",
- "from mlagents.envs import UnityEnvironment\n",
- "\n",
- "%matplotlib inline\n",
- "\n",
- "print(\"Python version:\")\n",
- "print(sys.version)\n",
- "\n",
- "# check Python version\n",
- "if (sys.version_info[0] < 3):\n",
- " raise Exception(\"ERROR: ML-Agents Toolkit (v0.3 onwards) requires Python 3\")"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 3. Start the environment\n",
- "`UnityEnvironment` launches and begins communication with the environment when instantiated.\n",
- "\n",
- "Environments contain _brains_ which are responsible for deciding the actions of their associated _agents_. Here we check for the first brain available, and set it as the default brain we will be controlling from Python."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "INFO:mlagents.envs:\n",
- "'Academy' started successfully!\n",
- "Unity Academy name: Academy\n",
- " Number of Brains: 1\n",
- " Number of Training Brains : 1\n",
- " Reset Parameters :\n",
- "\t\t\n",
- "Unity brain name: CozmoLearningBrain\n",
- " Number of Visual Observations (per agent): 1\n",
- " Vector Observation space size (per agent): 0\n",
- " Number of stacked Vector Observation: 1\n",
- " Vector Action space type: discrete\n",
- " Vector Action space size (per agent): [4]\n",
- " Vector Action descriptions: \n"
- ]
- }
- ],
- "source": [
- "env = UnityEnvironment(file_name=env_name)\n",
- "\n",
- "# Set the default brain to work with\n",
- "default_brain = env.brain_names[0]\n",
- "brain = env.brains[default_brain]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 4. Examine the observation and state spaces\n",
- "We can reset the environment to be provided with an initial set of observations and states for all the agents within the environment. In ML-Agents, _states_ refer to a vector of variables corresponding to relevant aspects of the environment for an agent. Likewise, _observations_ refer to a set of relevant pixel-wise visuals for an agent."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Agent state looks like: \n",
- "[]\n",
- "Agent observations look like:\n"
- ]
- },
- {
- "data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q2VBmniy3Z4CHGPzHnR4uscrtmfYq3NB6dXZ2/jPzdPlBeQv4Eu8sCztTc0ScyyB8vpqZ3yjDnZ7rcTX3Ya6HMvPHwL8x2NO8KCKGr58xWtvbdZf3/xKTb/9squ3QfArYWc6Cncdg0/ZwyzWdJSLeGxEXDu8DHwWOM6h1XzlsH/BwOxVuar06DwO3ljO71wGvDZeWbVuz3/dxBvMNg5r3ljOkVwI7gSdbqC+ALwMnMvMLI+/q7FyvV3MP5vp9EXFRuf+LwO8w2I99FPhEOWztXA//Dz4B/GuWs0Jz0fSZsDFnxm5icBbve8Bn265nnRqvYnAW8Rng+WGdDPZJjgAvlttLOlDrfQyWWP/H4DfubevVyWAZ87dl7p8Ddneo5r8vNT1bfgi2jhz/2VLzC8CNLdX8YQZLvmeBp8vbTV2e6w1q7vpc/zrwH6W+48CflfGrGIT4CvCPwPll/D3l8Up5/1XzrMdnBElSBW0vzyWpVwxNSarA0JSkCgxNSarA0JSkCgxNSarA0JSkCgxNSarg/wHuebcd/X63rwAAAABJRU5ErkJggg==\n",
- "text/plain": [
- "<Figure size 432x288 with 1 Axes>"
- ]
- },
- "metadata": {
- "needs_background": "light"
- },
- "output_type": "display_data"
- }
- ],
- "source": [
- "# Reset the environment\n",
- "env_info = env.reset(train_mode=train_mode)[default_brain]\n",
- "\n",
- "# Examine the state space for the default brain\n",
- "print(\"Agent state looks like: \\n{}\".format(env_info.vector_observations[0]))\n",
- "\n",
- "# Examine the observation space for the default brain\n",
- "for observation in env_info.visual_observations:\n",
- " print(\"Agent observations look like:\") \n",
- " \n",
- " #change to numpy array instead of list\n",
- " #observation = cv2.Canny(observation,100,100)\n",
- " observation = np.array(observation)\n",
- " #slice1Copy = np.uint8(observation)\n",
- " #edges = cv2.Canny(slice1Copy,100,100)\n",
- " \n",
- " \n",
- " #if edges.shape[3] == 3:\n",
- " #plt.imshow(observation[0])\n",
- " #else:\n",
- " # plt.imshow(edges[0,:,:,0])\n",
- " \n",
- " if observation.shape[3] == 3:\n",
- " plt.imshow(observation[0,:,:,:])\n",
- " else:\n",
- " plt.imshow(observation[0,:,:,0])"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 5. Take random actions in the environment\n",
- "Once we restart an environment, we can step the environment forward and provide actions to all of the agents within the environment. Here we simply choose random actions based on the `action_space_type` of the default brain. \n",
- "\n",
- "Once this cell is executed, 10 messages will be printed that detail how much reward will be accumulated for the next 10 episodes. The Unity environment will then pause, waiting for further signals telling it what to do next. Thus, not seeing any animation is expected when running this cell."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Total reward this episode: 762.3000014424324\n",
- "Total reward this episode: -992.6999998688698\n",
- "Total reward this episode: -1000.0\n",
- "Total reward this episode: -992.1999996900558\n",
- "Total reward this episode: -999.0999999642372\n"
- ]
- },
- {
- "ename": "MemoryError",
- "evalue": "",
- "output_type": "error",
- "traceback": [
- "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
- "\u001b[1;31mMemoryError\u001b[0m Traceback (most recent call last)",
- "\u001b[1;32m<ipython-input-5-3140409a1130>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 10\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 11\u001b[0m \u001b[0maction\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcolumn_stack\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrandom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrandint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maction_size\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msize\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0menv_info\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0magents\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0maction_size\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 12\u001b[1;33m \u001b[0menv_info\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0menv\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0maction\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mdefault_brain\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 13\u001b[0m \u001b[0mepisode_rewards\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[0menv_info\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrewards\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 14\u001b[0m \u001b[0mdone\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0menv_info\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlocal_done\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
- "\u001b[1;32mc:\\users\\tobi\\downloads\\ml-agents-0.8.1\\ml-agents-envs\\mlagents\\envs\\environment.py\u001b[0m in \u001b[0;36mstep\u001b[1;34m(self, vector_action, memory, text_action, value, custom_action)\u001b[0m\n\u001b[0;32m 436\u001b[0m \u001b[1;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 437\u001b[0m \u001b[0mrl_output\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0moutputs\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrl_output\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 438\u001b[1;33m \u001b[0mstate\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_get_state\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrl_output\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 439\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_global_done\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mstate\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 440\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0m_b\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_external_brain_names\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
- "\u001b[1;32mc:\\users\\tobi\\downloads\\ml-agents-0.8.1\\ml-agents-envs\\mlagents\\envs\\environment.py\u001b[0m in \u001b[0;36m_get_state\u001b[1;34m(self, output)\u001b[0m\n\u001b[0;32m 496\u001b[0m \u001b[0magent_info_list\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0moutput\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0magentInfos\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mbrain_name\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvalue\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 497\u001b[0m _data[brain_name] = BrainInfo.from_agent_proto(agent_info_list,\n\u001b[1;32m--> 498\u001b[1;33m self.brains[brain_name])\n\u001b[0m\u001b[0;32m 499\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0m_data\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mglobal_done\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 500\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
- "\u001b[1;32mc:\\users\\tobi\\downloads\\ml-agents-0.8.1\\ml-agents-envs\\mlagents\\envs\\brain.py\u001b[0m in \u001b[0;36mfrom_agent_proto\u001b[1;34m(agent_info_list, brain_params)\u001b[0m\n\u001b[0;32m 91\u001b[0m obs = [BrainInfo.process_pixels(x.visual_observations[i],\n\u001b[0;32m 92\u001b[0m brain_params.camera_resolutions[i]['blackAndWhite'])\n\u001b[1;32m---> 93\u001b[1;33m for x in agent_info_list]\n\u001b[0m\u001b[0;32m 94\u001b[0m \u001b[0mvis_obs\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[0mobs\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 95\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0magent_info_list\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
- "\u001b[1;32mc:\\users\\tobi\\downloads\\ml-agents-0.8.1\\ml-agents-envs\\mlagents\\envs\\brain.py\u001b[0m in \u001b[0;36m<listcomp>\u001b[1;34m(.0)\u001b[0m\n\u001b[0;32m 91\u001b[0m obs = [BrainInfo.process_pixels(x.visual_observations[i],\n\u001b[0;32m 92\u001b[0m brain_params.camera_resolutions[i]['blackAndWhite'])\n\u001b[1;32m---> 93\u001b[1;33m for x in agent_info_list]\n\u001b[0m\u001b[0;32m 94\u001b[0m \u001b[0mvis_obs\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[0mobs\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 95\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0magent_info_list\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
- "\u001b[1;32mc:\\users\\tobi\\downloads\\ml-agents-0.8.1\\ml-agents-envs\\mlagents\\envs\\brain.py\u001b[0m in \u001b[0;36mprocess_pixels\u001b[1;34m(image_bytes, gray_scale)\u001b[0m\n\u001b[0;32m 76\u001b[0m \u001b[0ms\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mbytearray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mimage_bytes\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 77\u001b[0m \u001b[0mimage\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mImage\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mio\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mBytesIO\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 78\u001b[1;33m \u001b[0ms\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0marray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mimage\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m/\u001b[0m \u001b[1;36m255.0\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 79\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mgray_scale\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 80\u001b[0m \u001b[0ms\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmean\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
- "\u001b[1;31mMemoryError\u001b[0m: "
- ]
- }
- ],
- "source": [
- "for episode in range(100):\n",
- " env_info = env.reset(train_mode=train_mode)[default_brain]\n",
- " done = False\n",
- " episode_rewards = 0\n",
- " while not done:\n",
- " action_size = brain.vector_action_space_size\n",
- " if brain.vector_action_space_type == 'continuous':\n",
- " env_info = env.step(np.random.randn(len(env_info.agents), \n",
- " action_size[0]))[default_brain]\n",
- " else:\n",
- " action = np.column_stack([np.random.randint(0, action_size[i], size=(len(env_info.agents))) for i in range(len(action_size))])\n",
- " env_info = env.step(action)[default_brain]\n",
- " episode_rewards += env_info.rewards[0]\n",
- " done = env_info.local_done[0]\n",
- " print(\"Total reward this episode: {}\".format(episode_rewards))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 6. Close the environment when finished\n",
- "When we are finished using an environment, we can close it with the function below."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [],
- "source": [
- "env.close()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": []
- }
- ],
- "metadata": {
- "anaconda-cloud": {},
- "kernelspec": {
- "display_name": "Python 3",
- "language": "python",
- "name": "python3"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.6.7"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 1
- }
|