Skip to content

Commit

Permalink
✅ Pass gym.make test for common HCraft envs
Browse files Browse the repository at this point in the history
  • Loading branch information
MathisFederico committed Mar 25, 2023
1 parent c845de6 commit da95b04
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 47 deletions.
13 changes: 11 additions & 2 deletions src/hcraft/examples/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
import hcraft.examples.recursive as recursive
import hcraft.examples.tower as tower

from hcraft.examples.minecraft.env import MineHcraftEnv
from hcraft.examples.minecraft import MineHcraftEnv, MINEHCRAFT_GYM_ENVS
from hcraft.examples.random_simple import RandomHcraftEnv
from hcraft.examples.recursive import LightRecursiveHcraftEnv, RecursiveHcraftEnv
from hcraft.examples.tower import TowerHcraftEnv
from hcraft.examples.minicraft import MINICRAFT_ENVS
from hcraft.examples.minicraft import MINICRAFT_ENVS, MINICRAFT_GYM_ENVS

EXAMPLE_ENVS = [
MineHcraftEnv,
Expand All @@ -35,6 +35,15 @@
# RandomHcraftEnv,
]

HCRAFT_GYM_ENVS = [
*MINEHCRAFT_GYM_ENVS,
*MINICRAFT_GYM_ENVS,
"TowerHcraft-v1",
"RecursiveHcraft-v1",
"LightRecursiveHcraft-v1",
]


__all__ = [
"minecraft",
"minicraft",
Expand Down
10 changes: 8 additions & 2 deletions src/hcraft/examples/minecraft/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from hcraft.purpose import Purpose, RewardShaping, platinium_purpose
from hcraft.task import GetItemTask

MINEHCRAFT_GYM_ENVS = []

# gym is an optional dependency
try:
import gym
Expand All @@ -24,13 +26,15 @@
entry_point=ENV_PATH,
kwargs={"purpose": None},
)
MINEHCRAFT_GYM_ENVS.append("MineHcraft-NoReward-v1")

# Get all items, place all zones_items and go everywhere
gym.register(
id="MineHcraft-v1",
entry_point=ENV_PATH,
kwargs={"purpose": platinium_purpose(MineHcraftEnv().world)},
)
MINEHCRAFT_GYM_ENVS.append("MineHcraft-v1")

def _to_camel_case(name: str):
return "".join([subname.capitalize() for subname in name.split("_")])
Expand All @@ -50,13 +54,15 @@ def _register_minehcraft_single_item(
)
if name is None:
name = _to_camel_case(item.name)
gym_name = f"MineHcraft-{name}-v{version}"
gym.register(
id=f"MineHcraft-{name}-v{version}",
id=gym_name,
entry_point=ENV_PATH,
kwargs={"purpose": purpose},
)
MINEHCRAFT_GYM_ENVS.append(gym_name)

_register_minehcraft_single_item(items.DIRT)
_register_minehcraft_single_item(items.WOOD)
_register_minehcraft_single_item(items.COBBLESTONE, name="Stone")
_register_minehcraft_single_item(items.IRON_INGOT, name="Iron")
_register_minehcraft_single_item(items.DIAMOND)
Expand Down
76 changes: 49 additions & 27 deletions src/hcraft/examples/minicraft/__init__.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,56 @@
from hcraft.examples.minicraft.empty import MiniCraftEmpty
from hcraft.examples.minicraft.fourrooms import MiniCraftFourRooms
from hcraft.examples.minicraft.multiroom import MiniCraftMultiRoom
from hcraft.examples.minicraft.crossing import MiniCraftCrossing
from hcraft.examples.minicraft.doorkey import MiniCraftDoorKey
from hcraft.examples.minicraft.unlock import MiniCraftUnlock
from hcraft.examples.minicraft.unlockpickup import MiniCraftUnlockPickup
from hcraft.examples.minicraft.unlockpickupblocked import MiniCraftBlockedUnlockPickup
from hcraft.examples.minicraft.keycorridor import MiniCraftKeyCorridor
import inspect
from pathlib import Path

