
    ni                     v    S r SSKJr  SSKJr  SSKJr   SSKJs  Jr   " S S\5      rg! \	 a    SSK
r Nf = f)Memory that stores episodes.    )absolute_import)division)print_functionNc                   P    \ rS rSrSrS rSS jrSS jrSS jrSS jr	SS	 jr
S
rg)EpisodeMemory   r   c                    X l         X0l        [        R                  " U5       nXPl        [        R
                  " [        R                  " U[        R                  5      S5      U l        U Vs/ s HU  n[        R
                  " [        R                  " X#/UR                  R                  5       -   UR                  5      S5      PMW     snU l        SSS5        gs  snf ! , (       d  f       g= f)a  Create a memory that stores episodes.

Each transition tuple consists of quantities specified by the template.
These quantities would typically be be observartions, actions, rewards, and
done indicators.

Args:
  template: List of tensors to derive shapes and dtypes of each transition.
  capacity: Number of episodes, or rows, hold by the memory.
  max_length: Allocated sequence length for the episodes.
  scope: Variable scope to use for internal variables.
FN)	_capacity_max_lengthtfvariable_scope_scopeVariablezerosint32_lengthshapeas_listdtype_buffers)selftemplatecapacity
max_lengthscope	var_scopeelems          Y/home/james-whalen/.local/lib/python3.13/site-packages/pybullet_envs/agents/ppo/memory.py__init__EpisodeMemory.__init__   s     N!			5	!Yk[[(BHH!=uEdl d ++bhh5

8J8J8LLdjjY[`
adm 
"	! 
"	!s   A
C-AC	CC
C-Nc                     Uc   [         R                  " U R                  5      OUn[         R                  " U R                  U5      $ )zTensor holding the current length of episodes.

Args:
  rows: Episodes to select length from, defaults to all.

Returns:
  Batch tensor of sequence lengths.
)r   ranger   gatherr   r   rowss     r   lengthEpisodeMemory.length4   s1     (,|288DNN#D99T\\4((    c           	      "   Uc   [         R                  " U R                  5      OUnUR                  R                  S:X  d   e[         R
                  " X R                  SS9n[         R                  " U/5         [         R
                  " [         R                  " U R                  U5      U R                  SS9nSSS5        / n[         R                  " W/5         [        U R                  U5       Hd  u  pg[         R                  " U R                  U5      n[         R                  " X(/S5      n	UR                  [         R                  " XiU5      5        Mf     SSS5        [         R                  " U5         [         R                  " [         R                   " X R                  [         R"                  S9S5      n
U R                  R%                  U
5      sSSS5        $ ! , (       d  f       GN,= f! , (       d  f       N= f! , (       d  f       g= f)zAppend a batch of transitions to rows of the memory.

Args:
  transitions: Tuple of transition quantities with batch dimension.
  rows: Episodes to append to, defaults to all.

Returns:
  Operation.
N   capacity exceededmessagemax length exceeded)r   r   )r   r#   r   r   ndimsassert_lesscontrol_dependenciesr$   r   r   zipr   stackappendscatter_nd_update
reduce_sumone_hotr   
assign_add)r   transitionsr&   assert_capacityassert_max_length
append_opsbuffer_elementstimestepindicesepisode_masks              r   r5   EpisodeMemory.append@   sm    (,|288DNN#D::q   nnT>>CVWO		 	 /!2	3..4<<)F)-)9)91FH 
4 J		 	 "3!4	5"4==+>
'99T\\40((D+Q/"..wJK  ? 
6
 
	 	 	,]]2::dNN"((#SUVWl\\$$\2 
-	, 
4	3
 
6	5
 
-	,s'   8A GA?G/6AH 
G,/
G= 
Hc                    Uc   [         R                  " U R                  5      OUnUR                  R                  S:X  d   e[         R
                  " X0R                  SS9n[         R                  " U/5         [         R                  " UU R                  SS9nSSS5        / n[         R                  " W/5         [        U R                  U5       H-  u  px[         R                  " XsU5      n	UR                  U	5        M/     SSS5        [         R                  " U5         [         R                  " U R                  X25      sSSS5        $ ! , (       d  f       N= f! , (       d  f       N`= f! , (       d  f       g= f)zReplace full episodes.

Args:
  episodes: Tuple of transition quantities with batch and time dimensions.
  length: Batch of sequence lengths.
  rows: Episodes to replace, defaults to all.

Returns:
  Operation.
Nr+   r,   r-   r/   )r   r#   r   r   r0   r1   r2   assert_less_equalr   r3   r   scatter_updater5   r   )
r   episodesr'   r&   r;   r<   replace_opsr>   r?   
replace_ops
             r   replaceEpisodeMemory.replace[   s    (,|288DNN#D::q   nnT>>CVWO		 	 /!2	3..v/3/?/?7LN 
4 K		 	 "3!4	5"4==(;
'&&wh?
:&  < 
6 
	 	 	-t||T: 
.	- 
4	3
 
6	5 
.	-s%   8!E:AE !E-
E
E*-
E;c                 6   Uc   [         R                  " U R                  5      OUnUR                  R                  S:X  d   eU R
                   Vs/ s H  n[         R                  " X!5      PM     nn[         R                  " U R                  U5      nX44$ s  snf )a@  Access a batch of episodes from the memory.

Padding elements after the length of each episode are unspecified and might
contain old data.

Args:
  rows: Episodes to select, defaults to all.

Returns:
  Tuple containing a tuple of transition quantiries with batch and time
  dimensions, and a batch of sequence lengths.
r+   )r   r#   r   r   r0   r   r$   r   )r   r&   r>   episoder'   s        r   dataEpisodeMemory.datau   sw     (,|288DNN#D::q   7;}}E}Gryy'}GEYYt||T*F? Fs    Bc                     Uc   [         R                  " U R                  5      OUnUR                  R                  S:X  d   e[         R
                  " U R                  U[         R                  " U5      5      $ )zReset episodes in the memory.

Internally, this only sets their lengths to zero. The memory entries will
be overridden by future calls to append() or replace().

Args:
  rows: Episodes to clear, defaults to all.

Returns:
  Operation.
r+   )r   r#   r   r   r0   rF   r   
zeros_liker%   s     r   clearEpisodeMemory.clear   sV     (,|288DNN#D::q   T\\4t1DEEr)   )r   r   r   r   r   )N)__name__
__module____qualname____firstlineno____doc__r    r'   r5   rJ   rN   rR   __static_attributes__ r)   r   r   r      s$    $.
)36;4&Fr)   r   )rX   
__future__r   r   r   tensorflow.compat.v1compatv1r   	Exception
tensorflowobjectr   rZ   r)   r   <module>rb      sB    # &  %##
|FF |F	  s   	+ 
88