a
    ij7                     @   s4  d Z ddlZddlmZ ddlmZmZ ddlZddlmZ ddl	Z
ddl	mZ G dd dZdad	d
 Zee dejjjddddddddeejeej f eejeej f ejeej eeee eej eej eej ee edddZdd Zdd Zdd Zdd Zdd Zdd ZdddZdS ) z]Roll out open-loop trajectories from initial states, get subsequent states and sensor values.    N)Sequence)OptionalUnion)_rollout)typingc                   @   s   e Zd ZdZddee dddZdd Zdd	 Zd
d Z	de
jjjdddddddee
jee
j f ee
jee
j f ejeej eeee eej eej eej ee dddZdS )Rolloutz>Rollout object containing a thread pool for parallel rollouts.Nnthreadc                C   s&   |du rdn|| _ tj| j d| _dS )zConstruct a rollout object containing a thread pool for parallel rollouts.

    Args:
      nthread: Number of threads in pool.
        If zero, this pool is not started and rollouts run on the calling thread.
    Nr   r   )r	   r   r   rollout_)selfr	    r   N/home/ghrups/robot-bench/.venv/lib64/python3.9/site-packages/mujoco/rollout.py__init__   s    zRollout.__init__c                 C   s   | S Nr   r   r   r   r   	__enter__(   s    zRollout.__enter__c                 C   s   |    d S r   )close)r   exc_typeexc_valexc_tbr   r   r   __exit__+   s    zRollout.__exit__c                 C   s   | ` d | _ d S r   )r
   r   r   r   r   r   .   s    zRollout.closeFcontrol_specskip_checksnstepinitial_warmstartstate
sensordata
chunk_size)modeldatainitial_statecontrolr   r   r   r   r   r   r   c                C   s  | j du rtd|r<| j ||||||||	|
|
 |	|
fS t|tjsPt|}|tjjj	 @ rht
d|r~t|ts~t
d|rt|tst
dt||||	|
d td||d td	||	|
d
 t|}t|}t|}t|	}	t|
}
tdd||||	|
d}t|tr |dkr t|}t|trTt||krTt
d| dt| nt|tsf|g}t|tsx|g}td|pd||	|
d
}t|d tjjj	}t|d |}|d j}|d j}|dd D ]P}|t|tjjj	ks|t||ks||jks||jkrt
dqt|||	d t||d t||d t||
d t|dkrp|| n|}t||}t||}t|||}|	du rt|||f}	|
du rt|||f}
| j ||||||||	|
|
 |	|
fS )a   Rolls out open-loop trajectories from initial states, get subsequent state and sensor values.

    Python wrapper for rollout.cc, see documentation therein.
    Infers nroll and nstep.
    Tiles inputs with singleton dimensions.
    Allocates outputs if none are given.

    Args:
      model: An instance or length nroll sequence of MjModel with the same size signature.
      data: Associated mjData instance or sequence of instances with length nthread.
      initial_state: Array of initial states from which to roll out trajectories.
        ([nroll or 1] x nstate)
      control: Open-loop controls array to apply during the rollouts.
        ([nroll or 1] x [nstep or 1] x ncontrol)
      control_spec: mjtState specification of control vectors.
      skip_checks: Whether to skip internal shape and type checks.
      nstep: Number of steps in rollouts (inferred if unspecified).
      initial_warmstart: Initial qfrc_warmstart array (optional).
        ([nroll or 1] x nv)
      state: State output array (optional).
        (nroll x nstep x nstate)
      sensordata: Sensor data output array (optional).
        (nroll x nstep x nsensordata)
      chunk_size: Determines threadpool chunk size. If unspecified,
                  chunk_size = max(1, nroll / (nthread * 10))

    Returns:
      state:
        State output array, (nroll x nstep x nstate).
      sensordata:
        Sensor data output array, (nroll x nstep x nsensordata).

    Raises:
      RuntimeError: rollout requested after thread pool shutdown.
      ValueError: bad shapes or sizes.
    Nz,rollout requested after thread pool shutdownz2control_spec can only contain bits in mjSTATE_USERznstep must be an integerzchunk_size must be an integer)r!   r   r"   r   r      )r!   r      )r"   r   r   r      znroll inferred as z but model is length zmodels are not compatible)r!   r   )r"   )r   )r   )r
   RuntimeErrorrollout
isinstancemujocoMjModellistmjtStateZmjSTATE_USERvalue
ValueErrorint_check_must_be_numeric_check_number_of_dimensions
_ensure_2d
_ensure_3d_infer_dimensionlenZmj_stateSizeZmjSTATE_FULLPHYSICSnvnsensordata_check_trailing_dimension_tile_if_requirednpempty)r   r   r    r!   r"   r   r   r   r   r   r   r   ZnrollZnstateZncontrolr6   r7   mr   r   r   r'   2   s    4
		





zRollout.rollout)N)__name__
__module____qualname____doc__r   r/   r   r   r   r   r)   r,   mjSTATE_CTRLr-   r   r*   r   MjDatanpt	ArrayLikeboolr'   r   r   r   r   r      s6   
	 r   c                   C   s   t durt   da dS )zShutdown the persistent thread pool that is optionally created by rollout.

  This is called automatically interpreter shutdown, but can also be called manually.
  N)persistent_rolloutr   r   r   r   r   shutdown_persistent_pool   s    rG   F)r   r   r   r   r   r   r   persistent_pool)r   r    r!   r"   r   r   r   r   r   r   r   rH   c                C   s   t |ts|g}t|dkr$t|nd}|r`tdu r>t|datj|krZt  t|dat}n
