{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Simple Example 3 - Manual Control\n", "\n", "By default this runs a few \"move forward\" actions for two agents, in a separate window.\n", "\n", "If you uncomment the \"input\" line below, it opens a text box in the Jupyter notebook, allowing basic manual control.\n", "\n", "eg Enter `\"0 2 s<enter>\"` to tell agent 0 to move forward, and step the environment.\n", "\n", "You should be able to see the red agent step forward, and get a reward from the env, looking like this:\n", "\n", "`Rewards: {0: -1.0, 1: -1.0} [done= {0: False, 1: False, '__all__': False} ]`\n", "\n", "Note that this example is set up to use the straightforward \"PIL\" renderer - without the special SBB artwork!\n", "The agent observations are displayed as squares of varying sizes, with a paler version of the agent colour. The targets are half-size squares in the full agent colour.\n", "\n", "You can switch to the \"PILSVG\" renderer which is prettier but currently renders the agents one step behind, because it needs to know which way the agent is turning. This can be confusing if you are debugging step-by-step.\n", "\n", "The image below is what the separate window should look like." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import random\n", "import numpy as np\n", "import time\n", "from flatland.envs.generators import random_rail_generator\n", "from flatland.envs.observations import TreeObsForRailEnv\n", "from flatland.envs.rail_env import RailEnv\n", "from flatland.utils.rendertools import RenderTool" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "random.seed(1)\n", "np.random.seed(1)\n", "\n", "env = RailEnv(width=7,\n", " height=7,\n", " rail_generator=random_rail_generator(),\n", " number_of_agents=2,\n", " obs_builder_object=TreeObsForRailEnv(max_depth=2))\n", "\n", "# Print the observation vector for agent 0\n", "obs, all_rewards, done, _ = env.step({0: 0})\n", "for i in range(env.get_num_agents()):\n", " env.obs_builder.util_print_obs_subtree(tree=obs[i], num_features_per_node=7)\n", "\n", "env_renderer = RenderTool(env, gl=\"PIL\")\n", "# env_renderer = RenderTool(env, gl=\"PILSVG\")\n", "\n", "env_renderer.renderEnv(show=True, frames=True)\n", "\n", "print(\"Manual control: s=perform step, q=quit, [agent id] [1-2-3 action] \\\n", " (turnleft+move, move to front, turnright+move)\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for step in range(10):\n", "\n", " # This is an example command, setting agent 0's action to 2 (move forward), and agent 1's action to 2, \n", " # then stepping the environment.\n", " cmd = \"0 2 1 2 s\"\n", " \n", " # uncomment this input statement if you want to try interactive manual commands\n", " # cmd = input(\">> \")\n", " \n", " cmds = cmd.split(\" \")\n", "\n", " action_dict = {}\n", "\n", " i = 0\n", " while i < len(cmds):\n", " if cmds[i] == 'q':\n", " import sys\n", "\n", " sys.exit()\n", " elif cmds[i] == 's':\n", " obs, all_rewards, done, _ = env.step(action_dict)\n", " action_dict = {}\n", " print(\"Rewards: \", all_rewards, \" [done=\", done, \"]\")\n", " else:\n", " agent_id = int(cmds[i])\n", " action = int(cmds[i + 1])\n", " action_dict[agent_id] = action\n", " i = i + 1\n", " i += 1\n", "\n", " env_renderer.renderEnv(show=True, frames=True)\n", " \n", " time.sleep(0.3)" ] } ], "metadata": { "hide_input": false, "kernelspec": { "display_name": "ve367", "language": "python", "name": "ve367" }, "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" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }