-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix/deterministic action space sampling in SubprocVectorEnv (#1103)
- Loading branch information
1 parent
6935a11
commit a043711
Showing
2 changed files
with
58 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import gymnasium as gym | ||
|
||
from tianshou.env import DummyVectorEnv, ShmemVectorEnv, SubprocVectorEnv | ||
|
||
|
||
def test_gym_env_action_space() -> None: | ||
env = gym.make("Pendulum-v1") | ||
env.action_space.seed(0) | ||
action1 = env.action_space.sample() | ||
|
||
env.action_space.seed(0) | ||
action2 = env.action_space.sample() | ||
|
||
assert action1 == action2 | ||
|
||
|
||
def test_dummy_vec_env_action_space() -> None: | ||
num_envs = 4 | ||
envs = DummyVectorEnv([lambda: gym.make("Pendulum-v1") for _ in range(num_envs)]) | ||
envs.seed(0) | ||
action1 = [ac_space.sample() for ac_space in envs.action_space] | ||
|
||
envs.seed(0) | ||
action2 = [ac_space.sample() for ac_space in envs.action_space] | ||
|
||
assert action1 == action2 | ||
|
||
|
||
def test_subproc_vec_env_action_space() -> None: | ||
num_envs = 4 | ||
envs = SubprocVectorEnv([lambda: gym.make("Pendulum-v1") for _ in range(num_envs)]) | ||
envs.seed(0) | ||
action1 = [ac_space.sample() for ac_space in envs.action_space] | ||
|
||
envs.seed(0) | ||
action2 = [ac_space.sample() for ac_space in envs.action_space] | ||
|
||
assert action1 == action2 | ||
|
||
|
||
def test_shmem_vec_env_action_space() -> None: | ||
num_envs = 4 | ||
envs = ShmemVectorEnv([lambda: gym.make("Pendulum-v1") for _ in range(num_envs)]) | ||
envs.seed(0) | ||
action1 = [ac_space.sample() for ac_space in envs.action_space] | ||
|
||
envs.seed(0) | ||
action2 = [ac_space.sample() for ac_space in envs.action_space] | ||
|
||
assert action1 == action2 | ||
|
||
|
||
if __name__ == "__main__": | ||
test_gym_env_action_space() | ||
test_dummy_vec_env_action_space() | ||
test_subproc_vec_env_action_space() | ||
test_shmem_vec_env_action_space() |
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