openai/gym

Discrete Class , shape function broken

Closed this issue · 14 comments

I have upgraded gym to it's latest version.

When I run this code

import gym
env = gym.make('MountainCar-v0')
print(env.action_space.shape)

I get the output

()

However, git cloning the repo and running it from inside the folder gives the correct output

(3,)

Any Ideas?

It's discrete action space, it doesn't have shape, only n?

Yes but there is property function defined

@property 
def shape(self):
    return (self.n,)

Hm... Is n also wrong in your setup?

Nope. n is working fine.
I just found the function hence issued it. It fine :)

one issue is that if we want to train across many domains, if the discrete action spaces have a different shape interface, we have to do a bunch of if statements. if it's discrete, do this, if it's not, do something else

that is less than ideal and it would be best if all environments used the same commands to retrieve the dimensions of the observation and action space. this could be streamlined A LOT

yceny commented

Yes but there is property function defined

@property 
def shape(self):
    return (self.n,)

I got the same error that it gives () shape. Where should this property function defined?

I have the same issue.

I also have the same issue

Yeah I have the same issue. In the latest version of gym, the Discrete class no longer has the shape function defined. Is there a reason why this was done/is there a workaround>

wait why is this closed? the problem still exist from what i can see

PRs are welcome for this

Is the intended behavior really (3,)? I think this can be "fixed" by passing the shape in the super call in gym/spaces/discrete.py#L17, but this would imply that the the value would be a 3-element vector. In reality, elements of a discrete space are (generally?) represented by integers, which in numpy have a shape of ()

@RedTachyon from what I can understand gym is expected to convert the discrete spaces into one-hot vectors and hence in this case the actions which are i assume are left, right, and do nothing would end up having a one-hot vector dimension of 3.

To hopefully avoid further confusion, I'm pretty sure this is currently working as intended.

Nowhere in gym (I think) are discrete actions represented as one-hot vectors, this only (potentially) comes into play on the algorithmic side. But the actions passed to a Discrete action space are individual integers, same with the .sample() method. And individual integers do have a shape of (). Changing this would lead to a pretty confusing behavior in which an action of the shape env.action_space.shape is invalid, and where env.action_space.sample() has a different shape than the action space itself.