src.environment.parallelenv.vectorenvs¶
Module Contents¶
Classes¶
Dummy vectorized environment wrapper, implemented in for-loop. |
|
Vectorized environment wrapper based on subprocess. |
|
Vectorized environment wrapper based on ray. |
|
Vectorized environment wrapper based on ray. |
API¶
- class src.environment.parallelenv.vectorenvs.BaseVectorEnv(env_fns: List[Callable[[], gym.Env]], worker_fn: Callable[[Callable[[], gym.Env]], src.environment.parallelenv.worker.EnvWorker], wait_num: Optional[int] = None, timeout: Optional[float] = None, norm_obs: bool = False, update_obs_rms: bool = True, num_cpu_per_worker=1, num_gpu_per_worker=0, sub_worker_fn=DummyEnvWorker, no_warning=False)[source]¶
Bases:
gym.EnvInitialization
- __getattribute__(key: str) Any[source]¶
Switch the attribute getter depending on the key.
Any class who inherits
gym.Envwill inherit some attributes, likeaction_space. However, we would like the attribute lookup to go straight into the worker (in fact, this vector env’s action_space is always None).
- get_env_attr(key: str, id: Optional[Union[int, List[int], numpy.ndarray]] = None) List[Any][source]¶
Get an attribute from the underlying environments.
If id is an int, retrieve the attribute denoted by key from the environment underlying the worker at index id. The result is returned as a list with one element. Otherwise, retrieve the attribute for all workers at indices id and return a list that is ordered correspondingly to id.
- Parameters:
key (str) – The key of the desired attribute.
id – Indice(s) of the desired worker(s). Default to None for all env_id.
- Return list:
The list of environment attributes.
- set_env_attr(key: str, value: Any, id: Optional[Union[int, List[int], numpy.ndarray]] = None) None[source]¶
Set an attribute in the underlying environments.
If id is an int, set the attribute denoted by key from the environment underlying the worker at index id to value. Otherwise, set the attribute for all workers at indices id.
- Parameters:
key (str) – The key of the desired attribute.
value (Any) – The new value of the attribute.
id – Indice(s) of the desired worker(s). Default to None for all env_id.
- _wrap_id(id: Optional[Union[int, List[int], numpy.ndarray]] = None) Union[List[int], numpy.ndarray][source]¶
- reset(id: Optional[Union[int, List[int], numpy.ndarray]] = None) numpy.ndarray[source]¶
Reset the state of some envs and return initial observations.
If id is None, reset the state of all the environments and return initial observations, otherwise reset the specific environments with the given id, either an int or a list.
- step(action: numpy.ndarray, id: Optional[Union[int, List[int], numpy.ndarray]] = None) Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray, numpy.ndarray][source]¶
Run one timestep of some environments’ dynamics.
If id is None, run one timestep of all the environments’ dynamics; otherwise run one timestep for some environments with given id, either an int or a list. When the end of episode is reached, you are responsible for calling reset(id) to reset this environment’s state.
Accept a batch of action and return a tuple (batch_obs, batch_rew, batch_done, batch_info) in numpy format.
- Parameters:
action (numpy.ndarray) – a batch of action provided by the agent.
- Returns:
A tuple including four items:
obsa numpy.ndarray, the agent’s observation of current environmentsrewa numpy.ndarray, the amount of rewards returned after previous actionsdonea numpy.ndarray, whether these episodes have ended, in which case further step() calls will return undefined resultsinfoa numpy.ndarray, contains auxiliary diagnostic information (helpful for debugging, and sometimes learning)
For the async simulation:
Provide the given action to the environments. The action sequence should correspond to the
idargument, and theidargument should be a subset of theenv_idin the last returnedinfo(initially they are env_ids of all the environments). If action is None, fetch unfinished step() calls instead.
- seed(seed: Optional[Union[int, List[int]]] = None) List[Optional[List[int]]][source]¶
Set the seed for all environments.
Accept
None, an int (which will extendito[i, i + 1, i + 2, ...]) or a list.- Returns:
The list of seeds used in this env’s random number generators. The first value in the list should be the “main” seed, or the value which a reproducer pass to “seed”.
- customized_method(call_method, data=None, id: Optional[Union[int, List[int], numpy.ndarray]] = None)[source]¶
- class src.environment.parallelenv.vectorenvs.DummyVectorEnv(env_fns: List[Callable[[], gym.Env]], num_cpus=1, num_gpus=0, no_warning=False, **kwargs: Any)[source]¶
Bases:
src.environment.parallelenv.vectorenvs.BaseVectorEnvDummy vectorized environment wrapper, implemented in for-loop.
Initialization
- class src.environment.parallelenv.vectorenvs.SubprocVectorEnv(env_fns: List[Callable[[], gym.Env]], num_cpus=1, num_gpus=0, no_warning=False, **kwargs: Any)[source]¶
Bases:
src.environment.parallelenv.vectorenvs.BaseVectorEnvVectorized environment wrapper based on subprocess.
Initialization
- class src.environment.parallelenv.vectorenvs.RayVectorEnv(env_fns: List[Callable[[], gym.Env]], num_cpus=1, num_gpus=0, no_warning=False, temp_ray_dir=None, num_cpu_per_worker=1, num_gpu_per_worker=0, **kwargs: Any)[source]¶
Bases:
src.environment.parallelenv.vectorenvs.BaseVectorEnvVectorized environment wrapper based on ray.
Initialization
- Parameters:
num_cpus (int) – the number of cpu cores allocated.
no_warning (bool) – whether to show warnings
temp_ray_dir (str) – the absolute dir path to store temporal files generated by ray during execution, default to creating a
ray_tmpdir in workdir.
- class src.environment.parallelenv.vectorenvs.RaySubprocVectorEnv(env_fns: List[Callable[[], gym.Env]], num_cpus, num_gpus=0, no_warning=False, temp_ray_dir=None, num_cpu_per_worker=1, num_gpu_per_worker=0, **kwargs: Any)[source]¶
Bases:
src.environment.parallelenv.vectorenvs.BaseVectorEnvVectorized environment wrapper based on ray.
This is a choice to run distributed environments in a cluster.
Initialization