
    &`i                         d dl mZ d dlmZmZmZ d dlZd dlm	Z	 d dl
mZ d dlmZ dZdZe G d	 d
e	                      ZdS )    )OrderedDict)DictListOptionalN)MultiAgentEnv)DeveloperAPI)AgentID_group_rewards_group_infoc            
            e Zd ZdZ	 	 ddedeeee         f         de	e
j                 de	e
j                 f fdZdddd	e	e         d
e	e         fdZd Zd ZddZ xZS )GroupAgentsWrapperzWraps a MultiAgentEnv environment with agents grouped as specified.

    See multi_agent_env.py for the specification of groups.

    This API is experimental.
    Nenvgroups	obs_space	act_spacec                    t                                                       || _        || _        i | _        |                                D ]?\  }}|D ]7}|| j        v r"t          d                    |                    || j        |<   8@||| _        ||| _	        |
                                D ]}| j                            |           dS )a  Wrap an existing MultiAgentEnv to group agent ID together.

        See `MultiAgentEnv.with_agent_groups()` for more detailed usage info.

        Args:
            env: The env to wrap and whose agent IDs to group into new agents.
            groups: Mapping from group id to a list of the agent ids
                of group members. If an agent id is not present in any group
                value, it will be left ungrouped. The group id becomes a new agent ID
                in the final environment.
            obs_space: Optional observation space for the grouped
                env. Must be a tuple space. If not provided, will infer this to be a
                Tuple of n individual agents spaces (n=num agents in a group).
            act_space: Optional action space for the grouped env.
                Must be a tuple space. If not provided, will infer this to be a Tuple
                of n individual agents spaces (n=num agents in a group).
        z!Agent id {} is in multiple groupsN)super__init__r   r   agent_id_to_groupitems
ValueErrorformatobservation_spaceaction_spacekeys
_agent_idsadd)	selfr   r   r   r   group_id	agent_idsagent_id	__class__s	           /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/rllib/env/wrappers/group_agents_wrapper.pyr   zGroupAgentsWrapper.__init__$   s    0 	!##)<<>> 	< 	<Hi% < <t555$;BB8LL   4<&x00<  %.D"  )D 	* 	*HO))))	* 	*    seedoptionsr&   r'   c                    | j                             ||          \  }}|                     |          |                     |d           fS )Nr%   c                 R    t           t          |                                           iS N
GROUP_INFOlistvaluesgvalss    r#   <lambda>z*GroupAgentsWrapper.reset.<locals>.<lambda>U   s    j$u||~~2F2F%G r$   agg_fn)r   reset_group_items)r   r&   r'   obsinfos        r#   r4   zGroupAgentsWrapper.resetN   s[    HNNgN>>	T c""GG   
 	
r$   c                    |                      |          }| j                            |          \  }}}}}|                     |          }|                     |d           }|                     |d           }|                     |d           }|                     |d           }|                                D ]E\  }}t          |t                    r+t          |          ||<   ||vri ||<   |||         t          <   F|||||fS )Nc                 D    t          |                                           S r*   r-   r.   r/   s    r#   r1   z)GroupAgentsWrapper.step.<locals>.<lambda>`   s    $u||~~BVBV r$   r2   c                 D    t          |                                           S r*   allr.   r/   s    r#   r1   z)GroupAgentsWrapper.step.<locals>.<lambda>c   s    c%,,...A.A r$   c                 D    t          |                                           S r*   r<   r/   s    r#   r1   z)GroupAgentsWrapper.step.<locals>.<lambda>h   s    U\\^^!4!4 r$   c                 R    t           t          |                                           iS r*   r+   r/   s    r#   r1   z)GroupAgentsWrapper.step.<locals>.<lambda>k   s    T%,,..5I5I(J r$   )	_ungroup_itemsr   stepr5   r   
isinstancer-   sumGROUP_REWARDS)	r   action_dictr6   rewardsterminateds
truncatedsinfosr!   rews	            r#   rA   zGroupAgentsWrapper.stepY   sD   ))+667;x}}[7Q7Q4Wk:u $$##G4V4V#WW'' A A ( 
 
 &&44 ' 
 

 !!JJ " 
 

 %]]__ 	5 	5MHc#t$$ 5$'HH!5((&(E(O14h.G[*e;;r$   c                 "   i }|                                 D ]w\  }}|| j        v rdt          |          t          | j        |                   k    sJ ||| j        f            t          | j        |         |          D ]
\  }}|||<   r|||<   x|S r*   )r   r   lenzip)r   r   outr!   valueavs          r#   r@   z!GroupAgentsWrapper._ungroup_itemsx   s    ${{}} 
	& 
	&OHe4;&&5zzSX)>%?%????KB???
  H 5u==  DAqCFF !&H
r$   c           	      H   |d }i }|                                 D ]\  }}|| j        v rr| j        |         }||v r t                      }| j        |         D ]5}||v r||         ||<   t	          d                    |||                     ||          ||<   |||<   |S )Nc                 D    t          |                                           S r*   r:   r/   s    r#   r1   z1GroupAgentsWrapper._group_items.<locals>.<lambda>   s    4#7#7 r$   z"Missing member of group {}: {}: {})r   r   r   r   r   r   )	r   r   r3   grouped_itemsr!   itemr   	group_outrP   s	            r#   r5   zGroupAgentsWrapper._group_items   s    >77F#kkmm 	/ 	/NHd41111(;},,'MM	X.  AEzz',Qx	!(@GG (!U   
 +1&*;*;h''*.h''r$   )NNr*   )__name__
__module____qualname____doc__r   r   strr   r	   r   gymSpacer   intdictr4   rA   r@   r5   __classcell__)r"   s   @r#   r   r      s         *.)-(* (*(* S$w-'((* CI&	(*
 CI&(* (* (* (* (* (*T .2T 	
 	
 	
Xc] 	
HTN 	
 	
 	
 	
< < <>         r$   r   )collectionsr   typingr   r   r   	gymnasiumr\   ray.rllib.env.multi_agent_envr   ray.rllib.utils.annotationsr   ray.rllib.utils.typingr	   rD   r,   r    r$   r#   <module>rh      s    # # # # # # ' ' ' ' ' ' ' ' ' '     7 7 7 7 7 7 4 4 4 4 4 4 * * * * * * ! 
 B B B B B B B B B Br$   