
    &`i                         d dl mZ d dlZd dlmZ d dlmZ e G d de                      Ze G d de                      Z	dS )	    )OptionalN)MultiAgentEnv)	PublicAPIc                   z     e Zd ZdZ fdZddddee         dee         fdZd Z	d	 Z
d
 Zed             Z xZS )PettingZooEnvaa
  An interface to the PettingZoo MARL environment library.

    See: https://github.com/Farama-Foundation/PettingZoo

    Inherits from MultiAgentEnv and exposes a given AEC
    (actor-environment-cycle) game from the PettingZoo project via the
    MultiAgentEnv public API.

    Note that the wrapper has the following important limitation:

    Environments are positive sum games (-> Agents are expected to cooperate
       to maximize reward). This isn't a hard restriction, it just that
       standard algorithms aren't expected to work well in highly competitive
       games.

    Also note that the earlier existing restriction of all agents having the same
    observation- and action spaces has been lifted. Different agents can now have
    different spaces and the entire environment's e.g. `self.action_space` is a Dict
    mapping agent IDs to individual agents' spaces. Same for `self.observation_space`.

    .. testcode::
        :skipif: True

        from pettingzoo.butterfly import prison_v3
        from ray.rllib.env.wrappers.pettingzoo_env import PettingZooEnv
        env = PettingZooEnv(prison_v3.env())
        obs, infos = env.reset()
        # only returns the observation for the agent which should be stepping
        print(obs)

    .. testoutput::

        {
            'prisoner_0': array([[[0, 0, 0],
                [0, 0, 0],
                [0, 0, 0],
                ...,
                [0, 0, 0],
                [0, 0, 0],
                [0, 0, 0]]], dtype=uint8)
        }

    .. testcode::
        :skipif: True

        obs, rewards, terminateds, truncateds, infos = env.step({
            "prisoner_0": 1
        })
        # only returns the observation, reward, info, etc, for
        # the agent who's turn is next.
        print(obs)

    .. testoutput::

        {
            'prisoner_1': array([[[0, 0, 0],
                [0, 0, 0],
                [0, 0, 0],
                ...,
                [0, 0, 0],
                [0, 0, 0],
                [0, 0, 0]]], dtype=uint8)
        }

    .. testcode::
        :skipif: True

        print(rewards)

    .. testoutput::

        {
            'prisoner_1': 0
        }

    .. testcode::
        :skipif: True

        print(terminateds)

    .. testoutput::

        {
            'prisoner_1': False, '__all__': False
        }

    .. testcode::
        :skipif: True

        print(truncateds)

    .. testoutput::

        {
            'prisoner_1': False, '__all__': False
        }

    .. testcode::
        :skipif: True

        print(infos)

    .. testoutput::

        {
            'prisoner_1': {'map_tuple': (1, 0)}
        }
    c                 J    t                                                       | _        |                                 t	           j        j                   _         j        st           j                   _         j        s j        	                                 _         fd j        D              _
         fd j        D              _        t          j                             j
                   _        t          j                             j                   _        d S )Nc                 F    i | ]}|j                             |          S  )envobservation_space.0aidselfs     y/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/rllib/env/wrappers/pettingzoo_env.py
<dictcomp>z*PettingZooEnv.__init__.<locals>.<dictcomp>   s8     #
 #
 #
58C++C00#
 #
 #
    c                 F    i | ]}|j                             |          S r
   )r   action_spacer   s     r   r   z*PettingZooEnv.__init__.<locals>.<dictcomp>   s8     
 
 
03C&&s++
 
 
r   )super__init__r   resetsetagents
_agent_idslistpossible_agentscopyobservation_spacesaction_spacesgymspacesDictr   r   r   r   	__class__s   ` r   r   zPettingZooEnv.__init__x   s   		dho.. { 	0t//DK# 	6#';#3#3#5#5D #
 #
 #
 #
<@O#
 #
 #

 
 
 
7;
 
 
 "%1H!I!IJOOD,>??r   Nseedoptionsr'   r(   c                    | j                             ||          }| j         j        | j                             | j         j                  i|pi fS Nr&   )r   r   agent_selectionobserve)r   r'   r(   infos       r   r   zPettingZooEnv.reset   sL    x~~4~99X%tx'7'78P'Q'QRJB
 	
r   c                    | j                             || j         j                            i }i }i }i }i }| j         j        r| j                                         \  }}}	}
}| j         j        }|||<   |||<   |	||<   |
||<   |||<   | j         j        | j         j                 s| j         j        | j         j                 r| j                             d            nn| j         j        | j         j         }|o t          |                                          |d<   |o t          |                                          |d<   |||||fS N__all__)	r   stepr+   r   lastterminationstruncationsallvalues)r   actionobs_drew_dterminated_dtruncated_dinfo_dobsrew
terminated	truncatedr-   agent_idall_gones                 r   r1   zPettingZooEnv.step   sV   fTX56777ho 	48HMMOO1Cj)Tx/H!E(O!E(O%/L"$-K!#F8%dh&>?8'(@A d#### ho 	  x&"*"Is<3F3F3H3H/I/IY!)!Gc+2D2D2F2F.G.GIe\;>>r   c                 8    | j                                          d S N)r   closer   s    r   rE   zPettingZooEnv.close   s    r   c                 @    | j                             | j                  S rD   )r   renderrender_moderF   s    r   rH   zPettingZooEnv.render   s    xt/000r   c                     | j         j        S rD   )r   	unwrappedrF   s    r   get_sub_environmentsz"PettingZooEnv.get_sub_environments   s    x!!r   )__name__
__module____qualname____doc__r   r   intdictr   r1   rE   rH   propertyrL   __classcell__r%   s   @r   r   r   	   s        k kZ@ @ @ @ @0 .2T 
 
 
Xc] 
HTN 
 
 
 
