(dynamics, policy, state_dim, batch_size, action_limit=None, zero_pad=0)
return constrained_batch
Sample states that do not map outside a bounded state space or to saturated control inputs.
Sample states that do not map outside a bounded state space or to saturated control inputs.
Sample states that do not map outside a bounded state space or to saturated control inputs .
def constrained_batch_sampler(dynamics, policy, state_dim, batch_size, action_limit=None, zero_pad=0):
    """Sample states that do not map outside a bounded state space or to saturated control inputs."""
    batch = tf.random_uniform([int(batch_size), state_dim], -1, 1,
                              dtype=TF_DTYPE, name='batch_sample')
    actions = policy(batch)
    future_batch = dynamics(batch, actions)
    maps_inside = tf.reduce_all(tf.logical_and(future_batch >= -1, future_batch <= 1), axis=1)
    maps_inside_idx = tf.squeeze(tf.where(maps_inside))
    constrained_batch = tf.gather(batch, maps_inside_idx)
    if action_limit is not None:
        c = np.abs(action_limit)
        undersaturated = tf.reduce_all(tf.logical_and(actions >= -c, actions <= c), axis=1)
        undersaturated_idx = tf.squeeze(tf.where(undersaturated))
        constrained_batch = tf.gather(batch, undersaturated_idx)
    if constrained_batch.get_shape()[0] == 0:
        constrained_batch = tf.zeros([1, state_dim], dtype=TF_DTYPE)
    if zero_pad > 0:
        zero_padding = tf.constant([[0, int(zero_pad)], [0, 0]])
        constrained_batch = tf.pad(constrained_batch, zero_padding)
    return constrained_batch
def constrained_batch_sampler ( dynamics , policy , state_dim , batch_size , action_limit = None , zero_pad = 0 ) : batch = tf . random_uniform ( [ int ( batch_size ) , state_dim ] , - 1 , 1 , dtype = TF_DTYPE , name = 'batch_sample' ) actions = policy ( batch ) future_batch = dynamics ( batch , actions ) maps_inside = tf . reduce_all ( tf . logical_and ( future_batch >= - 1 , future_batch <= 1 ) , axis = 1 ) maps_inside_idx = tf . squeeze ( tf . where ( maps_inside ) ) constrained_batch = tf . gather ( batch , maps_inside_idx ) if action_limit is not None : c = np . abs ( action_limit ) undersaturated = tf . reduce_all ( tf . logical_and ( actions >= - c , actions <= c ) , axis = 1 ) undersaturated_idx = tf . squeeze ( tf . where ( undersaturated ) ) constrained_batch = tf . gather ( batch , undersaturated_idx ) if constrained_batch . get_shape ( ) [ 0 ] == 0 : constrained_batch = tf . zeros ( [ 1 , state_dim ] , dtype = TF_DTYPE ) if zero_pad > 0 : zero_padding = tf . constant ( [ [ 0 , int ( zero_pad ) ] , [ 0 , 0 ] ] ) constrained_batch = tf . pad ( constrained_batch , zero_padding ) return constrained_batch