(grid, closed_loop_dynamics, reward_function, discount, horizon=250, tol=1e-3)
return rollout
Compute a finite-horizon rollout of a reward function over a discretization of the state space.
Compute a finite-horizon rollout of a reward function over a discretization of the state space.
Compute a finite - horizon rollout of a reward function over a discretization of the state space .
def reward_rollout(grid, closed_loop_dynamics, reward_function, discount, horizon=250, tol=1e-3):
    """Compute a finite-horizon rollout of a reward function over a discretization of the state space."""
    if isinstance(grid, np.ndarray):
        all_points = grid
        nindex = grid.shape[0]
    else: # grid is a GridWorld instance
        all_points = grid.all_points
        nindex = grid.nindex

    converged = False
    rollout = np.zeros(nindex)
    current_states = all_points
    for t in range(horizon):
        temp = (discount ** t) * reward_function(current_states).ravel()
        rollout += temp
        if np.max(np.abs(temp)) < tol:
            converged = True
            break
        current_states = closed_loop_dynamics(current_states)
    if converged:
        print('Reward sums converged after {} steps!'.format(t + 1))
    else:
        print('Reward sums did not converge!')
    return rollout
def reward_rollout ( grid , closed_loop_dynamics , reward_function , discount , horizon = 250 , tol = 1e-3 ) : if isinstance ( grid , np . ndarray ) : all_points = grid nindex = grid . shape [ 0 ] else : # grid is a GridWorld instance all_points = grid . all_points nindex = grid . nindex converged = False rollout = np . zeros ( nindex ) current_states = all_points for t in range ( horizon ) : temp = ( discount ** t ) * reward_function ( current_states ) . ravel ( ) rollout += temp if np . max ( np . abs ( temp ) ) < tol : converged = True break current_states = closed_loop_dynamics ( current_states ) if converged : print ( 'Reward sums converged after {} steps!' . format ( t + 1 ) ) else : print ( 'Reward sums did not converge!' ) return rollout