6

I try to learn MC- Monte Carlo Method applied in blackjack using openAI Gym. And I do not understand these lines:

def __init__(self, natural=False):
    self.action_space = spaces.Discrete(2)
    self.observation_space = spaces.Tuple((
        spaces.Discrete(32),
        spaces.Discrete(11),
        spaces.Discrete(2)))
    self.seed()

Source from: https://github.com/openai/gym/blob/master/gym/envs/toy_text/blackjack.py

doob
  • 77
  • 1
  • 1
  • 5

1 Answers1

8

The observation space and the action space has been defined in the comments here

Observation Space:

The observation of a 3-tuple of: the player's current sum,
the dealer's one showing card (1-10 where 1 is ace),
and whether or not the player holds a usable ace (0 or 1).

eg: (14, 9, False) means the current sum is 14, card shown is 9 and there is no usable ace(because ace can be used as 1 or 11)

Action Space:

The player can request additional cards (hit=1) until they decide to stop
(stick=0) or exceed 21 (bust).

Discrete spaces are used when we have a discrete action/observation space to be defined in the environment. So spaces.Discrete(2) means that we have a discrete variable which can take one of the two possible values.

In the Blackjack environment,

self.action_space = spaces.Discrete(2)
# here spaces.Discrete(2) means that action can either be True or False.

self.observation_space = spaces.Tuple((
        spaces.Discrete(32),
        spaces.Discrete(11),
        spaces.Discrete(2)))
# here spaces.Discrete(32) corresponds to the 32 possible sum of card number possible
# here spaces.Discrete(11) corresponds to the 11 possible cards which can be dealed
# by the dealer: [1,2,3,4,5,6,7,8,9,10(king,queen,jack),11(ace if possible)]
# here spaces.Discrete(2) corresponds to the two possible uses of the ace: [True, False]
# True if it can be used as 11.

nsidn98
  • 1,037
  • 1
  • 9
  • 23
  • what is 32 possible sum of card number? How to get the 32 possible sum? thanks! – doob Aug 22 '19 at 13:58
  • @doob I guess u missing "The observation of a 3-tuple of: the players current sum, the dealer's one showing card (1-10 where 1 is ace), and whether or not the player holds a usable ace (0 or 1)." in annotate. The players current sum is least one, while the max sum is obtained when players draw a 'ace' when current sum is '21'. 32 is definitely calculated by 11 + 21. Even I thought the max sum should be 30 by 10 + 20 ) – Pavlos Panteliadis Jan 15 '20 at 11:19
  • @doob, `Discrete(32)` has 32 possible values in this space. The range of value is actually __[0, 31]__, i.e., the max sum should be 31 = 11 (ace) + 10 + 10. Please also refer to [this discussion](https://github.com/openai/gym/issues/1410) – Chenglong Ma Jan 20 '22 at 23:18