? ? ?:  1 1 1 " " X" " " " "r   r   c                   v     e Zd Z fdZddddee         dee         fdZd Zd Z	d	 Z
ed
             Z xZS )ParallelPettingZooEnvc                 $    t                                                       | _         j                                         t	           j        j                   _         j        st           j                   _         j        s j        	                                 _        t          j                             fd j        D                        _        t          j                             fd j        D                        _        d S )Nc                 F    i | ]}|j                             |          S r
   )par_envr   r   s     r   r   z2ParallelPettingZooEnv.__init__.<locals>.<dictcomp>   s+    QQQ#S$,0055QQQr   c                 F    i | ]}|j                             |          S r
   )rZ   r   r   s     r   r   z2ParallelPettingZooEnv.__init__.<locals>.<dictcomp>   s+    LLLSS$,++C00LLLr   )r   r   rZ   r   r   r   r   r   r   r   r!   r"   r#   r   r   r$   s   ` r   r   zParallelPettingZooEnv.__init__   s    dl122 { 	0t//DK# 	6#';#3#3#5#5D !$QQQQQQQ"
 "
  JOOLLLLDOLLL
 
r   Nr&   r'   r(   c                L    | j                             ||          \  }}||pi fS r*   )rZ   r   )r   r'   r(   r=   r-   s        r   r   zParallelPettingZooEnv.reset   s.    L&&D'&BB	TDJBr   c                     | j                             |          \  }}}}}t          |                                          |d<   t          |                                          |d<   |||||fS r/   )rZ   r1   r5   r6   )r   action_dictobssrewsterminateds
truncatedsinfoss          r   r1   zParallelPettingZooEnv.step   so    59\5F5F{5S5S2dKU!$[%7%7%9%9!:!:I #J$5$5$7$7 8 8
9T;
E99r   c                 8    | j                                          d S rD   )rZ   rE   rF   s    r   rE   zParallelPettingZooEnv.close   s    r   c                 @    | j                             | j                  S rD   )rZ   rH   rI   rF   s    r   rH   zParallelPettingZooEnv.render   s    |""4#3444r   c                     | j         j        S rD   )rZ   rK   rF   s    r   rL   z*ParallelPettingZooEnv.get_sub_environments   s    |%%r   )rM   rN   rO   r   r   rQ   rR   r   r1   rE   rH   rS   rL   rT   rU   s   @r   rW   rW      s        
 
 
 
 
& .2T   Xc] HTN    : : :  5 5 5 & & X& & & & &r   rW   )
typingr   	gymnasiumr!   ray.rllib.env.multi_agent_envr   ray.rllib.utils.annotationsr   r   rW   r
   r   r   <module>rk      s              7 7 7 7 7 7 1 1 1 1 1 1 r" r" r" r" r"M r" r" r"j && && && && &&M && && && && &&r   