Improve error messages

This commit is contained in:
Antonin RAFFIN 2020-04-27 11:12:19 +02:00
parent 52ad917a3a
commit 559f43b522

View file

@ -226,9 +226,9 @@ class BaseRLModel(ABC):
return self._vec_normalize_env return self._vec_normalize_env
@staticmethod @staticmethod
def check_env(env: GymEnv, observation_space: gym.spaces.Space, action_space: gym.spaces.Space) -> bool: def check_env(env: GymEnv, observation_space: gym.spaces.Space, action_space: gym.spaces.Space):
""" """
Checks the validity of the environment and returns if it is consistent. Checks the validity of the environment to load vs the one used for training.
Checked parameters: Checked parameters:
- observation_space - observation_space
- action_space - action_space
@ -236,18 +236,15 @@ class BaseRLModel(ABC):
:param env: (GymEnv) :param env: (GymEnv)
:param observation_space: (gym.spaces.Space) :param observation_space: (gym.spaces.Space)
:param action_space: (gym.spaces.Space) :param action_space: (gym.spaces.Space)
:return: (bool) True if environment seems to be coherent
""" """
if (observation_space != env.observation_space if (observation_space != env.observation_space
# Special cases for images that need to be transposed # Special cases for images that need to be transposed
and not (is_image_space(env.observation_space) and not (is_image_space(env.observation_space)
and observation_space == VecTransposeImage.transpose_space(env.observation_space) and observation_space == VecTransposeImage.transpose_space(env.observation_space)
)): )):
return False raise ValueError(f'Observation spaces do not match: {observation_space} != {env.observation_space}')
if action_space != env.action_space: if action_space != env.action_space:
return False raise ValueError(f'Action spaces do not match: {action_space} != {env.action_space}')
# return true if no check failed
return True
def set_env(self, env: GymEnv) -> None: def set_env(self, env: GymEnv) -> None:
""" """
@ -259,9 +256,7 @@ class BaseRLModel(ABC):
:param env: The environment for learning a policy :param env: The environment for learning a policy
""" """
if self.check_env(env, self.observation_space, self.action_space) is False: self.check_env(env, self.observation_space, self.action_space)
raise ValueError("The given environment is not compatible with model: "
"observation and action spaces do not match")
# it must be coherent now # it must be coherent now
# if it is not a VecEnv, make it a VecEnv # if it is not a VecEnv, make it a VecEnv
env = self._wrap_env(env) env = self._wrap_env(env)
@ -346,8 +341,8 @@ class BaseRLModel(ABC):
if ("observation_space" not in data or "action_space" not in data) and "env" not in data: if ("observation_space" not in data or "action_space" not in data) and "env" not in data:
raise ValueError("The observation_space and action_space was not given, can't verify new environments") raise ValueError("The observation_space and action_space was not given, can't verify new environments")
# check if given env is valid # check if given env is valid
if env is not None and cls.check_env(env, data["observation_space"], data["action_space"]) is False: if env is not None:
raise ValueError("The given environment does not comply to the model") cls.check_env(env, data["observation_space"], data["action_space"])
# if no new env was given use stored env if possible # if no new env was given use stored env if possible
if env is None and "env" in data: if env is None and "env" in data:
env = data["env"] env = data["env"]