From afbd479a155b0bf6be21c38fd894202593d624ff Mon Sep 17 00:00:00 2001 From: Theresa Eimer Date: Tue, 9 Jan 2024 16:39:22 +0100 Subject: [PATCH] fix: gravity limits inverted --- Makefile | 3 +++ carl/envs/__init__.py | 35 ++++++++++++++++++++++------ carl/envs/dmc/carl_dm_finger.py | 2 +- carl/envs/dmc/carl_dm_fish.py | 2 +- carl/envs/dmc/carl_dm_quadruped.py | 2 +- carl/envs/dmc/carl_dm_walker.py | 2 +- carl/envs/dmc/dmc_tasks/utils.py | 6 ++++- carl/envs/gymnasium/__init__.py | 37 ++++++++++++++++++++++++++++++ 8 files changed, 77 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 6c5fce1c..65613bfb 100644 --- a/Makefile +++ b/Makefile @@ -40,6 +40,9 @@ install-dev: $(PIP) install -e ".[dev, docs]" pre-commit install +install: + $(PIP) install -e . + check-black: $(BLACK) carl test --check || : diff --git a/carl/envs/__init__.py b/carl/envs/__init__.py index b03f906c..0578a7a5 100644 --- a/carl/envs/__init__.py +++ b/carl/envs/__init__.py @@ -4,10 +4,15 @@ import warnings # Classic control is in gym and thus necessary for the base version to run -from carl import envs from carl.envs.gymnasium import * -__all__ = envs.gymnasium.__all__ +__all__ = [ + "CARLAcrobot", + "CARLCartPole", + "CARLMountainCar", + "CARLMountainCarContinuous", + "CARLPendulum", +] def check_spec(spec_name: str) -> bool: @@ -39,28 +44,44 @@ def check_spec(spec_name: str) -> bool: if found: from carl.envs.gymnasium.box2d import * - __all__ += envs.gymnasium.box2d.__all__ + __all__ += ["CARLBipedalWalker", "CARLLunarLander", "CARLVehicleRacing"] found = check_spec("brax") if found: from carl.envs.brax import * - __all__ += envs.brax.__all__ + __all__ += [ + "CARLBraxAnt", + "CARLBraxHalfcheetah", + "CARLBraxHopper", + "CARLBraxHumanoid", + "CARLBraxHumanoidStandup", + "CARLBraxInvertedDoublePendulum", + "CARLBraxInvertedPendulum", + "CARLBraxPusher", + "CARLBraxReacher", + "CARLBraxWalker2d", + ] found = check_spec("py4j") if found: from carl.envs.mario import * - __all__ += envs.mario.__all__ + __all__ += ["CARLMarioEnv"] found = check_spec("dm_control") if found: from carl.envs.dmc import * - __all__ += envs.dmc.__all__ + __all__ += [ + "CARLDmcFingerEnv", + "CARLDmcFishEnv", + "CARLDmcQuadrupedEnv", + "CARLDmcWalkerEnv", + ] found = check_spec("distance") if found: from carl.envs.rna import * - __all__ += envs.rna.__all__ + __all__ += ["CARLRnaDesignEnv"] diff --git a/carl/envs/dmc/carl_dm_finger.py b/carl/envs/dmc/carl_dm_finger.py index 88be38e2..ce1ef575 100644 --- a/carl/envs/dmc/carl_dm_finger.py +++ b/carl/envs/dmc/carl_dm_finger.py @@ -13,7 +13,7 @@ class CARLDmcFingerEnv(CARLDmcEnv): def get_context_features() -> dict[str, ContextFeature]: return { "gravity": UniformFloatContextFeature( - "gravity", lower=-np.inf, upper=-0.1, default_value=-9.81 + "gravity", lower=0.1, upper=np.inf, default_value=9.81 ), "friction_torsional": UniformFloatContextFeature( "friction_torsional", lower=0, upper=np.inf, default_value=1.0 diff --git a/carl/envs/dmc/carl_dm_fish.py b/carl/envs/dmc/carl_dm_fish.py index 31e077c6..669c88dc 100644 --- a/carl/envs/dmc/carl_dm_fish.py +++ b/carl/envs/dmc/carl_dm_fish.py @@ -13,7 +13,7 @@ class CARLDmcFishEnv(CARLDmcEnv): def get_context_features() -> dict[str, ContextFeature]: return { "gravity": UniformFloatContextFeature( - "gravity", lower=-np.inf, upper=-0.1, default_value=-9.81 + "gravity", lower=0.1, upper=np.inf, default_value=9.81 ), "friction_torsional": UniformFloatContextFeature( "friction_torsional", lower=0, upper=np.inf, default_value=1.0 diff --git a/carl/envs/dmc/carl_dm_quadruped.py b/carl/envs/dmc/carl_dm_quadruped.py index 3e00d5f8..697750d0 100644 --- a/carl/envs/dmc/carl_dm_quadruped.py +++ b/carl/envs/dmc/carl_dm_quadruped.py @@ -13,7 +13,7 @@ class CARLDmcQuadrupedEnv(CARLDmcEnv): def get_context_features() -> dict[str, ContextFeature]: return { "gravity": UniformFloatContextFeature( - "gravity", lower=-np.inf, upper=-0.1, default_value=-9.81 + "gravity", lower=0.1, upper=np.inf, default_value=9.81 ), "friction_torsional": UniformFloatContextFeature( "friction_torsional", lower=0, upper=np.inf, default_value=1.0 diff --git a/carl/envs/dmc/carl_dm_walker.py b/carl/envs/dmc/carl_dm_walker.py index b16cdd70..9e88e051 100644 --- a/carl/envs/dmc/carl_dm_walker.py +++ b/carl/envs/dmc/carl_dm_walker.py @@ -13,7 +13,7 @@ class CARLDmcWalkerEnv(CARLDmcEnv): def get_context_features() -> dict[str, ContextFeature]: return { "gravity": UniformFloatContextFeature( - "gravity", lower=-np.inf, upper=-0.1, default_value=-9.81 + "gravity", lower=0.1, upper=np.inf, default_value=9.81 ), "friction_torsional": UniformFloatContextFeature( "friction_torsional", lower=0, upper=np.inf, default_value=1.0 diff --git a/carl/envs/dmc/dmc_tasks/utils.py b/carl/envs/dmc/dmc_tasks/utils.py index 7482dfbe..08abc366 100644 --- a/carl/envs/dmc/dmc_tasks/utils.py +++ b/carl/envs/dmc/dmc_tasks/utils.py @@ -122,17 +122,21 @@ def adapt_context(xml_string: bytes, context: Context) -> bytes: # find option settings and override them if they exist, otherwise create new option option = mjcf.find(".//option") + import logging + if option is None: option = etree.Element("option") mjcf.append(option) if "gravity" in context: gravity = option.get("gravity") + logging.info(gravity) if gravity is not None: g = gravity.split(" ") gravity = " ".join([g[0], g[1], str(-context["gravity"])]) else: - gravity = " ".join(["0", "0", str(-context["gravity"])]) + gravity = " ".join(["0", "0", f"{str(-context['gravity'])}"]) + logging.info(gravity) option.set("gravity", gravity) if "wind_x" in context and "wind_y" in context and "wind_z" in context: diff --git a/carl/envs/gymnasium/__init__.py b/carl/envs/gymnasium/__init__.py index 62ba5092..7df661e1 100644 --- a/carl/envs/gymnasium/__init__.py +++ b/carl/envs/gymnasium/__init__.py @@ -1,3 +1,8 @@ +# flake8: noqa: F401 +# Modular imports +import importlib.util as iutil +import warnings + from carl.envs.gymnasium.classic_control import ( CARLAcrobot, CARLCartPole, @@ -13,3 +18,35 @@ "CARLMountainCarContinuous", "CARLPendulum", ] + + +def check_spec(spec_name: str) -> bool: + """Check if the spec is installed + + Parameters + ---------- + spec_name : str + Name of package that is necessary for the environment suite. + + Returns + ------- + bool + Whether the spec was found. + """ + spec = iutil.find_spec(spec_name) + found = spec is not None + if not found: + with warnings.catch_warnings(): + warnings.simplefilter("once") + warnings.warn( + f"Module {spec_name} not found. If you want to use these environments, please follow the installation guide." + ) + return found + + +# Environment loading +found = check_spec("Box2D") +if found: + from carl.envs.gymnasium.box2d import * + + __all__ += ["CARLBipedalWalker", "CARLLunarLander", "CARLVehicleRacing"]