Skip to content

Commit

Permalink
advances on gymnasium compatibility: environment header is now execut…
Browse files Browse the repository at this point in the history
…ed during environment creation
  • Loading branch information
Pierre Schumacher committed Jul 11, 2024
1 parent 2217d0e commit 85a669c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
2 changes: 2 additions & 0 deletions deprl/custom_distributed.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ def distribute(
env=environment, parallel=parallel, sequential=sequential
)

if "header" in tonic_conf:
exec(tonic_conf["header"])
dummy_environment = build_env_from_dict(build_dict)
max_episode_steps = dummy_environment._max_episode_steps
del dummy_environment
Expand Down
17 changes: 14 additions & 3 deletions deprl/vendor/tonic/environments/builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,42 @@

import gym.wrappers
import numpy as np
from myosuite.utils import gym

from deprl.vendor.tonic import environments
from deprl.vendor.tonic.utils import logger


def gym_environment(*args, **kwargs):
"""Returns a wrapped Gym environment."""
if "header" in kwargs:
kwargs.pop("header")

def _builder(*args, **kwargs):
return gym.make(*args, **kwargs)

return build_environment(_builder, *args, **kwargs)
return build_environment(_builder, *args, **kwargs, header=None)


def bullet_environment(*args, **kwargs):
"""Returns a wrapped PyBullet environment."""
if "header" in kwargs:
kwargs.pop("header")

def _builder(*args, **kwargs):
import pybullet_envs # noqa

return gym.make(*args, **kwargs)

return build_environment(_builder, *args, **kwargs)
return build_environment(_builder, *args, **kwargs, header=None)


def control_suite_environment(*args, **kwargs):
"""Returns a wrapped Control Suite environment."""

if "header" in kwargs:
kwargs.pop("header")

def _builder(name, *args, **kwargs):
domain, task = name.split("-")
environment = ControlSuiteEnvironment(
Expand All @@ -44,7 +52,7 @@ def _builder(name, *args, **kwargs):
)
return gym.wrappers.TimeLimit(environment, time_limit)

return build_environment(_builder, *args, **kwargs)
return build_environment(_builder, *args, **kwargs, header=None)


def build_environment(
Expand All @@ -54,6 +62,7 @@ def build_environment(
time_feature=False,
max_episode_steps="default",
scaled_actions=True,
header=None,
*args,
**kwargs,
):
Expand All @@ -62,6 +71,8 @@ def build_environment(
time_feature=True, see https://arxiv.org/pdf/1712.00378.pdf for more
details.
"""
if header is not None:
exec(header)

# Build the environment.
environment = builder(name, *args, **kwargs)
Expand Down
6 changes: 3 additions & 3 deletions experiments/myosuite_training_files/myoLegWalk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ tonic:
environment: deprl.environments.Gym('myoLegWalk-v0', scaled_actions=False, reset_type='random')
environment_name: deprl_baseline
full_save: 1
header: import deprl, gym, myosuite
header: import deprl, myosuite; from myosuite.utils import gym
name: myoLeg
parallel: 20
parallel: 1
resume: 1
seed: 0
sequential: 10
sequential: 1
test_environment: null
trainer: deprl.custom_trainer.Trainer(steps=int(1e8), epoch_steps=int(2e5), save_steps=int(1e6))
working_dir: ./baselines_DEPRL
7 changes: 4 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 85a669c

Please sign in to comment.