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

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值