t|d}z0|j| |||||||||	|
dW |s|  S n|s|  0 dS )a  Rolls out open-loop trajectories from initial states, get subsequent states and sensor values.

  Python wrapper for rollout.cc, see documentation therein.
  Infers nroll and nstep.
  Tiles inputs with singleton dimensions.
  Allocates outputs if none are given.

  Args:
    model: An instance or length nroll sequence of MjModel with the same size signature.
    data: Associated mjData instance or sequence of instances with length nthread.
    initial_state: Array of initial states from which to roll out trajectories.
      ([nroll or 1] x nstate)
    control: Open-loop controls array to apply during the rollouts.
      ([nroll or 1] x [nstep or 1] x ncontrol)
    control_spec: mjtState specification of control vectors.
    skip_checks: Whether to skip internal shape and type checks.
    nstep: Number of steps in rollouts (inferred if unspecified).
    initial_warmstart: Initial qfrc_warmstart array (optional).
      ([nroll or 1] x nv)
    state: State output array (optional).
      (nroll x nstep x nstate)
    sensordata: Sensor data output array (optional).
      (nroll x nstep x nsensordata)
    chunk_size: Determines threadpool chunk size. If unspecified,
                chunk_size = max(1, nroll / (nthread * 10))
    persistent_pool: Determines if a persistent thread pool is created or reused.

  Returns:
    state:
      State output array, (nroll x nstep x nstate).
    sensordata:
      Sensor data output array, (nroll x nstep x nsensordata).

  Raises:
    ValueError: bad shapes or sizes.
  r%   r   Nr   r   )r(   r+   r5   rF   r   r	   r   r'   )r   r    r!   r"   r   r   r   r   r   r   r   rH   r	   r
   r   r   r   r'     s@    3




 r'   c                  K   sD   |   D ]6\}}|d u rqt|tjst|tst| dqd S )Nz must be a numpy array or float)itemsr(   r:   Zndarrayfloatr.   )kwargskeyr-   r   r   r   r0   ]  s
    r0   c                 K   s>   |  D ]0\}}|d u rq|j| krt| d|  dqd S )Nz can have at most z dimensions)rI   ndimr.   )rM   rK   rL   r-   r   r   r   r1   e  s
    
r1   c              	   K   sN   |  D ]@\}}|d u rq|jd | krtd| d|  d|jd  qd S )Nztrailing dimension of z	 must be z, got rI   shaper.   )dimrK   rL   r-   r   r   r   r8   m  s    r8   c                 C   s&   | d u rd S t jt | t jdS d S )NZdtype)r:   ascontiguousarrayZ
atleast_2dfloat64argr   r   r   r2   w  s    r2   c                 C   sx   | d u rd S | j dkr.| tjtjtjdf } n6| j dkrL| tjtjdf } n| j dkrd| tjdf } tj| tjdS d S )Nr   .r%   r#   rR   )rM   r:   ZnewaxisrS   rT   rU   r   r   r   r3   ~  s    


r3   c                 K   sv   |  D ]h\}}|du rq|j|  |kr|dkr<|j|  }q|j|  dkrtd|  d| d| d|j|   q|S )a{  Infers dimension `dim` given guess `value` from set of arrays.

  Args:
    dim: Dimension to be inferred.
    value: Initial guess of inferred value (1: unknown).
    **kwargs: List of arrays which should all have the same size (or 1) along
      dimension dim.

  Returns:
    Inferred dimension.

  Raises:
    ValueError: If mismatch between array shapes or initial guess.
  Nr%   z
dimension z inferred as z but z has rO   )rQ   r-   rK   namearrayr   r   r   r4     s    r4   c                 C   s\   | d u rd S t j| jtd}| jd dkr2||d< |d urP| jd dkrP||d< t | |S )NrR   r   r%   )r:   ZonesrM   r/   rP   Ztile)rX   Zdim0Zdim1Zrepsr   r   r   r9     s    r9   )N)N) r@   atexitcollections.abcr   r   r   r   r)   r   numpyr:   rC   r   rF   rG   registerr,   rA   r-   r*   rB   rD   r/   rE   r'   r0   r1   r8   r2   r3   r4   r9   r   r   r   r   <module>   sV    Z
 X
