Exemplo n.º 1
0
def test_metadata():
    md = bindings.PluginMetadata()
    assert not md.isValid(), "The metadata should not be valid"

    environment_name = "EnvironmentName"
    md.setEnvironmentName(environment_name)
    assert md.getEnvironmentName() == environment_name, "Failed to store the " \
                                                        "environment name"
    library_name = "plugin_foo"
    md.setLibraryName(library_name)
    assert md.getLibraryName() == library_name, "Failed to store the library name"

    class_name = "foo::bar::GymppPlugin"
    md.setClassName(class_name)
    assert md.getClassName() == class_name, "Failed to store the class name"

    model_name = "RobotModel.sdf"
    md.setModelFileName(model_name)
    assert md.getModelFileName() == model_name, "Failed to store the model file name"

    world_name = "Environment.world"
    md.setWorldFileName(world_name)
    assert md.getWorldFileName() == world_name, "Failed to store the world file name"

    agent_rate = 1000
    md.setAgentRate(agent_rate)
    assert md.getAgentRate() == agent_rate, "Failed to store the agent rate"

    real_time_factor = 1E9
    max_physics_step_size = 0.001
    md.setPhysicsData(bindings.PhysicsData(real_time_factor, max_physics_step_size))
    physics_data = md.getPhysicsData()
    assert physics_data.rtf == real_time_factor, "Failed to store RTF"
    assert physics_data.maxStepSize == max_physics_step_size, "Failed to store the " \
                                                              "max physics step size"
Exemplo n.º 2
0
    def _plugin_metadata(self) -> bindings.PluginMetadata:
        md = bindings.PluginMetadata()

        # Configure ignition environment
        md.setEnvironmentName("CartPole")
        md.setLibraryName("CartPolePlugin")
        md.setClassName("gympp::plugins::CartPole")
        md.setWorldFileName("DefaultEmptyWorld.world")
        md.setModelFileName("CartPole/CartPole.sdf")

        md.setAgentRate(1000)

        real_time_factor = 1E9
        max_physics_step_size = 0.001
        md.setPhysicsData(
            bindings.PhysicsData(real_time_factor, max_physics_step_size))

        # Configure the action space
        action_space_md = bindings.SpaceMetadata()
        action_space_md.setType(bindings.SpaceType_Discrete)
        action_space_md.setDimensions([2])

        # Configure the observation space
        observation_space_md = bindings.SpaceMetadata()
        observation_space_md.setType(bindings.SpaceType_Box)
        max_float = float(np.finfo(np.float32).max)
        observation_space_md.setLowLimit([-2.5, -max_float, -24, -max_float])
        observation_space_md.setHighLimit([2.5, max_float, 24, max_float])

        # Store the spaces information
        md.setActionSpaceMetadata(action_space_md)
        md.setObservationSpaceMetadata(observation_space_md)

        # Return the metadata
        assert md.isValid(), "The plugin metadata object is not valid"
        return md
Exemplo n.º 3
0
def test_gymfactory():
    # Get the factory
    factory = bindings.GymFactory.Instance()

    # Register a plugin with empty metadata
    md = bindings.PluginMetadata()
    assert not factory.registerPlugin(md), "The empty plugin metadata should not be valid"

    # Get a not registered environment
    env = factory.make("foo")
    assert not env, "The environment should not be valid"

    # Check that the CartPole plugin exists
    assert "IGN_GAZEBO_SYSTEM_PLUGIN_PATH" in os.environ, \
        "Variable IGN_GAZEBO_SYSTEM_PLUGIN_PATH not set in the environment"
    directories = os.environ['IGN_GAZEBO_SYSTEM_PLUGIN_PATH'].split(os.pathsep)

    found = False
    for directory in directories:
        matching_plugins = list(Path(directory).glob("*CartPole*"))
        found = found or (len(matching_plugins) is not 0)
    assert found, "Failed to find CartPole plugin"

    # Create the metadata
    md = bindings.PluginMetadata()
    md.setEnvironmentName("CartPole")
    md.setLibraryName("CartPolePlugin")
    md.setClassName("gympp::plugins::CartPole")
    md.setWorldFileName("DefaultEmptyWorld.world")
    md.setModelFileName("CartPole/CartPole.urdf")
    md.setAgentRate(1000)
    md.setPhysicsData(bindings.PhysicsData(1.0, 0.001))
    action_space_md = bindings.SpaceMetadata()
    action_space_md.setType(bindings.SpaceType_Discrete)
    action_space_md.setDimensions([2])
    observation_space_md = bindings.SpaceMetadata()
    observation_space_md.setType(bindings.SpaceType_Box)
    max_float = float(np.finfo(np.float32).max)
    observation_space_md.setLowLimit([-2.5, -max_float, -24, -max_float])
    observation_space_md.setHighLimit([2.5, max_float, 24, max_float])
    md.setActionSpaceMetadata(action_space_md)
    md.setObservationSpaceMetadata(observation_space_md)
    assert md.isValid(), "Metadata for CartPole environment is not valid"

    # Register the metadata
    assert factory.registerPlugin(md), "Failed to register the plugin metadata in the " \
                                       "factory"

    # Get the environment
    env = factory.make("CartPole")
    assert env, "Failed to create CartPoleIgnition environment from the factory"

    # Get the gazebo wrapper
    gazebo = bindings.envToGazeboWrapper(env)
    assert gazebo, "Failed to get gazebo wrapper"

    # Get the ignition environment
    ign_env = bindings.envToIgnEnv(env)
    assert ign_env, "Failed to get the ignition environment"

    # Set verbosity
    bindings.GazeboWrapper.setVerbosity(4)

    # Use the environment
    env.reset()
    action = env.action_space.sample()
    state_opt = env.step(action)
    assert state_opt.has_value()

    state = state_opt.value()
    assert list(state.observation.getBuffer_d())

    observation = list(state.observation.getBuffer_d())
    assert len(observation) == 4

    # Try to register another environment
    assert factory.registerPlugin(md), "Failed to re-register the plugin metadata in " \
                                       "the factory"

    # Try to get another environment
    env2 = factory.make("CartPole")
    assert env2, "Failed to create CartPoleIgnition environment from the factory"
    assert env != env2, "Environment created from the factory are the same"
    env2.reset()
    env2.step(action)