o
    `۷iK                     @   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)EnvConfigDict)DeveloperAPIc                       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   O/home/ubuntu/vllm_env/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 )Nz	, 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.typingr   ray.util.annotationsr   r   r   r   r   r   r   <module>   s    