2021-09-09 07:10:06 +02:00
|
|
|
import copy
|
2021-01-19 10:09:39 +01:00
|
|
|
from typing import Optional
|
|
|
|
|
2019-01-23 21:27:26 -08:00
|
|
|
from ray.rllib.utils.annotations import PublicAPI
|
2020-08-15 13:24:22 +02:00
|
|
|
from ray.rllib.utils.typing import EnvConfigDict
|
2018-06-25 22:33:57 -07:00
|
|
|
|
2019-01-23 21:27:26 -08:00
|
|
|
|
|
|
|
@PublicAPI
|
2018-06-25 22:33:57 -07:00
|
|
|
class EnvContext(dict):
|
|
|
|
"""Wraps env configurations to include extra rllib metadata.
|
|
|
|
|
|
|
|
These attributes can be used to parameterize environments per process.
|
|
|
|
For example, one might use `worker_index` to control which data file an
|
|
|
|
environment reads in on initialization.
|
|
|
|
|
|
|
|
RLlib auto-sets these attributes when constructing registered envs.
|
|
|
|
"""
|
|
|
|
|
2020-06-19 13:09:05 -07:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
env_config: EnvConfigDict,
|
|
|
|
worker_index: int,
|
|
|
|
vector_index: int = 0,
|
2021-01-19 10:09:39 +01:00
|
|
|
remote: bool = False,
|
|
|
|
num_workers: Optional[int] = None,
|
2022-04-08 15:33:28 +02:00
|
|
|
recreated_worker: bool = False,
|
2021-01-19 10:09:39 +01:00
|
|
|
):
|
2021-10-29 10:46:52 +02:00
|
|
|
"""Initializes an EnvContext instance.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
env_config: The env's configuration defined under the
|
|
|
|
"env_config" key in the Trainer's config.
|
|
|
|
worker_index: When there are multiple workers created, this
|
|
|
|
uniquely identifies the worker the env is created in.
|
|
|
|
0 for local worker, >0 for remote workers.
|
|
|
|
vector_index: When there are multiple envs per worker, this
|
|
|
|
uniquely identifies the env index within the worker.
|
|
|
|
Starts from 0.
|
|
|
|
remote: Whether individual sub-environments (in a vectorized
|
|
|
|
env) should be @ray.remote actors or not.
|
2022-04-08 15:33:28 +02:00
|
|
|
num_workers: The total number of (remote) workers in the set.
|
|
|
|
0 if only a local worker exists.
|
|
|
|
recreated_worker: Whether the worker that holds this env is a recreated one.
|
|
|
|
This means that it replaced a previous (failed) worker when
|
|
|
|
`recreate_failed_workers=True` in the Trainer's config.
|
2021-10-29 10:46:52 +02:00
|
|
|
"""
|
|
|
|
# Store the env_config in the (super) dict.
|
2018-06-25 22:33:57 -07:00
|
|
|
dict.__init__(self, env_config)
|
2021-10-29 10:46:52 +02:00
|
|
|
|
|
|
|
# Set some metadata attributes.
|
2018-06-25 22:33:57 -07:00
|
|
|
self.worker_index = worker_index
|
2018-08-01 16:29:27 -07:00
|
|
|
self.vector_index = vector_index
|
2019-02-13 19:08:26 +01:00
|
|
|
self.remote = remote
|
2021-10-29 10:46:52 +02:00
|
|
|
self.num_workers = num_workers
|
2022-04-08 15:33:28 +02:00
|
|
|
self.recreated_worker = recreated_worker
|
2018-08-01 16:29:27 -07:00
|
|
|
|
2019-02-13 19:08:26 +01:00
|
|
|
def copy_with_overrides(
|
|
|
|
self,
|
2021-10-29 10:46:52 +02:00
|
|
|
env_config: Optional[EnvConfigDict] = None,
|
|
|
|
worker_index: Optional[int] = None,
|
|
|
|
vector_index: Optional[int] = None,
|
|
|
|
remote: Optional[bool] = None,
|
2021-05-16 17:35:10 +02:00
|
|
|
num_workers: Optional[int] = None,
|
2022-04-08 15:33:28 +02:00
|
|
|
recreated_worker: Optional[bool] = None,
|
2021-05-16 17:35:10 +02:00
|
|
|
) -> "EnvContext":
|
2021-10-29 10:46:52 +02:00
|
|
|
"""Returns a copy of this EnvContext with some attributes overridden.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
env_config: Optional env config to use. None for not overriding
|
|
|
|
the one from the source (self).
|
|
|
|
worker_index: Optional worker index to use. None for not
|
|
|
|
overriding the one from the source (self).
|
|
|
|
vector_index: Optional vector index to use. None for not
|
|
|
|
overriding the one from the source (self).
|
|
|
|
remote: Optional remote setting to use. None for not overriding
|
|
|
|
the one from the source (self).
|
|
|
|
num_workers: Optional num_workers to use. None for not overriding
|
|
|
|
the one from the source (self).
|
2022-04-08 15:33:28 +02:00
|
|
|
recreated_worker: Optional flag, indicating, whether the worker that holds
|
|
|
|
the env is a recreated one. This means that it replaced a previous
|
|
|
|
(failed) worker when `recreate_failed_workers=True` in the Trainer's
|
|
|
|
config.
|
2021-10-29 10:46:52 +02:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
A new EnvContext object as a copy of self plus the provided
|
|
|
|
overrides.
|
|
|
|
"""
|
2018-08-01 16:29:27 -07:00
|
|
|
return EnvContext(
|
2021-09-09 07:10:06 +02:00
|
|
|
copy.deepcopy(env_config) if env_config is not None else self,
|
2019-02-13 19:08:26 +01:00
|
|
|
worker_index if worker_index is not None else self.worker_index,
|
|
|
|
vector_index if vector_index is not None else self.vector_index,
|
|
|
|
remote if remote is not None else self.remote,
|
2021-01-19 10:09:39 +01:00
|
|
|
num_workers if num_workers is not None else self.num_workers,
|
2022-04-08 15:33:28 +02:00
|
|
|
recreated_worker if recreated_worker is not None else self.recreated_worker,
|
2019-02-13 19:08:26 +01:00
|
|
|
)
|
2022-01-27 13:58:12 +01:00
|
|
|
|
|
|
|
def set_defaults(self, defaults: dict) -> None:
|
|
|
|
"""Sets missing keys of self to the values given in `defaults`.
|
|
|
|
|
|
|
|
If `defaults` contains keys that already exist in self, don't override
|
|
|
|
the values with these defaults.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
defaults: The key/value pairs to add to self, but only for those
|
|
|
|
keys in `defaults` that don't exist yet in self.
|
|
|
|
|
|
|
|
Examples:
|
2022-03-25 01:04:02 +01:00
|
|
|
>>> from ray.rllib.env.env_context import EnvContext
|
|
|
|
>>> env_ctx = EnvContext({"a": 1, "b": 2}, worker_index=0) # doctest: +SKIP
|
|
|
|
>>> env_ctx.set_defaults({"a": -42, "c": 3}) # doctest: +SKIP
|
|
|
|
>>> print(env_ctx) # doctest: +SKIP
|
|
|
|
{"a": 1, "b": 2, "c": 3}
|
2022-01-27 13:58:12 +01:00
|
|
|
"""
|
|
|
|
for key, value in defaults.items():
|
|
|
|
if key not in self:
|
|
|
|
self[key] = value
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return (
|
|
|
|
super().__str__()[:-1]
|
|
|
|
+ f", worker={self.worker_index}/{self.num_workers}, "
|
|
|
|
f"vector_idx={self.vector_index}, remote={self.remote}" + "}"
|
2022-01-29 18:41:57 -08:00
|
|
|
)
|