(env, gamma = 1.0)
return v
Value-iteration algorithm
Value-iteration algorithm
Value - iteration algorithm
def value_iteration(env, gamma = 1.0):
    """ Value-iteration algorithm """
    v = np.zeros(env.env.nS)  # initialize value-function
    max_iterations = 100000
    eps = 1e-20
    for i in range(max_iterations):
        prev_v = np.copy(v)
        for s in range(env.env.nS):
            q_sa = [sum([p*(r + gamma * prev_v[s_]) for p, s_, r, _ in env.env.P[s][a]]) for a in range(env.env.nA)] 
            v[s] = max(q_sa)
        if (np.sum(np.fabs(prev_v - v)) <= eps):
            print ('Value-iteration converged at iteration# %d.' %(i+1))
            break
    return v
def value_iteration ( env , gamma = 1.0 ) : v = np . zeros ( env . env . nS ) # initialize value-function max_iterations = 100000 eps = 1e-20 for i in range ( max_iterations ) : prev_v = np . copy ( v ) for s in range ( env . env . nS ) : q_sa = [ sum ( [ p * ( r + gamma * prev_v [ s_ ] ) for p , s_ , r , _ in env . env . P [ s ] [ a ] ] ) for a in range ( env . env . nA ) ] v [ s ] = max ( q_sa ) if ( np . sum ( np . fabs ( prev_v - v ) ) <= eps ) : print ( 'Value-iteration converged at iteration# %d.' % ( i + 1 ) ) break return v