-
-
Notifications
You must be signed in to change notification settings - Fork 859
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
StickyAction wrapper can repeat the old action for more than 1 step (#…
…1240) Co-authored-by: Mark Towers <[email protected]>
- Loading branch information
1 parent
90d04f2
commit ebe70a1
Showing
2 changed files
with
105 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,63 @@ | ||
"""Test suite for StickyAction wrapper.""" | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
from gymnasium.error import InvalidProbability | ||
from gymnasium.error import InvalidBound, InvalidProbability | ||
from gymnasium.spaces import Discrete | ||
from gymnasium.wrappers import StickyAction | ||
from tests.testing_env import GenericTestEnv | ||
from tests.wrappers.utils import NUM_STEPS, record_action_as_obs_step | ||
|
||
|
||
def test_sticky_action(): | ||
from tests.wrappers.utils import record_action_as_obs_step | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"repeat_action_probability,repeat_action_duration,actions,expected_action", | ||
[ | ||
(0.25, 1, [0, 1, 2, 3, 4, 5, 6, 7], [0, 0, 2, 3, 3, 3, 6, 6]), | ||
(0.25, 2, [0, 1, 2, 3, 4, 5, 6, 7], [0, 0, 0, 3, 4, 4, 4, 4]), | ||
(0.25, (1, 3), [0, 1, 2, 3, 4, 5, 6, 7], [0, 0, 0, 0, 4, 4, 4, 4]), | ||
], | ||
) | ||
def test_sticky_action( | ||
repeat_action_probability, repeat_action_duration, actions, expected_action | ||
): | ||
"""Tests the sticky action wrapper.""" | ||
env = StickyAction( | ||
GenericTestEnv(step_func=record_action_as_obs_step), | ||
repeat_action_probability=0.5, | ||
GenericTestEnv( | ||
step_func=record_action_as_obs_step, observation_space=Discrete(7) | ||
), | ||
repeat_action_probability=repeat_action_probability, | ||
repeat_action_duration=repeat_action_duration, | ||
) | ||
env.reset(seed=11) | ||
|
||
previous_action = None | ||
for _ in range(NUM_STEPS): | ||
input_action = env.action_space.sample() | ||
executed_action, _, _, _, _ = env.step(input_action) | ||
|
||
assert np.all(executed_action == input_action) or np.all( | ||
executed_action == previous_action | ||
) | ||
previous_action = executed_action | ||
assert len(actions) == len(expected_action) | ||
for action, action_taken in zip(actions, expected_action): | ||
executed_action, _, _, _, _ = env.step(action) | ||
assert executed_action == action_taken | ||
|
||
|
||
@pytest.mark.parametrize("repeat_action_probability", [-1, 1, 1.5]) | ||
def test_sticky_action_raise(repeat_action_probability): | ||
def test_sticky_action_raise_probability(repeat_action_probability): | ||
"""Tests the stick action wrapper with probabilities that should raise an error.""" | ||
with pytest.raises(InvalidProbability): | ||
StickyAction( | ||
GenericTestEnv(), repeat_action_probability=repeat_action_probability | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"repeat_action_duration", | ||
[ | ||
-4, | ||
0, | ||
(0, 0), | ||
(4, 2), | ||
[1, 2], | ||
], | ||
) | ||
def test_sticky_action_raise_duration(repeat_action_duration): | ||
"""Tests the stick action wrapper with durations that should raise an error.""" | ||
with pytest.raises((ValueError, InvalidBound)): | ||
StickyAction( | ||
GenericTestEnv(), 0.5, repeat_action_duration=repeat_action_duration | ||
) |