MiniCraft - Unlock
Reproduces the minigrid Unlock gridworld environment as a HierarchyCraft environment.
Minigrid representation:
HierarchyCraft requirements graph:
1from typing import List 2 3from hcraft.elements import Item, Zone 4from hcraft.task import PlaceItemTask 5from hcraft.transformation import Transformation, Use, Yield, PLAYER, CURRENT_ZONE 6 7from hcraft.examples.minicraft.minicraft import MiniCraftEnv 8 9 10MINICRAFT_NAME = "Unlock" 11__doc__ = MiniCraftEnv.description(MINICRAFT_NAME, for_module_header=True) 12 13 14class MiniHCraftUnlock(MiniCraftEnv): 15 MINICRAFT_NAME = MINICRAFT_NAME 16 __doc__ = MiniCraftEnv.description(MINICRAFT_NAME) 17 18 START = Zone("start_room") 19 """Start room.""" 20 21 KEY = Item("key") 22 """Key used to unlock the door.""" 23 24 OPEN_DOOR = Item("open_door") 25 """Open door between the two rooms.""" 26 LOCKED_DOOR = Item("locked_door") 27 """Locked door between the two rooms, can be unlocked with a key.""" 28 29 def __init__(self, **kwargs) -> None: 30 self.task = PlaceItemTask(self.OPEN_DOOR) 31 super().__init__( 32 self.MINICRAFT_NAME, 33 purpose=self.task, 34 start_zone=self.START, 35 **kwargs, 36 ) 37 38 def build_transformations(self) -> List[Transformation]: 39 transformations = [] 40 41 search_for_key = Transformation( 42 "search_for_key", 43 inventory_changes=[ 44 Yield(CURRENT_ZONE, self.KEY, create=1, max=0), 45 Yield(PLAYER, self.KEY, create=0, max=0), 46 ], 47 zone=self.START, 48 ) 49 transformations.append(search_for_key) 50 51 pickup = Transformation( 52 "pickup_key", 53 inventory_changes=[ 54 Use(CURRENT_ZONE, self.KEY, consume=1), 55 Yield(PLAYER, self.KEY, create=1), 56 ], 57 ) 58 put_down = Transformation( 59 "put_down_key", 60 inventory_changes=[ 61 Use(PLAYER, self.KEY, consume=1), 62 Yield(CURRENT_ZONE, self.KEY, create=1), 63 ], 64 ) 65 transformations += [pickup, put_down] 66 67 search_for_door = Transformation( 68 "search_for_door", 69 inventory_changes=[ 70 Yield(CURRENT_ZONE, self.LOCKED_DOOR, max=0), 71 Yield(CURRENT_ZONE, self.OPEN_DOOR, create=0, max=0), 72 ], 73 zone=self.START, 74 ) 75 transformations.append(search_for_door) 76 77 unlock_door = Transformation( 78 "unlock_door", 79 inventory_changes=[ 80 Use(PLAYER, self.KEY), 81 Use(CURRENT_ZONE, self.LOCKED_DOOR, consume=1), 82 Yield(CURRENT_ZONE, self.OPEN_DOOR, create=1), 83 ], 84 ) 85 transformations.append(unlock_door) 86 87 return transformations
API Documentation
MINICRAFT_NAME =
'Unlock'
class
MiniHCraftUnlock(typing.Generic[~ObsType, ~ActType]):
15class MiniHCraftUnlock(MiniCraftEnv): 16 MINICRAFT_NAME = MINICRAFT_NAME 17 __doc__ = MiniCraftEnv.description(MINICRAFT_NAME) 18 19 START = Zone("start_room") 20 """Start room.""" 21 22 KEY = Item("key") 23 """Key used to unlock the door.""" 24 25 OPEN_DOOR = Item("open_door") 26 """Open door between the two rooms.""" 27 LOCKED_DOOR = Item("locked_door") 28 """Locked door between the two rooms, can be unlocked with a key.""" 29 30 def __init__(self, **kwargs) -> None: 31 self.task = PlaceItemTask(self.OPEN_DOOR) 32 super().__init__( 33 self.MINICRAFT_NAME, 34 purpose=self.task, 35 start_zone=self.START, 36 **kwargs, 37 ) 38 39 def build_transformations(self) -> List[Transformation]: 40 transformations = [] 41 42 search_for_key = Transformation( 43 "search_for_key", 44 inventory_changes=[ 45 Yield(CURRENT_ZONE, self.KEY, create=1, max=0), 46 Yield(PLAYER, self.KEY, create=0, max=0), 47 ], 48 zone=self.START, 49 ) 50 transformations.append(search_for_key) 51 52 pickup = Transformation( 53 "pickup_key", 54 inventory_changes=[ 55 Use(CURRENT_ZONE, self.KEY, consume=1), 56 Yield(PLAYER, self.KEY, create=1), 57 ], 58 ) 59 put_down = Transformation( 60 "put_down_key", 61 inventory_changes=[ 62 Use(PLAYER, self.KEY, consume=1), 63 Yield(CURRENT_ZONE, self.KEY, create=1), 64 ], 65 ) 66 transformations += [pickup, put_down] 67 68 search_for_door = Transformation( 69 "search_for_door", 70 inventory_changes=[ 71 Yield(CURRENT_ZONE, self.LOCKED_DOOR, max=0), 72 Yield(CURRENT_ZONE, self.OPEN_DOOR, create=0, max=0), 73 ], 74 zone=self.START, 75 ) 76 transformations.append(search_for_door) 77 78 unlock_door = Transformation( 79 "unlock_door", 80 inventory_changes=[ 81 Use(PLAYER, self.KEY), 82 Use(CURRENT_ZONE, self.LOCKED_DOOR, consume=1), 83 Yield(CURRENT_ZONE, self.OPEN_DOOR, create=1), 84 ], 85 ) 86 transformations.append(unlock_door) 87 88 return transformations
Reproduces the minigrid Unlock gridworld environment as a HierarchyCraft environment.
MiniHCraftUnlock(**kwargs)
30 def __init__(self, **kwargs) -> None: 31 self.task = PlaceItemTask(self.OPEN_DOOR) 32 super().__init__( 33 self.MINICRAFT_NAME, 34 purpose=self.task, 35 start_zone=self.START, 36 **kwargs, 37 )
Arguments:
- invalid_reward: Reward given to the agent for invalid actions. Defaults to -1.0.
- max_step: Maximum number of steps before episode truncation. If None, never truncates the episode. Defaults to None.
- render_window: Window using to render the environment with pygame.
LOCKED_DOOR =
Item(name='locked_door')
Locked door between the two rooms, can be unlocked with a key.
39 def build_transformations(self) -> List[Transformation]: 40 transformations = [] 41 42 search_for_key = Transformation( 43 "search_for_key", 44 inventory_changes=[ 45 Yield(CURRENT_ZONE, self.KEY, create=1, max=0), 46 Yield(PLAYER, self.KEY, create=0, max=0), 47 ], 48 zone=self.START, 49 ) 50 transformations.append(search_for_key) 51 52 pickup = Transformation( 53 "pickup_key", 54 inventory_changes=[ 55 Use(CURRENT_ZONE, self.KEY, consume=1), 56 Yield(PLAYER, self.KEY, create=1), 57 ], 58 ) 59 put_down = Transformation( 60 "put_down_key", 61 inventory_changes=[ 62 Use(PLAYER, self.KEY, consume=1), 63 Yield(CURRENT_ZONE, self.KEY, create=1), 64 ], 65 ) 66 transformations += [pickup, put_down] 67 68 search_for_door = Transformation( 69 "search_for_door", 70 inventory_changes=[ 71 Yield(CURRENT_ZONE, self.LOCKED_DOOR, max=0), 72 Yield(CURRENT_ZONE, self.OPEN_DOOR, create=0, max=0), 73 ], 74 zone=self.START, 75 ) 76 transformations.append(search_for_door) 77 78 unlock_door = Transformation( 79 "unlock_door", 80 inventory_changes=[ 81 Use(PLAYER, self.KEY), 82 Use(CURRENT_ZONE, self.LOCKED_DOOR, consume=1), 83 Yield(CURRENT_ZONE, self.OPEN_DOOR, create=1), 84 ], 85 ) 86 transformations.append(unlock_door) 87 88 return transformations
Build transformations for this MiniCraft environment
Inherited Members
- hcraft.examples.minicraft.minicraft.MiniCraftEnv
- description
- hcraft.env.HcraftEnv
- world
- invalid_reward
- max_step
- name
- render_window
- render_mode
- state
- current_step
- current_score
- cumulated_score
- episodes
- task_successes
- terminal_successes
- purpose
- metadata
- truncated
- observation_space
- action_space
- action_masks
- step
- render
- reset
- close
- all_behaviors
- solving_behavior
- planning_problem
- infos
- gymnasium.core.Env
- spec
- unwrapped
- np_random_seed
- np_random
- has_wrapper_attr
- get_wrapper_attr
- set_wrapper_attr