|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "metadata": { |
| 7 | + "collapsed": false |
| 8 | + }, |
| 9 | + "outputs": [], |
| 10 | + "source": [ |
| 11 | + "import numpy as np\n", |
| 12 | + "import pprint\n", |
| 13 | + "import sys\n", |
| 14 | + "if \"../\" not in sys.path:\n", |
| 15 | + " sys.path.append(\"../\") \n", |
| 16 | + "from lib.envs.gridworld import GridworldEnv" |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "code", |
| 21 | + "execution_count": null, |
| 22 | + "metadata": { |
| 23 | + "collapsed": true |
| 24 | + }, |
| 25 | + "outputs": [], |
| 26 | + "source": [ |
| 27 | + "pp = pprint.PrettyPrinter(indent=2)\n", |
| 28 | + "env = GridworldEnv()" |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "code", |
| 33 | + "execution_count": 13, |
| 34 | + "metadata": { |
| 35 | + "collapsed": false |
| 36 | + }, |
| 37 | + "outputs": [], |
| 38 | + "source": [ |
| 39 | + "def policy_eval(policy, env, discount_factor=1.0, theta=0.00001):\n", |
| 40 | + " \"\"\"\n", |
| 41 | + " Evaluate a policy given an environment and a full description of the environment's dynamics.\n", |
| 42 | + " \n", |
| 43 | + " Args:\n", |
| 44 | + " policy: [S, A] shaped matrix representing the policy.\n", |
| 45 | + " env: OpenAI env. env.P represents the transition probabilities of the environment.\n", |
| 46 | + " env.P[s][a] is a (prob, next_state, reward, done) tuple.\n", |
| 47 | + " theta: We stop evaluation one our value function change is less than theta for all states.\n", |
| 48 | + " discount_factor: lambda discount factor.\n", |
| 49 | + " \n", |
| 50 | + " Returns:\n", |
| 51 | + " Vector of length env.nS representing the value function.\n", |
| 52 | + " \"\"\"\n", |
| 53 | + " # Start with a random (all 0) value function\n", |
| 54 | + " V = np.zeros(env.nS)\n", |
| 55 | + " while True:\n", |
| 56 | + " # TODO: Implement!\n", |
| 57 | + " break\n", |
| 58 | + " return np.array(V)" |
| 59 | + ] |
| 60 | + }, |
| 61 | + { |
| 62 | + "cell_type": "code", |
| 63 | + "execution_count": 16, |
| 64 | + "metadata": { |
| 65 | + "collapsed": false |
| 66 | + }, |
| 67 | + "outputs": [], |
| 68 | + "source": [ |
| 69 | + "random_policy = np.ones([env.nS, env.nA]) / env.nA\n", |
| 70 | + "v = policy_eval(random_policy, env)" |
| 71 | + ] |
| 72 | + }, |
| 73 | + { |
| 74 | + "cell_type": "code", |
| 75 | + "execution_count": 17, |
| 76 | + "metadata": { |
| 77 | + "collapsed": false |
| 78 | + }, |
| 79 | + "outputs": [ |
| 80 | + { |
| 81 | + "ename": "AssertionError", |
| 82 | + "evalue": "\nArrays are not almost equal to 2 decimals\n\n(mismatch 87.5%)\n x: array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n 0., 0., 0.])\n y: array([ 0, -14, -20, -22, -14, -18, -20, -20, -20, -20, -18, -14, -22,\n -20, -14, 0])", |
| 83 | + "output_type": "error", |
| 84 | + "traceback": [ |
| 85 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
| 86 | + "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", |
| 87 | + "\u001b[0;32m<ipython-input-17-235f39fb115c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Test: Make sure the evaluated policy is what we expected\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mexpected_v\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m14\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m20\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m22\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m14\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m18\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m20\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m20\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m20\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m20\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m18\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m14\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m22\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m20\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m14\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[0;32m----> 3\u001b[0;31m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtesting\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0massert_array_almost_equal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mv\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexpected_v\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdecimal\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", |
| 88 | + "\u001b[0;32m/Users/dennybritz/venvs/tf/lib/python3.5/site-packages/numpy/testing/utils.py\u001b[0m in \u001b[0;36massert_array_almost_equal\u001b[0;34m(x, y, decimal, err_msg, verbose)\u001b[0m\n\u001b[1;32m 914\u001b[0m assert_array_compare(compare, x, y, err_msg=err_msg, verbose=verbose,\n\u001b[1;32m 915\u001b[0m \u001b[0mheader\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Arrays are not almost equal to %d decimals'\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0mdecimal\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 916\u001b[0;31m precision=decimal)\n\u001b[0m\u001b[1;32m 917\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 918\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", |
| 89 | + "\u001b[0;32m/Users/dennybritz/venvs/tf/lib/python3.5/site-packages/numpy/testing/utils.py\u001b[0m in \u001b[0;36massert_array_compare\u001b[0;34m(comparison, x, y, err_msg, verbose, header, precision)\u001b[0m\n\u001b[1;32m 735\u001b[0m names=('x', 'y'), precision=precision)\n\u001b[1;32m 736\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mcond\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 737\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mAssertionError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmsg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 738\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 739\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mtraceback\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 90 | + "\u001b[0;31mAssertionError\u001b[0m: \nArrays are not almost equal to 2 decimals\n\n(mismatch 87.5%)\n x: array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n 0., 0., 0.])\n y: array([ 0, -14, -20, -22, -14, -18, -20, -20, -20, -20, -18, -14, -22,\n -20, -14, 0])" |
| 91 | + ] |
| 92 | + } |
| 93 | + ], |
| 94 | + "source": [ |
| 95 | + "# Test: Make sure the evaluated policy is what we expected\n", |
| 96 | + "expected_v = np.array([0, -14, -20, -22, -14, -18, -20, -20, -20, -20, -18, -14, -22, -20, -14, 0])\n", |
| 97 | + "np.testing.assert_array_almost_equal(v, expected_v, decimal=2)" |
| 98 | + ] |
| 99 | + }, |
| 100 | + { |
| 101 | + "cell_type": "code", |
| 102 | + "execution_count": null, |
| 103 | + "metadata": { |
| 104 | + "collapsed": true |
| 105 | + }, |
| 106 | + "outputs": [], |
| 107 | + "source": [] |
| 108 | + } |
| 109 | + ], |
| 110 | + "metadata": { |
| 111 | + "kernelspec": { |
| 112 | + "display_name": "Python 3", |
| 113 | + "language": "python", |
| 114 | + "name": "python3" |
| 115 | + }, |
| 116 | + "language_info": { |
| 117 | + "codemirror_mode": { |
| 118 | + "name": "ipython", |
| 119 | + "version": 3 |
| 120 | + }, |
| 121 | + "file_extension": ".py", |
| 122 | + "mimetype": "text/x-python", |
| 123 | + "name": "python", |
| 124 | + "nbconvert_exporter": "python", |
| 125 | + "pygments_lexer": "ipython3", |
| 126 | + "version": "3.5.1" |
| 127 | + } |
| 128 | + }, |
| 129 | + "nbformat": 4, |
| 130 | + "nbformat_minor": 0 |
| 131 | +} |
0 commit comments