
    h                        S r SSKJr  SSKJr  SSKJr  SSKrSSK	J
r
  SSKJrJr   " S S	\\
\R                        5      rg)
zOImplementation of a space that consists of binary np.ndarrays of a fixed shape.    )annotations)Sequence)AnyN)NDArray)MaskNDArraySpacec                     ^  \ rS rSrSr S   SU 4S jjjr\SS j5       r\S 5       r S     SS jjr	SS jr
SS jrSS	 jrSS
 jrSS jrSrU =r$ )MultiBinary   a  An n-shape binary space.

Elements of this space are binary arrays of a shape that is fixed during construction.

Example:
    >>> from gymnasium.spaces import MultiBinary
    >>> observation_space = MultiBinary(5, seed=42)
    >>> observation_space.sample()
    array([1, 0, 1, 0, 1], dtype=int8)
    >>> observation_space = MultiBinary([3, 2], seed=42)
    >>> observation_space.sample()
    array([[1, 0],
           [1, 0],
           [1, 1]], dtype=int8)
c                  > [        U[        5      (       aD  [        U5      =U l        nU4n[        R                  " U5      S:  R                  5       (       d   eO[        U[        [        R                  45      (       aH  [        S U 5       5      =U l        n[        R                  " U5      S:  R                  5       (       d   eO[        S[        U5       35      e[        TU ]1  U[        R                  U5        g)a^  Constructor of :class:`MultiBinary` space.

Args:
    n: This will fix the shape of elements of the space. It can either be an integer (if the space is flat)
        or some sort of sequence (tuple, list or np.ndarray) if there are multiple axes.
    seed: Optionally, you can use this argument to seed the RNG that is used to sample from the space.
r   c              3  8   #    U  H  n[        U5      v   M     g 7fN)int).0is     W/home/james-whalen/.local/lib/python3.13/site-packages/gymnasium/spaces/multi_binary.py	<genexpr>'MultiBinary.__init__.<locals>.<genexpr>0   s     $7QSVVQs   z<Expected n to be an int or a sequence of ints, actual type: N)
isinstancer   nnpasarrayallr   ndarraytuple
ValueErrortypesuper__init__int8)selfr   seedinput_n	__class__s       r   r   MultiBinary.__init__   s     aQDFQdGJJw'!+002222Hbjj122$$7Q$777DFWJJw'!+002222NtTUwiX  	"''40    c                    U R                   $ )z.Has stricter type than gym.Space - never None.)_shaper!   s    r   shapeMultiBinary.shape9   s     {{r&   c                    g)zDChecks whether this space can be flattened to a :class:`spaces.Box`.T r)   s    r   is_np_flattenableMultiBinary.is_np_flattenable>   s     r&   c           
     2   Ub  Ub  [        SU SU 35      eUGb5  [        U[        R                  5      (       d   S[	        U5       35       eUR
                  [        R                  :X  d   SUR
                   35       eUR                  U R                  :X  d!   SU R                   SUR                   35       e[        R                  " US:H  US:H  -  US	:H  -  5      (       d
   S
U 35       e[        R                  " US	:H  U R                  R                  SS	U R                  U R
                  S9UR                  U R
                  5      5      $ UGb  [        U[        R                  5      (       d   S[	        U5       35       eUR
                  [        R                  :X  d   SUR
                   35       eUR                  U R                  :X  d   SU R                   SU 35       e[        R                  " [        R                  " US:  US:*  5      5      (       d
   SU 35       eU R                  R!                  U R                  S9U:*  R                  U R
                  5      $ U R                  R                  SS	U R                  U R
                  S9$ )a1  Generates a single random sample from this space.

A sample is drawn by independent, fair coin tosses (one toss per binary variable of the space).

Args:
    mask: An optional ``np.ndarray`` to mask samples with expected shape of ``space.shape``.
        For ``mask == 0`` then the samples will be ``0``, for a ``mask == 1`` then the samples will be ``1``.
        For random samples, using a mask value of ``2``.
        The expected mask shape is the space shape and mask dtype is ``np.int8``.
    probability: An optional ``np.ndarray`` to mask samples with expected shape of space.shape where each element
        represents the probability of the corresponding sample element being a 1.
        The expected mask shape is the space shape and mask dtype is ``np.float64``.

Returns:
    Sampled values from space
zIOnly one of `mask` or `probability` can be provided, actual values: mask=z, probability=z:The expected type of the mask is np.ndarray, actual type: z9The expected dtype of the mask is np.int8, actual dtype: z"The expected shape of the mask is z, actual shape: r         z9All values of a mask should be 0, 1 or 2, actual values: )lowhighsizedtypezAThe expected type of the probability is np.ndarray, actual type: zCThe expected dtype of the probability is np.float64, actual dtype: z)The expected shape of the probability is zOAll values of the sample probability should be between 0 and 1, actual values: )r5   )r   r   r   r   r   r6   r    r*   r   where	np_randomintegersr   astypefloat64logical_andrandom)r!   maskprobabilitys      r   sampleMultiBinary.sampleC   s   &  7[\`[aaop{o|}  bjj  YKDQUJ<XY  

bgg%XJ4::,WX% 

djj(]3DJJ<?OPTPZPZ|\](66tqy)TQY7  RJ4&QR  88	''AADFF$**'UDJJ' 
 $RZZ  gRSWXcSdRefg  !!RZZ/iTU`UfUfTghi/ !!TZZ/e:4::,FVWbVcde/66{a/1AB  o`al`mno  NN))tzz):kIQQ

  >>**qqtvvTZZ*XXr&   c           	     N   [        U[        5      (       a  [        R                  " U5      n[	        [        U[        R
                  5      =(       aQ    U R                  UR                  :H  =(       a1    [        R                  " [        R                  " US:H  US:H  5      5      5      $ )z?Return boolean specifying if x is a valid member of this space.r   r1   )	r   r   r   arrayboolr   r*   r   
logical_or)r!   xs     r   containsMultiBinary.contains   sp    a""Aq"**% 6

agg%6r}}Q!VQ!V45
 	
r&   c                J    [         R                  " U5      R                  5       $ )zCConvert a batch of samples from this space to a JSONable data type.)r   rC   tolist)r!   sample_ns     r   to_jsonableMultiBinary.to_jsonable   s    xx!((**r&   c                n    U Vs/ s H#  n[         R                  " X R                  5      PM%     sn$ s  snf )zCConvert a JSONable data type to a batch of samples from this space.)r   r   r6   )r!   rK   r@   s      r   from_jsonableMultiBinary.from_jsonable   s'    =EFX6

6::.XFFFs   *2c                "    SU R                    S3$ )z,Gives a string representation of this space.zMultiBinary()r   r)   s    r   __repr__MultiBinary.__repr__   s    dffXQ''r&   c                b    [        U[        5      =(       a    U R                  UR                  :H  $ )z5Check whether `other` is equivalent to this instance.)r   r
   r   )r!   others     r   __eq__MultiBinary.__eq__   s!    %-C$&&EGG2CCr&   rS   r   )r   z.NDArray[np.integer[Any]] | Sequence[int] | intr"   z int | np.random.Generator | None)returnztuple[int, ...])NN)r>   MaskNDArray | Noner?   r[   rZ   zNDArray[np.int8])rF   r   rZ   rD   )rK   zSequence[NDArray[np.int8]]rZ   list[Sequence[int]])rK   r\   rZ   zlist[NDArray[np.int8]])rZ   str)rW   r   rZ   rD   )__name__
__module____qualname____firstlineno____doc__r   propertyr*   r.   r@   rG   rL   rO   rT   rX   __static_attributes____classcell__)r$   s   @r   r
   r
      s    & 26191 /1 14    
 RV<Y&<Y<N<Y	<Y|	
+G(D Dr&   r
   )rb   
__future__r   collections.abcr   typingr   numpyr   numpy.typingr   gymnasium.spaces.spacer   r   r    r
   r-   r&   r   <module>rl      s8    U " $     5LD%() LDr&   