In [23]:
import numpy as np
import sys
if "../" not in sys.path:
  sys.path.append("../") 
from lib.envs.gridworld import GridworldEnv

In [24]:
env = GridworldEnv()

In [25]:
def policy_eval(policy, env, discount_factor=1.0, theta=0.00001):
    """
    Evaluate a policy given an environment and a full description of the environment's dynamics.
    
    Args:
        policy: [S, A] shaped matrix representing the policy.
        env: OpenAI env. env.P represents the transition probabilities of the environment.
            env.P[s][a] is a list of transition tuples (prob, next_state, reward, done).
            env.nS is a number of states in the environment. 
            env.nA is a number of actions in the environment.
        theta: We stop evaluation once our value function change is less than theta for all states.
        discount_factor: Gamma discount factor.
    
    Returns:
        Vector of length env.nS representing the value function.
    """
    # Start with a random (all 0) value function
    V = np.zeros(env.nS)
    while True:
        # TODO: Implement!
        break
    return np.array(V)

In [26]:
random_policy = np.ones([env.nS, env.nA]) / env.nA
v = policy_eval(random_policy, env)

In [22]:
# Test: Make sure the evaluated policy is what we expected
expected_v = np.array([0, -14, -20, -22, -14, -18, -20, -20, -20, -20, -18, -14, -22, -20, -14, 0])
np.testing.assert_array_almost_equal(v, expected_v, decimal=2)


---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-22-235f39fb115c> in <module>()
      1 # Test: Make sure the evaluated policy is what we expected
      2 expected_v = np.array([0, -14, -20, -22, -14, -18, -20, -20, -20, -20, -18, -14, -22, -20, -14, 0])
----> 3 np.testing.assert_array_almost_equal(v, expected_v, decimal=2)

/Users/dennybritz/venvs/tf/lib/python3.5/site-packages/numpy/testing/utils.py in assert_array_almost_equal(x, y, decimal, err_msg, verbose)
    914     assert_array_compare(compare, x, y, err_msg=err_msg, verbose=verbose,
    915              header=('Arrays are not almost equal to %d decimals' % decimal),
--> 916              precision=decimal)
    917 
    918 

/Users/dennybritz/venvs/tf/lib/python3.5/site-packages/numpy/testing/utils.py in assert_array_compare(comparison, x, y, err_msg, verbose, header, precision)
    735                                 names=('x', 'y'), precision=precision)
    736             if not cond:
--> 737                 raise AssertionError(msg)
    738     except ValueError:
    739         import traceback

AssertionError: 
Arrays are not almost equal to 2 decimals

(mismatch 87.5%)
 x: array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
        0.,  0.,  0.])
 y: array([  0, -14, -20, -22, -14, -18, -20, -20, -20, -20, -18, -14, -22,
       -20, -14,   0])

In [ ]: