(env, policy, gamma=1.0)
return v
Iteratively evaluate the value-function under policy.
    Alternatively, we could formulate a set of linear equations in iterms of v[s] 
    and solve them to find the value function.
Iteratively evaluate the value-function under policy.
    Alternatively, we could formulate a set of linear equations in iterms of v[s] 
    and solve them to find the value function.
Iteratively evaluate the value - function under policy . Alternatively we could formulate a set of linear equations in iterms of v [ s ] and solve them to find the value function .
def compute_policy_v(env, policy, gamma=1.0):
    """ Iteratively evaluate the value-function under policy.
    Alternatively, we could formulate a set of linear equations in iterms of v[s] 
    and solve them to find the value function.
    """
    v = np.zeros(env.env.nS)
    eps = 1e-10
    while True:
        prev_v = np.copy(v)
        for s in range(env.env.nS):
            policy_a = policy[s]
            v[s] = sum([p * (r + gamma * prev_v[s_]) for p, s_, r, _ in env.env.P[s][policy_a]])
        if (np.sum((np.fabs(prev_v - v))) <= eps):
            # value converged
            break
    return v
def compute_policy_v ( env , policy , gamma = 1.0 ) : v = np . zeros ( env . env . nS ) eps = 1e-10 while True : prev_v = np . copy ( v ) for s in range ( env . env . nS ) : policy_a = policy [ s ] v [ s ] = sum ( [ p * ( r + gamma * prev_v [ s_ ] ) for p , s_ , r , _ in env . env . P [ s ] [ policy_a ] ] ) if ( np . sum ( ( np . fabs ( prev_v - v ) ) ) <= eps ) : # value converged break return v