o
    ci                     @   sl   d dl mZ d dlZd dlmZmZmZ d dlm	Z	 d dl
mZ d dlmZ dZdZeG d	d
 d
e	ZdS )    )OrderedDictN)DictListOptional)MultiAgentEnv)DeveloperAPI)AgentID_group_rewards_group_infoc                
       s   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dZddd	d
e	e de	e fddZdd Zdd ZdddZ  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                    s   t    || _|| _i | _| D ]\}}|D ]}|| jv r&td||| j|< qq|dur4|| _|dur;|| _	|
 D ]}| j| q?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__ _/home/ubuntu/.local/lib/python3.10/site-packages/ray/rllib/env/wrappers/group_agents_wrapper.pyr   #   s&   

zGroupAgentsWrapper.__init__seedoptionsr$   r%   c                C   s0   | j j||d\}}| || j|dd dfS )Nr#   c                 S      t t|  iS N
GROUP_INFOlistvaluesgvalsr!   r!   r"   <lambda>T       z*GroupAgentsWrapper.reset.<locals>.<lambda>agg_fn)r   reset_group_items)r   r$   r%   obsinfor!   r!   r"   r2   M   s   zGroupAgentsWrapper.resetc           	      C   s   |  |}| j|\}}}}}| |}| j|dd d}| j|dd d}| j|dd d}| j|dd d}| D ]\}}t|trZt|||< ||vrTi ||< ||| t< q=|||||fS )Nc                 S      t |  S r'   r*   r+   r,   r!   r!   r"   r.   _       z)GroupAgentsWrapper.step.<locals>.<lambda>r0   c                 S   r6   r'   allr+   r,   r!   r!   r"   r.   b   r8   c                 S   r6   r'   r9   r,   r!   r!   r"   r.   g   r8   c                 S   r&   r'   r(   r,   r!   r!   r"   r.   j   r/   )	_ungroup_itemsr   stepr3   r   
isinstancer*   sumGROUP_REWARDS)	r   action_dictr4   rewardsterminateds
truncatedsinfosr   rewr!   r!   r"   r<   X   s,   


zGroupAgentsWrapper.stepc                 C   sv   i }|  D ]2\}}|| jv r4t|t| j| ks"J ||| jft| j| |D ]\}}|||< q*q|||< q|S r'   )r   r   lenzip)r   r   outr   valueavr!   r!   r"   r;   w   s   


z!GroupAgentsWrapper._ungroup_itemsc           	      C   s   |d u rdd }i }|  D ]<\}}|| jv rF| j| }||v r!qt }| j| D ]}||v r6|| ||< q)td|||||||< q|||< q|S )Nc                 S   r6   r'   r7   r,   r!   r!   r"   r.      r8   z1GroupAgentsWrapper._group_items.<locals>.<lambda>z"Missing member of group {}: {}: {})r   r   r   r   r   r   )	r   r   r1   grouped_itemsr   itemr   	group_outrJ   r!   r!   r"   r3      s(   


zGroupAgentsWrapper._group_items)NNr'   )__name__
__module____qualname____doc__r   r   strr   r   r   gymSpacer   intdictr2   r<   r;   r3   __classcell__r!   r!   r   r"   r      s"    "*r   )collectionsr   	gymnasiumrT   typingr   r   r   ray.rllib.env.multi_agent_envr   ray.rllib.utils.annotationsr   ray.rllib.utils.typingr   r?   r)   r   r!   r!   r!   r"   <module>   s    