Files
Gymnasium/gymnasium/error.py

96 lines
3.1 KiB
Python
Raw Permalink Normal View History

2022-09-08 10:10:07 +01:00
"""Set of Error classes for gymnasium."""
2016-04-27 08:00:58 -07:00
class Error(Exception):
"""Error superclass."""
2016-04-27 08:00:58 -07:00
2021-07-29 02:26:34 +02:00
# Registration errors
class UnregisteredEnv(Error):
"""Raised when the user requests an env from the registry that does not actually exist."""
2021-07-29 02:26:34 +02:00
Remove mandatory version in environment name (#2535) * refactor: required version in env name * refactor: rename env_id function * refactor: regex + function logic * test: some more edge cases + error * refactor: version max/comparison - Move version max/comparison in a method - Remove type ignore to improve type checking * fix: failing tests & error introduced in rebasing * refactor: simplify map and remove type ignore * refactor: remove _is_lesser_version() method * refactor: _versions() to return NamedTuples * chore: remove minor changes * Refactor registration EnvSpec and EnvSpecTree * test: move tests from #2513 here * fix: typing * test: try to fix unregistered env * refactor: change InitVar id name for typing Changed InitVar id name to fix the following typing error: error: Declaration "id" is obscured by a declaration of the same name * refactor: return only the first difflib match * test: improve tear down of added test env * refactor: dataclass fields * refactor: compile regex pattern only once * refactor: `_assert_version_exists()` `_assert_version_exists()` rewritten from @JesseFarebro: https://github.com/openai/gym/pull/2535#discussion_r777573839 * refactor: latest_spec -> latest_versioned_spec * fix: bug + typing + test * Add default/versioned tests * Fix Env field defaults * feat: improve versioned/unversioned env handling * Disallow versioned/unversioned registration/lookup * test: remove warning + check default suggestion Co-authored-by: Jesse Farebrother <jessefarebro@gmail.com>
2022-01-19 13:50:25 -05:00
class NamespaceNotFound(UnregisteredEnv):
"""Raised when the user requests an env from the registry where the namespace doesn't exist."""
Remove mandatory version in environment name (#2535) * refactor: required version in env name * refactor: rename env_id function * refactor: regex + function logic * test: some more edge cases + error * refactor: version max/comparison - Move version max/comparison in a method - Remove type ignore to improve type checking * fix: failing tests & error introduced in rebasing * refactor: simplify map and remove type ignore * refactor: remove _is_lesser_version() method * refactor: _versions() to return NamedTuples * chore: remove minor changes * Refactor registration EnvSpec and EnvSpecTree * test: move tests from #2513 here * fix: typing * test: try to fix unregistered env * refactor: change InitVar id name for typing Changed InitVar id name to fix the following typing error: error: Declaration "id" is obscured by a declaration of the same name * refactor: return only the first difflib match * test: improve tear down of added test env * refactor: dataclass fields * refactor: compile regex pattern only once * refactor: `_assert_version_exists()` `_assert_version_exists()` rewritten from @JesseFarebro: https://github.com/openai/gym/pull/2535#discussion_r777573839 * refactor: latest_spec -> latest_versioned_spec * fix: bug + typing + test * Add default/versioned tests * Fix Env field defaults * feat: improve versioned/unversioned env handling * Disallow versioned/unversioned registration/lookup * test: remove warning + check default suggestion Co-authored-by: Jesse Farebrother <jessefarebro@gmail.com>
2022-01-19 13:50:25 -05:00
class NameNotFound(UnregisteredEnv):
"""Raised when the user requests an env from the registry where the name doesn't exist."""
Remove mandatory version in environment name (#2535) * refactor: required version in env name * refactor: rename env_id function * refactor: regex + function logic * test: some more edge cases + error * refactor: version max/comparison - Move version max/comparison in a method - Remove type ignore to improve type checking * fix: failing tests & error introduced in rebasing * refactor: simplify map and remove type ignore * refactor: remove _is_lesser_version() method * refactor: _versions() to return NamedTuples * chore: remove minor changes * Refactor registration EnvSpec and EnvSpecTree * test: move tests from #2513 here * fix: typing * test: try to fix unregistered env * refactor: change InitVar id name for typing Changed InitVar id name to fix the following typing error: error: Declaration "id" is obscured by a declaration of the same name * refactor: return only the first difflib match * test: improve tear down of added test env * refactor: dataclass fields * refactor: compile regex pattern only once * refactor: `_assert_version_exists()` `_assert_version_exists()` rewritten from @JesseFarebro: https://github.com/openai/gym/pull/2535#discussion_r777573839 * refactor: latest_spec -> latest_versioned_spec * fix: bug + typing + test * Add default/versioned tests * Fix Env field defaults * feat: improve versioned/unversioned env handling * Disallow versioned/unversioned registration/lookup * test: remove warning + check default suggestion Co-authored-by: Jesse Farebrother <jessefarebro@gmail.com>
2022-01-19 13:50:25 -05:00
class VersionNotFound(UnregisteredEnv):
"""Raised when the user requests an env from the registry where the version doesn't exist."""
Remove mandatory version in environment name (#2535) * refactor: required version in env name * refactor: rename env_id function * refactor: regex + function logic * test: some more edge cases + error * refactor: version max/comparison - Move version max/comparison in a method - Remove type ignore to improve type checking * fix: failing tests & error introduced in rebasing * refactor: simplify map and remove type ignore * refactor: remove _is_lesser_version() method * refactor: _versions() to return NamedTuples * chore: remove minor changes * Refactor registration EnvSpec and EnvSpecTree * test: move tests from #2513 here * fix: typing * test: try to fix unregistered env * refactor: change InitVar id name for typing Changed InitVar id name to fix the following typing error: error: Declaration "id" is obscured by a declaration of the same name * refactor: return only the first difflib match * test: improve tear down of added test env * refactor: dataclass fields * refactor: compile regex pattern only once * refactor: `_assert_version_exists()` `_assert_version_exists()` rewritten from @JesseFarebro: https://github.com/openai/gym/pull/2535#discussion_r777573839 * refactor: latest_spec -> latest_versioned_spec * fix: bug + typing + test * Add default/versioned tests * Fix Env field defaults * feat: improve versioned/unversioned env handling * Disallow versioned/unversioned registration/lookup * test: remove warning + check default suggestion Co-authored-by: Jesse Farebrother <jessefarebro@gmail.com>
2022-01-19 13:50:25 -05:00
class DeprecatedEnv(Error):
"""Raised when the user requests an env from the registry with an older version number than the latest env with the same name."""
[WIP] add support for seeding environments (#135) * Make environments seedable * Fix monitor bugs - Set monitor_id before setting the infix. This was a bug that would yield incorrect results with multiple monitors. - Remove extra pid from stats recorder filename. This should be purely cosmetic. * Start uploading seeds in episode_batch * Fix _bigint_from_bytes for python3 * Set seed explicitly in random_agent * Pass through seed argument * Also pass through random state to spaces * Pass random state into the observation/action spaces * Make all _seed methods return the list of used seeds * Switch over to np.random where possible * Start hashing seeds, and also seed doom engine * Fixup seeding determinism in many cases * Seed before loading the ROM * Make seeding more Python3 friendly * Make the MuJoCo skipping a bit more forgiving * Remove debugging PDB calls * Make setInt argument into raw bytes * Validate and upload seeds * Skip box2d * Make seeds smaller, and change representation of seeds in upload * Handle long seeds * Fix RandomAgent example to be deterministic * Handle integer types correctly in Python2 and Python3 * Try caching pip * Try adding swap * Add df and free calls * Bump swap * Bump swap size * Try setting overcommit * Try other sysctls * Try fixing overcommit * Try just setting overcommit_memory=1 * Add explanatory comment * Add what's new section to readme * BUG: Mark ElevatorAction-ram-v0 as non-deterministic for now * Document seed * Move nondetermistic check into spec
2016-05-29 09:07:09 -07:00
2021-07-29 02:26:34 +02:00
Remove mandatory version in environment name (#2535) * refactor: required version in env name * refactor: rename env_id function * refactor: regex + function logic * test: some more edge cases + error * refactor: version max/comparison - Move version max/comparison in a method - Remove type ignore to improve type checking * fix: failing tests & error introduced in rebasing * refactor: simplify map and remove type ignore * refactor: remove _is_lesser_version() method * refactor: _versions() to return NamedTuples * chore: remove minor changes * Refactor registration EnvSpec and EnvSpecTree * test: move tests from #2513 here * fix: typing * test: try to fix unregistered env * refactor: change InitVar id name for typing Changed InitVar id name to fix the following typing error: error: Declaration "id" is obscured by a declaration of the same name * refactor: return only the first difflib match * test: improve tear down of added test env * refactor: dataclass fields * refactor: compile regex pattern only once * refactor: `_assert_version_exists()` `_assert_version_exists()` rewritten from @JesseFarebro: https://github.com/openai/gym/pull/2535#discussion_r777573839 * refactor: latest_spec -> latest_versioned_spec * fix: bug + typing + test * Add default/versioned tests * Fix Env field defaults * feat: improve versioned/unversioned env handling * Disallow versioned/unversioned registration/lookup * test: remove warning + check default suggestion Co-authored-by: Jesse Farebrother <jessefarebro@gmail.com>
2022-01-19 13:50:25 -05:00
class RegistrationError(Error):
"""Raised when the user attempts to register an invalid env. For example, an unversioned env when a versioned env exists."""
Remove mandatory version in environment name (#2535) * refactor: required version in env name * refactor: rename env_id function * refactor: regex + function logic * test: some more edge cases + error * refactor: version max/comparison - Move version max/comparison in a method - Remove type ignore to improve type checking * fix: failing tests & error introduced in rebasing * refactor: simplify map and remove type ignore * refactor: remove _is_lesser_version() method * refactor: _versions() to return NamedTuples * chore: remove minor changes * Refactor registration EnvSpec and EnvSpecTree * test: move tests from #2513 here * fix: typing * test: try to fix unregistered env * refactor: change InitVar id name for typing Changed InitVar id name to fix the following typing error: error: Declaration "id" is obscured by a declaration of the same name * refactor: return only the first difflib match * test: improve tear down of added test env * refactor: dataclass fields * refactor: compile regex pattern only once * refactor: `_assert_version_exists()` `_assert_version_exists()` rewritten from @JesseFarebro: https://github.com/openai/gym/pull/2535#discussion_r777573839 * refactor: latest_spec -> latest_versioned_spec * fix: bug + typing + test * Add default/versioned tests * Fix Env field defaults * feat: improve versioned/unversioned env handling * Disallow versioned/unversioned registration/lookup * test: remove warning + check default suggestion Co-authored-by: Jesse Farebrother <jessefarebro@gmail.com>
2022-01-19 13:50:25 -05:00
# Environment errors
2016-04-27 08:00:58 -07:00
class DependencyNotInstalled(Error):
"""Raised when the user has not installed a dependency."""
2016-04-27 08:00:58 -07:00
2021-07-29 02:26:34 +02:00
class UnsupportedMode(Error):
"""Raised when the user requests a rendering mode not supported by the environment."""
2021-07-29 02:26:34 +02:00
2016-04-27 08:00:58 -07:00
2022-11-28 14:34:32 +01:00
class InvalidMetadata(Error):
"""Raised when the metadata of an environment is not valid."""
class ResetNeeded(Error):
"""When the order enforcing is violated, i.e. step or render is called before reset."""
2021-07-29 02:26:34 +02:00
class InvalidAction(Error):
"""Raised when the user performs an action not contained within the action space."""
2021-07-29 02:26:34 +02:00
class MissingArgument(Error):
"""Raised when a required argument in the initializer is missing."""
class InvalidProbability(Error):
"""Raised when given an invalid value for a probability."""
2022-11-20 00:57:10 +01:00
class InvalidBound(Error):
"""Raised when the clipping an array with invalid upper and/or lower bound."""
# Wrapper errors
class DeprecatedWrapper(ImportError):
"""Error message for importing an old version of a wrapper."""
# Vectorized environments errors
class AlreadyPendingCallError(Exception):
"""Raised when `reset`, or `step` is called asynchronously (e.g. with `reset_async`, or `step_async` respectively), and `reset_async`, or `step_async` (respectively) is called again (without a complete call to `reset_wait`, or `step_wait` respectively)."""
def __init__(self, message: str, name: str):
"""Initialises the exception with name attributes."""
super().__init__(message)
self.name = name
2021-07-29 02:26:34 +02:00
class NoAsyncCallError(Exception):
"""Raised when an asynchronous `reset`, or `step` is not running, but `reset_wait`, or `step_wait` (respectively) is called."""
2021-07-29 02:26:34 +02:00
def __init__(self, message: str, name: str):
"""Initialises the exception with name attributes."""
super().__init__(message)
self.name = name
2021-07-29 02:26:34 +02:00
class ClosedEnvironmentError(Exception):
"""Trying to call `reset`, or `step`, while the environment is closed."""
2021-07-29 02:26:34 +02:00
class CustomSpaceError(Exception):
2022-09-08 10:10:07 +01:00
"""The space is a custom gymnasium.Space instance, and is not supported by `AsyncVectorEnv` with `shared_memory=True`."""