
    &`i                         d dl mZ d dlmZ d dlmZmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZmZ e G d	 d
e                      Z eej        e           dS )    defaultdict)Any)AgentConnectorConnectorContext)register_connector)AgentCollector)SampleBatch)OldAPIStack)AgentConnectorDataTypeAgentConnectorsOutputc                   j     e Zd ZdZdef fdZdefdZdedefdZ	d	 Z
eded
efd            Z xZS )ViewRequirementAgentConnectorau  This connector does 2 things:
    1. It filters data columns based on view_requirements for training and inference.
    2. It buffers the right amount of history for computing the sample batch for
       action computation.
    The output of this connector is AgentConnectorsOut, which basically is
    a tuple of 2 things:
    {
        "raw_dict": {"obs": ...}
        "sample_batch": SampleBatch
    }
    raw_dict, which contains raw data for the latest time slice,
    can be used to construct a complete episode by Sampler for training purpose.
    The "for_action" SampleBatch can be used to directly call the policy.
    ctxc                      t                                                     j         _        dt	           fd           _        d S )NFc                  ,    t           fd          S )Nc            	          t          j        j        d         d         j        j                            dd          j        d           S )Nmodelmax_seq_len_disable_action_flatteningF)r   intial_statesdisable_action_flatteningis_policy_recurrentis_training_enable_new_api_stack)r	   _view_requirementsconfiginitial_statesgetr   r   r   selfs   /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/rllib/connectors/agent/view_requirement.py<lambda>zJViewRequirementAgentConnector.__init__.<locals>.<lambda>.<locals>.<lambda>,   sY    + #
7 3M B"%"4.1jnn4e/ / ),(? !&*?       r   r    s   r"   r#   z8ViewRequirementAgentConnector.__init__.<locals>.<lambda>+   s/    K       r$   )super__init__view_requirementsr   r   agent_collectors)r!   r   r   	__class__s   ``@r"   r&   z&ViewRequirementAgentConnector.__init__#   sg    "%"7 % !,     !
 !
r$   env_idc                 ,    || j         v r
| j         |= d S d S N)r(   )r!   r*   s     r"   resetz#ViewRequirementAgentConnector.reset@   s)    T***%f--- +*r$   ac_datareturnc           	      6   |j         }t          |          t          u s
J d            |j        }|j        }t
          j        |vr|n|t
          j                 }||J d| d            | j        s
J d            |}| j        |         |         }t
          j	        |vrt          d| d          t
          j        |v r|t
          j                 }n#	 t          |          }n# t          $ r d}Y nw xY w|                                rH|                    ||||t
          j	                 |                    t
          j                             n|                    |           |                                }	t'          ||t)          ||	                    }
|
S )	Nz=Single agent data must be of type Dict[str, TensorStructType]z.ViewRequirementAgentConnector requires env_id(z) and agent_id({agent_id})z:ViewRequirements required by ViewRequirementAgentConnectorzconnector data z should contain next_obs.)
episode_idagent_indexr*   init_obs
init_infos)datatypedictr*   agent_idr
   EPS_IDr   r(   NEXT_OBS
ValueErrorAGENT_INDEXfloatis_emptyadd_init_obsr   INFOSadd_action_reward_next_obsbuild_for_inferencer   r   )r!   r.   dr*   r9   r2   training_dictagent_collectorr3   sample_batchreturn_datas              r"   	transformz'ViewRequirementAgentConnector.transformD   s   LGGtOOOJ OO #*1::VV+BT@U
!h&:&:'V ' ' ' ';&:: #	H 	HG	H 	H# /7Aq((KqKKKLLL
 "a''K34KK!#Hoo ! ! ! !##%% 		:((%';/055!233 )     66q999&::<<,H3M<PP
 
 s   C# #C21C2c                     t           j        d fS r,   )r   __name__)r!   s    r"   to_statez&ViewRequirementAgentConnector.to_state}   s    ,5t;;r$   paramsc                      t          |           S r,   )r   )r   rM   s     r"   
from_statez(ViewRequirementAgentConnector.from_state   s    ,S111r$   )rK   
__module____qualname____doc__r   r&   strr-   r   rI   rL   staticmethodr   rO   __classcell__)r)   s   @r"   r   r      s         
, 
 
 
 
 
 
:.C . . . .7!7 7<R 7 7 7 7r< < < 2( 2# 2 2 2 \2 2 2 2 2r$   r   N)collectionsr   typingr   ray.rllib.connectors.connectorr   r   ray.rllib.connectors.registryr   /ray.rllib.evaluation.collectors.agent_collectorr	   ray.rllib.policy.sample_batchr
   ray.rllib.utils.annotationsr   ray.rllib.utils.typingr   r   r   rK    r$   r"   <module>r_      s,   # # # # # #              = < < < < < J J J J J J 5 5 5 5 5 5 3 3 3 3 3 3        o2 o2 o2 o2 o2N o2 o2 o2d  !*,I    r$   