gym.spaces.Discrete()

功能:创建一个离散的n维空间,n为整数

 

样例

样例1

def __init__(self, ball_speed=4, bat_speed=4, max_num_rounds=20):
        SCREEN_WIDTH, SCREEN_HEIGHT = 160, 210
        self.observation_space = spaces.Tuple([
            spaces.Box(
                low=0, high=255, shape=(SCREEN_HEIGHT, SCREEN_WIDTH, 3)),
            spaces.Box(
                low=0, high=255, shape=(SCREEN_HEIGHT, SCREEN_WIDTH, 3))
        ])
        self.action_space = spaces.Tuple(
            [spaces.Discrete(3), spaces.Discrete(3)])

        pygame.init()
        self._surface = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT))

        self._viewer = None
        self._game = PongGame(
            has_double_players=True,
            window_size=(SCREEN_WIDTH, SCREEN_HEIGHT),
            ball_speed=ball_speed,
            bat_speed=bat_speed,
            max_num_rounds=max_num_rounds) 

样例2

def __init__(self, *args, **kwargs):
        super(TestConverters, self).__init__(*args, **kwargs)
        self.space_d = spaces.Discrete(4)
        self.gym_out_d = 2
        self.rf_out_d = [0, 0, 1, 0]

        self.space_c = spaces.Box(-1, 1, [2, 4])
        self.gym_out_c = np.random.uniform(low=-1, high=1, size=(2, 4))
        self.rf_out_c = self.gym_out_c

        self.space_b = spaces.MultiBinary(4)
        self.gym_out_b = [0, 1, 0, 1]
        self.rf_out_b = [[1, 0], [0, 1], [1, 0], [0, 1]]

        self.space_t = spaces.Tuple((self.space_d,
                                     self.space_c,
                                     self.space_b,
                                     spaces.Tuple((self.space_d, self.space_c))
                                     ))
        self.gym_out_t = tuple([self.gym_out_d, self.gym_out_c, self.gym_out_b,
                                tuple([self.gym_out_d, self.gym_out_c])])
        self.rf_out_t = tuple([self.rf_out_d, self.rf_out_c, self.rf_out_b,
                               tuple([self.rf_out_d, self.rf_out_c])]) 

 

 

函数源代码如下:

import numpy as np
from .space import Space


class Discrete(Space):
    """A discrete space in :math:`\{ 0, 1, \dots, n-1 \}`. 
    
    Example::
    
        >>> Discrete(2)
        
    """
    def __init__(self, n):
        assert n >= 0
        self.n = n
        super(Discrete, self).__init__((), np.int64)

    def sample(self):
        return self.np_random.randint(self.n)

    def contains(self, x):
        if isinstance(x, int):
            as_int = x
        elif isinstance(x, (np.generic, np.ndarray)) and (x.dtype.kind in np.typecodes['AllInteger'] and x.shape == ()):
            as_int = int(x)
        else:
            return False
        return as_int >= 0 and as_int < self.n

    def __repr__(self):
        return "Discrete(%d)" % self.n

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

 

 

参考资料

https://www.programcreek.com/python/example/97538/gym.spaces.Discrete

你可能感兴趣的:(Python,gym)