Checks the validity of the environment, and if it is coherent, set it as the current environment. Furthermore wrap any non vectorized env into a vectorized checked parameters: - observation_space - action_space :param env: The environment for learnin
(self, env: GymEnv, force_reset: bool = True)
| 474 | return self._vec_normalize_env |
| 475 | |
| 476 | def set_env(self, env: GymEnv, force_reset: bool = True) -> None: |
| 477 | """ |
| 478 | Checks the validity of the environment, and if it is coherent, set it as the current environment. |
| 479 | Furthermore wrap any non vectorized env into a vectorized |
| 480 | checked parameters: |
| 481 | - observation_space |
| 482 | - action_space |
| 483 | |
| 484 | :param env: The environment for learning a policy |
| 485 | :param force_reset: Force call to ``reset()`` before training |
| 486 | to avoid unexpected behavior. |
| 487 | See issue https://github.com/DLR-RM/stable-baselines3/issues/597 |
| 488 | """ |
| 489 | # if it is not a VecEnv, make it a VecEnv |
| 490 | # and do other transformations (dict obs, image transpose) if needed |
| 491 | env = self._wrap_env(env, self.verbose) |
| 492 | assert env.num_envs == self.n_envs, ( |
| 493 | "The number of environments to be set is different from the number of environments in the model: " |
| 494 | f"({env.num_envs} != {self.n_envs}), whereas `set_env` requires them to be the same. To load a model with " |
| 495 | f"a different number of environments, you must use `{self.__class__.__name__}.load(path, env)` instead" |
| 496 | ) |
| 497 | # Check that the observation spaces match |
| 498 | check_for_correct_spaces(env, self.observation_space, self.action_space) |
| 499 | # Update VecNormalize object |
| 500 | # otherwise the wrong env may be used, see https://github.com/DLR-RM/stable-baselines3/issues/637 |
| 501 | self._vec_normalize_env = unwrap_vec_normalize(env) |
| 502 | |
| 503 | # Discard `_last_obs`, this will force the env to reset before training |
| 504 | # See issue https://github.com/DLR-RM/stable-baselines3/issues/597 |
| 505 | if force_reset: |
| 506 | self._last_obs = None |
| 507 | |
| 508 | self.n_envs = env.num_envs |
| 509 | self.env = env |
| 510 | |
| 511 | @abstractmethod |
| 512 | def learn( |