Farama-Foundation / Minari

A standard format for offline reinforcement learning datasets, with popular reference datasets and related utilities
https://minari.farama.org
Other
300 stars 44 forks source link

[Bug Report] Serialization failed for BabyAI MissionSpace #123

Closed helenlu66 closed 3 months ago

helenlu66 commented 1 year ago

Describe the bug When creating a BabyAI dataset, I get the following NotImplementedError No serialization method available for MissionSpace(<function BabyAIMissionSpace._gen_mission at 0x7f7a93fcdb90>, None)

Code example

import gymnasium as gym
import minari
from minari import DataCollectorV0

dataset_id = 'BabyAI-GoToLocal-v0'
env = gym.make(dataset_id)
env = DataCollectorV0(env, record_infos=True, max_buffer_steps=100000)

total_episodes = 100

for _ in range(total_episodes):
    env.reset(seed=123)
    while True:
        # random action policy
        action = env.action_space.sample()
        obs, rew, terminated, truncated, info = env.step(action)

        if terminated or truncated:
            break

dataset = minari.create_dataset_from_collector_env(dataset_id=dataset_id, 
                                               collector_env=env,
                                               algorithm_name="Random-Policy"
                                               )

System Info Describe the characteristic of your environment:

Additional context BabyAI is part of MiniGrid: BabyAI GoToLocal

Checklist

balisujohn commented 1 year ago

Thanks for your report. It looks like minigrid envs have MissionSpace spaces in their observation spaces https://github.com/Farama-Foundation/Minigrid/blob/546c040a9424f339b7ba69e911e4126cee216e64/minigrid/minigrid_env.py#L78. Minari only supports the spaces listed here: https://minari.farama.org/content/dataset_standards/#supported-spaces. The recommended solution would be to use a StepDataCallback like in this tutorial: https://minari.farama.org/tutorials/dataset_creation/observation_space_subseting/ Crucially, the dataset will have to have a different observation space than the actual env.

balisujohn commented 1 year ago

Also, to use this feature, you will need to use minari 0.4.1 which supports python3.8 and later. If need to use python3.7, then I would recommend creating a wrapper env for the GoToLocal env with a minari-compliant observation space.

rodrigodelazcano commented 1 year ago

Hey @helenlu66 . As @balisujohn mentioned we only support the spaces that are listed in the docs. However, for the Minigrid envrionments you can still store the full observation with a minor hack.

import gymnasium as gym
from gymnasium.spaces.text import alphanumeric, Text
import minari
from minari import DataCollectorV0
import copy

dataset_id = 'BabyAI-GoToLocal-v0'
env = gym.make(dataset_id)
dataset_observation_space = copy.deepcopy(env.observation_space)

# include all alphanumeric characters plus space
charset = " "
for character in alphanumeric:
    charset += character

dataset_observation_space["mission"] = Text(max_length=200, charset=charset)
env = DataCollectorV0(env, record_infos=True, max_buffer_steps=100000, observation_space=dataset_observation_space)

total_episodes = 100

for _ in range(total_episodes):
    env.reset(seed=123)
    while True:
        # random action policy
        action = env.action_space.sample()
        obs, rew, terminated, truncated, info = env.step(action)

        if terminated or truncated:
            break

dataset = minari.create_dataset_from_collector_env(dataset_id=dataset_id, 
                                               collector_env=env,
                                               algorithm_name="Random-Policy"
                                               )

The differences from the code that you shared is that I substitute the MissionSpace in the observation space for a Gymnasium Text space. This will be your new dataset_observation_space which you can set in the DataCollectorV0.

The reason why this works is because MissionSpace is basically bounding a Text space to contain viable missions of the current minigrid environment so that you can sample valid observations from it. A text space should contain all of the possible mission values.

And also, as @balisujohn already said I strongly suggest updating to python >=3.8 and using the latest minari release 0.4.1

rodrigodelazcano commented 1 year ago

@balisujohn @younik this could be a cool use case for custom dataset spaces to include in the tutorials. What do you think?

balisujohn commented 1 year ago

@rodrigodelazcano I don't think it would hurt to add a tutorial for using StepDataCallback with this environment. I think it would also be good to make this error message No serialization method available for space) explicitly say "Minari does not support this space"