2021-12-08 22:14:15 +01:00
|
|
|
from typing import Optional, Union, List
|
|
|
|
|
2019-06-21 17:29:44 -04:00
|
|
|
import gym
|
2021-12-08 22:14:15 +01:00
|
|
|
from gym.logger import warn, deprecation
|
2019-06-21 17:29:44 -04:00
|
|
|
from gym.spaces import Tuple
|
|
|
|
from gym.vector.utils.spaces import batch_space
|
|
|
|
|
2021-07-29 02:26:34 +02:00
|
|
|
__all__ = ["VectorEnv"]
|
2019-06-21 17:29:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
class VectorEnv(gym.Env):
|
2019-10-25 23:17:29 +02:00
|
|
|
r"""Base class for vectorized environments.
|
|
|
|
|
2021-07-29 02:26:34 +02:00
|
|
|
Each observation returned from vectorized environment is a batch of observations
|
|
|
|
for each sub-environment. And :meth:`step` is also expected to receive a batch of
|
2019-10-25 23:17:29 +02:00
|
|
|
actions for each sub-environment.
|
2021-07-29 02:26:34 +02:00
|
|
|
|
2019-10-25 23:17:29 +02:00
|
|
|
.. note::
|
2021-07-29 02:26:34 +02:00
|
|
|
|
2019-10-25 23:17:29 +02:00
|
|
|
All sub-environments should share the identical observation and action spaces.
|
2021-07-29 02:26:34 +02:00
|
|
|
In other words, a vector of multiple different environments is not supported.
|
2019-06-21 17:29:44 -04:00
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
num_envs : int
|
|
|
|
Number of environments in the vectorized environment.
|
|
|
|
|
2021-11-14 08:59:04 -05:00
|
|
|
observation_space : :class:`gym.spaces.Space`
|
2019-06-21 17:29:44 -04:00
|
|
|
Observation space of a single environment.
|
|
|
|
|
2021-11-14 08:59:04 -05:00
|
|
|
action_space : :class:`gym.spaces.Space`
|
2019-06-21 17:29:44 -04:00
|
|
|
Action space of a single environment.
|
|
|
|
"""
|
2021-07-29 02:26:34 +02:00
|
|
|
|
2019-06-21 17:29:44 -04:00
|
|
|
def __init__(self, num_envs, observation_space, action_space):
|
|
|
|
self.num_envs = num_envs
|
2021-08-18 16:36:40 -04:00
|
|
|
self.is_vector_env = True
|
2019-06-21 17:29:44 -04:00
|
|
|
self.observation_space = batch_space(observation_space, n=num_envs)
|
|
|
|
self.action_space = Tuple((action_space,) * num_envs)
|
|
|
|
|
|
|
|
self.closed = False
|
|
|
|
self.viewer = None
|
|
|
|
|
|
|
|
# The observation and action spaces of a single environment are
|
|
|
|
# kept in separate properties
|
|
|
|
self.single_observation_space = observation_space
|
|
|
|
self.single_action_space = action_space
|
|
|
|
|
2021-12-08 22:14:15 +01:00
|
|
|
def reset_async(self, seed: Optional[Union[int, List[int]]] = None):
|
2019-06-21 17:29:44 -04:00
|
|
|
pass
|
|
|
|
|
2021-12-08 22:14:15 +01:00
|
|
|
def reset_wait(self, seed: Optional[Union[int, List[int]]] = None, **kwargs):
|
2019-06-21 17:29:44 -04:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
2021-12-08 22:14:15 +01:00
|
|
|
def reset(self, seed: Optional[Union[int, List[int]]] = None):
|
2019-10-25 23:17:29 +02:00
|
|
|
r"""Reset all sub-environments and return a batch of initial observations.
|
2021-07-29 02:26:34 +02:00
|
|
|
|
2019-10-09 15:08:10 -07:00
|
|
|
Returns
|
|
|
|
-------
|
2021-11-14 08:59:04 -05:00
|
|
|
element of :attr:`observation_space`
|
2019-10-09 15:08:10 -07:00
|
|
|
A batch of observations from the vectorized environment.
|
|
|
|
"""
|
2021-12-08 22:14:15 +01:00
|
|
|
self.reset_async(seed=seed)
|
|
|
|
return self.reset_wait(seed=seed)
|
2019-06-21 17:29:44 -04:00
|
|
|
|
|
|
|
def step_async(self, actions):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def step_wait(self, **kwargs):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def step(self, actions):
|
2021-07-29 02:26:34 +02:00
|
|
|
r"""Take an action for each sub-environments.
|
2019-10-25 23:17:29 +02:00
|
|
|
|
2019-10-09 15:08:10 -07:00
|
|
|
Parameters
|
|
|
|
----------
|
2021-11-14 08:59:04 -05:00
|
|
|
actions : element of :attr:`action_space`
|
|
|
|
Batch of actions.
|
2019-10-09 15:08:10 -07:00
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
2021-11-14 08:59:04 -05:00
|
|
|
observations : element of :attr:`observation_space`
|
2019-10-09 15:08:10 -07:00
|
|
|
A batch of observations from the vectorized environment.
|
|
|
|
|
2021-11-14 08:59:04 -05:00
|
|
|
rewards : :obj:`np.ndarray`, dtype :obj:`np.float_`
|
2019-10-09 15:08:10 -07:00
|
|
|
A vector of rewards from the vectorized environment.
|
|
|
|
|
2021-11-14 08:59:04 -05:00
|
|
|
dones : :obj:`np.ndarray`, dtype :obj:`np.bool_`
|
2019-10-09 15:08:10 -07:00
|
|
|
A vector whose entries indicate whether the episode has ended.
|
|
|
|
|
|
|
|
infos : list of dict
|
2019-10-25 23:17:29 +02:00
|
|
|
A list of auxiliary diagnostic information dicts from sub-environments.
|
2019-10-09 15:08:10 -07:00
|
|
|
"""
|
2019-10-25 23:17:29 +02:00
|
|
|
|
2019-06-21 17:29:44 -04:00
|
|
|
self.step_async(actions)
|
|
|
|
return self.step_wait()
|
|
|
|
|
2019-10-26 00:18:54 +02:00
|
|
|
def close_extras(self, **kwargs):
|
2021-07-29 02:26:34 +02:00
|
|
|
r"""Clean up the extra resources e.g. beyond what's in this base class."""
|
2021-12-08 19:55:09 -05:00
|
|
|
pass
|
2019-10-26 00:18:54 +02:00
|
|
|
|
|
|
|
def close(self, **kwargs):
|
|
|
|
r"""Close all sub-environments and release resources.
|
2021-07-29 02:26:34 +02:00
|
|
|
|
2019-10-26 00:18:54 +02:00
|
|
|
It also closes all the existing image viewers, then calls :meth:`close_extras` and set
|
2021-07-29 02:26:34 +02:00
|
|
|
:attr:`closed` as ``True``.
|
|
|
|
|
2019-10-26 00:18:54 +02:00
|
|
|
.. warning::
|
2021-07-29 02:26:34 +02:00
|
|
|
|
2019-10-26 00:18:54 +02:00
|
|
|
This function itself does not close the environments, it should be handled
|
2021-07-29 02:26:34 +02:00
|
|
|
in :meth:`close_extras`. This is generic for both synchronous and asynchronous
|
|
|
|
vectorized environments.
|
|
|
|
|
2019-10-26 00:18:54 +02:00
|
|
|
.. note::
|
2021-07-29 02:26:34 +02:00
|
|
|
|
|
|
|
This will be automatically called when garbage collected or program exited.
|
|
|
|
|
2019-10-26 00:18:54 +02:00
|
|
|
"""
|
|
|
|
if self.closed:
|
|
|
|
return
|
|
|
|
if self.viewer is not None:
|
|
|
|
self.viewer.close()
|
|
|
|
self.close_extras(**kwargs)
|
|
|
|
self.closed = True
|
|
|
|
|
2021-12-08 22:14:15 +01:00
|
|
|
def seed(self, seed=None):
|
2021-11-14 08:59:04 -05:00
|
|
|
"""Set the random seed in all sub-environments.
|
|
|
|
|
2019-10-09 15:08:10 -07:00
|
|
|
Parameters
|
|
|
|
----------
|
2021-12-08 22:14:15 +01:00
|
|
|
seed : list of int, or int, optional
|
|
|
|
Random seed for each sub-environment. If ``seed`` is a list of
|
2021-11-14 08:59:04 -05:00
|
|
|
length ``num_envs``, then the items of the list are chosen as random
|
2021-12-08 22:14:15 +01:00
|
|
|
seeds. If ``seed`` is an int, then each sub-environment uses the random
|
|
|
|
seed ``seed + n``, where ``n`` is the index of the sub-environment
|
2021-11-14 08:59:04 -05:00
|
|
|
(between ``0`` and ``num_envs - 1``).
|
2019-10-09 15:08:10 -07:00
|
|
|
"""
|
2021-12-08 22:14:15 +01:00
|
|
|
deprecation(
|
|
|
|
"Function `env.seed(seed)` is marked as deprecated and will be removed in the future. "
|
|
|
|
"Please use `env.reset(seed=seed) instead in VectorEnvs."
|
|
|
|
)
|
2019-10-09 15:08:10 -07:00
|
|
|
|
2019-06-21 17:29:44 -04:00
|
|
|
def __del__(self):
|
2021-07-29 02:26:34 +02:00
|
|
|
if not getattr(self, "closed", True):
|
2021-12-08 19:55:09 -05:00
|
|
|
self.close()
|
2019-10-26 00:38:52 +02:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
if self.spec is None:
|
2021-11-14 14:51:32 +01:00
|
|
|
return f"{self.__class__.__name__}({self.num_envs})"
|
2019-10-26 00:38:52 +02:00
|
|
|
else:
|
2021-11-14 14:51:32 +01:00
|
|
|
return f"{self.__class__.__name__}({self.spec.id}, {self.num_envs})"
|
2019-11-01 22:29:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
class VectorEnvWrapper(VectorEnv):
|
2021-07-29 02:26:34 +02:00
|
|
|
r"""Wraps the vectorized environment to allow a modular transformation.
|
|
|
|
|
2019-11-01 22:29:39 +01:00
|
|
|
This class is the base class for all wrappers for vectorized environments. The subclass
|
|
|
|
could override some methods to change the behavior of the original vectorized environment
|
2021-07-29 02:26:34 +02:00
|
|
|
without touching the original code.
|
|
|
|
|
2019-11-01 22:29:39 +01:00
|
|
|
.. note::
|
2021-07-29 02:26:34 +02:00
|
|
|
|
2019-11-01 22:29:39 +01:00
|
|
|
Don't forget to call ``super().__init__(env)`` if the subclass overrides :meth:`__init__`.
|
2021-07-29 02:26:34 +02:00
|
|
|
|
2019-11-01 22:29:39 +01:00
|
|
|
"""
|
2021-07-29 02:26:34 +02:00
|
|
|
|
2019-11-01 22:29:39 +01:00
|
|
|
def __init__(self, env):
|
|
|
|
assert isinstance(env, VectorEnv)
|
|
|
|
self.env = env
|
|
|
|
|
2020-08-14 14:20:56 -07:00
|
|
|
# explicitly forward the methods defined in VectorEnv
|
|
|
|
# to self.env (instead of the base class)
|
2021-12-08 22:14:15 +01:00
|
|
|
def reset_async(self, **kwargs):
|
|
|
|
return self.env.reset_async(**kwargs)
|
2020-08-14 14:20:56 -07:00
|
|
|
|
2021-12-08 22:14:15 +01:00
|
|
|
def reset_wait(self, **kwargs):
|
|
|
|
return self.env.reset_wait(**kwargs)
|
2020-08-14 14:20:56 -07:00
|
|
|
|
|
|
|
def step_async(self, actions):
|
|
|
|
return self.env.step_async(actions)
|
|
|
|
|
|
|
|
def step_wait(self):
|
|
|
|
return self.env.step_wait()
|
|
|
|
|
|
|
|
def close(self, **kwargs):
|
|
|
|
return self.env.close(**kwargs)
|
|
|
|
|
|
|
|
def close_extras(self, **kwargs):
|
|
|
|
return self.env.close_extras(**kwargs)
|
|
|
|
|
2021-12-08 22:14:15 +01:00
|
|
|
def seed(self, seed=None):
|
|
|
|
return self.env.seed(seed)
|
2020-08-14 14:20:56 -07:00
|
|
|
|
|
|
|
# implicitly forward all other methods and attributes to self.env
|
2019-11-01 22:29:39 +01:00
|
|
|
def __getattr__(self, name):
|
2021-07-29 02:26:34 +02:00
|
|
|
if name.startswith("_"):
|
2021-11-14 14:51:32 +01:00
|
|
|
raise AttributeError(f"attempted to get missing private attribute '{name}'")
|
2019-11-01 22:29:39 +01:00
|
|
|
return getattr(self.env, name)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unwrapped(self):
|
|
|
|
return self.env.unwrapped
|
|
|
|
|
|
|
|
def __repr__(self):
|
2021-11-14 14:51:32 +01:00
|
|
|
return f"<{self.__class__.__name__}, {self.env}>"
|