from hcraft.examples.minicraft.empty import MiniHCraftEmpty
from hcraft.examples.minicraft.fourrooms import MiniHCraftFourRooms
from hcraft.examples.minicraft.multiroom import MiniHCraftMultiRoom
from hcraft.examples.minicraft.crossing import MiniHCraftCrossing
from hcraft.examples.minicraft.doorkey import MiniHCraftDoorKey
from hcraft.examples.minicraft.unlock import MiniHCraftUnlock
from hcraft.examples.minicraft.unlockpickup import MiniHCraftUnlockPickup
from hcraft.examples.minicraft.unlockpickupblocked import MiniHCraftBlockedUnlockPickup
from hcraft.examples.minicraft.keycorridor import MiniHCraftKeyCorridor


MINICRAFT_ENVS = [
MiniCraftEmpty,
MiniCraftFourRooms,
MiniCraftMultiRoom,
MiniCraftCrossing,
MiniCraftDoorKey,
MiniCraftUnlock,
MiniCraftUnlockPickup,
MiniCraftBlockedUnlockPickup,
MiniCraftKeyCorridor,
MiniHCraftEmpty,
MiniHCraftFourRooms,
MiniHCraftMultiRoom,
MiniHCraftCrossing,
MiniHCraftDoorKey,
MiniHCraftUnlock,
MiniHCraftUnlockPickup,
MiniHCraftBlockedUnlockPickup,
MiniHCraftKeyCorridor,
]

__all__ = [
"MiniCraftEmpty",
"MiniCraftFourRooms",
"MiniCraftMultiRoom",
"MiniCraftCrossing",
"MiniCraftDoorKey",
"MiniCraftUnlock",
"MiniCraftUnlockPickup",
"MiniCraftBlockedUnlockPickup",
"MiniCraftKeyCorridor",
"MiniHCraftEmpty",
"MiniHCraftFourRooms",
"MiniHCraftMultiRoom",
"MiniHCraftCrossing",
"MiniHCraftDoorKey",
"MiniHCraftUnlock",
"MiniHCraftUnlockPickup",
"MiniHCraftBlockedUnlockPickup",
"MiniHCraftKeyCorridor",
]

MINICRAFT_GYM_ENVS = []

try:
import gym

ENV_PATH = "hcraft.examples.minicraft"

for env_class in MINICRAFT_ENVS:
env_name = env_class().name
submodule = Path(inspect.getfile(env_class)).name.split(".")[0]
env_path = f"{ENV_PATH}.{submodule}:{env_class.__name__}"
gym_name = f"{env_name}-v1"
gym.register(id=gym_name, entry_point=env_path)
MINICRAFT_GYM_ENVS.append(gym_name)


except ImportError:
pass
2 changes: 1 addition & 1 deletion src/hcraft/examples/minicraft/crossing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from hcraft.examples.minicraft.minicraft import MiniCraftEnv


class MiniCraftCrossing(MiniCraftEnv):
class MiniHCraftCrossing(MiniCraftEnv):
MINICRAFT_NAME = "Crossing"
__doc__ = MiniCraftEnv.description(MINICRAFT_NAME)

Expand Down
2 changes: 1 addition & 1 deletion src/hcraft/examples/minicraft/doorkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from hcraft.examples.minicraft.minicraft import MiniCraftEnv


class MiniCraftDoorKey(MiniCraftEnv):
class MiniHCraftDoorKey(MiniCraftEnv):
MINICRAFT_NAME = "DoorKey"
__doc__ = MiniCraftEnv.description(MINICRAFT_NAME)

Expand Down
2 changes: 1 addition & 1 deletion src/hcraft/examples/minicraft/empty.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from hcraft.examples.minicraft.minicraft import MiniCraftEnv


class MiniCraftEmpty(MiniCraftEnv):
class MiniHCraftEmpty(MiniCraftEnv):
MINICRAFT_NAME = "Empty"
__doc__ = MiniCraftEnv.description(MINICRAFT_NAME)

Expand Down
2 changes: 1 addition & 1 deletion src/hcraft/examples/minicraft/fourrooms.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from hcraft.examples.minicraft.minicraft import MiniCraftEnv


class MiniCraftFourRooms(MiniCraftEnv):
class MiniHCraftFourRooms(MiniCraftEnv):
MINICRAFT_NAME = "FourRooms"
__doc__ = MiniCraftEnv.description(MINICRAFT_NAME)

Expand Down
2 changes: 1 addition & 1 deletion src/hcraft/examples/minicraft/keycorridor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from hcraft.examples.minicraft.minicraft import MiniCraftEnv


