Skip to main content

HierarchyCraft - Environements builder for hierarchical reasoning research

Fury - PyPi stable version PePy - Downloads PePy - Downloads per week Licence - GPLv3

Codacy - grade Codacy - coverage CodeStyle - Black CodeStyle - Ruff

HierarchyCraft

HierarchyCraft (hcraft for short) is a Python library designed to create arbitrary hierarchical environments that are compatible with both the OpenAI Gym Reinforcement Learning Framework and AIPlan4EU Unified Planning Framework. This library enables users to easily create complex hierarchical structures that can be used to test and develop various reinforcement learning or planning algorithms.

In environments built with HierarchyCraft the agent (player) has an inventory and can navigate into abstract zones that themselves have inventories.

The action space of HierarchyCraft environments consists of sub-tasks, referred to as Transformations, as opposed to detailed movements and controls. But each Transformations has specific requirements to be valid (eg. have enought of an item, be in the right place), and these requirements may necessitate the execution of other Transformations first, inherently creating a hierarchical structure in HierarchyCraft environments.

This concept is visually represented by the Requirements graph depicting the hierarchical relationships within each HierarchyCraft environment. The Requirements graph is directly constructed from the list of Transformations composing the environement.

More details about requirements graph can be found in the documentation at hcraft.requirements and example of requirements graph for some HierarchyCraft environements can be found in hcraft.examples.

No feature extraction for fast research even with low compute

HierarchyCraft returns vectorized state information, which plainly and directly describes the player's inventory, current positions, and the inventory of the current zone. Compared to benchmarks that return grids, pixel arrays, text or sound, we directly return a low-dimensional latent representation that doesn't need to be learned. Therefore saving compute time and allowing researchers to focus only the the hierarchical reasoning part.

See hcraft.state for more details.

Create your own tailored HierarchyCraft environments

You can use HierarchyCraft to create various custom hierarchical environments from a list of customized Transformations.

See hcraft.env for a complete tutorial on creating custom environments.

Installation

Using pip

Without optional dependencies:

pip install hcraft

All hcraft environments can use a common graphical user interface that can be used with gui requirements:

pip install hcraft[gui]

Gym environment can be obtained with gym requirements:

pip install hcraft[gym]

Planning problems can be obtained throught the upf interface with planning requirements:

pip install hcraft[planning]

Some complex graph can be represented in html interactive visualisation:

pip install hcraft[htmlvis]

Quickstart

Play yourself!

A player knowing Minecraft will find MineHcraft easy.

Install the graphical user interface optional dependencies:

pip install hcraft[gui]

Using the command line interface

You can directly try to play yourself with the GUI available for any HierarchyCraft environments, for example:

hcraft minecraft

For more examples:

hcraft --help

Using the programmatic interface:

from hcraft import get_human_action
from hcraft.examples import MineHcraftEnv

env = MineHcraftEnv()
# or env: MineHcraftEnv = gym.make("MineHcraft-NoReward-v1")
n_episodes = 2
for _ in range(n_episodes):
    env.reset()
    done = False
    total_reward = 0
    while not done:
        env.render()
        action = get_human_action(env)
        print(f"Human pressed: {env.world.transformations[action]}")

        _observation, reward, done, _info = env.step(action)
        total_reward += reward

    print(f"SCORE: {total_reward}")

As a Gym RL environment

Using the programmatic interface, any HierarchyCraft environment can easily be interfaced with classic reinforcement learning agents.

import numpy as np
from hcraft.examples import MineHcraftEnv

def random_legal_agent(observation, action_is_legal):
    action = np.random.choice(np.nonzero(action_is_legal)[0])
    return int(action)

env = MineHcraftEnv(max_step=10)
done = False
observation, _info = env.reset()
while not done:
    action_is_legal = env.action_masks()
    action = random_legal_agent(observation, action_is_legal)
    _observation, _reward, terminated, truncated, _info = env.step(action)
# Other examples of HierarchyCraft environments
from hcraft.examples import  TowerHcraftEnv, RecursiveHcraftEnv, RandomHcraftEnv

tower_env = TowerHcraftEnv(height=3, width=2)
# or tower_env = gym.make("TowerHcraft-v1", height=3, width=2)
recursive_env = RecursiveHcraftEnv(n_items=6)
# or recursive_env = gym.make("RecursiveHcraft-v1", n_items=6)
random_env = RandomHcraftEnv(n_items_per_n_inputs={0:2, 1:5, 2:10}, seed=42)
# or random_env = gym.make("RandomHcraft-v1", n_items_per_n_inputs={0:2, 1:5, 2:10}, seed=42)

See hcraft.env for a more complete description.

As a UPF problem for planning

HierarchyCraft environments can be converted to planning problem in one line thanks to the Unified Planning Framework (UPF):

# Example env
env = TowerHcraftEnv(height=3, width=2)

# Make it into a unified planning problem
planning_problem = env.planning_problem()
print(planning_problem.upf_problem)

Then they can be solved with any compatible planner for UPF:

# Solve the planning problem and show the plan
planning_problem.solve()
print(planning_problem.plan)

The planning_problem can also give actions to do in the environment, triggering replaning if necessary:

done = False
_observation, _info = env.reset()
while not done:
    # Automatically replan at the end of each plan until env termination

    # Observations are not used when blindly following a current plan
    # But the state in required in order to replan if there is no plan left
    action = planning_problem.action_from_plan(env.state)
    if action is None:
        # Plan is existing but empty, thus nothing to do, thus terminates
        done = True
        continue
    _observation, _reward, terminated, truncated, _info = env.step(action)
    done = terminated or truncated

if terminated:
    print("Success ! The plan worked in the actual environment !")
else:
    print("Failed ... Something went wrong with the plan or the episode was truncated.")

See hcraft.planning for a more complete description.

More about HierarchyCraft

Online documentation

Learn more in the DOCUMENTATION

Contributing

You want to contribute to HierarchyCraft ? See our contributions guidelines and join us !

Metadata

Release files for hcraft 2.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hcraft 2.0.1
File Size Uploaded
hcraft-2.0.1.tar.gz 13.0 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for hcraft 2.0.1
File Interpreter ABI Platform
hcraft-2.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 16.8 MB

Release files / hcraft-2.0.1.tar.gz

Download URL hcraft-2.0.1.tar.gz
Size 13.0 MB
Tags Source
SHA-256 checksum
How to use checksums
e378320d46f6f97a3be0afe5edafc4609e38304aa83ecd22c984b3fc6fd01399
BLAKE2b-256 checksum
How to use checksums
bc2c64293247ae2ebe4d533dc21bfc687465ff88fcf481b0dd50634d48b34bba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release files / hcraft-2.0.1-py3-none-any.whl

Download URL hcraft-2.0.1-py3-none-any.whl
Size 3.9 MB
Tags Python 3
SHA-256 checksum
How to use checksums
25482a31b3d516be4ec9ad0840786868c4525cb0758b17b895207ee18ee9ed90
BLAKE2b-256 checksum
How to use checksums
ce1fcec003eb7e623c5bf9ff13e1651bed4d1ff5429bad3b4d361d1ffd05dad6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release history Release notifications | RSS feed

This release

2.0.1 This release

2 release files

2.0.0

2 release files

1.2.5

2 release files

1.2.4

2 release files

1.2.3

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page