def __init__(self, observable, physics, random_state, strip_singleton_buffer_dim, pad_with_initial_value): self.observable = observable self.observation_callable = (observable.observation_callable( physics, random_state)) self._bind_attribute_from_observable('update_interval', DEFAULT_UPDATE_INTERVAL, random_state) self._bind_attribute_from_observable('delay', DEFAULT_DELAY, random_state) self._bind_attribute_from_observable('buffer_size', DEFAULT_BUFFER_SIZE, random_state) obs_spec = self.observable.array_spec if obs_spec is None: # We take an observation to determine the shape and dtype of the array. # This occurs outside of an episode and doesn't affect environment # behavior. At this point the physics state is not guaranteed to be valid, # so we might get a `PhysicsError` if the observation callable calls # `physics.forward`. We suppress such errors since they do not matter as # far as the shape and dtype of the observation are concerned. with physics.suppress_physics_errors(): obs_array = self.observation_callable() obs_array = np.asarray(obs_array) obs_spec = specs.Array(shape=obs_array.shape, dtype=obs_array.dtype) self.buffer = obs_buffer.Buffer( buffer_size=self.buffer_size, shape=obs_spec.shape, dtype=obs_spec.dtype, pad_with_initial_value=pad_with_initial_value, strip_singleton_buffer_dim=strip_singleton_buffer_dim) self.update_schedule = collections.deque()
def testStripSingletonDimension(self, shape): buf = obs_buffer.Buffer(buffer_size=1, shape=shape, dtype=np.float, strip_singleton_buffer_dim=True) expected_value = np.full(shape, 42, dtype=np.float) buf.insert(timestamp=0, delay=0, value=expected_value) np.testing.assert_array_equal(buf.read(current_time=1), expected_value)
def testOutOfOrderArrival(self): buf = obs_buffer.Buffer(buffer_size=3, shape=(), dtype=float) buf.insert(timestamp=0, delay=4, value=1) buf.insert(timestamp=1, delay=2, value=2) buf.insert(timestamp=2, delay=3, value=3) np.testing.assert_array_equal(buf.read(current_time=2), [0., 0., 0.]) np.testing.assert_array_equal(buf.read(current_time=3), [0., 0., 2.]) np.testing.assert_array_equal(buf.read(current_time=4), [0., 2., 1.]) np.testing.assert_array_equal(buf.read(current_time=5), [2., 1., 3.]) np.testing.assert_array_equal(buf.read(current_time=6), [2., 1., 3.])
def testPlanTwoStepsAhead(self): buf = obs_buffer.Buffer( buffer_size=1, shape=(), dtype=np.float) control_timestep = 5 observation_schedule = _generate_constant_schedule( update_timestep=2, delay=3, control_timestep=control_timestep, n_observed_steps=2) buf.drop_unobserved_upcoming_items( observation_schedule, read_interval=control_timestep) self.assertEqual(observation_schedule, [(2, 3), (6, 3), (10, 3)])
def testPlanToSingleUndelayedObservation(self): buf = obs_buffer.Buffer( buffer_size=1, shape=(), dtype=np.float) control_timestep = 20 observation_schedule = _generate_constant_schedule( update_timestep=1, delay=0, control_timestep=control_timestep, n_observed_steps=1) buf.drop_unobserved_upcoming_items( observation_schedule, read_interval=control_timestep) self.assertEqual(observation_schedule, [(20, 0)])
def __init__(self, observable, physics, random_state, strip_singleton_buffer_dim): self.observable = observable # We take an observation here to determine the shape and size. # This occurs outside of an episode and doesn't affect environment behavior. obs_value = np.array( observable.observation_callable(physics, random_state)()) self.buffer = obs_buffer.Buffer( buffer_size=(observable.buffer_size or DEFAULT_BUFFER_SIZE), shape=obs_value.shape, dtype=obs_value.dtype, strip_singleton_buffer_dim=strip_singleton_buffer_dim) self.update_schedule = collections.deque()