openai / gym

A toolkit for developing and comparing reinforcement learning algorithms.
https://www.gymlibrary.dev
Other
34.77k stars 8.61k forks source link

Discrete Class , shape function broken #791

Closed aishik-pyne closed 3 years ago

aishik-pyne commented 6 years ago

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?

olegklimov commented 6 years ago

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

aishik-pyne commented 6 years ago

Yes but there is property function defined

@property 
def shape(self):
    return (self.n,)
olegklimov commented 6 years ago

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

aishik-pyne commented 6 years ago

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

bionicles commented 6 years ago

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 5 years ago

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?

gianmarcoaversanoenx commented 3 years ago

I have the same issue.

joshuap2613 commented 3 years ago

I also have the same issue

siddhant-pradhan commented 3 years ago

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>

naji-s commented 3 years ago

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

jkterry1 commented 3 years ago

PRs are welcome for this

RedTachyon commented 3 years ago

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 ()

naji-s commented 3 years ago

@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.

RedTachyon commented 3 years ago

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.