'How to check the actions available in OpenAI gym environment?

When using OpenAI gym, after importing the library with import gym, the action space can be checked with env.action_space. But this gives only the size of the action space. I would like to know what kind of actions each element of the action space corresponds to. Is there a simple way to do it?



Solution 1:[1]

If your action space is discrete and one dimensional, env.action_space will give you a Discrete object. You can access the number of actions available (which simply is an integer) like this:

env = gym.make("Acrobot-v1")
a = env.action_space
print(a)                    #prints Discrete(3)
print(a.n)                  #prints 3

If your action space is discrete and multi dimensional, you'd get a MultiDiscrete (instead of Discrete) object, on which you can call nvec (instead of n) to get an array describing the number of available action for each dimension. But note that it is not a very common case.

If you have a continous action space, env.action_space will give you a Box object. Here is how to access its properties:

env = gym.make("MountainCarContinuous-v0")
a = env.action_space
print(a)                    #prints Box(1,)
print(a.shape)              #prints (1,), note that you can do a.shape[0] which is 1 here
print(a.is_bounded())       #prints True if your action space is bounded
print(a.high)               #prints [1.] an array with the maximum value for each dim
print(a.low)                #prints [-1.] same for minimum value

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 upe