def trajectory(obs_space: gym.Space, act_space: gym.Space, length: int) -> types.Trajectory: """Fixture to generate trajectory of length `length` iid sampled from spaces.""" obs = np.array([obs_space.sample() for _ in range(length + 1)]) acts = np.array([act_space.sample() for _ in range(length)]) infos = np.array([{} for _ in range(length)]) return types.Trajectory(obs=obs, acts=acts, infos=infos)
def _slice_trajectory(trajectory: types.Trajectory, start: int, end: int) -> types.Trajectory: """Slice trajectory from timestep start to timestep end.""" infos = trajectory.infos[ start:end] if trajectory.infos is not None else None return types.Trajectory( obs=trajectory.obs[start:end + 1], acts=trajectory.acts[start:end], infos=infos, )
def test_zero_length_fails(): """Check zero-length trajectory and transitions fail.""" empty = np.array([]) with pytest.raises(ValueError, match=r"Degenerate trajectory.*"): types.Trajectory(obs=np.array([42]), acts=empty, infos=None) with pytest.raises(ValueError, match=r"Must have non-zero number of.*"): types.Transitions( obs=empty, acts=empty, next_obs=empty, dones=empty.astype(np.bool), )
def test_zero_length_fails(): """Check zero-length trajectory and transitions fail.""" empty = np.array([]) with pytest.raises(ValueError, match=r"Degenerate trajectory.*"): types.Trajectory(obs=np.array([42]), acts=empty, infos=None)