Esempio n. 1
0
    def to_base_env(env,
                    make_env=None,
                    num_envs=1,
                    remote_envs=False,
                    async_remote_envs=False):
        """Wraps any env type as needed to expose the async interface."""

        from ray.rllib.env.remote_vector_env import RemoteVectorEnv
        if (remote_envs or async_remote_envs) and num_envs == 1:
            raise ValueError(
                "Remote envs only make sense to use if num_envs > 1 "
                "(i.e. vectorization is enabled).")
        if remote_envs and async_remote_envs:
            raise ValueError("You can only specify one of remote_envs or "
                             "async_remote_envs.")

        if not isinstance(env, BaseEnv):
            if isinstance(env, MultiAgentEnv):
                if remote_envs:
                    env = RemoteVectorEnv(make_env,
                                          num_envs,
                                          multiagent=True,
                                          sync=True)
                elif async_remote_envs:
                    env = RemoteVectorEnv(make_env,
                                          num_envs,
                                          multiagent=True,
                                          sync=False)
                else:
                    env = _MultiAgentEnvToBaseEnv(make_env=make_env,
                                                  existing_envs=[env],
                                                  num_envs=num_envs)
            elif isinstance(env, ExternalEnv):
                if num_envs != 1:
                    raise ValueError(
                        "ExternalEnv does not currently support num_envs > 1.")
                env = _ExternalEnvToBaseEnv(env)
            elif isinstance(env, VectorEnv):
                env = _VectorEnvToBaseEnv(env)
            else:
                if remote_envs:
                    env = RemoteVectorEnv(make_env,
                                          num_envs,
                                          multiagent=False,
                                          sync=True)
                elif async_remote_envs:
                    env = RemoteVectorEnv(make_env,
                                          num_envs,
                                          multiagent=False,
                                          sync=False)
                else:
                    env = VectorEnv.wrap(
                        make_env=make_env,
                        existing_envs=[env],
                        num_envs=num_envs,
                        action_space=env.action_space,
                        observation_space=env.observation_space)
                    env = _VectorEnvToBaseEnv(env)
        assert isinstance(env, BaseEnv), env
        return env
Esempio n. 2
0
    def to_base_env(env, make_env=None, num_envs=1, remote_envs=False):
        """Wraps any env type as needed to expose the async interface."""
        if remote_envs and num_envs == 1:
            raise ValueError(
                "Remote envs only make sense to use if num_envs > 1 "
                "(i.e. vectorization is enabled).")
        if not isinstance(env, BaseEnv):
            if isinstance(env, MultiAgentEnv):
                if remote_envs:
                    raise NotImplementedError(
                        "Remote multiagent environments are not implemented")

                env = _MultiAgentEnvToBaseEnv(make_env=make_env,
                                              existing_envs=[env],
                                              num_envs=num_envs)
            elif isinstance(env, ExternalEnv):
                if num_envs != 1:
                    raise ValueError(
                        "ExternalEnv does not currently support num_envs > 1.")
                env = _ExternalEnvToBaseEnv(env)
            elif isinstance(env, VectorEnv):
                env = _VectorEnvToBaseEnv(env)
            else:
                env = VectorEnv.wrap(make_env=make_env,
                                     existing_envs=[env],
                                     num_envs=num_envs,
                                     remote_envs=remote_envs,
                                     action_space=env.action_space,
                                     observation_space=env.observation_space)
                env = _VectorEnvToBaseEnv(env)
        assert isinstance(env, BaseEnv)
        return env
Esempio n. 3
0
    def to_base_env(
        env: EnvType,
        make_env: Callable[[int], EnvType] = None,
        num_envs: int = 1,
        remote_envs: bool = False,
        remote_env_batch_wait_ms: int = 0,
        policy_config: PartialTrainerConfigDict = None,
    ) -> "BaseEnv":
        """Wraps any env type as needed to expose the async interface."""

        from ray.rllib.env.remote_vector_env import RemoteVectorEnv
        if remote_envs and num_envs == 1:
            raise ValueError(
                "Remote envs only make sense to use if num_envs > 1 "
                "(i.e. vectorization is enabled).")

        if not isinstance(env, BaseEnv):
            if isinstance(env, MultiAgentEnv):
                if remote_envs:
                    env = RemoteVectorEnv(
                        make_env,
                        num_envs,
                        multiagent=True,
                        remote_env_batch_wait_ms=remote_env_batch_wait_ms)
                else:
                    env = _MultiAgentEnvToBaseEnv(make_env=make_env,
                                                  existing_envs=[env],
                                                  num_envs=num_envs)
            elif isinstance(env, ExternalEnv):
                if num_envs != 1:
                    raise ValueError(
                        "External(MultiAgent)Env does not currently support "
                        "num_envs > 1. One way of solving this would be to "
                        "treat your Env as a MultiAgentEnv hosting only one "
                        "type of agent but with several copies.")
                env = _ExternalEnvToBaseEnv(env)
            elif isinstance(env, VectorEnv):
                env = _VectorEnvToBaseEnv(env)
            else:
                if remote_envs:
                    env = RemoteVectorEnv(
                        make_env,
                        num_envs,
                        multiagent=False,
                        remote_env_batch_wait_ms=remote_env_batch_wait_ms,
                        existing_envs=[env],
                    )
                else:
                    env = VectorEnv.wrap(
                        make_env=make_env,
                        existing_envs=[env],
                        num_envs=num_envs,
                        action_space=env.action_space,
                        observation_space=env.observation_space,
                        policy_config=policy_config,
                    )
                    env = _VectorEnvToBaseEnv(env)
        assert isinstance(env, BaseEnv), env
        return env
