def _with_is_last(self, iterable): """ Patch for this 'with_is_last' which messes up iterating over an RL env. """ if isinstance(iterable, gym.Env) and has_wrapper(iterable, GymDataLoader): env = iterable assert isinstance(env, IterableWrapper), env while not env.is_closed(): for step, obs in enumerate(env): if env.is_closed(): yield obs, True break else: yield obs, False else: yield from super()._with_is_last(iterable)
def _wrap_env(env: GymEnv, verbose: int = 0, monitor_wrapper: bool = True) -> VecEnv: """ " Wrap environment with the appropriate wrappers if needed. For instance, to have a vectorized environment or to re-order the image channels. :param env: :param verbose: :param monitor_wrapper: Whether to wrap the env in a ``Monitor`` when possible. :return: The wrapped environment. """ # if not isinstance(env, VecEnv): if not (isinstance(env, (VecEnv, VectorEnv)) or isinstance(env.unwrapped, (VecEnv, VectorEnv))): # if not is_wrapped(env, Monitor) and monitor_wrapper: if monitor_wrapper and not (is_wrapped(env, Monitor) or is_wrapped(env, gym.wrappers.Monitor) or has_wrapper(env, gym.wrappers.Monitor)): if verbose >= 1: print("Wrapping the env with a `Monitor` wrapper") env = Monitor(env) if verbose >= 1: print("Wrapping the env in a DummyVecEnv.") env = DummyVecEnv([lambda: env]) if is_image_space( env.observation_space) and not is_wrapped(env, VecTransposeImage): if verbose >= 1: print("Wrapping the env in a VecTransposeImage.") env = VecTransposeImage(env) # check if wrapper for dict support is needed when using HER if isinstance(env.observation_space, gym.spaces.dict.Dict): env = ObsDictWrapper(env) return env