Closed elisavio closed 2 months ago
The simplest way around is to flatten the observation space.
from gymnasium.wrappers import FlattenObservation
env = FlattenObservation(CustomEnv())
Thank you very much for your answer. If I try your command with the above example and then sample a random observation I get something totally different from what I want
env1 = CustomEnv()
env1.observation_space.shape
env1.observation_space.sample()
env2 = FlattenObservation(CustomEnv())
env2.observation_space.shape
env2.observation_space.sample()
The two shapes and the results of the samples are different: in the case of env1 we have a shape of (2,2), in the case of env2 we have (10,).
A question naturally arises: are there differences in the performance of an algorithm depending on the way I represent the observation (in this case, a flattened or not flattened observation) ?
Indeed, it's different from what I expected too. It seems that flatten in the multi-discrete case works in a very counter-intuitive way (at least for me).
As far as I can see, there's no wrapper that allows this, so you'll have to create your own wrapper:
from gymnasium import ObservationWrapper
class FlattenMultiDiscrete(ObservationWrapper):
def __init__(self, env):
super().__init__(env)
self.observation_space = MultiDiscrete(env.observation_space.nvec.flatten())
def observation(self, observation):
return observation.flatten()
env = FlattenMultiDiscrete(CustomEnv())
Note: the env checker must be updated to warn users that we don't support multi-dim multi discrete and propose a fix (the one from @qgallouedec ).
Thank you very much for the answer. Tell me If I should close the issue, or I can leave it open until the bug is fixed.
Please let it open until the env checker is updated :)
🐛 Bug
I am implementing a simple custom environment for using PPO with MultiDiscrete observation space. It works if I use MultiDiscrete([ 5, 2, 2 ]), but when it becomes a multidimensional array it fails. In the code I attach I am using the MultiDiscrete observation given as example in https://gymnasium.farama.org/api/spaces/fundamental/#gymnasium.spaces.MultiDiscrete .
Code example
Relevant log output / Error message
System Info
Checklist