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?
Asked
Active
Viewed 8,867 times
6

user12394113
- 381
- 3
- 13
1 Answers
7
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

upe
- 1,862
- 1
- 19
- 33

Valentin Macé
- 1,150
- 1
- 10
- 25
-
What would be the shape of my action space (a.shape) if I have 8 continuous actions each in the range from -1 to 1? I'm trying to convert a python script to tfjs and see that `a.shape` and have no idea what would be the value for my case. – Artod Jan 24 '22 at 20:49