2019-09-20 13:18:25 +00:00
|
|
|
# flake8: noqa F401
|
2020-03-12 11:34:25 +00:00
|
|
|
import typing
|
2019-11-14 13:35:00 +00:00
|
|
|
from copy import deepcopy
|
2020-08-05 10:12:02 +00:00
|
|
|
from typing import Optional, Type, Union
|
|
|
|
|
|
|
|
|
|
from stable_baselines3.common.vec_env.base_vec_env import CloudpickleWrapper, VecEnv, VecEnvWrapper
|
2020-05-05 13:02:35 +00:00
|
|
|
from stable_baselines3.common.vec_env.dummy_vec_env import DummyVecEnv
|
2021-05-11 10:29:30 +00:00
|
|
|
from stable_baselines3.common.vec_env.stacked_observations import StackedDictObservations, StackedObservations
|
2020-05-05 13:02:35 +00:00
|
|
|
from stable_baselines3.common.vec_env.subproc_vec_env import SubprocVecEnv
|
2020-07-16 14:12:16 +00:00
|
|
|
from stable_baselines3.common.vec_env.vec_check_nan import VecCheckNan
|
2021-04-13 16:09:31 +00:00
|
|
|
from stable_baselines3.common.vec_env.vec_extract_dict_obs import VecExtractDictObs
|
2020-05-05 13:02:35 +00:00
|
|
|
from stable_baselines3.common.vec_env.vec_frame_stack import VecFrameStack
|
2021-04-13 16:09:31 +00:00
|
|
|
from stable_baselines3.common.vec_env.vec_monitor import VecMonitor
|
2020-05-05 13:02:35 +00:00
|
|
|
from stable_baselines3.common.vec_env.vec_normalize import VecNormalize
|
|
|
|
|
from stable_baselines3.common.vec_env.vec_transpose import VecTransposeImage
|
2020-05-05 14:28:38 +00:00
|
|
|
from stable_baselines3.common.vec_env.vec_video_recorder import VecVideoRecorder
|
2019-11-14 13:35:00 +00:00
|
|
|
|
2020-03-12 11:34:25 +00:00
|
|
|
# Avoid circular import
|
|
|
|
|
if typing.TYPE_CHECKING:
|
2020-05-05 13:02:35 +00:00
|
|
|
from stable_baselines3.common.type_aliases import GymEnv
|
2019-11-14 13:35:00 +00:00
|
|
|
|
2020-03-12 11:34:25 +00:00
|
|
|
|
2020-08-05 10:12:02 +00:00
|
|
|
def unwrap_vec_wrapper(env: Union["GymEnv", VecEnv], vec_wrapper_class: Type[VecEnvWrapper]) -> Optional[VecEnvWrapper]:
|
2019-11-14 13:35:00 +00:00
|
|
|
"""
|
2020-08-05 10:12:02 +00:00
|
|
|
Retrieve a ``VecEnvWrapper`` object by recursively searching.
|
|
|
|
|
|
2020-10-02 17:05:55 +00:00
|
|
|
:param env:
|
|
|
|
|
:param vec_wrapper_class:
|
|
|
|
|
:return:
|
2019-11-14 13:35:00 +00:00
|
|
|
"""
|
|
|
|
|
env_tmp = env
|
|
|
|
|
while isinstance(env_tmp, VecEnvWrapper):
|
2020-08-05 10:12:02 +00:00
|
|
|
if isinstance(env_tmp, vec_wrapper_class):
|
2019-11-14 13:35:00 +00:00
|
|
|
return env_tmp
|
|
|
|
|
env_tmp = env_tmp.venv
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
2020-08-05 10:12:02 +00:00
|
|
|
def unwrap_vec_normalize(env: Union["GymEnv", VecEnv]) -> Optional[VecNormalize]:
|
|
|
|
|
"""
|
2020-10-02 17:05:55 +00:00
|
|
|
:param env:
|
|
|
|
|
:return:
|
2020-08-05 10:12:02 +00:00
|
|
|
"""
|
|
|
|
|
return unwrap_vec_wrapper(env, VecNormalize) # pytype:disable=bad-return-type
|
|
|
|
|
|
|
|
|
|
|
2020-11-16 10:52:28 +00:00
|
|
|
def is_vecenv_wrapped(env: Union["GymEnv", VecEnv], vec_wrapper_class: Type[VecEnvWrapper]) -> bool:
|
2020-10-22 09:56:43 +00:00
|
|
|
"""
|
|
|
|
|
Check if an environment is already wrapped by a given ``VecEnvWrapper``.
|
|
|
|
|
|
|
|
|
|
:param env:
|
|
|
|
|
:param vec_wrapper_class:
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
return unwrap_vec_wrapper(env, vec_wrapper_class) is not None
|
|
|
|
|
|
|
|
|
|
|
2019-11-14 13:35:00 +00:00
|
|
|
# Define here to avoid circular import
|
2020-07-16 14:12:16 +00:00
|
|
|
def sync_envs_normalization(env: "GymEnv", eval_env: "GymEnv") -> None:
|
2019-11-14 13:35:00 +00:00
|
|
|
"""
|
|
|
|
|
Sync eval env and train env when using VecNormalize
|
|
|
|
|
|
2020-10-02 17:05:55 +00:00
|
|
|
:param env:
|
|
|
|
|
:param eval_env:
|
2019-11-14 13:35:00 +00:00
|
|
|
"""
|
|
|
|
|
env_tmp, eval_env_tmp = env, eval_env
|
|
|
|
|
while isinstance(env_tmp, VecEnvWrapper):
|
|
|
|
|
if isinstance(env_tmp, VecNormalize):
|
2022-05-08 18:54:34 +00:00
|
|
|
# Only synchronize if observation normalization exists
|
|
|
|
|
if hasattr(env_tmp, "obs_rms"):
|
|
|
|
|
eval_env_tmp.obs_rms = deepcopy(env_tmp.obs_rms)
|
2020-03-12 11:34:25 +00:00
|
|
|
eval_env_tmp.ret_rms = deepcopy(env_tmp.ret_rms)
|
2019-11-14 13:35:00 +00:00
|
|
|
env_tmp = env_tmp.venv
|
2020-01-20 10:17:55 +00:00
|
|
|
eval_env_tmp = eval_env_tmp.venv
|