MineHcraft: Inspired from the popular game Minecraft.

A rather large and complex requirements graph:

 1"""# MineHcraft: Inspired from the popular game Minecraft.
 2
 3<img src="https://raw.githubusercontent.com/IRLL/HierarchyCraft/master/docs/images/minehcraft_human_demo.gif" width=100%/>
 4
 5A rather large and complex requirements graph:
 6<div class="graph">
 7.. include:: ../../../../docs/images/requirements_graphs/MineHcraft.html
 8</div>
 9"""
10
11from typing import Optional
12
13import hcraft.examples.minecraft.items as items
14from hcraft.examples.minecraft.env import ALL_ITEMS, MineHcraftEnv
15
16from hcraft.purpose import Purpose, RewardShaping
17from hcraft.task import GetItemTask
18
19MINEHCRAFT_GYM_ENVS = []
20__all__ = ["MineHcraftEnv"]
21
22
23# gym is an optional dependency
24try:
25    import gymnasium as gym
26
27    ENV_PATH = "hcraft.examples.minecraft.env:MineHcraftEnv"
28
29    # Simple MineHcraft with no reward, only penalty on illegal actions
30    gym.register(
31        id="MineHcraft-NoReward-v1",
32        entry_point=ENV_PATH,
33        kwargs={"purpose": None},
34    )
35    MINEHCRAFT_GYM_ENVS.append("MineHcraft-NoReward-v1")
36
37    # Get all items, place all zones_items and go everywhere
38    gym.register(
39        id="MineHcraft-v1",
40        entry_point=ENV_PATH,
41        kwargs={"purpose": "all"},
42    )
43    MINEHCRAFT_GYM_ENVS.append("MineHcraft-v1")
44
45    def _to_camel_case(name: str):
46        return "".join([subname.capitalize() for subname in name.split("_")])
47
48    def _register_minehcraft_single_item(
49        item: items.Item,
50        name: Optional[str] = None,
51        success_reward: float = 10.0,
52        timestep_reward: float = -0.1,
53        reward_shaping: RewardShaping = RewardShaping.REQUIREMENTS_ACHIVEMENTS,
54        version: int = 1,
55    ):
56        purpose = Purpose(timestep_reward=timestep_reward)
57        purpose.add_task(
58            GetItemTask(item, reward=success_reward),
59            reward_shaping=reward_shaping,
60        )
61        if name is None:
62            name = _to_camel_case(item.name)
63        gym_name = f"MineHcraft-{name}-v{version}"
64        gym.register(
65            id=gym_name,
66            entry_point=ENV_PATH,
67            kwargs={"purpose": purpose},
68        )
69        MINEHCRAFT_GYM_ENVS.append(gym_name)
70
71    replacement_names = {
72        items.COBBLESTONE: "Stone",
73        items.IRON_INGOT: "Iron",
74        items.GOLD_INGOT: "Gold",
75        items.ENDER_DRAGON_HEAD: "Dragon",
76    }
77
78    for item in ALL_ITEMS:
79        cap_item_name = "".join([part.capitalize() for part in item.name.split("_")])
80        item_id = replacement_names.get(item, cap_item_name)
81        _register_minehcraft_single_item(item, name=item_id)
82
83
84except ImportError:
85    pass

API Documentation

class MineHcraftEnv(typing.Generic[~ObsType, ~ActType]):
35class MineHcraftEnv(HcraftEnv):
36    """MineHcraft Environment: A minecraft-like HierarchyCraft Environment.
37
38    Default purpose is None (sandbox).
39
40    """
41
42    def __init__(self, **kwargs):
43        mc_transformations = build_minehcraft_transformations()
44        start_zone = kwargs.pop("start_zone", FOREST)
45        purpose = kwargs.pop("purpose", None)
46        if purpose == "all":
47            purpose = get_platinum_purpose()
48        mc_world = world_from_transformations(
49            mc_transformations,
50            start_zone=start_zone,
51            start_zones_items={
52                NETHER: [Stack(OPEN_NETHER_PORTAL)],
53                STRONGHOLD: [Stack(CLOSE_ENDER_PORTAL)],
54            },
55        )
56        mc_world.resources_path = Path(__file__).parent / "resources"
57        super().__init__(world=mc_world, name="MineHcraft", purpose=purpose, **kwargs)
58        self.metadata["video.frames_per_second"] = kwargs.pop("fps", 10)

MineHcraft Environment: A minecraft-like HierarchyCraft Environment.

Default purpose is None (sandbox).

MineHcraftEnv(**kwargs)
42    def __init__(self, **kwargs):
43        mc_transformations = build_minehcraft_transformations()
44        start_zone = kwargs.pop("start_zone", FOREST)
45        purpose = kwargs.pop("purpose", None)
46        if purpose == "all":
47            purpose = get_platinum_purpose()
48        mc_world = world_from_transformations(
49            mc_transformations,
50            start_zone=start_zone,
51            start_zones_items={
52                NETHER: [Stack(OPEN_NETHER_PORTAL)],
53                STRONGHOLD: [Stack(CLOSE_ENDER_PORTAL)],
54            },
55        )
56        mc_world.resources_path = Path(__file__).parent / "resources"
57        super().__init__(world=mc_world, name="MineHcraft", purpose=purpose, **kwargs)
58        self.metadata["video.frames_per_second"] = kwargs.pop("fps", 10)
Arguments:
  • world: World defining the environment.
  • purpose: Purpose of the player, defining rewards and termination. Defaults to None, hence a sandbox environment.
  • invalid_reward: Reward given to the agent for invalid actions. Defaults to -1.0.
  • render_window: Window using to render the environment with pygame.
  • name: Name of the environement. Defaults to 'HierarchyCraft'.
  • max_step: (Optional[int], optional): Maximum number of steps before episode truncation. If None, never truncates the episode. Defaults to None.