# _*_ coding utf-8 _*_
"""
@File : multi_binary_fixed.py
@Author: yxwang
@Date : 2021/1/12
@Desc :
"""
import numpy as np
import random
from .space import Space


class MultiBinaryFixed(Space):
    '''
    n:total number
    N:count of value 1
    '''

    def __init__(self, n, N):
        self.n = n
        self.N = N
        super(MultiBinaryFixed, self).__init__((self.n,), np.int8)

    def sample(self):
        # sample_list = [i for i in range(self.n)]
        # sample_list = random.sample(sample_list, self.N)
        # multi = np.zeros((self.n, 1))
        # multi[sample_list] = 1
        # multi = multi.reshape((n, ))
        # return self.np_random.randint(low=0, high=2, size=self.n, dtype=self.dtype)
        while True:
            sample = self.np_random.randint(low=0, high=2, size=self.n, dtype=self.dtype)
            if np.count_nonzero(sample) == self.N:
                return sample

    def contains(self, x):
        if isinstance(x, list):
            x = np.array(x)  # Promote list to array for contains check
        return ((x==0) | (x==1)).all()

    def to_jsonable(self, sample_n):
        return np.array(sample_n).tolist()

    def from_jsonable(self, sample_n):
        return [np.asarray(sample) for sample in sample_n]

    def __repr__(self):
        return "MultiBinary({})".format(self.n)

    def __eq__(self, other):
        return isinstance(other, MultiBinary) and self.n == other.n
f5c793c62d63f4f153e418ca256e9b6780e1ae7c
<mask token>


class MultiBinaryFixed(Space):
    <mask token>

    def __init__(self, n, N):
        self.n = n
        self.N = N
        super(MultiBinaryFixed, self).__init__((self.n,), np.int8)
    <mask token>

    def contains(self, x):
        if isinstance(x, list):
            x = np.array(x)
        return ((x == 0) | (x == 1)).all()

    def to_jsonable(self, sample_n):
        return np.array(sample_n).tolist()

    def from_jsonable(self, sample_n):
        return [np.asarray(sample) for sample in sample_n]
    <mask token>

    def __eq__(self, other):
        return isinstance(other, MultiBinary) and self.n == other.n

<mask token>


class MultiBinaryFixed(Space):
    <mask token>

    def __init__(self, n, N):
        self.n = n
        self.N = N
        super(MultiBinaryFixed, self).__init__((self.n,), np.int8)

    def sample(self):
        while True:
            sample = self.np_random.randint(low=0, high=2, size=self.n,
                dtype=self.dtype)
            if np.count_nonzero(sample) == self.N:
                return sample

    def contains(self, x):
        if isinstance(x, list):
            x = np.array(x)
        return ((x == 0) | (x == 1)).all()

    def to_jsonable(self, sample_n):
        return np.array(sample_n).tolist()

    def from_jsonable(self, sample_n):
        return [np.asarray(sample) for sample in sample_n]

    def __repr__(self):
        return 'MultiBinary({})'.format(self.n)

    def __eq__(self, other):
        return isinstance(other, MultiBinary) and self.n == other.n

<mask token>


class MultiBinaryFixed(Space):
    """
    n:total number
    N:count of value 1
    """

    def __init__(self, n, N):
        self.n = n
        self.N = N
        super(MultiBinaryFixed, self).__init__((self.n,), np.int8)

    def sample(self):
        while True:
            sample = self.np_random.randint(low=0, high=2, size=self.n,
                dtype=self.dtype)
            if np.count_nonzero(sample) == self.N:
                return sample

    def contains(self, x):
        if isinstance(x, list):
            x = np.array(x)
        return ((x == 0) | (x == 1)).all()

    def to_jsonable(self, sample_n):
        return np.array(sample_n).tolist()

    def from_jsonable(self, sample_n):
        return [np.asarray(sample) for sample in sample_n]

    def __repr__(self):
        return 'MultiBinary({})'.format(self.n)

    def __eq__(self, other):
        return isinstance(other, MultiBinary) and self.n == other.n

<mask token>
import numpy as np
import random
from .space import Space


class MultiBinaryFixed(Space):
    """
    n:total number
    N:count of value 1
    """

    def __init__(self, n, N):
        self.n = n
        self.N = N
        super(MultiBinaryFixed, self).__init__((self.n,), np.int8)

    def sample(self):
        while True:
            sample = self.np_random.randint(low=0, high=2, size=self.n,
                dtype=self.dtype)
            if np.count_nonzero(sample) == self.N:
                return sample

    def contains(self, x):
        if isinstance(x, list):
            x = np.array(x)
        return ((x == 0) | (x == 1)).all()

    def to_jsonable(self, sample_n):
        return np.array(sample_n).tolist()

    def from_jsonable(self, sample_n):
        return [np.asarray(sample) for sample in sample_n]

    def __repr__(self):
        return 'MultiBinary({})'.format(self.n)

    def __eq__(self, other):
        return isinstance(other, MultiBinary) and self.n == other.n

# _*_ coding utf-8 _*_
"""
@File : multi_binary_fixed.py
@Author: yxwang
@Date : 2021/1/12
@Desc :
"""
import numpy as np
import random
from .space import Space


class MultiBinaryFixed(Space):
    '''
    n:total number
    N:count of value 1
    '''

    def __init__(self, n, N):
        self.n = n
        self.N = N
        super(MultiBinaryFixed, self).__init__((self.n,), np.int8)

    def sample(self):
        # sample_list = [i for i in range(self.n)]
        # sample_list = random.sample(sample_list, self.N)
        # multi = np.zeros((self.n, 1))
        # multi[sample_list] = 1
        # multi = multi.reshape((n, ))
        # return self.np_random.randint(low=0, high=2, size=self.n, dtype=self.dtype)
        while True:
            sample = self.np_random.randint(low=0, high=2, size=self.n, dtype=self.dtype)
            if np.count_nonzero(sample) == self.N:
                return sample

    def contains(self, x):
        if isinstance(x, list):
            x = np.array(x)  # Promote list to array for contains check
        return ((x==0) | (x==1)).all()

    def to_jsonable(self, sample_n):
        return np.array(sample_n).tolist()

    def from_jsonable(self, sample_n):
        return [np.asarray(sample) for sample in sample_n]

    def __repr__(self):
        return "MultiBinary({})".format(self.n)

    def __eq__(self, other):
        return isinstance(other, MultiBinary) and self.n == other.n