예제 #1
0
def build_dummy_structured_env() -> DummyStructuredEnvironment:
    """
    Instantiates a DummyStructuredEnvironment.

    :return: Instance of a DummyStructuredEnvironment
    """
    return DummyStructuredEnvironment(maze_env=build_dummy_maze_env())
예제 #2
0
def test_records_once_per_maze_step_in_multistep_envs():
    """In multi-step envs, trajectory should be recorded once per Maze env step (not in each sub-step)."""

    observation_conversion = ObservationConversion()
    maze_env = DummyEnvironment(
        core_env=DummyCoreEnvironment(observation_conversion.space()),
        action_conversion=[DictActionConversion()],
        observation_conversion=[observation_conversion]
    )
    env = DummyStructuredEnvironment(maze_env)

    class TestWriter(TrajectoryWriter):
        """Mock writer checking the recorded data"""

        def __init__(self):
            self.episode_count = 0
            self.step_count = 0

        def write(self, episode_record: StateTrajectoryRecord):
            """Count steps and episodes"""
            self.episode_count += 1
            self.step_count += len(episode_record.step_records)

    writer = TestWriter()
    TrajectoryWriterRegistry.writers = []  # Ensure there is no other writer
    TrajectoryWriterRegistry.register_writer(writer)

    env = TrajectoryRecordingWrapper.wrap(env)
    for _ in range(5):
        env.reset()
        for i in range(10):
            env.step(env.action_space.sample())

    # final env reset required
    env.reset()
    env.close()

    # The step count should correspond to core env steps (disregarding sub-steps),
    # I.e., 5 flat steps per 10 structured + 1 final state for each episode
    assert writer.step_count == 5 * (5 + 1)
    assert writer.episode_count == 5
예제 #3
0
def test_dummy_struct_env():
    """
    Unit test for the DummyStructuredEnvironment
    """
    maze_env = build_dummy_maze_env()
    env = DummyStructuredEnvironment(maze_env)
    env.reset()

    # check observation space
    assert isinstance(env.observation_space, spaces.Dict)

    for i in range(10):
        action = env.action_spaces_dict[env.actor_id()[0]].sample()
        observation, _, _, _ = env.step(action)

    env.close()
예제 #4
0
def build_dummy_structured_environment() -> DummyStructuredEnvironment:
    """
    Instantiates the DummyStructuredEnvironment.

    :return: Instance of a DummyStructuredEnvironment
    """

    observation_conversion = ObservationConversion()

    maze_env = DummyEnvironment(
        core_env=DummyCoreEnvironment(observation_conversion.space()),
        action_conversion=[DictActionConversion()],
        observation_conversion=[observation_conversion])

    return DummyStructuredEnvironment(maze_env=maze_env)
예제 #5
0
def build_dummy_structured_environment() -> DummyStructuredEnvironment:
    """Instantiates the DummyStructuredEnvironment."""
    return DummyStructuredEnvironment(maze_env=build_dummy_maze_environment())