Skip to content

Commit

Permalink
Add warning for Discrete action space with non-zero (DLR-RM#1295)
Browse files Browse the repository at this point in the history
  • Loading branch information
araffin committed Jan 25, 2023
1 parent 0431c7a commit e5575d8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions stable_baselines3/common/env_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ def _check_unsupported_spaces(env: gym.Env, observation_space: spaces.Space, act
"You can use a wrapper or update your observation space."
)

if isinstance(action_space, spaces.Discrete) and action_space.start != 0:
warnings.warn(
"Discrete action space with a non-zero start is not supported by Stable-Baselines3. "
"You can use a wrapper or update your action space."
)

if not _is_numpy_array_space(action_space):
warnings.warn(
"The action space is not based off a numpy array. Typically this means it's either a Dict or Tuple space. "
Expand Down
8 changes: 8 additions & 0 deletions tests/test_envs.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ def patched_step(_action):
spaces.Box(low=-np.inf, high=1, shape=(2,), dtype=np.float32),
# Almost good, except for one dim
spaces.Box(low=np.array([-1, -1, -1]), high=np.array([1, 1, 0.99]), dtype=np.float32),
# Non zero start index
spaces.Discrete(3, start=-1),
],
)
def test_non_default_action_spaces(new_action_space):
Expand All @@ -174,6 +176,12 @@ def test_non_default_action_spaces(new_action_space):
# Change the action space
env.action_space = new_action_space

# Discrete action space
if isinstance(new_action_space, spaces.Discrete):
with pytest.warns(UserWarning):
check_env(env)
return

low, high = new_action_space.low[0], new_action_space.high[0]
# Unbounded action space throws an error,
# the rest only warning
Expand Down

0 comments on commit e5575d8

Please sign in to comment.