|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": { |
| 7 | + "collapsed": true |
| 8 | + }, |
| 9 | + "outputs": [], |
| 10 | + "source": [ |
| 11 | + "%matplotlib inline" |
| 12 | + ] |
| 13 | + }, |
| 14 | + { |
| 15 | + "cell_type": "code", |
| 16 | + "execution_count": 2, |
| 17 | + "metadata": { |
| 18 | + "collapsed": true |
| 19 | + }, |
| 20 | + "outputs": [], |
| 21 | + "source": [ |
| 22 | + "from JSAnimation.IPython_display import display_animation\n", |
| 23 | + "from matplotlib import animation\n", |
| 24 | + "import matplotlib.pyplot as plt\n", |
| 25 | + "from IPython.display import display" |
| 26 | + ] |
| 27 | + }, |
| 28 | + { |
| 29 | + "cell_type": "code", |
| 30 | + "execution_count": 3, |
| 31 | + "metadata": { |
| 32 | + "collapsed": true |
| 33 | + }, |
| 34 | + "outputs": [], |
| 35 | + "source": [ |
| 36 | + "import gym" |
| 37 | + ] |
| 38 | + }, |
| 39 | + { |
| 40 | + "cell_type": "code", |
| 41 | + "execution_count": 4, |
| 42 | + "metadata": { |
| 43 | + "collapsed": true |
| 44 | + }, |
| 45 | + "outputs": [], |
| 46 | + "source": [ |
| 47 | + "def display_frames_as_gif(frames):\n", |
| 48 | + " \"\"\"\n", |
| 49 | + " Displays a list of frames as a gif, with controls\n", |
| 50 | + " \"\"\"\n", |
| 51 | + " plt.figure(figsize=(frames[0].shape[1] / 72.0, frames[0].shape[0] / 72.0), dpi = 72)\n", |
| 52 | + " patch = plt.imshow(frames[0])\n", |
| 53 | + " plt.axis('off')\n", |
| 54 | + "\n", |
| 55 | + " def animate(i):\n", |
| 56 | + " patch.set_data(frames[i])\n", |
| 57 | + "\n", |
| 58 | + " anim = animation.FuncAnimation(plt.gcf(), animate, frames = len(frames), interval=50)\n", |
| 59 | + " display(display_animation(anim, default_mode='loop'))" |
| 60 | + ] |
| 61 | + }, |
| 62 | + { |
| 63 | + "cell_type": "code", |
| 64 | + "execution_count": 5, |
| 65 | + "metadata": { |
| 66 | + "collapsed": false |
| 67 | + }, |
| 68 | + "outputs": [ |
| 69 | + { |
| 70 | + "name": "stdout", |
| 71 | + "output_type": "stream", |
| 72 | + "text": [ |
| 73 | + "\r", |
| 74 | + "Episode 0/40 finished after 13 timesteps\r", |
| 75 | + "Episode 1/40 finished after 15 timesteps\r", |
| 76 | + "Episode 2/40 finished after 23 timesteps\r", |
| 77 | + "Episode 3/40 finished after 13 timesteps\r", |
| 78 | + "Episode 4/40 finished after 12 timesteps\r", |
| 79 | + "Episode 5/40 finished after 22 timesteps\r", |
| 80 | + "Episode 6/40 finished after 21 timesteps\r", |
| 81 | + "Episode 7/40 finished after 14 timesteps\r", |
| 82 | + "Episode 8/40 finished after 10 timesteps\r", |
| 83 | + "Episode 9/40 finished after 18 timesteps\r", |
| 84 | + "Episode 10/40 finished after 22 timesteps\r", |
| 85 | + "Episode 11/40 finished after 13 timesteps\r", |
| 86 | + "Episode 12/40 finished after 20 timesteps\r", |
| 87 | + "Episode 13/40 finished after 43 timesteps\r", |
| 88 | + "Episode 14/40 finished after 34 timesteps\r", |
| 89 | + "Episode 15/40 finished after 32 timesteps\r", |
| 90 | + "Episode 16/40 finished after 14 timesteps\r", |
| 91 | + "Episode 17/40 finished after 14 timesteps\r", |
| 92 | + "Episode 18/40 finished after 16 timesteps\r", |
| 93 | + "Episode 19/40 finished after 14 timesteps\r", |
| 94 | + "Episode 20/40 finished after 17 timesteps\r", |
| 95 | + "Episode 21/40 finished after 20 timesteps\r", |
| 96 | + "Episode 22/40 finished after 13 timesteps\r", |
| 97 | + "Episode 23/40 finished after 21 timesteps\r", |
| 98 | + "Episode 24/40 finished after 12 timesteps\r", |
| 99 | + "Episode 25/40 finished after 17 timesteps\r", |
| 100 | + "Episode 26/40 finished after 23 timesteps\r", |
| 101 | + "Episode 27/40 finished after 30 timesteps\r", |
| 102 | + "Episode 28/40 finished after 43 timesteps\r", |
| 103 | + "Episode 29/40 finished after 17 timesteps\r", |
| 104 | + "Episode 30/40 finished after 28 timesteps\r", |
| 105 | + "Episode 31/40 finished after 26 timesteps\r", |
| 106 | + "Episode 32/40 finished after 19 timesteps\r", |
| 107 | + "Episode 33/40 finished after 15 timesteps\r", |
| 108 | + "Episode 34/40 finished after 44 timesteps\r", |
| 109 | + "Episode 35/40 finished after 22 timesteps\r", |
| 110 | + "Episode 36/40 finished after 70 timesteps\r", |
| 111 | + "Episode 37/40 finished after 24 timesteps\r", |
| 112 | + "Episode 38/40 finished after 23 timesteps\r", |
| 113 | + "Episode 39/40 finished after 15 timesteps" |
| 114 | + ] |
| 115 | + } |
| 116 | + ], |
| 117 | + "source": [ |
| 118 | + "env = gym.make('CartPole-v0')\n", |
| 119 | + "cum_reward = 0\n", |
| 120 | + "frames = []\n", |
| 121 | + "num_episodes=40\n", |
| 122 | + "for i_episode in range(num_episodes):\n", |
| 123 | + " observation = env.reset()\n", |
| 124 | + " for t in range(500):\n", |
| 125 | + " # Render into buffer. \n", |
| 126 | + " frames.append(env.render(mode = 'rgb_array', close=True))\n", |
| 127 | + " action = env.action_space.sample() # random action\n", |
| 128 | + " observation, reward, done, info = env.step(action)\n", |
| 129 | + " if done:\n", |
| 130 | + " print(\"\\rEpisode {}/{} finished after {} timesteps\".format(i_episode, num_episodes, t+1), end=\"\")\n", |
| 131 | + " break" |
| 132 | + ] |
| 133 | + }, |
| 134 | + { |
| 135 | + "cell_type": "code", |
| 136 | + "execution_count": 6, |
| 137 | + "metadata": { |
| 138 | + "collapsed": false |
| 139 | + }, |
| 140 | + "outputs": [ |
| 141 | + { |
| 142 | + "ename": "AttributeError", |
| 143 | + "evalue": "'NoneType' object has no attribute 'shape'", |
| 144 | + "output_type": "error", |
| 145 | + "traceback": [ |
| 146 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
| 147 | + "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", |
| 148 | + "\u001b[0;32m<ipython-input-6-d67b09ec9573>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mdisplay_frames_as_gif\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mframes\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", |
| 149 | + "\u001b[0;32m<ipython-input-4-9da36626cf94>\u001b[0m in \u001b[0;36mdisplay_frames_as_gif\u001b[0;34m(frames)\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mDisplays\u001b[0m \u001b[0ma\u001b[0m \u001b[0mlist\u001b[0m \u001b[0mof\u001b[0m \u001b[0mframes\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0ma\u001b[0m \u001b[0mgif\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mcontrols\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \"\"\"\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mplt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfigure\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfigsize\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mframes\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0;36m72.0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mframes\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0;36m72.0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdpi\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m72\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 6\u001b[0m \u001b[0mpatch\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mplt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mimshow\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mframes\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mplt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0maxis\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'off'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 150 | + "\u001b[0;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'shape'" |
| 151 | + ] |
| 152 | + } |
| 153 | + ], |
| 154 | + "source": [ |
| 155 | + "display_frames_as_gif(frames)" |
| 156 | + ] |
| 157 | + } |
| 158 | + ], |
| 159 | + "metadata": { |
| 160 | + "anaconda-cloud": {}, |
| 161 | + "kernelspec": { |
| 162 | + "display_name": "Python [conda root]", |
| 163 | + "language": "python", |
| 164 | + "name": "conda-root-py" |
| 165 | + }, |
| 166 | + "language_info": { |
| 167 | + "codemirror_mode": { |
| 168 | + "name": "ipython", |
| 169 | + "version": 3 |
| 170 | + }, |
| 171 | + "file_extension": ".py", |
| 172 | + "mimetype": "text/x-python", |
| 173 | + "name": "python", |
| 174 | + "nbconvert_exporter": "python", |
| 175 | + "pygments_lexer": "ipython3", |
| 176 | + "version": "3.5.2" |
| 177 | + } |
| 178 | + }, |
| 179 | + "nbformat": 4, |
| 180 | + "nbformat_minor": 1 |
| 181 | +} |
0 commit comments