Esempio n. 4
0
    def to_base_env(env,
                    make_env=None,
                    num_envs=1,
                    remote_envs=False,
                    async_remote_envs=False):
        """Wraps any env type as needed to expose the async interface."""

        from ray.rllib.env.remote_vector_env import RemoteVectorEnv
        if (remote_envs or async_remote_envs) and num_envs == 1:
            raise ValueError(
                "Remote envs only make sense to use if num_envs > 1 "
                "(i.e. vectorization is enabled).")
        if remote_envs and async_remote_envs:
            raise ValueError("You can only specify one of remote_envs or "
                             "async_remote_envs.")

        if not isinstance(env, BaseEnv):
            if isinstance(env, MultiAgentEnv):
                if remote_envs:
                    env = RemoteVectorEnv(
                        make_env, num_envs, multiagent=True, sync=True)
                elif async_remote_envs:
                    env = RemoteVectorEnv(
                        make_env, num_envs, multiagent=True, sync=False)
                else:
                    env = _MultiAgentEnvToBaseEnv(
                        make_env=make_env,
                        existing_envs=[env],
                        num_envs=num_envs)
            elif isinstance(env, ExternalEnv):
                if num_envs != 1:
                    raise ValueError(
                        "ExternalEnv does not currently support num_envs > 1.")
                env = _ExternalEnvToBaseEnv(env)
            elif isinstance(env, VectorEnv):
                env = _VectorEnvToBaseEnv(env)
            else:
                if remote_envs:
                    env = RemoteVectorEnv(
                        make_env, num_envs, multiagent=False, sync=True)
                elif async_remote_envs:
                    env = RemoteVectorEnv(
                        make_env, num_envs, multiagent=False, sync=False)
                else:
                    env = VectorEnv.wrap(
                        make_env=make_env,
                        existing_envs=[env],
                        num_envs=num_envs,
                        action_space=env.action_space,
                        observation_space=env.observation_space)
                    env = _VectorEnvToBaseEnv(env)
        assert isinstance(env, BaseEnv), env
        return env
Esempio n. 5
0
 def wrap_async(env, make_env=None, num_envs=1):
     """Wraps any env type as needed to expose the async interface."""
     if not isinstance(env, AsyncVectorEnv):
         if isinstance(env, MultiAgentEnv):
             env = _MultiAgentEnvToAsync(
                 make_env=make_env, existing_envs=[env], num_envs=num_envs)
         elif isinstance(env, ServingEnv):
             if num_envs != 1:
                 raise ValueError(
                     "ServingEnv does not currently support num_envs > 1.")
             env = _ServingEnvToAsync(env)
         elif isinstance(env, VectorEnv):
             env = _VectorEnvToAsync(env)
         else:
             env = VectorEnv.wrap(
                 make_env=make_env, existing_envs=[env], num_envs=num_envs)
             env = _VectorEnvToAsync(env)
     assert isinstance(env, AsyncVectorEnv)
     return env
Esempio n. 6
0
 def wrap_async(env, make_env=None, num_envs=1):
     """Wraps any env type as needed to expose the async interface."""
     if not isinstance(env, AsyncVectorEnv):
         if isinstance(env, MultiAgentEnv):
             env = _MultiAgentEnvToAsync(
                 make_env=make_env, existing_envs=[env], num_envs=num_envs)
         elif isinstance(env, ExternalEnv):
             if num_envs != 1:
                 raise ValueError(
                     "ExternalEnv does not currently support num_envs > 1.")
             env = _ExternalEnvToAsync(env)
         elif isinstance(env, VectorEnv):
             env = _VectorEnvToAsync(env)
         else:
             env = VectorEnv.wrap(
                 make_env=make_env, existing_envs=[env], num_envs=num_envs)
             env = _VectorEnvToAsync(env)
     assert isinstance(env, AsyncVectorEnv)
     return env
Esempio n. 7
0
 def test_nested_tuple_vector(self):
     self.do_test_nested_tuple(
         lambda _: VectorEnv.wrap(lambda i: NestedTupleEnv()))
Esempio n. 8
0
 def test_nested_dict_vector(self):
     self.do_test_nested_dict(
         lambda _: VectorEnv.wrap(lambda i: NestedDictEnv()))
 def test_vector_step(self):
     env = VectorEnv.wrap(lambda _: MockEnvDictSubclass(), num_envs=3)
     env.vector_step([0] * 3)
Esempio n. 10
0
 def testNestedTupleVector(self):
     self.doTestNestedTuple(
         lambda _: VectorEnv.wrap(lambda i: NestedTupleEnv()))
Esempio n. 11
0
 def testNestedDictVector(self):
     self.doTestNestedDict(
         lambda _: VectorEnv.wrap(lambda i: NestedDictEnv()))
Esempio n. 12
0
 def testNestedTupleVector(self):
     self.doTestNestedTuple(
         lambda _: VectorEnv.wrap(lambda i: NestedTupleEnv()))
Esempio n. 13
0
 def testNestedDictVector(self):
     self.doTestNestedDict(
         lambda _: VectorEnv.wrap(lambda i: NestedDictEnv()))