class MiniCraftKeyCorridor(MiniCraftEnv):
class MiniHCraftKeyCorridor(MiniCraftEnv):
MINICRAFT_NAME = "KeyCorridor"
__doc__ = MiniCraftEnv.description(MINICRAFT_NAME)

Expand Down
2 changes: 1 addition & 1 deletion src/hcraft/examples/minicraft/minicraft.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def __init__(
start_zones_items=start_zones_items,
)
world.resources_path = os.path.join(os.path.dirname(__file__), "resources")
super().__init__(world, name=f"MiniCraft{self.minicraft_name}", **kwargs)
super().__init__(world, name=f"MiniHCraft{self.minicraft_name}", **kwargs)

@abstractmethod
def build_transformations(self) -> List[Transformation]:
Expand Down
2 changes: 1 addition & 1 deletion src/hcraft/examples/minicraft/multiroom.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from hcraft.examples.minicraft.minicraft import MiniCraftEnv


class MiniCraftMultiRoom(MiniCraftEnv):
class MiniHCraftMultiRoom(MiniCraftEnv):
MINICRAFT_NAME = "MultiRoom"
__doc__ = MiniCraftEnv.description(MINICRAFT_NAME)

Expand Down
2 changes: 1 addition & 1 deletion src/hcraft/examples/minicraft/unlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from hcraft.examples.minicraft.minicraft import MiniCraftEnv


class MiniCraftUnlock(MiniCraftEnv):
class MiniHCraftUnlock(MiniCraftEnv):
MINICRAFT_NAME = "Unlock"
__doc__ = MiniCraftEnv.description(MINICRAFT_NAME)

Expand Down
2 changes: 1 addition & 1 deletion src/hcraft/examples/minicraft/unlockpickup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from hcraft.examples.minicraft.minicraft import MiniCraftEnv


class MiniCraftUnlockPickup(MiniCraftEnv):
class MiniHCraftUnlockPickup(MiniCraftEnv):
MINICRAFT_NAME = "UnlockPickup"
__doc__ = MiniCraftEnv.description(MINICRAFT_NAME)

Expand Down
2 changes: 1 addition & 1 deletion src/hcraft/examples/minicraft/unlockpickupblocked.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from hcraft.examples.minicraft.minicraft import MiniCraftEnv


class MiniCraftBlockedUnlockPickup(MiniCraftEnv):
class MiniHCraftBlockedUnlockPickup(MiniCraftEnv):
MINICRAFT_NAME = "BlockedUnlockPickup"
__doc__ = MiniCraftEnv.description(MINICRAFT_NAME)

Expand Down
18 changes: 12 additions & 6 deletions tests/examples/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
from unified_planning.io import PDDLWriter

from hcraft.render.human import render_env_with_human
from hcraft.examples import EXAMPLE_ENVS
from hcraft.examples import EXAMPLE_ENVS, HCRAFT_GYM_ENVS
from hcraft.examples.minicraft import (
MiniCraftKeyCorridor,
MiniCraftBlockedUnlockPickup,
MiniHCraftKeyCorridor,
MiniHCraftBlockedUnlockPickup,
)
from hcraft.env import HcraftEnv

Expand All @@ -35,7 +35,7 @@ def test_pddl_solve(env_class):
writer.write_domain(os.path.join(pddl_dir, "domain.pddl"))
writer.write_problem(os.path.join(pddl_dir, "problem.pddl"))

if isinstance(env, MiniCraftBlockedUnlockPickup):
if isinstance(env, MiniHCraftBlockedUnlockPickup):
return # Infinite loop for no reason ???

done = False
Expand All @@ -48,8 +48,8 @@ def test_pddl_solve(env_class):


KNOWN_TO_FAIL = [
MiniCraftBlockedUnlockPickup,
MiniCraftKeyCorridor,
MiniHCraftBlockedUnlockPickup,
MiniHCraftKeyCorridor,
]


Expand All @@ -72,6 +72,12 @@ def test_can_solve(env_class):
check.is_true(env.purpose.terminated)


@pytest.mark.parametrize("env_gym_id", HCRAFT_GYM_ENVS)
def test_gym_make(env_gym_id):
gym = pytest.importorskip("gym")
gym.make(env_gym_id)


@pytest.mark.parametrize("env_class", EXAMPLE_ENVS)
def test_requirements_graph(env_class):
draw = True
Expand Down

0 comments on commit da95b04

Please sign in to comment.