{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# Basic Simulations in OpenAI's Gym" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "OpenAI Gym is a toolkit for building, evaluating and comparing RL algorithms. It is\n", "compatible with algorithms written in any frameworks like TensoFlow, Theano, Keras etc... It\n", "is simple and easy to comprehend. It makes no assumption about the structure of our agent\n", "and provides an interface to all RL tasks.\n", "\n", "Now, we will see, how to simulate environments in gym." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## CartPole Environment" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First let us import the OpenAI's Gym library" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import gym" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "We use the make function for simulating the environment" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "env = gym.make('CartPole-v0')" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "Then, we initialize the environment using reset method" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "env.reset()" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "Now,we can loop for some time steps and render the environment at each step" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "for _ in range(1000):\n", " env.render()\n", " env.step(env.action_space.sample())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Different Types of Environments" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "OpenAI gym provides a lot of simulation environments for training, evaluating and\n", "building our agents. We can check the available environments by either checking their\n", "website or simply typing the following commands which will list the available environments." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from gym import envs\n", "print(envs.registry.all())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## CarRacing Environment" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ " Since Gym provides different interesting environments, let us simulate a car racing\n", "environment as shown below," ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import gym\n", "env = gym.make('CarRacing-v0')\n", "env.reset()\n", "for _ in range(1000):\n", " env.render()\n", " env.step(env.action_space.sample())" ] } ], "metadata": { "kernelspec": { "display_name": "Python [conda env:universe]", "language": "python", "name": "conda-env-universe-py" }, "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.5.4" } }, "nbformat": 4, "nbformat_minor": 2 }