(v, gamma = 1.0)
return policy
Extract the policy given a value-function
Extract the policy given a value-function
Extract the policy given a value - function
def extract_policy(v, gamma = 1.0):
    """ Extract the policy given a value-function """
    policy = np.zeros(env.env.nS)
    for s in range(env.env.nS):
        q_sa = np.zeros(env.env.nA)
        for a in range(env.env.nA):
            q_sa[a] = sum([p * (r + gamma * v[s_]) for p, s_, r, _ in  env.env.P[s][a]])
        policy[s] = np.argmax(q_sa)
    return policy
def extract_policy ( v , gamma = 1.0 ) : policy = np . zeros ( env . env . nS ) for s in range ( env . env . nS ) : q_sa = np . zeros ( env . env . nA ) for a in range ( env . env . nA ) : q_sa [ a ] = sum ( [ p * ( r + gamma * v [ s_ ] ) for p , s_ , r , _ in env . env . P [ s ] [ a ] ] ) policy [ s ] = np . argmax ( q_sa ) return policy