MiniCraft - DoorKey
Reproduces the minigrid DoorKey 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 GetItemTask 5from hcraft.transformation import Transformation, Use, Yield, PLAYER, CURRENT_ZONE 6 7from hcraft.examples.minicraft.minicraft import MiniCraftEnv 8 9 10MINICRAFT_NAME = "DoorKey" 11__doc__ = MiniCraftEnv.description(MINICRAFT_NAME, for_module_header=True) 12 13 14class MiniHCraftDoorKey(MiniCraftEnv): 15 MINICRAFT_NAME = MINICRAFT_NAME 16 __doc__ = MiniCraftEnv.description(MINICRAFT_NAME) 17 18 START = Zone("start_room") 19 """Start room.""" 20 LOCKED_ROOM = Zone("locked_room") 21 """Room behind a locked door.""" 22 23 KEY = Item("key") 24 """Key used to unlock the door.""" 25 GOAL = Item("goal") 26 """Goal to reach.""" 27 28 OPEN_DOOR = Item("open_door") 29 """Open door between the two rooms.""" 30 LOCKED_DOOR = Item("locked_door") 31 """Locked door between the two rooms, can be unlocked with a key.""" 32 33 def __init__(self, **kwargs) -> None: 34 self.task = GetItemTask(self.GOAL) 35 super().__init__( 36 self.MINICRAFT_NAME, 37 purpose=self.task, 38 start_zone=self.START, 39 **kwargs, 40 ) 41 42 def build_transformations(self) -> List[Transformation]: 43 transformations = [] 44 45 # Ensure key cannot be created if anywhere else 46 search_for_key = Transformation( 47 "search_for_key", 48 inventory_changes=[ 49 Yield(CURRENT_ZONE, self.KEY, create=1, max=0), 50 Yield(PLAYER, self.KEY, create=0, max=0), 51 Yield(self.START, self.KEY, create=0, max=0), 52 Yield(self.LOCKED_ROOM, self.KEY, create=0, max=0), 53 ], 54 zone=self.START, 55 ) 56 transformations.append(search_for_key) 57 58 pickup = Transformation( 59 "pickup_key", 60 inventory_changes=[ 61 Use(CURRENT_ZONE, self.KEY, consume=1), 62 Yield(PLAYER, self.KEY, create=1), 63 ], 64 ) 65 put_down = Transformation( 66 "put_down_key", 67 inventory_changes=[ 68 Use(PLAYER, self.KEY, consume=1), 69 Yield(CURRENT_ZONE, self.KEY, create=1), 70 ], 71 ) 72 transformations += [pickup, put_down] 73 74 search_for_door = Transformation( 75 "search_for_door", 76 inventory_changes=[ 77 Yield(CURRENT_ZONE, self.LOCKED_DOOR, max=0), 78 Yield(CURRENT_ZONE, self.OPEN_DOOR, create=0, max=0), 79 ], 80 zone=self.START, 81 ) 82 transformations.append(search_for_door) 83 84 unlock_door = Transformation( 85 "unlock_door", 86 inventory_changes=[ 87 Use(PLAYER, self.KEY), 88 Use(CURRENT_ZONE, self.LOCKED_DOOR, consume=1), 89 Yield(CURRENT_ZONE, self.OPEN_DOOR, create=1), 90 ], 91 ) 92 transformations.append(unlock_door) 93 94 move_to_locked_room = Transformation( 95 "move_to_locked_room", 96 destination=self.LOCKED_ROOM, 97 inventory_changes=[Use(CURRENT_ZONE, self.OPEN_DOOR)], 98 zone=self.START, 99 ) 100 transformations.append(move_to_locked_room) 101 102 move_to_start_room = Transformation( 103 destination=self.START, 104 zone=self.LOCKED_ROOM, 105 ) 106 transformations.append(move_to_start_room) 107 108 find_goal = Transformation( 109 "find_goal", 110 inventory_changes=[Yield(CURRENT_ZONE, self.GOAL, max=0)], 111 zone=self.LOCKED_ROOM, 112 ) 113 transformations.append(find_goal) 114 115 reach_goal = Transformation( 116 "reach_goal", 117 inventory_changes=[ 118 Use(CURRENT_ZONE, self.GOAL, consume=1), 119 Yield(PLAYER, self.GOAL), 120 ], 121 ) 122 transformations.append(reach_goal) 123 124 return transformations
API Documentation
MINICRAFT_NAME =
'DoorKey'
class
MiniHCraftDoorKey(typing.Generic[~ObsType, ~ActType]):
15class MiniHCraftDoorKey(MiniCraftEnv): 16 MINICRAFT_NAME = MINICRAFT_NAME 17 __doc__ = MiniCraftEnv.description(MINICRAFT_NAME) 18 19 START = Zone("start_room") 20 """Start room.""" 21 LOCKED_ROOM = Zone("locked_room") 22 """Room behind a locked door.""" 23 24 KEY = Item("key") 25 """Key used to unlock the door.""" 26 GOAL = Item("goal") 27 """Goal to reach.""" 28 29 OPEN_DOOR = Item("open_door") 30 """Open door between the two rooms.""" 31 LOCKED_DOOR = Item("locked_door") 32 """Locked door between the two rooms, can be unlocked with a key.""" 33 34 def __init__(self, **kwargs) -> None: 35 self.task = GetItemTask(self.GOAL) 36 super().__init__( 37 self.MINICRAFT_NAME, 38 purpose=self.task, 39 start_zone=self.START, 40 **kwargs, 41 ) 42 43 def build_transformations(self) -> List[Transformation]: 44 transformations = [] 45 46 # Ensure key cannot be created if anywhere else 47 search_for_key = Transformation( 48 "search_for_key", 49 inventory_changes=[ 50 Yield(CURRENT_ZONE, self.KEY, create=1, max=0), 51 Yield(PLAYER, self.KEY, create=0, max=0), 52 Yield(self.START, self.KEY, create=0, max=0), 53 Yield(self.LOCKED_ROOM, self.KEY, create=0, max=0), 54 ], 55 zone=self.START, 56 ) 57 transformations.append(search_for_key) 58 59 pickup = Transformation( 60 "pickup_key", 61 inventory_changes=[ 62 Use(CURRENT_ZONE, self.KEY, consume=1), 63 Yield(PLAYER, self.KEY, create=1), 64 ], 65 ) 66 put_down = Transformation( 67 "put_down_key", 68 inventory_changes=[ 69 Use(PLAYER, self.KEY, consume=1), 70 Yield(CURRENT_ZONE, self.KEY, create=1), 71 ], 72 ) 73 transformations += [pickup, put_down] 74 75 search_for_door = Transformation( 76 "search_for_door", 77 inventory_changes=[ 78 Yield(CURRENT_ZONE, self.LOCKED_DOOR, max=0), 79 Yield(CURRENT_ZONE, self.OPEN_DOOR, create=0, max=0), 80 ], 81 zone=self.START, 82 ) 83 transformations.append(search_for_door) 84 85 unlock_door = Transformation( 86 "unlock_door", 87 inventory_changes=[ 88 Use(PLAYER, self.KEY), 89 Use(CURRENT_ZONE, self.LOCKED_DOOR, consume=1), 90 Yield(CURRENT_ZONE, self.OPEN_DOOR, create=1), 91 ], 92 ) 93 transformations.append(unlock_door) 94 95 move_to_locked_room = Transformation( 96 "move_to_locked_room", 97 destination=self.LOCKED_ROOM, 98 inventory_changes=[Use(CURRENT_ZONE, self.OPEN_DOOR)], 99 zone=self.START, 100 ) 101 transformations.append(move_to_locked_room) 102 103 move_to_start_room = Transformation( 104 destination=self.START, 105 zone=self.LOCKED_ROOM, 106 ) 107 transformations.append(move_to_start_room) 108 109 find_goal = Transformation( 110 "find_goal", 111 inventory_changes=[Yield(CURRENT_ZONE, self.GOAL, max=0)], 112 zone=self.LOCKED_ROOM, 113 ) 114 transformations.append(find_goal) 115 116 reach_goal = Transformation( 117 "reach_goal", 118 inventory_changes=[ 119 Use(CURRENT_ZONE, self.GOAL, consume=1), 120 Yield(PLAYER, self.GOAL), 121 ], 122 ) 123 transformations.append(reach_goal) 124 125 return transformations
Reproduces the minigrid DoorKey gridworld environment as a HierarchyCraft environment.
MiniHCraftDoorKey(**kwargs)
34 def __init__(self, **kwargs) -> None: 35 self.task = GetItemTask(self.GOAL) 36 super().__init__( 37 self.MINICRAFT_NAME, 38 purpose=self.task, 39 start_zone=self.START, 40 **kwargs, 41 )
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.
43 def build_transformations(self) -> List[Transformation]: 44 transformations = [] 45 46 # Ensure key cannot be created if anywhere else 47 search_for_key = Transformation( 48 "search_for_key", 49 inventory_changes=[ 50 Yield(CURRENT_ZONE, self.KEY, create=1, max=0), 51 Yield(PLAYER, self.KEY, create=0, max=0), 52 Yield(self.START, self.KEY, create=0, max=0), 53 Yield(self.LOCKED_ROOM, self.KEY, create=0, max=0), 54 ], 55 zone=self.START, 56 ) 57 transformations.append(search_for_key) 58 59 pickup = Transformation( 60 "pickup_key", 61 inventory_changes=[ 62 Use(CURRENT_ZONE, self.KEY, consume=1), 63 Yield(PLAYER, self.KEY, create=1), 64 ], 65 ) 66 put_down = Transformation( 67 "put_down_key", 68 inventory_changes=[ 69 Use(PLAYER, self.KEY, consume=1), 70 Yield(CURRENT_ZONE, self.KEY, create=1), 71 ], 72 ) 73 transformations += [pickup, put_down] 74 75 search_for_door = Transformation( 76 "search_for_door", 77 inventory_changes=[ 78 Yield(CURRENT_ZONE, self.LOCKED_DOOR, max=0), 79 Yield(CURRENT_ZONE, self.OPEN_DOOR, create=0, max=0), 80 ], 81 zone=self.START, 82 ) 83 transformations.append(search_for_door) 84 85 unlock_door = Transformation( 86 "unlock_door", 87 inventory_changes=[ 88 Use(PLAYER, self.KEY), 89 Use(CURRENT_ZONE, self.LOCKED_DOOR, consume=1), 90 Yield(CURRENT_ZONE, self.OPEN_DOOR, create=1), 91 ], 92 ) 93 transformations.append(unlock_door) 94 95 move_to_locked_room = Transformation( 96 "move_to_locked_room", 97 destination=self.LOCKED_ROOM, 98 inventory_changes=[Use(CURRENT_ZONE, self.OPEN_DOOR)], 99 zone=self.START, 100 ) 101 transformations.append(move_to_locked_room) 102 103 move_to_start_room = Transformation( 104 destination=self.START, 105 zone=self.LOCKED_ROOM, 106 ) 107 transformations.append(move_to_start_room) 108 109 find_goal = Transformation( 110 "find_goal", 111 inventory_changes=[Yield(CURRENT_ZONE, self.GOAL, max=0)], 112 zone=self.LOCKED_ROOM, 113 ) 114 transformations.append(find_goal) 115 116 reach_goal = Transformation( 117 "reach_goal", 118 inventory_changes=[ 119 Use(CURRENT_ZONE, self.GOAL, consume=1), 120 Yield(PLAYER, self.GOAL), 121 ], 122 ) 123 transformations.append(reach_goal) 124 125 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