o
    `۷iY                     @   s6   d dl Zd dlZd dlmZ eG dd dejZdS )    N)	PublicAPIc                       sX   e Zd ZdZdejf fdd	Zdd Zdd Zd	d
 Z	dd Z
dd Zdd Z  ZS )Simplexa  Represents a d - 1 dimensional Simplex in R^d.

    That is, all coordinates are in [0, 1] and sum to 1.
    The dimension d of the simplex is assumed to be shape[-1].

    Additionally one can specify the underlying distribution of
    the simplex as a Dirichlet distribution by providing concentration
    parameters. By default, sampling is uniform, i.e. concentration is
    all 1s.

    Example usage:
    self.action_space = spaces.Simplex(shape=(3, 4))
        --> 3 independent 4d Dirichlet with uniform concentration
    Nc                    s   t |ttfv s
J t || | jd | _|d ur7|jd |d ks2J |jd  d|d  || _d S t	dg| j | _d S )Nr   z vs    )
typetuplelistsuper__init__shapedimconcentrationnparray)selfr   r   dtype	__class__ T/home/ubuntu/vllm_env/lib/python3.10/site-packages/ray/rllib/utils/spaces/simplex.pyr
      s   
zSimplex.__init__c                 C   s$   t jj| j| jd d d| jS )Nr   )size)r   random	dirichletr   r   astyper   r   r   r   r   sample&   s   zSimplex.samplec                 C   s,   |j | j kottj|ddt|d S )Nr   )axis).r   )r   r   allclosesum	ones_like)r   xr   r   r   contains+   s   zSimplex.containsc                 C   s   t | S N)r   r   tolistr   sample_nr   r   r   to_jsonable0      zSimplex.to_jsonablec                 C   s   dd |D S )Nc                 S   s   g | ]}t |qS r   )r   asarray).0r   r   r   r   
<listcomp>4   s    z)Simplex.from_jsonable.<locals>.<listcomp>r   r$   r   r   r   from_jsonable3   r'   zSimplex.from_jsonablec                 C   s   d | j| jS )NzSimplex({}; {}))formatr   r   r   r   r   r   __repr__6   s   zSimplex.__repr__c                 C   s   t | j|jo| j|jkS r"   )r   r   r   r   )r   otherr   r   r   __eq__9   s   
zSimplex.__eq__)__name__
__module____qualname____doc__r   float32r
   r   r!   r&   r+   r-   r/   __classcell__r   r   r   r   r      s    r   )	gymnasiumgymnumpyr   ray.rllib.utils.annotationsr   Spacer   r   r   r   r   <module>   s
    