o
    c²“iP  ã                   @   sD   d dl Z d dlmZ d dlmZ d dlmZ eG dd„ deƒƒZdS )é    N)ÚOptional)ÚOldAPIStack)ÚEnvConfigDictc                       s®   e Zd ZdZ				ddedededed	ee d
efdd„Z						ddee dee dee dee d	ee d
ee dd fdd„Z	de
ddfdd„Z‡ fdd„Z‡  ZS )Ú
EnvContextaK  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.
    r   FNÚ
env_configÚworker_indexÚvector_indexÚremoteÚnum_workersÚrecreated_workerc                 C   s.   t  | |¡ || _|| _|| _|| _|| _dS )a  Initializes an EnvContext instance.

        Args:
            env_config: The env's configuration defined under the
                "env_config" key in the Algorithm'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.
            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
                `restart_failed_env_runners=True` in the Algorithm's config.
        N)ÚdictÚ__init__r   r   r	   r
   r   ©Úselfr   r   r   r	   r
   r   © r   úM/home/ubuntu/.local/lib/python3.10/site-packages/ray/rllib/env/env_context.pyr      s   
zEnvContext.__init__Úreturnc                 C   sl   t |dur
t |¡n| |dur|n| j|dur|n| j|dur!|n| j|dur)|n| j|dur2|ƒS | jƒS )a]  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).
            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 `restart_failed_env_runners=True` in the
                Algorithm's config.

        Returns:
            A new EnvContext object as a copy of self plus the provided
            overrides.
        N)r   ÚcopyÚdeepcopyr   r   r	   r
   r   r   r   r   r   Úcopy_with_overrides9   s   
úúzEnvContext.copy_with_overridesÚdefaultsc                 C   s&   |  ¡ D ]\}}|| vr|| |< qdS )a›  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.

        .. testcode::
            :skipif: True

            from ray.rllib.env.env_context import EnvContext
            env_ctx = EnvContext({"a": 1, "b": 2}, worker_index=0)
            env_ctx.set_defaults({"a": -42, "c": 3})
            print(env_ctx)

        .. testoutput::

            {"a": 1, "b": 2, "c": 3}
        N)Úitems)r   r   ÚkeyÚvaluer   r   r   Úset_defaultsa   s
   €þzEnvContext.set_defaultsc              	      s:   t ƒ  ¡ d d… d| j› d| j› d| j› d| j›  d S )Néÿÿÿÿz	, worker=ú/z, vector_idx=z	, remote=Ú})ÚsuperÚ__str__r   r
   r   r	   )r   ©Ú	__class__r   r   r   {   s   ÿÿÿþÿzEnvContext.__str__)r   FNF)NNNNNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚintÚboolr   r   r   r   r   r   Ú__classcell__r   r   r    r   r      sT    ùþýüûú
ù(ùþýüûúù
ø(r   )	r   Útypingr   Úray.rllib.utils.annotationsr   Úray.rllib.utils.typingr   r   r   r   r   r   r   Ú<module>   s    