Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[gym.vector] Add BatchedVectorEnv, (chunking + flexible n_envs) #2072

Closed
wants to merge 13 commits into from
10 changes: 9 additions & 1 deletion gym/vector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
except ImportError:
Iterable = (tuple, list)

from gym.vector.batched_vector_env import BatchedVectorEnv
from gym.vector.async_vector_env import AsyncVectorEnv
from gym.vector.sync_vector_env import SyncVectorEnv
from gym.vector.vector_env import VectorEnv, VectorEnvWrapper

__all__ = ["AsyncVectorEnv", "SyncVectorEnv", "VectorEnv", "VectorEnvWrapper", "make"]
__all__ = [
"BatchedVectorEnv",
"AsyncVectorEnv",
"SyncVectorEnv",
"VectorEnv",
"VectorEnvWrapper",
"make",
]


def make(id, num_envs=1, asynchronous=True, wrappers=None, **kwargs):
Expand Down
23 changes: 22 additions & 1 deletion gym/vector/async_vector_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,16 @@ def _worker(index, env_fn, pipe, parent_pipe, shared_memory, error_queue):
assert shared_memory is None
env = env_fn()
parent_pipe.close()

def step_fn(actions):
observation, reward, done, info = env.step(actions)
# Do nothing if the env is a VectorEnv, since it will automatically
# reset the envs that are done if needed in the 'step' method and return
# the initial observation instead of the final observation.
if not isinstance(env, VectorEnv) and done:
observation = env.reset()
return observation, reward, done, info

try:
while True:
command, data = pipe.recv()
Expand Down Expand Up @@ -440,6 +450,16 @@ def _worker_shared_memory(index, env_fn, pipe, parent_pipe, shared_memory, error
env = env_fn()
observation_space = env.observation_space
parent_pipe.close()

def step_fn(actions):
observation, reward, done, info = env.step(actions)
# Do nothing if the env is a VectorEnv, since it will automatically
# reset the envs that are done if needed in the 'step' method and return
# the initial observation instead of the final observation.
if not isinstance(env, VectorEnv) and done:
observation = env.reset()
return observation, reward, done, info

try:
while True:
command, data = pipe.recv()
Expand All @@ -451,7 +471,8 @@ def _worker_shared_memory(index, env_fn, pipe, parent_pipe, shared_memory, error
pipe.send((None, True))
elif command == "step":
observation, reward, done, info = env.step(data)
if done:
# BUG: See PR #2104: Currently unable to nest VectorEnvs because of this
if (done if isinstance(done, bool) else all(done)):
observation = env.reset()
write_to_shared_memory(
index, observation, shared_memory, observation_space
Expand Down
Loading