Getting used to spaces

In OpenAI Gym, actions and observations are mostly instances of the Discrete or Box class. These two classes represent different spaces. Box represents an n-dimensional array, while Discrete, on the other hand, is a space that allows a fixed range of non-negative numbers. In the preceding table, we have already seen that the observation of CartPole is encoded by four floats, meaning that it's an instance of the Box class. It is possible to check the type and dimension of the observation spaces by printing the env.observation_space variable:

import gym

env = gym.make('CartPole-v1')
print(env.observation_space)

Indeed, as we expected, the output is as follows:

>>  Box(4,)
In this book, we mark the output of print() by introducing the printed text with >>.

In the same way, it is possible to check the dimension of the action space:

print(env.action_space)

This results in the following output:

>> Discrete(2)

In particular, Discrete(2) means that the actions could either have the value 0 or 1. Indeed, if we use the sampling function used in the preceding example, we obtain 0 or 1 (in CartPole, this means left or right):

print(env.action_space.sample())
>> 0
print(env.action_space.sample())
>> 1

The low and high instance attributes return the minimum and maximum values allowed by a Box space: 

print(env.observation_space.low)
>> [-4.8000002e+00 -3.4028235e+38 -4.1887903e-01 -3.4028235e+38]
print(env.observation_space.high)
>> [4.8000002e+00 3.4028235e+38 4.1887903e-01 3.4028235e+38]
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset