(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.action_space.n)
        for a in range(env.action_space.n):
            for next_sr in env.env.P[s][a]:
                # next_sr is a tuple of (probability, next state, reward, done)
                p, s_, r, _ = next_sr
                q_sa[a] += (p * (r + gamma * v[s_]))
        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 . action_space . n ) for a in range ( env . action_space . n ) : for next_sr in env . env . P [ s ] [ a ] : # next_sr is a tuple of (probability, next state, reward, done) p , s_ , r , _ = next_sr q_sa [ a ] += ( p * ( r + gamma * v [ s_ ] ) ) policy [ s ] = np . argmax ( q_sa ) return policy