Skip to content

Commit

Permalink
Add support for Gymnasium 1.0.0 (#177)
Browse files Browse the repository at this point in the history
Co-authored-by: Mark Towers <[email protected]>
Co-authored-by: Omar Younis <[email protected]>
  • Loading branch information
3 people committed Aug 22, 2024
1 parent 79811d9 commit 1306bab
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
5 changes: 5 additions & 0 deletions minari/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ def show(dataset: Annotated[str, typer.Argument()]):

if env_spec_json is not None:
assert isinstance(env_spec_json, str)
env_spec_json = ( # for gymnasium 1.0.0 compatibility
env_spec_json.replace('"order_enforce": true,', "")
.replace('"apply_api_compatibility": false,', "")
.replace('"autoreset": false, ', "")
)
env_spec = EnvSpec.from_json(env_spec_json)
env_spec_table = Table(show_header=False, highlight=True)
env_spec_table.add_column(style="bold")
Expand Down
10 changes: 10 additions & 0 deletions minari/dataset/minari_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,22 @@ def __init__(
env_spec = metadata.get("env_spec")
if env_spec is not None:
assert isinstance(env_spec, str)
env_spec = ( # for gymnasium 1.0.0 compatibility
env_spec.replace('"order_enforce": true,', "")
.replace('"apply_api_compatibility": false,', "")
.replace('"autoreset": false, ', "")
)
env_spec = EnvSpec.from_json(env_spec)
self._env_spec = env_spec

eval_env_spec = metadata.get("eval_env_spec")
if eval_env_spec is not None:
assert isinstance(eval_env_spec, str)
eval_env_spec = ( # for gymnasium 1.0.0 compatibility
eval_env_spec.replace('"order_enforce": true,', "")
.replace('"apply_api_compatibility": false,', "")
.replace('"autoreset": false, ', "")
)
eval_env_spec = EnvSpec.from_json(eval_env_spec)
self._eval_env_spec = eval_env_spec

Expand Down
5 changes: 5 additions & 0 deletions minari/dataset/minari_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ def read(cls, data_path: PathLike) -> MinariStorage:
if action_space is None or observation_space is None:
env_spec_str = metadata.get("env_spec")
assert isinstance(env_spec_str, str)
env_spec_str = ( # for gymnasium 1.0.0 compatibility
env_spec_str.replace('"order_enforce": true,', "")
.replace('"apply_api_compatibility": false,', "")
.replace('"autoreset": false, ', "")
)
env_spec = EnvSpec.from_json(env_spec_str)
env = gym.make(env_spec)
if observation_space is None:
Expand Down
3 changes: 1 addition & 2 deletions minari/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from gymnasium.core import ActType, ObsType
from gymnasium.envs.registration import EnvSpec
from gymnasium.error import NameNotFound
from gymnasium.wrappers.record_episode_statistics import RecordEpisodeStatistics
from gymnasium.wrappers import RecordEpisodeStatistics # type: ignore

from minari.data_collector.episode_buffer import EpisodeBuffer
from minari.dataset.minari_dataset import MinariDataset
Expand Down Expand Up @@ -492,7 +492,6 @@ def get_env_spec_dict(env_spec: EnvSpec) -> Dict[str, str]:
"reward_threshold": str(env_spec.reward_threshold),
"nondeterministic": f"`{env_spec.nondeterministic}`",
"order_enforce": f"`{env_spec.order_enforce}`",
"autoreset": f"`{env_spec.autoreset}`",
"disable_env_checker": f"`{env_spec.disable_env_checker}`",
"kwargs": f"`{env_spec.kwargs}`",
"additional_wrappers": f"`{env_spec.additional_wrappers}`",
Expand Down
2 changes: 1 addition & 1 deletion tests/integrations/test_agile_rl.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def dataset_id():


@pytest.fixture(autouse=True)
def createAndDestroyMinariDataset(dataset_id):
def create_and_destroy_minari_dataset(dataset_id):
env = gym.make("CartPole-v1")
env = DataCollector(env, record_infos=True)

Expand Down

0 comments on commit 1306bab

Please sign in to comment.